Mercurial > repos > jvolkening > nanopore_qc
comparison yaml_to_html.pl @ 0:e0006d8bf849 draft
planemo upload for repository https://github.com/jvolkening/galaxy-tools/tree/master/tools/nanopore_qc commit b99a7d95d62b95ececc9d808f5f183b9eb718f80-dirty
author | jvolkening |
---|---|
date | Sat, 02 Mar 2024 03:35:34 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:e0006d8bf849 |
---|---|
1 #!/usr/bin/env perl | |
2 | |
3 use strict; | |
4 use warnings; | |
5 use 5.012; | |
6 | |
7 use YAML::XS qw/LoadFile/; | |
8 use MIME::Base64; | |
9 use autodie; | |
10 | |
11 my ($fn_yaml, $dir_in, $fn_out) = @ARGV; | |
12 | |
13 die "Can't find or read input file: $!\n" | |
14 if (! -r $fn_yaml); | |
15 | |
16 # set output filehandle based on arguments | |
17 my $fh = \*STDOUT; | |
18 if (defined $fn_out) { | |
19 open $fh, '>', $fn_out; | |
20 } | |
21 | |
22 my $yaml = LoadFile($fn_yaml); | |
23 | |
24 convert($yaml, $dir_in); | |
25 | |
26 sub convert { | |
27 | |
28 my ($yaml) = @_; | |
29 | |
30 print {$fh} header(); | |
31 | |
32 say {$fh} " <h3>Summary statistics</h3>"; | |
33 | |
34 for my $grp (sort keys %$yaml) { | |
35 | |
36 my $ref = $yaml->{$grp}; | |
37 | |
38 next if (! ref $ref); | |
39 next if (! defined $ref->{'total.gigabases'}); | |
40 | |
41 print {$fh} <<"CONTENT" | |
42 <table> | |
43 <caption>$grp</caption> | |
44 <tr> | |
45 <td>Total Yield (Gb)</td> | |
46 <td>$ref->{'total.gigabases'}</td> | |
47 </tr> | |
48 <tr> | |
49 <td>Total Reads</td> | |
50 <td>$ref->{'total.reads'}</td> | |
51 </tr> | |
52 <tr> | |
53 <td>Mean Length</td> | |
54 <td>$ref->{'mean.length'}</td> | |
55 </tr> | |
56 <tr> | |
57 <td>Median Length</td> | |
58 <td>$ref->{'median.length'}</td> | |
59 </tr> | |
60 <tr> | |
61 <td>Max Length</td> | |
62 <td>$ref->{'max.length'}</td> | |
63 </tr> | |
64 <tr> | |
65 <td>Mean Q</td> | |
66 <td>$ref->{'mean.q'}</td> | |
67 </tr> | |
68 <tr> | |
69 <td>Median Q</td> | |
70 <td>$ref->{'median.q'}</td> | |
71 </tr> | |
72 </table> | |
73 CONTENT | |
74 | |
75 } | |
76 | |
77 my %figs = ( | |
78 'length_histogram' => "Read length distribution", | |
79 'q_histogram' => "Mean quality score distribution", | |
80 'reads_per_hour' => "Yield over time", | |
81 'cumulative_yield' => "Cumulative yield over time", | |
82 'yield_summary' => "Yield by read length cutoff", | |
83 'flowcell_overview' => "Median read quality per channel", | |
84 'length_by_hour' => "Read length over time", | |
85 'q_by_hour' => "Read quality over time", | |
86 'length_vs_q' => "Read length vs. quality", | |
87 ); | |
88 | |
89 my @order = qw/ | |
90 length_histogram | |
91 q_histogram | |
92 reads_per_hour | |
93 cumulative_yield | |
94 yield_summary | |
95 flowcell_overview | |
96 length_by_hour | |
97 q_by_hour | |
98 length_vs_q | |
99 /; | |
100 | |
101 | |
102 say {$fh} " <h3>QC plots</h3>"; | |
103 say {$fh} " <p>(Click on plot for high-resolution version, or in Chrome \"Open link in new tab\")</p>"; | |
104 | |
105 for my $base (@order) { | |
106 | |
107 my $caption = $figs{$base} // die "No caption found for $base"; | |
108 | |
109 # Base64-encode images | |
110 my $fn_img_full = "$dir_in/$base.png"; | |
111 my $fn_img_screen = "$dir_in/$base.screen.png"; | |
112 die "Failed to find or read $fn_img_full" | |
113 if (! -r $fn_img_full); | |
114 die "Failed to find or read $fn_img_screen" | |
115 if (! -r $fn_img_screen); | |
116 my $img_full = encode($fn_img_full); | |
117 my $img_screen = encode($fn_img_screen); | |
118 | |
119 print {$fh} <<"CONTENT" | |
120 <a href="data:image/png;base64,$img_full"> | |
121 <figure> | |
122 <img src="data:image/png;base64,$img_screen" alt="$base" /> | |
123 <figcaption>$caption</figcaption> | |
124 </figure> | |
125 </a> | |
126 CONTENT | |
127 | |
128 } | |
129 | |
130 print {$fh} footer(); | |
131 | |
132 } | |
133 | |
134 sub encode { | |
135 | |
136 my ($fn) = @_; | |
137 open my $in, '<:raw', $fn; | |
138 local($/) = undef; | |
139 return encode_base64(<$in>); | |
140 | |
141 } | |
142 | |
143 sub header { | |
144 | |
145 return <<'HEADER'; | |
146 <?xml version="1.0" encoding="utf-8"?> | |
147 <!DOCTYPE html> | |
148 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> | |
149 <head> | |
150 <title></title> | |
151 <meta http-equiv="content-type" content="application/xhtml+xml; charset=utf-8" /> | |
152 <style> | |
153 h2 { | |
154 padding: 0.3em; | |
155 background-color: #000000; | |
156 color: #ffffff; | |
157 margin: 1em 0 2em 0; | |
158 } | |
159 h3 { | |
160 padding: 0em 0.2em 0em 0.2em; | |
161 color: #555555; | |
162 border: solid 1px black; | |
163 border-width: 0px 0px 1px 0px; | |
164 margin: 2em 0 0.4em 0; | |
165 } | |
166 tr { | |
167 margin: 0; | |
168 } | |
169 tr:nth-child(even) { | |
170 background-color: #bbbbbb; | |
171 } | |
172 tr:nth-child(odd) { | |
173 background-color: #eeeeee; | |
174 } | |
175 caption { | |
176 text-align: left; | |
177 font-weight: bold; | |
178 background-color: #550000; | |
179 color: #ffffff; | |
180 padding: 0.1em 0.2em; | |
181 } | |
182 table { | |
183 margin: 1em; | |
184 padding: 0.3em; | |
185 } | |
186 td { | |
187 margin: 0; | |
188 padding: 0 0.4em; | |
189 } | |
190 tr td:nth-child(1) { | |
191 color: #550000; | |
192 } | |
193 figure { | |
194 display: table; | |
195 margin: 2em 0; | |
196 } | |
197 figcaption { | |
198 display: table-caption; | |
199 caption-side: top; | |
200 font-size: 1.1em; | |
201 text-decoration: none; | |
202 text-align: center; | |
203 font-weight: bold; | |
204 background-color: #550000; | |
205 color: #ffffff; | |
206 padding: 0.1em 0.2em; | |
207 margin: 2em 0 0.7em 0; | |
208 } | |
209 | |
210 </style> | |
211 </head> | |
212 <body> | |
213 | |
214 <h2>NanoporeQC Report</h2> | |
215 HEADER | |
216 | |
217 } | |
218 | |
219 sub footer { | |
220 | |
221 return <<'FOOTER'; | |
222 | |
223 </body> | |
224 </html> | |
225 FOOTER | |
226 | |
227 } |