-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupset_plot.py
executable file
·625 lines (483 loc) · 21.7 KB
/
upset_plot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
import pandas as pd
# Parse command-line arguments
def parse_arguments() -> None:
import argparse
parser = argparse.ArgumentParser(description='Create Upset plots from a collection of lists.')
parser.add_argument(
'-de',
'--de_files',
type=str,
required=False,
help="List of de files to extract subsets of up-regulated and down-regulated genes."
)
parser.add_argument(
'-ls',
'--lists_of_strings',
type=str,
required=False,
help="Lists of strings, in the case of not provided de files."
)
parser.add_argument(
'-out',
'--outfile',
type=str,
required=False,
help="Output file to save results supporting the UpSet plot. Default: 'UpSet_genes.tsv' "
)
parser.add_argument(
'-plt',
'--plot',
type=str,
required=False,
help="Output filename to print UpSet plot. Default: 'UpSet_plot.svg' "
)
parser.add_argument(
'-img',
'--image_format',
required=False,
type=str,
help="Format to save image. Default: 'svg' "
)
parser.add_argument(
'-vlist',
'--variable_list',
required=False,
type=str,
help="List of variables and categorical columns for stacking bars of UpSetPlot."
)
parser.add_argument(
'-p_sum',
'--pie_sum',
required=False,
type=str,
help="Column to use for pie charts. Default: 'Orthology' "
)
parser.add_argument(
'-fl',
'--font_list',
required=False,
type=str,
help="List of priority-ordered font names. Default: ['Aileron', 'Arial']."
)
args = parser.parse_args()
if not any(vars(args).values()):
parser.print_help()
return args
# If a member is found in >1 categories, structure the 'stacked_categories' file as such,
# with the highest priority categories on top and the lowest priority categories at the end,
# to ensure that you keep the highest-priority category when dropping duplicate values.
# For example, I have an input file with 'Annotation' (categorical_variable) categories.
# To ensure that Uncharacterized will be the last assigned category (lowest priority),
# I structured it as following:
# ABC ABC transporters
# cath Cathepsins
# UDP UGTs
# ase Metabolic enzymes
# No hits Uncharacterized
# hypothetical Uncharacterized
# uncharacterized Uncharacterized
# The general idea is to go from as much specific to more broad/general categories (only the 1st column matters for the pattern matching and order identification)
def _import_stacking_variables(variable_list) -> dict:
stacks = {}
variables = {}
with open(variable_list, 'r') as vlist:
for lines in vlist:
categories = lines.strip().split('\t')[0]
stacks[categories] = {}
files = lines.strip().split('\t')[1]
with open(files, 'r') as category:
for line in category.readlines():
columns = line.strip().split('\t')
string = columns[0]
group = columns[1]
stacks[categories][string] = group
return stacks
# Classify subsets to stack bars
def classify_subsets(
stacks: dict,
column: pd.Series
) -> list:
import re
column_name = column.name
column_values = column
classified_subsets = []
for stack, string in stacks.items():
if re.search(column_name, stack, re.I): # Match the column name with the stack name
classified_stack = []
for value in column_values:
matched = False
for string_, group in string.items():
if re.search(str(string_), str(value)):
classified_stack.append(group)
matched = True
break
if not matched:
classified_stack.append('Other')
return classified_stack
def generate_lists_of_genes(
filenames,
variable_list=None,
de=True,
pie_sum='Orthology'
) -> dict:
import re
variables = {}
if variable_list:
variables = _import_stacking_variables(variable_list)
dfs = {}
with open(filenames, "r") as fls:
fl = fls.readlines()
for filename in fl:
filename = filename.strip('\n')
filename_regex = re.compile(r"\\..\\/|\\\..\*|\..*")
subset = re.sub(filename_regex, "", filename)
if de:
# Load pandas df with the contents of the de file and remove lines with Up- and Down-regulated flags
de_file = pd.read_csv(filename, sep='\t', header=0, na_values=['NA', 'na'], keep_default_na=True)
de_file['Geneid'] = de_file['Geneid'].astype(str)
filtered_de_file = de_file[~de_file['Geneid'].str.contains('Upregulated|Downregulated')]
# Isolate rows corresponding to up- and down-regulated genes
filtered_de_file['Fold_Change'] = filtered_de_file['Fold_Change'].astype(float)
upregulated = pd.DataFrame(filtered_de_file[filtered_de_file['Fold_Change'] > 0])
downregulated = pd.DataFrame(filtered_de_file[filtered_de_file['Fold_Change'] < 0])
if variable_list:
up_dfs = []
down_dfs = []
dfs[subset + "_UP"] = {}
dfs[subset + "_DOWN"] = {}
for cat_var in variables.keys():
# Create DataFrame for upregulated genes with classification
up_df = pd.DataFrame({
'Geneid': upregulated['Geneid'].tolist(),
cat_var: classify_subsets(variables, upregulated[cat_var])
})
# Create DataFrame for downregulated genes with classification
down_df = pd.DataFrame({
'Geneid': downregulated['Geneid'].tolist(),
cat_var: classify_subsets(variables, downregulated[cat_var])
})
# Append the DataFrames to the lists
up_dfs.append(up_df)
down_dfs.append(down_df)
# Merge all DataFrames in the lists on 'Geneid'
if up_dfs:
combined_up_df = upregulated[['Geneid']].copy()
for up_df in up_dfs:
combined_up_df = pd.merge(
combined_up_df,
up_df,
on='Geneid',
how='left'
)
dfs[subset + "_UP"] = combined_up_df
if down_dfs:
combined_down_df = downregulated[['Geneid']].copy()
for down_df in down_dfs:
combined_down_df = pd.merge(
combined_down_df,
down_df,
on='Geneid',
how='left'
)
dfs[subset + "_DOWN"] = combined_down_df
elif pie_sum:
dfs[subset + "_UP"] = pd.DataFrame({
'Geneid': upregulated['Geneid'],
pie_sum: upregulated[pie_sum].tolist()
})
dfs[subset + "_DOWN"] = pd.DataFrame({
'Geneid': downregulated['Geneid'],
pie_sum: downregulated[pie_sum].tolist()
})
else:
dfs[subset + "_UP"] = pd.DataFrame({ 'Geneid': upregulated['Geneid'].copy() })
dfs[subset + "_DOWN"] = pd.DataFrame({ 'Geneid': downregulated['Geneid'].copy() })
return dfs
def _identify_fonts(font_list):
"""
Identify and add fonts from a given list of font names to Matplotlib's font manager.
Parameters:
font_list (list): List of font names to identify and add.
"""
from matplotlib import font_manager
if isinstance(font_list, str):
return font_list
elif isinstance(font_list, list):
# Find all system font files
font_files = font_manager.findSystemFonts(fontpaths=None, fontext='otf')
# Add specified fonts to the font manager
for font_file in font_files:
for font_name in font_list:
if font_name in font_file:
font_manager.fontManager.addfont(font_file)
def upset_plots(
input_files,
variable_list,
de = True,
outfile = "UpSet_genes.tsv",
plot = "UpSet_plot",
image_format = "svg",
font_list = ['Aileron', 'Arial'],
fontsize = 12
) -> None:
import re
from upsetplot import UpSet
from matplotlib import pyplot as plt
from matplotlib import cm
from matplotlib.colors import LinearSegmentedColormap
from matplotlib import font_manager
from matplotlib.font_manager import FontProperties
# Open input files
if de and variable_list:
dfs = generate_lists_of_genes(filenames = input_files, variable_list = variable_list, de = True)
variables = _import_stacking_variables(variable_list)
elif de and not variable_list:
dfs = generate_lists_of_genes(filenames=input_files, de=True)
elif not de and variable_list:
dfs = generate_lists_of_genes(filenames=input_files, de=False, stacked=first_categories)
variables = _import_stacking_variables(variable_list)
elif not de and not variable_list:
dfs = generate_lists_of_genes(filenames=input_files, de=False)
sets = []
if dfs:
sets = {subset: df[df.columns[0]].tolist() for subset, df in dfs.items()} # Unpack the contents of the dictionary of Pandas dataframes into list
names = sets.keys()
# Get the unique elements in all subsets
unique_elems = list(set().union(*sets.values()))
# Identify presence/absence of each element in each subset
df = pd.DataFrame([[
e in st
for comp, st in sets.items()
for name in names
if name == comp
]
for e in unique_elems
],
columns = names,
index = unique_elems
)
# Clean column names for readability
garbage = re.compile('_geneids|.annotated.sorted|P1.*.tsv')
df.columns = df.columns.str.replace(garbage, '', regex=True)
df.columns = df.columns.str.replace('_', ' ', regex=True)
# Save results of per gene presence in tab-separated file
df.to_csv(outfile, sep = '\t', index = unique_elems, index_label = 'GeneID' if de else 'Index')
df.reset_index(inplace = True) # Move geneids from index to column
df.rename(columns = {'index': 'Geneid' if de else 'Index'}, inplace = True)
# Create UpSet plot depending on provided options
# Associate geneids in classified subsets with categorical variable from original parsing
if de:
if variables:
concatenated_dfs = pd.concat(dfs.values()).drop_duplicates(subset='Geneid')
dfm = pd.merge(
df,
concatenated_dfs,
on = 'Geneid',
how = 'left'
)
subsets = [ col for col in df.columns if re.search ('UP|DOWN', col) ]
dfm.set_index(subsets, inplace = True)
upset = UpSet(dfm, orientation = 'horizontal', show_counts = True, intersection_plot_elements = 0)
for stv in variables.keys():
upset.add_stacked_bars(by = stv, colors = cm.Accent, title = "Intersections", elements = 8)
else:
concatenated_dfs = pd.concat(dfs.values()).drop_duplicates(subset='Geneid')
dfm = pd.merge(
df,
concatenated_dfs[['Geneid']],
on = 'Geneid',
how = 'left'
)
subsets = [ col for col in df.columns if not re.search ('Geneid', col) ]
dfm.set_index(subsets, inplace = True)
upset = UpSet(dfm, orientation = 'horizontal', show_counts = True)
else:
dfm = df
subsets = [ col for col in df.columns if not re.search ('Index', col) ]
dfm.set_index(subsets, inplace = True)
if stacked:
upset = UpSet(dfm, orientation = 'horizontal', show_counts = True, intersection_plot_elements = 0)
upset.add_stacked_bars(by = first_variable, colors = colormap, title = "Intersections", elements = 10)
else:
upset = UpSet(dfm, orientation = 'horizontal', show_counts = True)
# Create the UpSet plot
_identify_fonts(font_list)
plt.rcParams['font.family'] = font_list
upset.plot()
fig = plt.gcf()
# Disable grid in all axes
all_axes = fig.get_axes()
for axis in all_axes:
axis.grid(False)
# Add bold ylabel
ax = fig.gca()
ax.set_ylabel('Intersections', fontfamily = font_list, fontsize = fontsize, fontweight='bold')
# Add borders in UpSet plot bars
for patch in ax.patches:
patch.set_edgecolor('black')
patch.set_linewidth(0.3)
# Add horizontal line in x axis
plt.axhline(y=-0.1, color='black', linestyle='-', linewidth = 1)
plt.savefig(plot, format = image_format, dpi = 600)
plt.show()
else:
print("Problem with input dictionary of Pandas dataframes.")
def pie_charts(
input_files,
column,
plot,
title,
de = True,
num_cols = 2,
image_format = 'svg',
font_list = [ 'Aileron', 'Arial']
) -> None:
import re
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import cm
from matplotlib.font_manager import FontProperties
from adjustText import adjust_text
if de:
dfs = generate_lists_of_genes(filenames=input_files, pie_sum=column, de=True)
else:
dfs = generate_lists_of_genes(filenames=input_files, pie_sum=column, de=False)
if dfs:
num_subsets = len(dfs)
num_rows = (num_subsets + num_cols - 1) // num_cols # Calculate the number of rows needed
# Create a single figure and set of axes
fig, axes = plt.subplots(num_rows, num_cols, figsize = (20, 14))
axes = axes.flatten() # Flatten the axes array to simplify the loop
# Consolidate all categories to define a common color map
all_categories = sorted(set(cat for df in dfs.values() for cat in df[column].unique()))
colors = plt.cm.tab20c(range(len(all_categories)))
color_map = dict(zip(all_categories, colors))
# Initialize a list to hold wedge handles for the legend
wedge_handles = []
for idx, (subset, df) in enumerate(dfs.items()):
if idx >= len(axes): # In case there are more subsets than axes available
break
# Count the occurrences of each 'Orthology' type
counts = df[column].value_counts()
# Adjust pie chart size based on the total number of elements
total_sum = counts.sum()
radius = np.sqrt(total_sum) / 15
# Plot the pie charts
ax = axes[idx]
wedges, texts, autotexts = ax.pie(
counts,
radius=radius,
autopct='%1.1f%%',
pctdistance=0.85,
explode=[0.06] * len(counts),
shadow=False,
colors=[color_map[cat] for cat in counts.index],
startangle=30,
wedgeprops={'linewidth': 1, 'edgecolor': 'black'},
labeldistance=1.1, # Position labels slightly outside the pie
textprops={'horizontalalignment': 'center', 'verticalalignment': 'center'}
)
# Adjust the labels
for autotext, wedge in zip(autotexts, wedges):
# Calculate the angle at the middle of the wedge
angle = (wedge.theta2 + wedge.theta1) / 2
# Rotate the label to align with the wedge
autotext.set_rotation(angle * 180 / np.pi)
# Set the font properties
autotext.set_fontsize(8)
autotext.set_fontfamily(font_list)
x, y = np.cos(angle), np.sin(angle)
horizontalalignment = {-1: "right", 1: "left"}[int(np.sign(x))]
connectionstyle = "arc,rad=0" # Set to straight line
autotext.set_position((x * 1.05, y * 1.05)) # Adjust the radial distance as needed
annot_texts = []
annot = ax.annotate(
value,
xy=(1.02*x, 1.02*y), # Coordinates at the edge of the wedge
xytext=(1.08* x, 1.08* y), # Adjust the offset as needed
horizontalalignment=horizontalalignment,
bbox=dict(
boxstyle="round,pad=0.1",
edgecolor="white",
facecolor="white",
alpha=0
),
arrowprops=dict(
arrowstyle="-",
connectionstyle=connectionstyle,
),
fontproperties=FontProperties(family=font_list, style='normal', size=8)
)
annot_texts.append(annot)
adjust_text(annot_texts, ax = ax)
cleaned_title = subset.split('.tsvf')[0].replace('_', ' ')
ax.set_title(cleaned_title, fontdict = {'family': font_list, 'size': 14}, pad = 2)
# Hide unused axes
for i in range(idx + 1, len(axes)):
axes[i].axis('off')
# Create a common legend from the handles collected
sorted_wedge_handles = sorted(wedge_handles, key=lambda x: (len(x[1].split()), x[1]))
fig.legend(
[handle for handle, label in sorted_wedge_handles],
[label for handle, label in sorted_wedge_handles],
loc = 'center left',
prop = FontProperties(family = font_list ,style = 'italic', size = 10),
title = title,
title_fontproperties = FontProperties(family = font_list, weight = 'bold', style = 'normal', size = 13),
frameon = False
)
plt.savefig(plot, format = image_format, dpi = 600)
plt.show()
else:
print("Problem with input dictionary of Pandas dataframes.")
def main() -> None:
import os
args = parse_arguments()
if args.de_files and not args.lists_of_strings:
if not os.path.exists(args.de_files):
print(f"Error: File '{args.de_files}' not found.")
return
if args.pie_sum:
pie_charts(
input_files = args.de_files.strip('\n'),
plot = args.plot if args.plot else 'Pie_charts.svg',
column = args.pie_sum if args.pie_sum else 'Orthology',
image_format = args.image_format if args.image_format else 'svg',
font_list = font_list if args.font_list else ['Aileron', 'Arial']
)
else:
upset_plots(
input_files = args.de_files.strip('\n'),
variable_list = args.variable_list,
plot = args.plot if args.plot else 'UpSet_plot.svg',
image_format = args.image_format if args.image_format else 'svg',
de = True,
font_list = font_list if args.font_list else ['Aileron', 'Arial']
)
elif not args.de_files and args.lists_of_strings:
if not os.path.exists(args.lists_of_strings):
print(f"Error: File '{args.de_files}' not found.")
return
if pie_sum:
pie_charts(
input_files = args.lists_of_strings.strip('\n'),
plot = args.plot if args.plot else 'Pie_charts.svg',
column = args.pie_sum if args.pie_sum else 'Orthology',
image_format = args.image_format if args.image_format else 'svg',
font_list = font_list if args.font_list else ['Aileron', 'Arial']
)
else:
upset_plots(
input_files = args.lists_of_strings.strip('\n'),
variable_list = args.variable_list,
plot = args.plot if args.plot else 'UpSet_plot.svg',
image_format = args.image_format if args.image_format else 'svg',
de = False,
font_list = font_list if args.font_list else ['Aileron', 'Arial']
)
elif args.de_files and args.lists_of_strings:
print ( "Please select either de files or lists of strings.")
else:
print ("Please provide a list of files as input.")
if __name__ == "__main__":
main()