]> git.donarmstrong.com Git - biopieces.git/blob - bp_scripts/QA_454_report.rb
8c079f379ba9de4f658a88214e4ed99245d3d55f
[biopieces.git] / bp_scripts / QA_454_report.rb
1 #!/usr/bin/env ruby
2
3 # Copyright (C) 2011 Martin A. Hansen (mail@maasha.dk).
4
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
19 # http://www.gnu.org/copyleft/gpl.html
20
21 require 'pp'
22 require 'tmpdir'
23 require 'base64'
24 require 'erb'
25
26 class Report
27   def initialize(sff_file)
28     @sff_file = sff_file
29     tmpdir    = Dir.mktmpdir
30
31     @seq_analysis   = SeqAnalyze.new(@sff_file, tmpdir)
32     @plots          = PlotData.new(@sff_file, tmpdir)
33     @table_mid_join = MidTable.new(@sff_file, tmpdir)
34   end
35
36   # Support templating of member data.
37   def get_binding
38     binding
39   end
40
41   private
42
43   class SeqAnalyze
44     attr_reader :count, :min, :max, :mean, :bases, :gc, :hard, :soft
45
46     def initialize(sff_file, tmpdir)
47       @sff_file  = sff_file
48       @out1_file = File.join(tmpdir, "out1.txt")
49       @out2_file = File.join(tmpdir, "out2.txt")
50       @count     = 0
51       @min       = 0
52       @max       = 0
53       @mean      = 0
54       @bases     = 0
55       @gc        = 0
56       @hard      = 0
57       @soft      = 0
58
59       bp_seq_analyze
60       parse_analyze_vals
61       parse_mean_vals
62     end
63
64     private
65
66     def bp_seq_analyze
67       STDERR.puts "Analyzing sequences ... "
68       system(
69         "read_sff -i #{@sff_file} |
70          progress_meter |
71          analyze_vals -k SEQ -o #{@out1_file} |
72          analyze_seq |
73          mean_vals -k 'GC%,HARD_MASK%,SOFT_MASK%' -o #{@out2_file} -x"
74        )
75       STDERR.puts "done.\n"
76     end
77
78     def parse_analyze_vals
79       File.open(@out1_file, "r") do |ios|
80         while not ios.eof?
81           line = ios.readline.chomp
82
83           case line
84           when /COUNT\s+(\d+)/; then @count = $1
85           when /MIN\s+(\d+)/;   then @min   = $1
86           when /MAX\s+(\d+)/;   then @max   = $1
87           when /MEAN\s+(\d+)/;  then @mean  = $1
88           when /SUM\s+(\d+)/;   then @bases = $1
89           end
90         end
91       end
92     end
93
94     def parse_mean_vals
95       File.open(@out2_file, "r") do |ios|
96         while not ios.eof?
97           line = ios.readline.chomp
98
99           case line
100           when /GC%_MEAN: (.+)/;        then @gc   = $1
101           when /HARD_MASK%_MEAN: (.+)/; then @hard = $1
102           when /SOFT_MASK%_MEAN: (.+)/; then @soft = $1
103           end
104         end
105       end
106     end
107   end
108
109   class PlotData
110     attr_reader :lendist_unclipped, :lendist_clipped, :scores_unclipped, :scores_clipped, :mean_scores, :nucleotide_dist500, :nucleotide_dist50
111
112     def initialize(sff_file, tmpdir)
113       @sff_file = sff_file
114       @plot1    = File.join(tmpdir, "plot1.png")
115       @plot2    = File.join(tmpdir, "plot2.png")
116       @plot3    = File.join(tmpdir, "plot3.png")
117       @plot4    = File.join(tmpdir, "plot4.png")
118       @plot5    = File.join(tmpdir, "plot5.png")
119       @plot6    = File.join(tmpdir, "plot6.png")
120       @plot7    = File.join(tmpdir, "plot7.png")
121
122       bp_plot
123
124       @lendist_unclipped  = png2base64(@plot1)
125       @lendist_clipped    = png2base64(@plot3)
126       @scores_unclipped   = png2base64(@plot2)
127       @scores_clipped     = png2base64(@plot4)
128       @mean_scores        = png2base64(@plot5)
129       @nucleotide_dist500 = png2base64(@plot6)
130       @nucleotide_dist50  = png2base64(@plot7)
131     end
132
133     def bp_plot
134       STDERR.puts "Creating plots ... "
135       system(
136         "read_sff -m -i #{@sff_file} |
137          progress_meter |
138          plot_distribution -k SEQ_LEN -T 'Length Distribution - unclipped' -t png -o #{@plot1} |
139          plot_scores -c -T 'Mean Quality Scores - unclipped' -t png -o #{@plot2} |
140          clip_seq |
141          plot_distribution -k SEQ_LEN -T 'Length Distribution - clipped' -t png -o #{@plot3} |
142          plot_scores -c -T 'Mean Quality Scores - clipped' -t png -o #{@plot4} |
143          mean_scores |
144          bin_vals -k SCORES_MEAN -b 5 |
145          plot_histogram -s num -k SCORES_MEAN_BIN -T 'Mean score bins' -X 'Bins (size 5)' -Y 'Count' -t png -o #{@plot5} |
146          extract_seq -l 500 |
147          plot_nucleotide_distribution -c -t png -o #{@plot6} |
148          extract_seq -l 50 |
149          plot_nucleotide_distribution -t png -o #{@plot7} -x"
150       )
151       STDERR.puts "done.\n"
152     end
153
154     private
155
156     def png2base64(file)
157       png = ""
158
159       File.open(file, "r") do |ios|
160         png = ios.read
161       end
162
163       "data:image/png;base64," + Base64.encode64(png)
164     end
165   end
166
167   class MidTable
168     def initialize(sff_file, tmpdir)
169       @sff_file  = sff_file
170       @mid1_file = File.join(tmpdir, "mid1.tab")
171       @mid2_file = File.join(tmpdir, "mid2.tab")
172       @mid3_file = File.join(tmpdir, "mid3.tab")
173       @mid4_file = File.join(tmpdir, "mid_join.tab")
174
175       bp_find_mids
176       bp_merge_mid_tables
177     end
178
179     def each
180       File.open(@mid4_file, "r") do |ios|
181         while not ios.eof? do
182           fields = ios.readline.chomp.split("\t")
183           yield MidRow.new(fields[0], fields[1], fields[2], fields[3], fields[4])
184         end
185       end
186     end
187
188     private
189
190     def bp_find_mids
191       STDERR.puts "Finding barcodes in raw sequences ... "
192       system(
193         "read_sff -i #{@sff_file} |
194          find_barcodes -p 4 -gr |
195          count_vals -k BARCODE_NAME |
196          uniq_vals -k BARCODE_NAME |
197          write_tab -c -k BARCODE_NAME,BARCODE,BARCODE_NAME_COUNT -o #{@mid1_file} -x"
198       )
199       STDERR.puts "done.\n"
200       STDERR.puts "Finding barcodes in sequences >= 250 ... "
201       system(
202         "read_sff -i #{@sff_file} |
203          grab -e 'SEQ_LEN >= 250' |
204          find_barcodes -p 4 -gr |
205          count_vals -k BARCODE_NAME |
206          uniq_vals -k BARCODE_NAME |
207          write_tab -c -k BARCODE_NAME,BARCODE,BARCODE_NAME_COUNT -o #{@mid2_file} -x"
208       )
209       STDERR.puts "done.\n"
210       STDERR.puts "Finding barcodes in sequences >= 250 with mean score >= 20 ... "
211       system(
212         "read_sff -i #{@sff_file} |
213          mean_scores |
214          grab -e 'SEQ_LEN >= 250' |
215          grab -e 'SCORES_MEAN >= 20' |
216          find_barcodes -p 4 -gr |
217          count_vals -k BARCODE_NAME |
218          uniq_vals -k BARCODE_NAME |
219          write_tab -c -k BARCODE_NAME,BARCODE,BARCODE_NAME_COUNT -o #{@mid3_file} -x"
220       )
221       STDERR.puts "done.\n"
222     end
223
224     def bp_merge_mid_tables
225       STDERR.print "Joining MID tables ... "
226       system(
227         "read_tab -i #{@mid1_file} |
228          rename_keys -k BARCODE_NAME,A |
229          rename_keys -k BARCODE_NAME_COUNT,TOTAL |
230          read_tab -i #{@mid2_file} |
231          rename_keys -k BARCODE_NAME,B |
232          rename_keys -k BARCODE_NAME_COUNT,L250 |
233          merge_records -k A,B |
234          read_tab -i #{@mid3_file} |
235          rename_keys -k BARCODE_NAME,C |
236          rename_keys -k BARCODE_NAME_COUNT,L250_S20 |
237          merge_records -k A,C |
238          rename_keys -k A,BARCODE_NAME |
239          sort_records -k BARCODE_NAME |
240          write_tab -ck BARCODE_NAME,BARCODE,TOTAL,L250,L250_S20 -o #{@mid4_file} -x"
241       )
242       STDERR.puts "done.\n"
243     end
244
245     class MidRow
246       attr_reader :mid_num, :mid_seq, :total, :l250, :l250_s20
247
248       def initialize(mid_num, mid_seq, total, l250, l250_s20)
249         @mid_num  = mid_num
250         @mid_seq  = mid_seq
251         @total    = total
252         @l250     = l250
253         @l250_s20 = l250_s20
254       end
255     end
256   end
257 end
258
259 template = %{
260   <html>
261     <head>
262       <title>QA 454 Report</title>
263     </head>
264     <body>
265       <h1>QA 454 Report</h1>
266       <p>Date: #{Time.now}</p>
267       <p>File: <%= @sff_file %></p>
268       <h2>Sequence analysis</h2>
269       <ul>
270         <li>Number of sequences in the file: <%= @seq_analysis.count %></li>
271         <li>Minimum sequence length found: <%= @seq_analysis.min %></li>
272         <li>Maximum sequence length found: <%= @seq_analysis.max %></li>
273         <li>Mean sequence length found: <%= @seq_analysis.mean %></li>
274         <li>Total number of bases in the file: <%= @seq_analysis.bases %></li>
275         <li>Mean GC% content: <%= @seq_analysis.gc %></li>
276         <li>Mean of hard masked sequence (i.e. % of N's): <%= @seq_analysis.hard %></li>
277         <li>Mean of soft masked sequence (i.e. % lowercase residues = clipped sequence): <%= @seq_analysis.soft %></li>
278       </ul>
279       <h2>Sequence length distribution</h2>
280       <p>The length distribution of unclipped reads:</p>
281       <p><img alt="plot_lendist_unclipped" src="<%= @plots.lendist_unclipped %>" width="600" /></p>
282       <p>The length distribution of clipped reads:</p>
283       <p><img alt="plot_lendist_clipped" src="<%= @plots.lendist_clipped %>" width="600" /></p>
284       <h2>Quality score means</h2>
285       <p>The mean scores of the unclipped sequences:</p>
286       <p><img alt="plot_scores_unclipped" src="<%= @plots.scores_unclipped %>" width="600" /></p>
287       <p>The mean scores of the clipped sequences:</p>
288       <p><img alt="plot_scores_clipped" src="<%= @plots.scores_clipped %>" width="600" /></p>
289       <p>Histogram of bins with mean quality scores:</p>
290       <p><img alt="plot_mean_scores" src="<%= @plots.mean_scores %>" width="600" /></p>
291       <h2>MID tag analysis</h2>
292       <p>The below table contains the identified MID tags and the number of times they were found:<p>
293       <ul>
294       <li>BARCODE_NAME is the MID tag identifier.</li>
295       <li>BARCODE is the sequence of the MID tag.</li>
296       <li>TOTAL is the number of times this MID tag was found.</li>
297       <li>L250 is the a subset count of TOTAL af sequences longer than 250 bases</li>
298       <li>L250_S20 is a subset count of L250 af sequences with a mean score above 20</li>
299       </ul>
300       <table>
301       <% @table_mid_join.each do |row| %>
302         <tr>
303         <td><%= row.mid_num %></td>
304         <td><%= row.mid_seq %></td>
305         <td><%= row.total %></td>
306         <td><%= row.l250 %></td>
307         <td><%= row.l250_s20 %></td>
308         </tr>
309       <% end %>
310       </table>
311       <h2>Residue frequency analysis</h2>
312       <p>Plot of nucleotide distribution in percent of the first 50 bases:</p>
313       <p><img alt="plot_nucleotide_distribution" src="<%= @plots.nucleotide_dist50 %>" width="600" /></p>
314       <p>Plot of nucleotide distribution in percent of the first 500 bases:</p>
315       <p><img alt="plot_nucleotide_distribution" src="<%= @plots.nucleotide_dist500 %>" width="600" /></p>
316     </body>
317   </html>
318 }.gsub(/^\s+/, '')
319
320 html = ERB.new(template)
321
322 ARGV.each do |file|
323   report = Report.new(file)
324                 
325   html.run(report.get_binding)
326 end
327
328 __END__