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 @anal_file = File.join(tmpdir, "out1.txt")
65 STDERR.puts "Analyzing sequences ... "
67 "read_sff -i #{@sff_file} |
70 analyze_vals -k SEQ,GC%,HARD_MASK%,SOFT_MASK% -x |
71 write_tab -o #{@anal_file} -x"
76 def parse_analyze_vals
77 File.open(@anal_file, "r") do |ios|
79 line = ios.readline.chomp
80 fields = line.split("\t")
83 when "SEQ" then @count, @min, @max, @bases, @mean = fields[2 .. 6]
84 when "GC%" then @gc = fields[6]
85 when "HARD_MASK%" then @hard = fields[6]
86 when "SOFT_MASK%" then @soft = fields[6]
94 attr_reader :lendist_unclipped, :lendist_clipped, :scores_unclipped, :scores_clipped, :mean_scores, :nucleotide_dist500, :nucleotide_dist50
96 def initialize(sff_file, tmpdir)
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")
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)
118 STDERR.puts "Creating plots ... "
120 "read_sff -m -i #{@sff_file} |
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} |
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} |
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} |
131 plot_nucleotide_distribution -c -t png -o #{@plot6} |
133 plot_nucleotide_distribution -t png -o #{@plot7} -x"
135 STDERR.puts "done.\n"
143 File.open(file, "r") do |ios|
147 "data:image/png;base64," + Base64.encode64(png)
152 def initialize(sff_file, tmpdir)
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")
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])
175 STDERR.puts "Finding barcodes in raw sequences ... "
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"
183 STDERR.puts "done.\n"
184 STDERR.puts "Finding barcodes in sequences >= 250 ... "
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"
193 STDERR.puts "done.\n"
194 STDERR.puts "Finding barcodes in sequences >= 250 with mean score >= 20 ... "
196 "read_sff -i #{@sff_file} |
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"
205 STDERR.puts "done.\n"
208 def bp_merge_mid_tables
209 STDERR.print "Joining MID tables ... "
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"
226 STDERR.puts "done.\n"
230 attr_reader :mid_num, :mid_seq, :total, :l250, :l250_s20
232 def initialize(mid_num, mid_seq, total, l250, l250_s20)
246 <title>QA 454 Report</title>
249 <h1>QA 454 Report</h1>
250 <p>Date: #{Time.now}</p>
251 <p>File: <%= @sff_file %></p>
252 <h2>Sequence analysis</h2>
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>
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>
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>
285 <% @table_mid_join.each do |row| %>
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>
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>
304 html = ERB.new(template)
307 report = Report.new(file)
309 html.run(report.get_binding)