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'
37 # Opening class Seq to add scores_mean_local(_C) methods.
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)
48 VALUE scores_mean_local_C(
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);
64 for (i = 0; i < window_size; i++)
65 sum += qual[i] - score_base;
67 mean = sum / window_size;
69 // run window across the rest of the scores
72 sum += qual[i] - score_base;
73 sum -= qual[i - window_size] - score_base;
75 mean = ( mean < sum / window_size ) ? mean : sum / window_size;
80 return rb_float_new(mean);
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}
91 options = Biopieces.options_parse(ARGV, casts)
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]
96 entry = Seq.new_bp(record)
99 record[:SCORES_MEAN_LOCAL] = entry.scores_mean_local(options[:window_size]).round(2)
101 record[:SCORES_MEAN] = entry.scores_mean.round(2)
110 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<