comparison mirbase_graphs.py @ 16:fa48ad87ae3e draft

Uploaded
author glogobyte
date Wed, 20 Oct 2021 08:43:37 +0000
parents d77b33e65501
children 3dd4977f3277
comparison
equal deleted inserted replaced
15:37aaa551e5fd 16:fa48ad87ae3e
79 if f==0: 79 if f==0:
80 t_templ+=1 80 t_templ+=1
81 t_tem_counts += x[2] 81 t_tem_counts += x[2]
82 82
83 fig = plt.figure(figsize=(7,5)) 83 fig = plt.figure(figsize=(7,5))
84 labels = 'miRNA RefSeq','Template', 'Unassigned','Non-template' 84 labels = 'miRNA RefSeq','template', 'unassigned','non-template'
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 ax1 = plt.subplot2grid((1,2),(0,0)) 87 ax1 = plt.subplot2grid((1,2),(0,0))
88 patches, texts, autotexts=plt.pie(sizes, labels=labels, colors=colors, startangle=140,autopct='%1.1f%%',radius=0.8) 88 patches, texts, autotexts=plt.pie(sizes, labels=labels, colors=colors, startangle=140,autopct='%1.1f%%',radius=0.8)
89 [x.set_fontsize(8) for x in texts] 89 [x.set_fontsize(10) for x in texts]
90 plt.title(group_name1 + ' Group (reads)',fontsize=12) 90 plt.title(group_name1.capitalize() + ' Group (reads)',fontsize=12)
91 labels = 'miRNA RefSeq','Template', 'Unassigned','non-template' 91 labels = 'miRNA RefSeq','Template', 'Unassigned','non-template'
92 sizes = [t_mat_counts, t_tem_counts, t_unmap_counts, t_non_counts] 92 sizes = [t_mat_counts, t_tem_counts, t_unmap_counts, t_non_counts]
93 colors = ['gold', 'yellowgreen', 'lightcoral', 'lightskyblue'] 93 colors = ['gold', 'yellowgreen', 'lightcoral', 'lightskyblue']
94 ax2 = plt.subplot2grid((1,2),(0,1)) 94 ax2 = plt.subplot2grid((1,2),(0,1))
95 patches, texts, autotexts=plt.pie(sizes, labels=labels, colors=colors, startangle=140,autopct='%1.1f%%',radius=0.8) 95 patches, texts, autotexts=plt.pie(sizes, labels=labels, colors=colors, startangle=140,autopct='%1.1f%%',radius=0.8)
96 [x.set_fontsize(8) for x in texts] 96 [x.set_fontsize(10) for x in texts]
97 plt.title(group_name2 + ' Group (reads)', fontsize=12) 97 plt.title(group_name2.capitalize() + ' Group (reads)', fontsize=12)
98 plt.savefig('pie_non.png',dpi=300) 98 plt.savefig('pie_non.png',dpi=300)
99 99
100 ###################################################################################################################################################### 100 ######################################################################################################################################################
101 101
102 102
156 t_templ+=1 156 t_templ+=1
157 t_tem_counts += x[2] 157 t_tem_counts += x[2]
158 158
159 159
160 fig = plt.figure() 160 fig = plt.figure()
161 labels = 'miRNA RefSeq','Template', 'Unassigned' 161 labels = 'miRNA RefSeq','template', 'unassigned'
162 sizes = [c_mat_counts, c_tem_counts, c_unmap_counts] 162 sizes = [c_mat_counts, c_tem_counts, c_unmap_counts]
163 colors = ['gold', 'yellowgreen', 'lightskyblue'] 163 colors = ['gold', 'yellowgreen', 'lightskyblue']
164 explode = (0.2, 0.05, 0.1) 164 explode = (0.2, 0.05, 0.1)
165 ax1 = plt.subplot2grid((1,2),(0,0)) 165 ax1 = plt.subplot2grid((1,2),(0,0))
166 patches, texts, autotexts=plt.pie(sizes, labels=labels, colors=colors, startangle=140,autopct='%1.1f%%',radius=0.8) 166 patches, texts, autotexts=plt.pie(sizes, labels=labels, colors=colors, startangle=140,autopct='%1.1f%%',radius=0.8)
167 [x.set_fontsize(8) for x in texts] 167 [x.set_fontsize(10) for x in texts]
168 plt.title(group_name1 + ' group (reads)', fontsize=12) 168 plt.title(group_name1.capitalize() + ' group (reads)', fontsize=12)
169 labels = 'miRNA RefSeq','Template', 'Unassigned' 169 labels = 'miRNA RefSeq','Template', 'Unassigned'
170 sizes = [t_mat_counts, t_tem_counts, t_unmap_counts] 170 sizes = [t_mat_counts, t_tem_counts, t_unmap_counts]
171 colors = ['gold', 'yellowgreen', 'lightskyblue'] 171 colors = ['gold', 'yellowgreen', 'lightskyblue']
172 explode = (0.2, 0.05, 0.1) 172 explode = (0.2, 0.05, 0.1)
173 ax2 = plt.subplot2grid((1,2),(0,1)) 173 ax2 = plt.subplot2grid((1,2),(0,1))
174 patches, texts, autotexts=plt.pie(sizes, labels=labels, colors=colors, startangle=140,autopct='%1.1f%%',radius=0.8) 174 patches, texts, autotexts=plt.pie(sizes, labels=labels, colors=colors, startangle=140,autopct='%1.1f%%',radius=0.8)
175 [x.set_fontsize(8) for x in texts] 175 [x.set_fontsize(10) for x in texts]
176 plt.title(group_name2 + ' group (reads)',fontsize = 12) 176 plt.title(group_name2.capitalize() + ' group (reads)',fontsize = 12)
177 plt.savefig('pie_tem.png',dpi=300) 177 plt.savefig('pie_tem.png',dpi=300)
178 178
179 ################################################################################################################################################################################################################### 179 ###################################################################################################################################################################################################################
180 180
181 181
299 299
300 radar_max = max(c_5, c_3, c_both,c_mature,c_exception,t_5,t_3,t_both,t_mature,t_exception) 300 radar_max = max(c_5, c_3, c_both,c_mature,c_exception,t_5,t_3,t_both,t_mature,t_exception)
301 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) 301 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)
302 302
303 df=pd.DataFrame({ 303 df=pd.DataFrame({
304 'group':[group_name1,group_name2], 304 'group':[group_name1.capitalize(),group_name2.capitalize()],
305 """5'3'-isomiRs""":[c_both,t_both], 305 """5'3'-isomiRs""":[c_both,t_both],
306 """3'-isomiRs""":[c_3,t_3], 306 """3'-isomiRs""":[c_3,t_3],
307 'RefSeq miRNA':[c_mature,t_mature], 307 'RefSeq miRNA':[c_mature,t_mature],
308 """5'-isomiRs""":[c_5,t_5], 308 """5'-isomiRs""":[c_5,t_5],
309 'Others*':[c_exception,t_exception]}) 309 'others*':[c_exception,t_exception]})
310 310
311 df1=pd.DataFrame({ 311 df1=pd.DataFrame({
312 'group':[group_name1,group_name2], 312 'group':[group_name1.capitalize(),group_name2.capitalize()],
313 """5'3'-isomiRs""":[c_both_counts,t_both_counts], 313 """5'3'-isomiRs""":[c_both_counts,t_both_counts],
314 """3'-isomiRs""":[c_3_counts,t_3_counts], 314 """3'-isomiRs""":[c_3_counts,t_3_counts],
315 'RefSeq miRNA':[c_mat_counts,t_mat_counts], 315 'RefSeq miRNA':[c_mat_counts,t_mat_counts],
316 """5'-isomiRs""":[c_5_counts,t_5_counts], 316 """5'-isomiRs""":[c_5_counts,t_5_counts],
317 'Others*':[c_exception_counts,t_exception_counts]}) 317 'others*':[c_exception_counts,t_exception_counts]})
318 318
319 spider_last(df,radar_max,1,group_name1,group_name2) 319 spider_last(df,radar_max,1,group_name1,group_name2)
320 spider_last(df1,radar_max_counts,2,group_name1,group_name2) 320 spider_last(df1,radar_max_counts,2,group_name1,group_name2)
321 321
322 ##################################################################################################################################################### 322 #####################################################################################################################################################
338 # If you want the first axis to be on top: 338 # If you want the first axis to be on top:
339 ax.set_theta_offset(pi/2) 339 ax.set_theta_offset(pi/2)
340 ax.set_theta_direction(-1) 340 ax.set_theta_direction(-1)
341 341
342 # Draw one axe per variable + add labels labels yet 342 # Draw one axe per variable + add labels labels yet
343 plt.xticks(angles[:-1], categories, fontsize=11) 343 plt.xticks(angles[:-1], categories, fontsize=13)
344 344
345 # Draw ylabels 345 # Draw ylabels
346 radar_max=round(radar_max+radar_max*0.1) 346 radar_max=round(radar_max+radar_max*0.1)
347 mul=len(str(radar_max))-1 347 mul=len(str(radar_max))-1
348 maxi=int(math.ceil(radar_max / pow(10,mul))) * pow(10,mul) 348 maxi=int(math.ceil(radar_max / pow(10,mul))) * pow(10,mul)
349 sep = round(maxi/4) 349 sep = round(maxi/4)
350 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) 350 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)
351 plt.ylim(0, maxi) 351 plt.ylim(0, maxi)
352 352
353 # ------- PART 2: Add plots 353 # ------- PART 2: Add plots
354 354
355 # Plot each individual = each line of the data 355 # Plot each individual = each line of the data
356 # I don't do a loop, because plotting more than 3 groups makes the chart unreadable 356 # I don't do a loop, because plotting more than 2 groups makes the chart unreadable
357 357
358 # Ind1 358 # Ind1
359 values=df.loc[0].drop('group').values.flatten().tolist() 359 values=df.loc[0].drop('group').values.flatten().tolist()
360 values += values[:1] 360 values += values[:1]
361 ax.plot(angles, values,'-o', linewidth=1, linestyle='solid', label=group_name1) 361 ax.plot(angles, values,'-o', linewidth=1, linestyle='solid', label=group_name1.capitalize())
362 ax.fill(angles, values, 'b', alpha=0.1) 362 ax.fill(angles, values, 'b', alpha=0.1)
363 363
364 # Ind2 364 # Ind2
365 values=df.loc[1].drop('group').values.flatten().tolist() 365 values=df.loc[1].drop('group').values.flatten().tolist()
366 values += values[:1] 366 values += values[:1]
367 ax.plot(angles, values, '-o' ,linewidth=1, linestyle='solid', label=group_name2) 367 ax.plot(angles, values, '-o' ,linewidth=1, linestyle='solid', label=group_name2.capitalize())
368 ax.fill(angles, values, 'r', alpha=0.1) 368 ax.fill(angles, values, 'r', alpha=0.1)
369 369
370 # Add legend 370 # Add legend
371 if flag==1: 371 if flag==1:
372 plt.legend(loc='upper right', bbox_to_anchor=(0.0, 0.1)) 372 plt.legend(loc='upper right', prop={'size': 11}, bbox_to_anchor=(0.0, 0.1))
373 plt.savefig('spider_non_red.png',dpi=300) 373 plt.savefig('spider_non_red.png',dpi=300)
374 else: 374 else:
375 plt.legend(loc='upper right', bbox_to_anchor=(0.0, 0.1)) 375 plt.legend(loc='upper right', prop={'size': 11}, bbox_to_anchor=(0.0, 0.1))
376 plt.savefig('spider_red.png',dpi=300) 376 plt.savefig('spider_red.png',dpi=300)
377 377
378 378
379 ############################################################################################################################################################################################################# 379 #############################################################################################################################################################################################################
380 380
384 cat=[] 384 cat=[]
385 total_reads=0 385 total_reads=0
386 seq=[] 386 seq=[]
387 387
388 if flag == "c": 388 if flag == "c":
389 title = "Length Distribution of "+ group_name +" group (Redudant reads)" 389 title = "Length distribution of "+ group_name.lower() +" group (redundant reads)"
390 if flag == "t": 390 if flag == "t":
391 title = "Length Distribution of "+ group_name +" group (Redudant reads)" 391 title = "Length distribution of "+ group_name.lower() +" group (redundant reads)"
392 392
393 # classification of the sequences on two categories mapped or unmapped 393 # classification of the sequences on two categories mapped or unmapped
394 for i in samples: 394 for i in samples:
395 for x in i: 395 for x in i:
396 lengths.append(x[3]) 396 lengths.append(x[3])
452 ################################################################################################################# 452 #################################################################################################################
453 453
454 def logo_seq_red(merge, flag, group_name): 454 def logo_seq_red(merge, flag, group_name):
455 455
456 if flag=="c": 456 if flag=="c":
457 titlos= group_name + " group (Redundant)" 457 titlos= group_name + " group (redundant)"
458 file_logo="c_logo.png" 458 file_logo="c_logo.png"
459 file_bar="c_bar.png" 459 file_bar="c_bar.png"
460 if flag=="t": 460 if flag=="t":
461 titlos= group_name + " group (Redundant)" 461 titlos= group_name + " group (redundant)"
462 file_logo="t_logo.png" 462 file_logo="t_logo.png"
463 file_bar="t_bar.png" 463 file_bar="t_bar.png"
464 464
465 c_samples=[[x[0],x[1],sum(int(i) for i in x[2:])] for x in merge] 465 c_samples=[[x[0],x[1],sum(int(i) for i in x[2:])] for x in merge]
466 466
557 pdf.add_page() 557 pdf.add_page()
558 # Set font to Arial, 'B'old, 16 pts 558 # Set font to Arial, 'B'old, 16 pts
559 pdf.set_font('Arial', 'B', 20.0) 559 pdf.set_font('Arial', 'B', 20.0)
560 560
561 # Page header 561 # Page header
562 pdf.cell(pdf.w-0.5, 0.5, 'IsomiR Profile Report',align='C') 562 pdf.cell(pdf.w-0.5, 0.5, 'IsomiR profile report',align='C')
563 pdf.ln(0.7) 563 pdf.ln(0.7)
564 pdf.set_font('Arial','B', 16.0) 564 pdf.set_font('Arial','B', 16.0)
565 pdf.cell(pdf.w-0.5, 0.5, 'sRNA length distribution',align='C') 565 pdf.cell(pdf.w-0.5, 0.5, 'sRNA length distribution',align='C')
566 566
567 # Smaller font for image captions 567 # Smaller font for image captions
572 pdf.image(images[0],x=0.3,w=4, h=3) 572 pdf.image(images[0],x=0.3,w=4, h=3)
573 pdf.image(images[1],x=4,y=yh, w=4, h=3) 573 pdf.image(images[1],x=4,y=yh, w=4, h=3)
574 pdf.ln(0.3) 574 pdf.ln(0.3)
575 575
576 pdf.cell(0.2) 576 pdf.cell(0.2)
577 pdf.cell(3.0, 0.0, " Mapped and unmapped reads to custom precussor arm reference DB (5p and 3p arms) in "+group_name1) 577 pdf.cell(3.0, 0.0, " Mapped and unmapped reads to custom precursor arm reference DB (5p and 3p arms) in "+group_name1.lower())
578 pdf.ln(0.2) 578 pdf.ln(0.2)
579 pdf.cell(0.2) 579 pdf.cell(0.2)
580 pdf.cell(3.0, 0.0, " (left) and "+group_name2+" (right) groups") 580 pdf.cell(3.0, 0.0, " (left) and "+group_name2.lower()+" (right) groups")
581 581
582 pdf.ln(0.5) 582 pdf.ln(0.5)
583 h1=FPDF.get_y(pdf) 583 h1=FPDF.get_y(pdf)
584 pdf.image(images[2],x=1, w=6.5, h=5) 584 pdf.image(images[2],x=1, w=6.5, h=5)
585 h2=FPDF.get_y(pdf) 585 h2=FPDF.get_y(pdf)
593 593
594 if analysis=="2": 594 if analysis=="2":
595 pdf.cell(3.0, 0.0, " RefSeq miRNAs, templated isomiRs, non-templated isomiRs and unassigned sequences as percentage") 595 pdf.cell(3.0, 0.0, " RefSeq miRNAs, templated isomiRs, non-templated isomiRs and unassigned sequences as percentage")
596 pdf.ln(0.2) 596 pdf.ln(0.2)
597 pdf.cell(0.2) 597 pdf.cell(0.2)
598 pdf.cell(3.0, 0.0, " of total sRNA reads in "+group_name1+" (left) and "+group_name2+" (right) groups") 598 pdf.cell(3.0, 0.0, " of total sRNA reads in "+group_name1.lower()+" (left) and "+group_name2.lower()+" (right) groups")
599 else: 599 else:
600 pdf.cell(3.0, 0.0, " RefSeq miRNAS, Templated isomiRs and unassigned sequences as percentage of total sRNA reads in") 600 pdf.cell(3.0, 0.0, " RefSeq miRNAS, Templated isomiRs and unassigned sequences as percentage of total sRNA reads in")
601 pdf.ln(0.2) 601 pdf.ln(0.2)
602 pdf.cell(0.2) 602 pdf.cell(0.2)
603 pdf.cell(3.0, 0.0, " "+group_name1+" (left) and "+group_name2 + " (right) groups") 603 pdf.cell(3.0, 0.0, " "+group_name1.lower()+" (left) and "+group_name2.lower() + " (right) groups")
604 604
605 pdf.add_page() 605 pdf.add_page()
606 pdf.set_font('Arial', 'B', 18.0) 606 pdf.set_font('Arial', 'B', 18.0)
607 pdf.cell(pdf.w-0.5, 0.5, "Templated isomiR subtypes",align='C') 607 pdf.cell(pdf.w-0.5, 0.5, "Templated isomiR subtypes",align='C')
608 pdf.ln(0.7) 608 pdf.ln(0.7)