]> git.donarmstrong.com Git - biopieces.git/blobdiff - code_ruby/lib/maasha/seq/trim.rb
add comments to ambiguity.rb
[biopieces.git] / code_ruby / lib / maasha / seq / trim.rb
index 09dff899d3727251bbf2c6ff4f8da72971c296fd..e7e9bb1daf6b019e77e78b8eabf44cad1abc2ea5 100644 (file)
@@ -1,3 +1,4 @@
+# Copyright (C) 2007-2013 Martin A. Hansen.
 
 # This program is free software; you can redistribute it and/or
 # modify it under the terms of the GNU General Public License
@@ -23,6 +24,9 @@
 
 require 'inline'
 
+# Error class for all exceptions to do with Trim.
+class TrimError < StandardError; end
+
 # Module containing methods for end trimming sequences with suboptimal quality
 # scores.
 module Trim
@@ -31,7 +35,7 @@ module Trim
   def quality_trim_right(min_qual, min_len = 1)
     check_trim_args(min_qual)
 
-    pos = trim_right_pos_c(self.qual, self.length, min_qual, min_len, SCORE_BASE)
+    pos = trim_right_pos_c(self.qual, self.length, min_qual, min_len, Seq::SCORE_BASE)
 
     self.subseq(0, pos)
   end
@@ -41,7 +45,7 @@ module Trim
   def quality_trim_right!(min_qual, min_len = 1)
     check_trim_args(min_qual)
 
-    pos = trim_right_pos_c(self.qual, self.length, min_qual, min_len, SCORE_BASE)
+    pos = trim_right_pos_c(self.qual, self.length, min_qual, min_len, Seq::SCORE_BASE)
 
     self.subseq!(0, pos)
   end
@@ -51,7 +55,7 @@ module Trim
   def quality_trim_left(min_qual, min_len = 1)
     check_trim_args(min_qual)
 
-    pos = trim_left_pos_c(self.qual, self.length, min_qual, min_len, SCORE_BASE)
+    pos = trim_left_pos_c(self.qual, self.length, min_qual, min_len, Seq::SCORE_BASE)
 
     self.subseq(pos)
   end
@@ -61,7 +65,7 @@ module Trim
   def quality_trim_left!(min_qual, min_len = 1)
     check_trim_args(min_qual)
 
-    pos = trim_left_pos_c(self.qual, self.length, min_qual, min_len, SCORE_BASE)
+    pos = trim_left_pos_c(self.qual, self.length, min_qual, min_len, Seq::SCORE_BASE)
 
     self.subseq!(pos)
   end
@@ -71,8 +75,8 @@ module Trim
   def quality_trim(min_qual, min_len = 1)
     check_trim_args(min_qual)
 
-    pos_right = trim_right_pos_c(self.qual, self.length, min_qual, min_len, SCORE_BASE)
-    pos_left  = trim_left_pos_c(self.qual, self.length, min_qual, min_len, SCORE_BASE)
+    pos_right = trim_right_pos_c(self.qual, self.length, min_qual, min_len, Seq::SCORE_BASE)
+    pos_left  = trim_left_pos_c(self.qual, self.length, min_qual, min_len, Seq::SCORE_BASE)
 
     pos_left = pos_right if pos_left > pos_right
 
@@ -84,8 +88,8 @@ module Trim
   def quality_trim!(min_qual, min_len = 1)
     check_trim_args(min_qual)
 
-    pos_right = trim_right_pos_c(self.qual, self.length, min_qual, min_len, SCORE_BASE)
-    pos_left  = trim_left_pos_c(self.qual, self.length, min_qual, min_len, SCORE_BASE)
+    pos_right = trim_right_pos_c(self.qual, self.length, min_qual, min_len, Seq::SCORE_BASE)
+    pos_left  = trim_left_pos_c(self.qual, self.length, min_qual, min_len, Seq::SCORE_BASE)
 
     pos_left = pos_right if pos_left > pos_right
 
@@ -97,10 +101,10 @@ module Trim
   # Method to check the arguments for trimming and raise on bad sequence, qualities,
   # and min_qual.
   def check_trim_args(min_qual)
-    raise SeqError, "no sequence"      if self.seq.nil?
-    raise SeqError, "no quality score" if self.qual.nil?
-    unless (SCORE_MIN .. SCORE_MAX).include? min_qual
-      raise SeqError, "minimum quality value: #{min_qual} out of range #{SCORE_MIN} .. #{SCORE_MAX}"
+    raise TrimError, "no sequence"      if self.seq.nil?
+    raise TrimError, "no quality score" if self.qual.nil?
+    unless (Seq::SCORE_MIN .. Seq::SCORE_MAX).include? min_qual
+      raise TrimError, "minimum quality value: #{min_qual} out of range #{Seq::SCORE_MIN} .. #{Seq::SCORE_MAX}"
     end
   end
 
@@ -129,7 +133,7 @@ module Trim
         {
           c = 0;
 
-          while ((c < min_len) && ((c + i) < len) && (qual[len - (c + i) - 1] - score_base > min_qual))
+          while ((c < min_len) && ((c + i) < len) && (qual[len - (c + i) - 1] - score_base >= min_qual))
             c++;
 
           if (c == min_len)
@@ -167,7 +171,7 @@ module Trim
         {
           c = 0;
 
-          while ((c < min_len) && ((c + i) < len) && (qual[c + i] - score_base > min_qual))
+          while ((c < min_len) && ((c + i) < len) && (qual[c + i] - score_base >= min_qual))
             c++;
 
           if (c == min_len)