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