3 # Copyright (C) 2011 Martin A. Hansen (mail@maasha.dk).
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.
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.
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.
19 # http://www.gnu.org/copyleft/gpl.html
27 def initialize(sff_file)
31 @seq_analysis = SeqAnalyze.new(@sff_file, tmpdir)
32 @plots = PlotData.new(@sff_file, tmpdir)
33 @table_mid_join = MidTable.new(@sff_file, tmpdir)
36 # Support templating of member data.
44 attr_reader :count, :min, :max, :mean, :bases, :gc, :hard, :soft
46 def initialize(sff_file, tmpdir)
48 @out1_file = File.join(tmpdir, "out1.txt")
49 @out2_file = File.join(tmpdir, "out2.txt")
67 STDERR.puts "Analyzing sequences ... "
69 "read_sff -i #{@sff_file} |
71 analyze_vals -k SEQ -o #{@out1_file} |
73 mean_vals -k 'GC%,HARD_MASK%,SOFT_MASK%' -o #{@out2_file} -x"
78 def parse_analyze_vals
79 File.open(@out1_file, "r") do |ios|
81 line = ios.readline.chomp
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
95 File.open(@out2_file, "r") do |ios|
97 line = ios.readline.chomp
100 when /GC%_MEAN: (.+)/; then @gc = $1
101 when /HARD_MASK%_MEAN: (.+)/; then @hard = $1
102 when /SOFT_MASK%_MEAN: (.+)/; then @soft = $1
110 attr_reader :lendist_unclipped, :lendist_clipped, :scores_unclipped, :scores_clipped, :mean_scores, :nucleotide_dist
112 def initialize(sff_file, tmpdir)
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")
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)
132 STDERR.puts "Creating plots ... "
134 "read_sff -m -i #{@sff_file} |
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} |
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} |
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} |
145 plot_nucleotide_distribution -t png -o #{@plot6} -x"
147 STDERR.puts "done.\n"
155 File.open(file, "r") do |ios|
159 "data:image/png;base64," + Base64.encode64(png)
164 def initialize(sff_file, tmpdir)
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")
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])
187 STDERR.puts "Finding barcodes in raw sequences ... "
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"
195 STDERR.puts "done.\n"
196 STDERR.puts "Finding barcodes in sequences >= 250 ... "
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"
205 STDERR.puts "done.\n"
206 STDERR.puts "Finding barcodes in sequences >= 250 with mean score >= 20 ... "
208 "read_sff -i #{@sff_file} |
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"
217 STDERR.puts "done.\n"
220 def bp_merge_mid_tables
221 STDERR.print "Joining MID tables ... "
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"
238 STDERR.puts "done.\n"
242 attr_reader :mid_num, :mid_seq, :total, :l250, :l250_s20
244 def initialize(mid_num, mid_seq, total, l250, l250_s20)
258 <title>QA 454 Report</title>
261 <h1>QA 454 Report</h1>
262 <p>Date: #{Time.now}</p>
263 <p>File: <%= @sff_file %></p>
264 <h2>Sequence analysis</h2>
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>
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>
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>
297 <% @table_mid_join.each do |row| %>
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>
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>
314 html = ERB.new(template)
317 report = Report.new(file)
319 html.run(report.get_binding)