annotate viz_graphs.py @ 8:fa26ea53c055 draft

Uploaded
author glogobyte
date Wed, 13 Oct 2021 11:20:06 +0000
parents
children f00fce910645
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
1 # Import FPDF class
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
2 from fpdf import FPDF
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
3
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
4 # Import glob module to find all the files matching a pattern
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
5 import glob
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
6
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
7 def pdf_before_DE(analysis):
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
8
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
9 # Image extensions
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
10 if analysis=="2":
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
11 image_extensions = ("c_hist_red.png","t_hist_red.png","pie_non.png","spider_red.png","spider_non_red.png","c_logo.png","t_logo.png","c_bar.png","t_bar.png")
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
12 else:
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
13 image_extensions = ("c_hist_red.png","t_hist_red.png","pie_tem.png","spider_red.png","spider_non_red.png")
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
14 # This list will hold the images file names
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
15 images = []
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
16
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
17 # Build the image list by merging the glob results (a list of files)
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
18 # for each extension. We are taking images from current folder.
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
19 for extension in image_extensions:
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
20 images.extend(glob.glob(extension))
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
21
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
22 # Create instance of FPDF class
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
23 pdf = FPDF('P', 'in', 'A4')
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
24 # Add new page. Without this you cannot create the document.
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
25 pdf.add_page()
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
26 # Set font to Arial, 'B'old, 16 pts
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
27 pdf.set_font('Arial', 'B', 20.0)
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
28
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
29 # Page header
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
30 pdf.cell(pdf.w-0.5, 0.5, 'IsomiR Profile Report',align='C')
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
31 pdf.ln(0.7)
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
32 pdf.set_font('Arial','', 16.0)
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
33 pdf.cell(pdf.w-0.5, 0.5, 'sRNA Length Distribution',align='C')
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
34
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
35 # Smaller font for image captions
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
36 pdf.set_font('Arial', '', 11.0)
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
37
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
38 # Image caption
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
39 pdf.ln(0.5)
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
40
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
41 yh=FPDF.get_y(pdf)
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
42 pdf.image(images[0],x=0.3,w=4, h=3)
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
43 pdf.image(images[1],x=4,y=yh, w=4, h=3)
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
44 pdf.ln(0.3)
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
45
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
46 # Image caption
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
47 pdf.cell(0.2)
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
48 pdf.cell(3.0, 0.0, " Mapped and unmapped reads to custom precussor arm reference DB (5p and 3p arms) in Control (left)")
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
49 pdf.ln(0.2)
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
50 pdf.cell(0.2)
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
51 pdf.cell(3.0, 0.0, " and Treated (right) groups")
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
52
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
53
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
54 pdf.ln(0.5)
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
55 h1=FPDF.get_y(pdf)
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
56 pdf.image(images[2],x=1, w=6.5, h=5)
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
57 h2=FPDF.get_y(pdf)
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
58 FPDF.set_y(pdf,h1+0.2)
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
59 pdf.set_font('Arial','', 14.0)
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
60 pdf.cell(pdf.w-0.5, 0.5, 'Template and non-template IsomiRs',align='C')
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
61 pdf.set_font('Arial', '', 11.0)
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
62 FPDF.set_y(pdf,h2)
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
63 FPDF.set_y(pdf,9.5)
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
64 # Image caption
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
65 pdf.cell(0.2)
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
66 if analysis=="2":
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
67 pdf.cell(3.0, 0.0, " Template, non-template, miRNA reference and unmapped sequences as percentage of total sRNA")
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
68 else:
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
69 pdf.cell(3.0, 0.0, " Template, miRNA reference and unmapped sequences as percentage of total sRNA")
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
70 pdf.ln(0.2)
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
71 pdf.cell(0.2)
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
72 pdf.cell(3.0, 0.0, " reads in Control (left) and treated (right) groups")
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
73
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
74
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
75
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
76 pdf.add_page()
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
77 pdf.set_font('Arial', 'B', 16.0)
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
78 pdf.cell(pdf.w-0.5, 0.5, "Reference form and isomiR among total miRNA reads",align='C')
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
79 pdf.ln(0.7)
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
80 pdf.set_font('Arial', 'B', 12.0)
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
81 pdf.cell(pdf.w-0.5, 0.5, "Template isomiR profile (redundant)",align='C')
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
82 pdf.ln(0.5)
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
83 pdf.image(images[3],x=1.5, w=5.5, h=4)
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
84 pdf.ln(0.6)
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
85 pdf.cell(pdf.w-0.5, 0.0, "Template isomiR profile (non-redundant)",align='C')
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
86 pdf.set_font('Arial', '', 12.0)
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
87 pdf.ln(0.2)
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
88 pdf.image(images[4],x=1.5, w=5.5, h=4)
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
89 pdf.ln(0.3)
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
90 pdf.set_font('Arial', '', 11.0)
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
91 pdf.cell(0.2)
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
92 pdf.cell(3.0, 0.0, " * IsomiRs potentialy initiated from multiple loci")
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
93
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
94
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
95 if analysis=="2":
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
96 pdf.add_page('L')
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
97
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
98 pdf.set_font('Arial', 'B', 16.0)
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
99 pdf.cell(pdf.w-0.5, 0.5, "Non-template IsomiRs",align='C')
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
100 pdf.ln(0.5)
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
101 pdf.set_font('Arial', 'B', 12.0)
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
102 pdf.cell(pdf.w-0.5, 0.5, "3' Additions of reference of isomiR sequence",align='C')
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
103 pdf.ln(0.7)
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
104
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
105 yh=FPDF.get_y(pdf)
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
106 pdf.image(images[5],x=1.5,w=3.65, h=2.65)
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
107 pdf.image(images[7],x=6.5,y=yh, w=3.65, h=2.65)
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
108 pdf.ln(0.5)
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
109 yh=FPDF.get_y(pdf)
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
110 pdf.image(images[6],x=1.5,w=3.65, h=2.65)
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
111 pdf.image(images[8],x=6.5,y=yh, w=3.65, h=2.65)
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
112
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
113 pdf.close()
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
114 pdf.output('report1.pdf','F')
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
115
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
116
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
117
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
118
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
119 #############################################################################################################################################################3
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
120
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
121 def pdf_after_DE(analysis,top,font_path,iso_star_fl,non_star_fl):
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
122
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
123 # Image extensions
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
124 if analysis=="2":
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
125 image_extensions = ("tem.png","a2.png","non.png")
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
126 else:
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
127 image_extensions = ("tem.png","a2.png")
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
128
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
129 # This list will hold the images file names
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
130 images = []
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
131
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
132 # Build the image list by merging the glob results (a list of files)
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
133 # for each extension. We are taking images from current folder.
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
134 for extension in image_extensions:
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
135 images.extend(glob.glob(extension))
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
136
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
137 # Create instance of FPDF class
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
138 pdf = FPDF('P', 'in', 'letter')
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
139 pdf.add_font('uni-arial', '', font_path+"/arial-unicode-ms.ttf", uni=True)
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
140 # Add new page. Without this you cannot create the document.
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
141 pdf.add_page()
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
142 # Set font to Arial, 'B'old, 16 pts
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
143 pdf.set_font('Arial', 'B', 16.0)
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
144
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
145 # Page header
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
146 pdf.cell(pdf.w-0.5, 0.5, 'Differential expression of miRNAs and isomiRs',align='C')
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
147 #pdf.ln(0.25)
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
148
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
149 pdf.ln(0.7)
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
150 pdf.set_font('Arial','B', 12.0)
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
151 if "tem.png" in images:
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
152 pdf.cell(pdf.w-0.5, 0.5, 'Top '+top+' differentially expressed miRNA and templated isoforms',align='C')
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
153 # Smaller font for image captions
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
154 pdf.set_font('Arial', '', 10.0)
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
155 # Image caption
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
156 pdf.ln(0.4)
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
157 pdf.image(images[images.index("tem.png")],x=0.8, w=7, h=8)
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
158 pdf.ln(0.3)
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
159 if iso_star_fl==1:
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
160 pdf.set_font('uni-arial', '', 9.0)
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
161 pdf.cell(0.2)
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
162 pdf.cell(3.0, 0.0, " ★ IsomiRs potentially generated from multiple loci")
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
163 #pdf.set_font('Arial','B', 12.0)
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
164 else:
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
165 print("WARNING: There aren't miRNAs which fullfiled these criteria" )
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
166 pdf.set_font('Arial','B', 12.0)
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
167 if "non.png" in images and analysis=="2":
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
168 if "tem.png" in images: pdf.add_page()
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
169 pdf.ln(0.7)
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
170 pdf.cell(pdf.w-0.5, 0.5, 'Top '+top+' differentially expressed non-templated isomiRs',align='C')
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
171 pdf.ln(0.4)
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
172 pdf.image(images[images.index("non.png")],x=0.8, w=7, h=8)
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
173 pdf.ln(0.3)
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
174 if non_star_fl==1:
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
175 pdf.set_font('uni-arial', '', 9.0)
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
176 pdf.cell(0.2)
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
177 pdf.cell(3.0, 0.0, " ★ IsomiRs potentially generated from multiple loci")
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
178
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
179 #pdf.image(images[images.index("non.png")],x=0.5, w=7.5, h=6.5)
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
180 else:
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
181 print("WARNING: There aren't non-template miRNAs which fullfiled these criteria" )
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
182
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
183 pdf.set_font('Arial','B', 12.0)
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
184 if "a2.png" in images:
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
185 if len(images)>=2: pdf.add_page()
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
186 pdf.ln(0.5)
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
187 pdf.cell(pdf.w-0.5, 0.5, 'Top '+top+' differentially expressed miRNAs and isomiRs grouped by arm',align='C')
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
188 pdf.ln(0.4)
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
189 pdf.image(images[images.index("a2.png")],x=0.8, w=7, h=8)
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
190 pdf.ln(0.3)
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
191 else:
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
192 print("WARNING: There aren't non-template miRNAs which fullfiled these criteria" )
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
193
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
194
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
195 pdf.output('report2.pdf', 'F')
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
196
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
197
fa26ea53c055 Uploaded
glogobyte
parents:
diff changeset
198