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 # This program is part of the Biopieces framework (www.biopieces.org).
25 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
27 # Calculate the mean or local mean of quality SCORES in the stream.
29 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
31 require 'maasha/biopieces'
35 # Opening class Seq to add scores_mean_local(_C) methods.
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)
46 VALUE scores_mean_local_C(
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);
62 for (i = 0; i < window_size; i++)
63 sum += qual[i] - score_base;
65 mean = sum / window_size;
67 // run window across the rest of the scores
70 sum += qual[i] - score_base;
71 sum -= qual[i - window_size] - score_base;
73 mean = ( mean < sum / window_size ) ? mean : sum / window_size;
78 return rb_float_new(mean);
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}
89 options = Biopieces.options_parse(ARGV, casts)
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)
97 record[:SCORES_MEAN_LOCAL] = entry.scores_mean_local(options[:window_size]).round(2)
99 record[:SCORES_MEAN] = entry.scores_mean.round(2)
108 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<