]> git.donarmstrong.com Git - biopieces.git/blob - bp_scripts/QA_Illumina_report.rb
7b54e4df88ac875912e37bd15ca8434c786dbce7
[biopieces.git] / bp_scripts / QA_Illumina_report.rb
1 #!/usr/bin/env ruby
2
3 # Copyright (C) 2012 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 def parse_analysis(file)
27   data = {}
28
29   File.open(file, 'r') do |ios|
30     ios.each do |line|
31       key, val = line.chomp.split(' ')
32       data[key] = val.to_i;
33     end
34   end
35
36   return data
37 end
38
39 def png2base64(file)
40   png = ""
41
42   File.open(file, "r") do |ios|
43     png = ios.read
44   end
45
46   "data:image/png;base64," + Base64.encode64(png)
47 end
48
49 if ARGV.empty?
50   $stderr.puts "Usage: QA_Illumina_report.rb <FASTQ file> > <HTML file>" 
51   exit
52 end
53
54 tmpdir   = Dir.mktmpdir
55 seq_file = ARGV.shift
56
57 analyze_vals_file      = File.join(tmpdir, 'analyze_vals.txt')
58 analyze_vals_trim_file = File.join(tmpdir, 'analyze_vals_trim.txt')
59 lendist_file           = File.join(tmpdir, 'lendist.png')
60 scores_file            = File.join(tmpdir, 'scores.png')
61 nucdist_file           = File.join(tmpdir, 'nucdist.png')
62 lendist_bin_file       = File.join(tmpdir, 'lendist_bin.png')
63 scores_bin_file        = File.join(tmpdir, 'scores_bin.png')
64
65 STDERR.puts "Analyzing sequences ... "
66
67 system(
68   "read_fastq -i #{seq_file} |
69    progress_meter |
70    analyze_vals -k SEQ -o #{analyze_vals_file} |
71    trim_seq -l 3 -m 25 |
72    grab -e 'SEQ_LEN > 0' |
73    analyze_vals -k SEQ -o #{analyze_vals_trim_file} |
74    plot_distribution -k SEQ_LEN -T 'Sequence length distribution' -X 'Sequence length' -t png -o #{lendist_file} |
75    plot_scores -c -t png -o #{scores_file} |
76    plot_nucleotide_distribution -t png -o #{nucdist_file} |
77    bin_vals -k SEQ_LEN -b 50 |
78    plot_distribution -T '50 bases bin sequence length distribution' -X 'Sequence length' -k SEQ_LEN_BIN -t png -o #{lendist_bin_file} |
79    mean_scores |
80    bin_vals -k SCORES_MEAN -b 5 |
81    plot_distribution -k SCORES_MEAN_BIN -T '5 bin mean score distribution' -X 'Mean scores' -t png -o #{scores_bin_file} -x"
82 )
83
84 STDERR.puts "done.\n"
85
86 analysis1 = parse_analysis(analyze_vals_file)
87 analysis2 = parse_analysis(analyze_vals_trim_file)
88
89 template = %{
90   <html>
91     <head>
92       <title>QA Illumina Report</title>
93     </head>
94     <body>
95       <h1>QA Illumina Report</h1>
96       <p>Date: #{Time.now}</p>
97       <p>File: <%= seq_file %></p>
98       <h2>Sequence composition</h2>
99       <table>
100       <tr><td></td><td>Before trimming</td><td>After trimming</td></tr>
101       <tr><td>Number of sequences</td><td align='right'><%= analysis1['COUNT'] %></td><td align='right'><%= analysis2['COUNT'] %></td></tr>
102       <tr><td>Number of bases</td><td align='right'><%= analysis1['SUM'] %></td><td align='right'><%= analysis2['SUM'] %></td></tr>
103       <tr><td>Min sequence length</td><td align='right'><%= analysis1['MIN'] %></td><td align='right'><%= analysis2['MIN'] %></td></tr>
104       <tr><td>Max sequence length</td><td align='right'><%= analysis1['MAX'] %></td><td align='right'><%= analysis2['MAX'] %></td></tr>
105       <tr><td>Mean sequence length</td><td align='right'><%= analysis1['MEAN'] %></td><td align='right'><%= analysis2['MEAN'] %></td></tr>
106       </table>
107       <p>Sequence trimming was performed by removing from the ends all residues until 3 consecutive</p>
108       <p>residues with quality score larger than or equal to 25.</p>
109       <p>All plots are after sequence trimming.</p>
110       <h2>Sequence length distribution</h2>
111       <p><img src="<%= png2base64(lendist_file) %>" width="600" /></p>
112       <p><img src="<%= png2base64(lendist_bin_file) %>" width="600" /></p>
113       <h2>Sequence quality scores</h2>
114       <p><img src="<%= png2base64(scores_file) %>" width="600" /></p>
115       <p><img src="<%= png2base64(scores_bin_file) %>" width="600" /></p>
116       <h2>Sequence nucleotide distribution</h2>
117       <p><img src="<%= png2base64(nucdist_file) %>" width="600" /></p>
118     </body>
119   </html>
120 }.gsub(/^\s+/, '')
121
122 html = ERB.new(template)
123
124 puts html.result(binding)
125
126 __END__