]> git.donarmstrong.com Git - biopieces.git/blob - bp_scripts/QA_454_report.rb
added nucleotide plot to QA_454_report
[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_dist
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
121       bp_plot
122
123       @lendist_unclipped = png2base64(@plot1)
124       @lendist_clipped   = png2base64(@plot3)
125       @scores_unclipped  = png2base64(@plot2)
126       @scores_clipped    = png2base64(@plot4)
127       @mean_scores       = png2base64(@plot5)
128       @nucleotide_dist   = png2base64(@plot6)
129     end
130
131     def bp_plot
132       STDERR.puts "Creating plots ... "
133       system(
134         "read_sff -m -i #{@sff_file} |
135          progress_meter |
136          plot_distribution -k SEQ_LEN -T 'Length Distribution - unclipped' -t png -o #{@plot1} |
137          plot_scores -c -T 'Mean Quality Scores - unclipped' -t png -o #{@plot2} |
138          clip_seq |
139          plot_distribution -k SEQ_LEN -T 'Length Distribution - clipped' -t png -o #{@plot3} |
140          plot_scores -c -T 'Mean Quality Scores - clipped' -t png -o #{@plot4} |
141          mean_scores |
142          bin_vals -k SCORES_MEAN -b 5 |
143          plot_histogram -s num -k SCORES_MEAN_BIN -T 'Mean score bins' -X 'Bins (size 5)' -Y 'Count' -t png -o #{@plot5} |
144          extract_seq -l 50 |
145          plot_nucleotide_distribution -t png -o #{@plot6} -x"
146       )
147       STDERR.puts "done.\n"
148     end
149
150     private
151
152     def png2base64(file)
153       png = ""
154
155       File.open(file, "r") do |ios|
156         png = ios.read
157       end
158
159       "data:image/png;base64," + Base64.encode64(png)
160     end
161   end
162
163   class MidTable
164     def initialize(sff_file, tmpdir)
165       @sff_file  = sff_file
166       @mid1_file = File.join(tmpdir, "mid1.tab")
167       @mid2_file = File.join(tmpdir, "mid2.tab")
168       @mid3_file = File.join(tmpdir, "mid3.tab")
169       @mid4_file = File.join(tmpdir, "mid_join.tab")
170
171       bp_find_mids
172       bp_merge_mid_tables
173     end
174
175     def each
176       File.open(@mid4_file, "r") do |ios|
177         while not ios.eof? do
178           fields = ios.readline.chomp.split("\t")
179           yield MidRow.new(fields[0], fields[1], fields[2], fields[3], fields[4])
180         end
181       end
182     end
183
184     private
185
186     def bp_find_mids
187       STDERR.puts "Finding barcodes in raw sequences ... "
188       system(
189         "read_sff -i #{@sff_file} |
190          find_barcodes -p 4 -gr |
191          count_vals -k BARCODE_NAME |
192          uniq_vals -k BARCODE_NAME |
193          write_tab -c -k BARCODE_NAME,BARCODE,BARCODE_NAME_COUNT -o #{@mid1_file} -x"
194       )
195       STDERR.puts "done.\n"
196       STDERR.puts "Finding barcodes in sequences >= 250 ... "
197       system(
198         "read_sff -i #{@sff_file} |
199          grab -e 'SEQ_LEN >= 250' |
200          find_barcodes -p 4 -gr |
201          count_vals -k BARCODE_NAME |
202          uniq_vals -k BARCODE_NAME |
203          write_tab -c -k BARCODE_NAME,BARCODE,BARCODE_NAME_COUNT -o #{@mid2_file} -x"
204       )
205       STDERR.puts "done.\n"
206       STDERR.puts "Finding barcodes in sequences >= 250 with mean score >= 20 ... "
207       system(
208         "read_sff -i #{@sff_file} |
209          mean_scores |
210          grab -e 'SEQ_LEN >= 250' |
211          grab -e 'SCORES_MEAN >= 20' |
212          find_barcodes -p 4 -gr |
213          count_vals -k BARCODE_NAME |
214          uniq_vals -k BARCODE_NAME |
215          write_tab -c -k BARCODE_NAME,BARCODE,BARCODE_NAME_COUNT -o #{@mid3_file} -x"
216       )
217       STDERR.puts "done.\n"
218     end
219
220     def bp_merge_mid_tables
221       STDERR.print "Joining MID tables ... "
222       system(
223         "read_tab -i #{@mid1_file} |
224          rename_keys -k BARCODE_NAME,A |
225          rename_keys -k BARCODE_NAME_COUNT,TOTAL |
226          read_tab -i #{@mid2_file} |
227          rename_keys -k BARCODE_NAME,B |
228          rename_keys -k BARCODE_NAME_COUNT,L250 |
229          merge_records -k A,B |
230          read_tab -i #{@mid3_file} |
231          rename_keys -k BARCODE_NAME,C |
232          rename_keys -k BARCODE_NAME_COUNT,L250_S20 |
233          merge_records -k A,C |
234          rename_keys -k A,BARCODE_NAME |
235          sort_records -k BARCODE_NAME |
236          write_tab -ck BARCODE_NAME,BARCODE,TOTAL,L250,L250_S20 -o #{@mid4_file} -x"
237       )
238       STDERR.puts "done.\n"
239     end
240
241     class MidRow
242       attr_reader :mid_num, :mid_seq, :total, :l250, :l250_s20
243
244       def initialize(mid_num, mid_seq, total, l250, l250_s20)
245         @mid_num  = mid_num
246         @mid_seq  = mid_seq
247         @total    = total
248         @l250     = l250
249         @l250_s20 = l250_s20
250       end
251     end
252   end
253 end
254
255 template = %{
256   <html>
257     <head>
258       <title>QA 454 Report</title>
259     </head>
260     <body>
261       <h1>QA 454 Report</h1>
262       <p>Date: #{Time.now}</p>
263       <p>File: <%= @sff_file %></p>
264       <h2>Sequence analysis</h2>
265       <ul>
266         <li>Number of sequences in the file: <%= @seq_analysis.count %></li>
267         <li>Minimum sequence length found: <%= @seq_analysis.min %></li>
268         <li>Maximum sequence length found: <%= @seq_analysis.max %></li>
269         <li>Mean sequence length found: <%= @seq_analysis.mean %></li>
270         <li>Total number of bases in the file: <%= @seq_analysis.bases %></li>
271         <li>Mean GC% content: <%= @seq_analysis.gc %></li>
272         <li>Mean of hard masked sequence (i.e. % of N's): <%= @seq_analysis.hard %></li>
273         <li>Mean of soft masked sequence (i.e. % lowercase residues = clipped sequence): <%= @seq_analysis.soft %></li>
274       </ul>
275       <h2>Sequence length distribution</h2>
276       <p>The length distribution of unclipped reads:</p>
277       <p><img alt="plot_lendist_unclipped" src="<%= @plots.lendist_unclipped %>" width="600" /></p>
278       <p>The length distribution of clipped reads:</p>
279       <p><img alt="plot_lendist_clipped" src="<%= @plots.lendist_clipped %>" width="600" /></p>
280       <h2>Quality score means</h2>
281       <p>The mean scores of the unclipped sequences:</p>
282       <p><img alt="plot_scores_unclipped" src="<%= @plots.scores_unclipped %>" width="600" /></p>
283       <p>The mean scores of the clipped sequences:</p>
284       <p><img alt="plot_scores_clipped" src="<%= @plots.scores_clipped %>" width="600" /></p>
285       <p>Histogram of bins with mean quality scores:</p>
286       <p><img alt="plot_mean_scores" src="<%= @plots.mean_scores %>" width="600" /></p>
287       <h2>MID tag analysis</h2>
288       <p>The below table contains the identified MID tags and the number of times they were found:<p>
289       <ul>
290       <li>BARCODE_NAME is the MID tag identifier.</li>
291       <li>BARCODE is the sequence of the MID tag.</li>
292       <li>TOTAL is the number of times this MID tag was found.</li>
293       <li>L250 is the a subset count of TOTAL af sequences longer than 250 bases</li>
294       <li>L250_S20 is a subset count of L250 af sequences with a mean score above 20</li>
295       </ul>
296       <table>
297       <% @table_mid_join.each do |row| %>
298         <tr>
299         <td><%= row.mid_num %></td>
300         <td><%= row.mid_seq %></td>
301         <td><%= row.total %></td>
302         <td><%= row.l250 %></td>
303         <td><%= row.l250_s20 %></td>
304         </tr>
305       <% end %>
306       </table>
307       <h2>Residue frequency analysis</h2>
308       <p>Plot of nucleotide distribution in percent of the first 50 bases:</p>
309       <p><img alt="plot_nucleotide_distribution" src="<%= @plots.nucleotide_dist %>" width="600" /></p>
310     </body>
311   </html>
312 }.gsub(/^\s+/, '')
313
314 html = ERB.new(template)
315
316 ARGV.each do |file|
317   report = Report.new(file)
318                 
319   html.run(report.get_binding)
320 end
321
322 __END__