]> git.donarmstrong.com Git - biopieces.git/commitdiff
added patscan_trim method to Seq
authormartinahansen <martinahansen@74ccb610-7750-0410-82ae-013aeee3265d>
Wed, 30 Oct 2013 10:56:28 +0000 (10:56 +0000)
committermartinahansen <martinahansen@74ccb610-7750-0410-82ae-013aeee3265d>
Wed, 30 Oct 2013 10:56:28 +0000 (10:56 +0000)
git-svn-id: http://biopieces.googlecode.com/svn/trunk@2252 74ccb610-7750-0410-82ae-013aeee3265d

code_ruby/lib/maasha/seq/trim.rb
code_ruby/test/maasha/seq/test_trim.rb

index e7e9bb1daf6b019e77e78b8eabf44cad1abc2ea5..2be3e56800a518a25ce5da5665053b0951d4de8e 100644 (file)
@@ -23,6 +23,9 @@
 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
 
 require 'inline'
+require 'maasha/seq/backtrack'
+
+include BackTrack
 
 # Error class for all exceptions to do with Trim.
 class TrimError < StandardError; end
@@ -96,6 +99,38 @@ module Trim
     self.subseq!(pos_left, pos_right - pos_left)
   end
 
+  # Method to locate a pattern in a sequence and trim all sequence to the left
+  # including the pattern.
+  def patmatch_trim_left!(pattern, options = {})
+    match = self.patmatch(pattern, options)
+
+    if match
+      stop = self.length
+
+      self.seq  = self.seq[match.pos + match.length .. stop]
+      self.qual = self.qual[match.pos + match.length .. stop] if self.qual
+
+      return self
+    end
+
+    nil
+  end
+
+  # Method to locate a pattern in a sequence and trim all sequence to the right
+  # including the pattern.
+  def patmatch_trim_right!(pattern, options = {})
+    match = self.patmatch(pattern, options)
+
+    if match
+      self.seq  = self.seq[0 ... match.pos]
+      self.qual = self.qual[0 ... match.pos] if self.qual
+
+      return self
+    end
+
+    nil
+  end
+
   private
 
   # Method to check the arguments for trimming and raise on bad sequence, qualities,
index 20a6dae3a54819bf2e49ead83acde359c19b2f82..2c5bafd7568a003c7d66e549f3c856c106e8822a 100755 (executable)
@@ -144,6 +144,56 @@ class TestTrim < Test::Unit::TestCase
     assert_equal("", @entry.seq) 
     assert_equal("", @entry.qual) 
   end
+
+  test "#patmatch_trim_right! without match don't trim" do
+    @entry.seq  = "GCTCAAACGTG"
+    @entry.patmatch_trim_left!("GAAAC")
+    assert_equal("GCTCAAACGTG", @entry.seq) 
+  end
+
+  test "#patmatch_trim_right! without match returns nil" do
+    @entry.seq  = "GCTCAAACGTG"
+    assert_nil(@entry.patmatch_trim_left!("GAAAC"))
+  end
+
+  test "#patmatch_trim_right! without qual trims correctly" do
+    @entry.seq  = "GCTCAAACGTG"
+    @entry.patmatch_trim_left!("AAAC")
+    assert_equal("GTG", @entry.seq) 
+  end
+
+  test "#patmatch_trim_right! with qual trims correctly" do
+    @entry.seq  = "GCTCAAACGTG"
+    @entry.qual = "IEFGHIHGFEI"
+    @entry.patmatch_trim_left!("AAAC")
+    assert_equal("GTG", @entry.seq) 
+    assert_equal("FEI", @entry.qual) 
+  end
+
+  test "#patmatch_trim_left! without match trims correctly" do
+    @entry.seq  = "GCTCAAACGTG"
+    @entry.patmatch_trim_right!("GAAAC")
+    assert_equal("GCTCAAACGTG", @entry.seq) 
+  end
+
+  test "#patmatch_trim_left! without match returns nil" do
+    @entry.seq  = "GCTCAAACGTG"
+    assert_nil(@entry.patmatch_trim_right!("GAAAC"))
+  end
+
+  test "#patmatch_trim_left! without qual trims correctly" do
+    @entry.seq  = "GCTCAAACGTG"
+    @entry.patmatch_trim_right!("AAAC")
+    assert_equal("GCTC", @entry.seq) 
+  end
+
+  test "#patmatch_trim_left! with qual trims correctly" do
+    @entry.seq  = "GCTCAAACGTG"
+    @entry.qual = "IEFGHIHGFEI"
+    @entry.patmatch_trim_right!("AAAC")
+    assert_equal("GCTC", @entry.seq) 
+    assert_equal("IEFG", @entry.qual) 
+  end
 end
 
 __END__