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