3 # Copyright (C) 2007-2012 Martin A. Hansen.
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
21 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
23 # Plot a histogram of mean sequence quality scores.
25 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
27 require 'maasha/biopieces'
32 terminals = "dumb,x11,aqua,post,pdf,png,svg"
33 title = "Mean Quality Scores"
34 xlabel = "Sequence position"
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}
46 options = Biopieces.options_parse(ARGV, casts)
50 scores_vec = NArray.int(SCORES_MAX)
51 count_vec = NArray.int(SCORES_MAX)
54 Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output|
55 input.each_record do |record|
57 scores = record[:SCORES]
60 raise BiopiecesError, "score string too long: #{scores.length} > #{SCORES_MAX}" if scores.length > SCORES_MAX
62 scores_vec[0 ... scores.length] += NArray.to_na(scores, "byte") - Seq::SCORE_BASE
63 count_vec[0 ... scores.length] += 1
65 max = scores.length if scores.length > max
69 output.puts record unless options[:no_stream]
73 mean_vec = NArray.sfloat(max)
74 mean_vec = scores_vec[0 ... max].to_f / count_vec[0 ... max]
75 count_vec = count_vec[0 ... max].to_f
76 count_vec *= (Seq::SCORE_MAX / count_vec.max(0).to_f)
83 Gnuplot::Plot.new(gp) do |plot|
84 plot.terminal options[:terminal]
85 plot.title options[:title]
86 plot.xlabel options[:xlabel]
87 plot.ylabel options[:ylabel]
88 plot.output options[:data_out] if options[:data_out]
89 plot.xrange "[#{x.min - 1}:#{x.max + 1}]"
90 plot.yrange "[#{Seq::SCORE_MIN}:#{Seq::SCORE_MAX}]"
91 plot.style "fill solid 0.5 border"
95 plot.data << Gnuplot::DataSet.new([x, y1]) do |ds|
97 ds.title = "mean score"
101 plot.data << Gnuplot::DataSet.new([x, y2]) do |ds|
102 ds.with = "lines lt rgb \"black\""
103 ds.title = "relative count"
110 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<