Mercurial > repos > glogobyte > isoread
comparison mirgene_graphs.py @ 17:0cdc0c3c821e draft
Uploaded
author | glogobyte |
---|---|
date | Wed, 20 Oct 2021 08:44:20 +0000 |
parents | d58f050acd18 |
children | 01679b6e886a |
comparison
equal
deleted
inserted
replaced
16:fa48ad87ae3e | 17:0cdc0c3c821e |
---|---|
79 t_templ+=1 | 79 t_templ+=1 |
80 t_tem_counts += x[2] | 80 t_tem_counts += x[2] |
81 | 81 |
82 fig = plt.figure(figsize=(7,5)) | 82 fig = plt.figure(figsize=(7,5)) |
83 | 83 |
84 labels = 'miRNA RefSeq','Template', 'Unassigned' ,'Non-template' | 84 labels = 'miRNA RefSeq','templated', 'unassigned' ,'non-templated' |
85 sizes = [c_mat_counts, c_tem_counts, c_unmap_counts,c_non_counts] | 85 sizes = [c_mat_counts, c_tem_counts, c_unmap_counts,c_non_counts] |
86 colors = ['gold', 'yellowgreen', 'lightcoral', 'lightskyblue'] | 86 colors = ['gold', 'yellowgreen', 'lightcoral', 'lightskyblue'] |
87 # Plot | 87 # Plot |
88 ax1 = plt.subplot2grid((1,2),(0,0)) | 88 ax1 = plt.subplot2grid((1,2),(0,0)) |
89 patches, texts, autotexts=plt.pie(sizes, labels=labels, colors=colors, startangle=140,autopct='%1.1f%%',radius=0.8) | 89 patches, texts, autotexts=plt.pie(sizes, labels=labels, colors=colors, startangle=140,autopct='%1.1f%%',radius=0.8) |
90 [x.set_fontsize(8) for x in texts] | 90 [x.set_fontsize(10) for x in texts] |
91 plt.title(group_name1 + ' Group (reads)',fontsize=12) | 91 plt.title(group_name1.capitalize() + ' group (reads)',fontsize=12) |
92 | 92 |
93 labels = 'miRNA RefSeq','Template', 'Unassigned','non-template' | 93 labels = 'miRNA RefSeq','templated', 'unassigned','non-templated' |
94 sizes = [t_mat_counts, t_tem_counts, t_unmap_counts, t_non_counts] | 94 sizes = [t_mat_counts, t_tem_counts, t_unmap_counts, t_non_counts] |
95 colors = ['gold', 'yellowgreen', 'lightcoral', 'lightskyblue'] | 95 colors = ['gold', 'yellowgreen', 'lightcoral', 'lightskyblue'] |
96 # Plot | 96 # Plot |
97 ax2 = plt.subplot2grid((1,2),(0,1)) | 97 ax2 = plt.subplot2grid((1,2),(0,1)) |
98 patches, texts, autotexts=plt.pie(sizes, labels=labels, colors=colors, startangle=140,autopct='%1.1f%%',radius=0.8) | 98 patches, texts, autotexts=plt.pie(sizes, labels=labels, colors=colors, startangle=140,autopct='%1.1f%%',radius=0.8) |
99 [x.set_fontsize(8) for x in texts] | 99 [x.set_fontsize(10) for x in texts] |
100 plt.title(group_name2 + ' Group (reads)', fontsize=12) | 100 plt.title(group_name2.capitalize() + ' group (reads)', fontsize=12) |
101 plt.savefig('pie_non.png',dpi=300) | 101 plt.savefig('pie_non.png',dpi=300) |
102 | 102 |
103 #################################################################################################################################################################################################################### | 103 #################################################################################################################################################################################################################### |
104 | 104 |
105 def pie_temp(merge_con,c_unmap,c_unmap_counts,merge_tre,t_unmap,t_unmap_counts,group_name1,group_name2): | 105 def pie_temp(merge_con,c_unmap,c_unmap_counts,merge_tre,t_unmap,t_unmap_counts,group_name1,group_name2): |
158 t_templ+=1 | 158 t_templ+=1 |
159 t_tem_counts += x[2] | 159 t_tem_counts += x[2] |
160 | 160 |
161 | 161 |
162 fig = plt.figure() | 162 fig = plt.figure() |
163 labels = 'miRNA RefSeq','Template', 'Unassigned' | 163 labels = 'miRNA RefSeq','templated', 'unassigned' |
164 sizes = [c_mat_counts, c_tem_counts, c_unmap_counts] | 164 sizes = [c_mat_counts, c_tem_counts, c_unmap_counts] |
165 colors = ['gold', 'yellowgreen', 'lightskyblue'] | 165 colors = ['gold', 'yellowgreen', 'lightskyblue'] |
166 explode = (0.2, 0.05, 0.1) | 166 explode = (0.2, 0.05, 0.1) |
167 # Plot | 167 # Plot |
168 ax1 = plt.subplot2grid((1,2),(0,0)) | 168 ax1 = plt.subplot2grid((1,2),(0,0)) |
169 patches, texts, autotexts=plt.pie(sizes, labels=labels, colors=colors, startangle=140,autopct='%1.1f%%',radius=0.8) | 169 patches, texts, autotexts=plt.pie(sizes, labels=labels, colors=colors, startangle=140,autopct='%1.1f%%',radius=0.8) |
170 [x.set_fontsize(8) for x in texts] | 170 [x.set_fontsize(10) for x in texts] |
171 plt.title(group_name1 + ' group (reads)', fontsize=12) | 171 plt.title(group_name1.capitalize() + ' group (reads)', fontsize=12) |
172 labels = 'miRNA RefSeq','Template', 'Unassigned' | 172 labels = 'miRNA RefSeq','templated', 'Unassigned' |
173 sizes = [t_mat_counts, t_tem_counts, t_unmap_counts] | 173 sizes = [t_mat_counts, t_tem_counts, t_unmap_counts] |
174 colors = ['gold', 'yellowgreen', 'lightskyblue'] | 174 colors = ['gold', 'yellowgreen', 'lightskyblue'] |
175 explode = (0.2, 0.05, 0.1) | 175 explode = (0.2, 0.05, 0.1) |
176 # Plot | 176 # Plot |
177 ax2 = plt.subplot2grid((1,2),(0,1)) | 177 ax2 = plt.subplot2grid((1,2),(0,1)) |
178 patches, texts, autotexts=plt.pie(sizes, labels=labels, colors=colors, startangle=140,autopct='%1.1f%%',radius=0.8) | 178 patches, texts, autotexts=plt.pie(sizes, labels=labels, colors=colors, startangle=140,autopct='%1.1f%%',radius=0.8) |
179 [x.set_fontsize(8) for x in texts] | 179 [x.set_fontsize(10) for x in texts] |
180 plt.title(group_name2 + ' group (reads)',fontsize = 12) | 180 plt.title(group_name2.capitalize() + ' group (reads)',fontsize = 12) |
181 plt.savefig('pie_tem.png',dpi=300) | 181 plt.savefig('pie_tem.png',dpi=300) |
182 | 182 |
183 ################################################################################################################################################################################################################### | 183 ################################################################################################################################################################################################################### |
184 | 184 |
185 def make_spider(merge_con,merge_tre,group_name1,group_name2): | 185 def make_spider(merge_con,merge_tre,group_name1,group_name2): |
334 | 334 |
335 radar_max = max(c_5, c_3, c_both,c_mature,c_exception,t_5,t_3,t_both,t_mature,t_exception) | 335 radar_max = max(c_5, c_3, c_both,c_mature,c_exception,t_5,t_3,t_both,t_mature,t_exception) |
336 radar_max_counts = max(c_5_counts,c_3_counts,c_both_counts,c_mat_counts,c_exception_counts,t_5_counts,t_3_counts,t_both_counts,t_mat_counts,t_exception_counts) | 336 radar_max_counts = max(c_5_counts,c_3_counts,c_both_counts,c_mat_counts,c_exception_counts,t_5_counts,t_3_counts,t_both_counts,t_mat_counts,t_exception_counts) |
337 | 337 |
338 df=pd.DataFrame({ | 338 df=pd.DataFrame({ |
339 'group':[group_name1,group_name2], | 339 'group':[group_name1.capitalize(),group_name2.capitalize()], |
340 """5'3'-isomiRs""":[c_both,t_both], | 340 """5'3'-isomiRs""":[c_both,t_both], |
341 """3'-isomiRs""":[c_3,t_3], | 341 """3'-isomiRs""":[c_3,t_3], |
342 'RefSeq miRNA':[c_mature,t_mature], | 342 'RefSeq miRNA':[c_mature,t_mature], |
343 """5'-isomiRs""":[c_5,t_5], | 343 """5'-isomiRs""":[c_5,t_5], |
344 'Others*':[c_exception,t_exception]}) | 344 'others*':[c_exception,t_exception]}) |
345 | 345 |
346 df1=pd.DataFrame({ | 346 df1=pd.DataFrame({ |
347 'group':[group_name1,group_name2], | 347 'group':[group_name1.capitalize(),group_name2.capitalize()], |
348 """5'-3'-isomiRs""":[c_both_counts,t_both_counts], | 348 """5'-3'-isomiRs""":[c_both_counts,t_both_counts], |
349 """3'-isomiRs""":[c_3_counts,t_3_counts], | 349 """3'-isomiRs""":[c_3_counts,t_3_counts], |
350 'RefSeq miRNA':[c_mat_counts,t_mat_counts], | 350 'RefSeq miRNA':[c_mat_counts,t_mat_counts], |
351 """5'-isomiRs""":[c_5_counts,t_5_counts], | 351 """5'-isomiRs""":[c_5_counts,t_5_counts], |
352 'Others*':[c_exception_counts,t_exception_counts]}) | 352 'others*':[c_exception_counts,t_exception_counts]}) |
353 | 353 |
354 spider_last(df,radar_max,1,group_name1,group_name2) | 354 spider_last(df,radar_max,1,group_name1,group_name2) |
355 spider_last(df1,radar_max_counts,2,group_name1,group_name2) | 355 spider_last(df1,radar_max_counts,2,group_name1,group_name2) |
356 | 356 |
357 | 357 |
374 # If you want the first axis to be on top: | 374 # If you want the first axis to be on top: |
375 ax.set_theta_offset(pi/2) | 375 ax.set_theta_offset(pi/2) |
376 ax.set_theta_direction(-1) | 376 ax.set_theta_direction(-1) |
377 | 377 |
378 # Draw one axe per variable + add labels labels yet | 378 # Draw one axe per variable + add labels labels yet |
379 plt.xticks(angles[:-1], categories, fontsize=11) | 379 plt.xticks(angles[:-1], categories, fontsize=13) |
380 | 380 |
381 # Draw ylabels | 381 # Draw ylabels |
382 radar_max=round(radar_max+radar_max*0.1) | 382 radar_max=round(radar_max+radar_max*0.1) |
383 mul=len(str(radar_max))-1 | 383 mul=len(str(radar_max))-1 |
384 maxi=int(math.ceil(radar_max / pow(10,mul))) * pow(10,mul) | 384 maxi=int(math.ceil(radar_max / pow(10,mul))) * pow(10,mul) |
385 sep = round(maxi/4) | 385 sep = round(maxi/4) |
386 plt.yticks([sep, 2*sep, 3*sep, 4*sep, 5*sep], [str(sep)+'%', str(2*sep)+'%', str(3*sep)+'%', str(4*sep)+'%', str(5*sep)+'%'], color="grey", size=10) | 386 plt.yticks([sep, 2*sep, 3*sep, 4*sep, 5*sep], [str(sep)+'%', str(2*sep)+'%', str(3*sep)+'%', str(4*sep)+'%', str(5*sep)+'%'], color="grey", size=12) |
387 plt.ylim(0, maxi) | 387 plt.ylim(0, maxi) |
388 | 388 |
389 # ------- PART 2: Add plots | 389 # ------- PART 2: Add plots |
390 | 390 |
391 # Plot each individual = each line of the data | 391 # Plot each individual = each line of the data |
392 # I don't do a loop, because plotting more than 3 groups makes the chart unreadable | 392 # I don't do a loop, because plotting more than 3 groups makes the chart unreadable |
393 | 393 |
394 # Ind1 | 394 # Ind1 |
395 values=df.loc[0].drop('group').values.flatten().tolist() | 395 values=df.loc[0].drop('group').values.flatten().tolist() |
396 values += values[:1] | 396 values += values[:1] |
397 ax.plot(angles, values,'-o', linewidth=1, linestyle='solid', label=group_name1) | 397 ax.plot(angles, values,'-o', linewidth=1, linestyle='solid', label=group_name1.capitalize()) |
398 ax.fill(angles, values, 'b', alpha=0.1) | 398 ax.fill(angles, values, 'b', alpha=0.1) |
399 | 399 |
400 # Ind2 | 400 # Ind2 |
401 values=df.loc[1].drop('group').values.flatten().tolist() | 401 values=df.loc[1].drop('group').values.flatten().tolist() |
402 values += values[:1] | 402 values += values[:1] |
403 ax.plot(angles, values, '-o' ,linewidth=1, linestyle='solid', label=group_name2) | 403 ax.plot(angles, values, '-o' ,linewidth=1, linestyle='solid', label=group_name2.capitalize()) |
404 ax.fill(angles, values, 'r', alpha=0.1) | 404 ax.fill(angles, values, 'r', alpha=0.1) |
405 | 405 |
406 # Add legend | 406 # Add legend |
407 if flag==1: | 407 if flag==1: |
408 plt.legend(loc='upper right', bbox_to_anchor=(0.0, 0.1)) | 408 plt.legend(loc='upper right', prop={'size': 11}, bbox_to_anchor=(0.0, 0.1)) |
409 plt.savefig('spider_non_red.png',dpi=300) | 409 plt.savefig('spider_non_red.png',dpi=300) |
410 else: | 410 else: |
411 plt.legend(loc='upper right', bbox_to_anchor=(0.0, 0.1)) | 411 plt.legend(loc='upper right', prop={'size': 11}, bbox_to_anchor=(0.0, 0.1)) |
412 plt.savefig('spider_red.png',dpi=300) | 412 plt.savefig('spider_red.png',dpi=300) |
413 | 413 |
414 ############################################################################################################################################################################################################# | 414 ############################################################################################################################################################################################################# |
415 | 415 |
416 def hist_red(samples,flag,group): | 416 def hist_red(samples,flag,group): |
419 cat=[] | 419 cat=[] |
420 total_reads=0 | 420 total_reads=0 |
421 seq=[] | 421 seq=[] |
422 | 422 |
423 if flag == "c": | 423 if flag == "c": |
424 title = "Length Distribution of "+group+" group (Redudant reads)" | 424 title = "Length distribution of "+group.lower()+" group (redundant reads)" |
425 if flag == "t": | 425 if flag == "t": |
426 title = "Length Distribution of "+group+" group (Redudant reads)" | 426 title = "Length distribution of "+group.lower()+" group (redundant reads)" |
427 | 427 |
428 for i in samples: | 428 for i in samples: |
429 for x in i: | 429 for x in i: |
430 lengths.append(x[3]) | 430 lengths.append(x[3]) |
431 if x[1]=="0": | 431 if x[1]=="0": |
490 ############################################################################################################################################################################################################################################## | 490 ############################################################################################################################################################################################################################################## |
491 | 491 |
492 def logo_seq_red(merge, flag, group): | 492 def logo_seq_red(merge, flag, group): |
493 | 493 |
494 if flag=="c": | 494 if flag=="c": |
495 titlos = group + " group (Redundant)" | 495 titlos = group + " group (redundant)" |
496 file_logo = "c_logo.png" | 496 file_logo = "c_logo.png" |
497 file_bar = "c_bar.png" | 497 file_bar = "c_bar.png" |
498 if flag=="t": | 498 if flag=="t": |
499 titlos = group + " group (Redundant)" | 499 titlos = group + " group (redundant)" |
500 file_logo = "t_logo.png" | 500 file_logo = "t_logo.png" |
501 file_bar = "t_bar.png" | 501 file_bar = "t_bar.png" |
502 | 502 |
503 c_samples=[[x[0],x[1],sum(int(i) for i in x[2:])] for x in merge] | 503 c_samples=[[x[0],x[1],sum(int(i) for i in x[2:])] for x in merge] |
504 | 504 |
593 pdf.add_page() | 593 pdf.add_page() |
594 # Set font to Arial, 'B'old, 16 pts | 594 # Set font to Arial, 'B'old, 16 pts |
595 pdf.set_font('Arial', 'B', 20.0) | 595 pdf.set_font('Arial', 'B', 20.0) |
596 | 596 |
597 # Page header | 597 # Page header |
598 pdf.cell(pdf.w-0.5, 0.5, 'IsomiR Profile Report',align='C') | 598 pdf.cell(pdf.w-0.5, 0.5, 'IsomiR profile report',align='C') |
599 pdf.ln(0.7) | 599 pdf.ln(0.7) |
600 pdf.set_font('Arial','', 16.0) | 600 pdf.set_font('Arial','', 16.0) |
601 pdf.cell(pdf.w-0.5, 0.5, 'sRNA length distribution',align='C') | 601 pdf.cell(pdf.w-0.5, 0.5, 'sRNA length distribution',align='C') |
602 | 602 |
603 # Smaller font for image captions | 603 # Smaller font for image captions |
611 pdf.image(images[1],x=4,y=yh, w=4, h=3) | 611 pdf.image(images[1],x=4,y=yh, w=4, h=3) |
612 pdf.ln(0.3) | 612 pdf.ln(0.3) |
613 | 613 |
614 # Image caption | 614 # Image caption |
615 pdf.cell(0.2) | 615 pdf.cell(0.2) |
616 pdf.cell(3.0, 0.0, " Mapped and unmapped reads to custom precussor arm reference DB (5p and 3p arms) in "+group_name1) | 616 pdf.cell(3.0, 0.0, " Mapped and unmapped reads to custom precursor arm reference DB (5p and 3p arms) in "+group_name1.lower()) |
617 pdf.ln(0.2) | 617 pdf.ln(0.2) |
618 pdf.cell(0.2) | 618 pdf.cell(0.2) |
619 pdf.cell(3.0, 0.0, " (left) and "+group_name2+" (right) groups") | 619 pdf.cell(3.0, 0.0, " (left) and "+group_name2.lower()+" (right) groups") |
620 | 620 |
621 | 621 |
622 pdf.ln(0.5) | 622 pdf.ln(0.5) |
623 h1=FPDF.get_y(pdf) | 623 h1=FPDF.get_y(pdf) |
624 pdf.image(images[2],x=1, w=6.5, h=5) | 624 pdf.image(images[2],x=1, w=6.5, h=5) |
633 pdf.cell(0.2) | 633 pdf.cell(0.2) |
634 if analysis=="2": | 634 if analysis=="2": |
635 pdf.cell(3.0, 0.0, " RefSeq miRNAs, templated isomiRs, non-templated isomiRs and unassigned sequences as percentage") | 635 pdf.cell(3.0, 0.0, " RefSeq miRNAs, templated isomiRs, non-templated isomiRs and unassigned sequences as percentage") |
636 pdf.ln(0.2) | 636 pdf.ln(0.2) |
637 pdf.cell(0.2) | 637 pdf.cell(0.2) |
638 pdf.cell(3.0, 0.0, " of total sRNA reads in "+group_name1+" (left) and "+group_name2+" (right) groups") | 638 pdf.cell(3.0, 0.0, " of total sRNA reads in "+group_name1.lower()+" (left) and "+group_name2.lower()+" (right) groups") |
639 | 639 |
640 else: | 640 else: |
641 pdf.cell(3.0, 0.0, " RefSeq miRNAS, Templated isomiRs and unassigned sequences as percentage of total sRNA reads in") | 641 pdf.cell(3.0, 0.0, " RefSeq miRNAS, Templated isomiRs and unassigned sequences as percentage of total sRNA reads in") |
642 pdf.ln(0.2) | 642 pdf.ln(0.2) |
643 pdf.cell(0.2) | 643 pdf.cell(0.2) |
644 pdf.cell(3.0, 0.0, " "+group_name1+" (left) and "+group_name2+" (right) groups") | 644 pdf.cell(3.0, 0.0, " "+group_name1.lower()+" (left) and "+group_name2.lower()+" (right) groups") |
645 | 645 |
646 pdf.add_page() | 646 pdf.add_page() |
647 pdf.set_font('Arial', 'B', 18.0) | 647 pdf.set_font('Arial', 'B', 18.0) |
648 pdf.cell(pdf.w-0.5, 0.5, "Templated isomiR subtypes",align='C') | 648 pdf.cell(pdf.w-0.5, 0.5, "Templated isomiR subtypes",align='C') |
649 pdf.ln(0.7) | 649 pdf.ln(0.7) |