]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/mean_scores
fixed global SCORE_BASE in mean_scores
[biopieces.git] / bp_bin / mean_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 # This program is part of the Biopieces framework (www.biopieces.org).
24
25 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
26
27 # Calculate the mean or local mean of quality SCORES in the stream.
28
29 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
30
31 require 'maasha/biopieces'
32 require 'maasha/seq'
33 require 'inline'
34
35 # Opening class Seq to add scores_mean_local(_C) methods.
36 class Seq
37   # Method to run a sliding window of a specified size across a Phred type
38   # scores string and calculate for each window the mean score and return
39   # the minimum mean score.
40   def scores_mean_local(window_size)
41     scores_mean_local_C(self.qual, self.length, Seq::SCORE_BASE, window_size)
42   end
43
44   inline do |builder|
45     builder.c %{
46       VALUE scores_mean_local_C(
47         VALUE _qual,
48         VALUE _qual_len,
49         VALUE _score_base,
50         VALUE _window_size
51       )
52       {
53         unsigned char *qual        = (unsigned char *) StringValuePtr(_qual);
54         unsigned int   qual_len    = FIX2UINT(_qual_len);
55         unsigned int   score_base  = FIX2UINT(_score_base);
56         unsigned int   window_size = FIX2UINT(_window_size);
57         unsigned int   sum         = 0;
58         unsigned int   i           = 0;
59         float          mean        = 0.0;
60
61         // fill window
62         for (i = 0; i < window_size; i++)
63           sum += qual[i] - score_base;
64
65         mean = sum / window_size;
66
67         // run window across the rest of the scores
68         while (i < qual_len)
69         {
70           sum += qual[i] - score_base;
71           sum -= qual[i - window_size] - score_base;
72
73           mean = ( mean < sum / window_size ) ? mean : sum / window_size;
74         
75           i++;
76         }
77
78         return rb_float_new(mean);
79       }
80     }
81   end
82 end
83
84 casts = []
85 casts << {:long=>'local',       :short=>'l', :type=>'flag',  :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
86 casts << {:long=>'window_size', :short=>'w', :type=>'uint',  :mandatory=>false, :default=>5,   :allowed=>nil, :disallowed=>'0'}
87 casts << {:long=>'min',         :short=>'m', :type=>'float', :mandatory=>false, :default=>15,  :allowed=>nil, :disallowed=>nil}
88
89 options = Biopieces.options_parse(ARGV, casts)
90
91 Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output|
92   input.each_record do |record|
93     if record[:SEQ] and record[:SCORES] and record[:SEQ].length > 0
94       entry = Seq.new_bp(record)
95
96       if options[:local]
97         record[:SCORES_MEAN_LOCAL] = entry.scores_mean_local(options[:window_size]).round(2)
98       else
99         record[:SCORES_MEAN] = entry.scores_mean.round(2)
100       end
101     end
102
103     output.puts record
104   end
105 end
106
107
108 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
109
110
111 __END__