]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/plot_scores
bf0023a6ea53c9d611a9f993f89fb1a8c721d642
[biopieces.git] / bp_bin / plot_scores
1 #!/usr/bin/env ruby
2
3 # Copyright (C) 2007-2012 Martin A. Hansen.
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 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
22
23 # Plot a histogram of mean sequence quality scores.
24
25 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
26
27 require 'maasha/biopieces'
28 require 'gnuplot'
29 require 'narray'
30 require 'pp'
31
32 terminals = "dumb,x11,aqua,post,pdf,png,svg"
33 title     = "Mean Quality Scores"
34 xlabel    = "Sequence position"
35 ylabel    = "Mean score"
36
37 casts = []
38 casts << {:long=>'no_stream', :short=>'x', :type=>'flag',   :mandatory=>false, :default=>nil,    :allowed=>nil,       :disallowed=>nil}
39 casts << {:long=>'count',     :short=>'c', :type=>'flag',   :mandatory=>false, :default=>nil,    :allowed=>nil,       :disallowed=>nil}
40 casts << {:long=>'data_out',  :short=>'o', :type=>'file',   :mandatory=>false, :default=>nil,    :allowed=>nil,       :disallowed=>nil}
41 casts << {:long=>'terminal',  :short=>'t', :type=>'string', :mandatory=>false, :default=>'dumb', :allowed=>terminals, :disallowed=>nil}
42 casts << {:long=>'title',     :short=>'T', :type=>'string', :mandatory=>false, :default=>title,  :allowed=>nil,       :disallowed=>nil}
43 casts << {:long=>'xlabel',    :short=>'X', :type=>'string', :mandatory=>false, :default=>xlabel, :allowed=>nil,       :disallowed=>nil}
44 casts << {:long=>'ylabel',    :short=>'Y', :type=>'string', :mandatory=>false, :default=>ylabel, :allowed=>nil,       :disallowed=>nil}
45
46 options = Biopieces.options_parse(ARGV, casts)
47
48 ILLUMINA_BASE = 64
49 ILLUMINA_MIN  = 0
50 ILLUMINA_MAX  = 40
51 SCORES_MAX    = 10_000
52
53 scores_vec = NArray.int(SCORES_MAX)
54 count_vec  = NArray.int(SCORES_MAX)
55 max        = 0
56
57 Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output|
58   input.each_record do |record|
59     if record[:SCORES]
60       scores = record[:SCORES]
61
62       if scores.length > 0
63         raise BiopiecesError, "score string too long: #{scores.length} > #{SCORES_MAX}" if scores.length > SCORES_MAX
64
65         scores_vec[0 ... scores.length] += NArray.to_na(scores, "byte") - ILLUMINA_BASE
66         count_vec[0 ... scores.length]  += 1
67
68         max = scores.length if scores.length > max
69       end
70     end
71
72     output.puts record unless options[:no_stream]
73   end
74 end
75
76 mean_vec   = NArray.sfloat(max)
77 mean_vec   = scores_vec[0 ... max].to_f / count_vec[0 ... max]
78 count_vec  = count_vec[0 ... max].to_f
79 count_vec *= (ILLUMINA_MAX / count_vec.max(0).to_f)
80
81 x  = (1 .. max).to_a
82 y1 = mean_vec.to_a
83 y2 = count_vec.to_a
84
85 Gnuplot.open do |gp|
86   Gnuplot::Plot.new(gp) do |plot|
87     plot.terminal options[:terminal]
88     plot.title    options[:title]
89     plot.xlabel   options[:xlabel]
90     plot.ylabel   options[:ylabel]
91     plot.output   options[:data_out] if options[:data_out]
92     plot.xrange   "[#{x.min - 1}:#{x.max + 1}]"
93     plot.yrange   "[#{ILLUMINA_MIN}:#{ILLUMINA_MAX}]"
94     plot.style    "fill solid 0.5 border"
95     plot.xtics    "out"
96     plot.ytics    "out"
97     
98     plot.data << Gnuplot::DataSet.new([x, y1]) do |ds|
99       ds.with  = "boxes"
100       ds.title = "mean score"
101     end
102
103     if options[:count]
104       plot.data << Gnuplot::DataSet.new([x, y2]) do |ds|
105         ds.with  = "lines lt rgb \"black\""
106         ds.title = "relative count"
107       end
108     end
109   end
110 end
111
112
113 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
114
115
116 __END__