]> git.donarmstrong.com Git - biopieces.git/commitdiff
worked on match code in Seq.rb
authormartinahansen <martinahansen@74ccb610-7750-0410-82ae-013aeee3265d>
Sat, 19 Mar 2011 21:07:42 +0000 (21:07 +0000)
committermartinahansen <martinahansen@74ccb610-7750-0410-82ae-013aeee3265d>
Sat, 19 Mar 2011 21:07:42 +0000 (21:07 +0000)
git-svn-id: http://biopieces.googlecode.com/svn/trunk@1303 74ccb610-7750-0410-82ae-013aeee3265d

code_ruby/Maasha/lib/seq.rb
code_ruby/Maasha/test/test_seq.rb

index 60f91bdf1b9a68581ae60cc427a33bf1f1070c6d..5dedb36b1de7abc9ca693307a47faa227e46d4f5 100644 (file)
@@ -433,13 +433,11 @@ class Seq
   # ------------------------------------------------------------------------------
   # Method to locate a pattern in a sequence and return the position of the match
   # or nil if no match was found. Hamming or Edit distance may be specified.
-  def match(pattern, pos = 0)
+  def match(pattern, pos = 0, hd = 0, ed = 0)
     while pos < self.length - pattern.length + 1
       str1 = self.seq[pos ... pos + pattern.length]
       str2 = pattern
 
-      puts "pos: #{pos} str1: #{str1} str2: #{str2}"
-
       rows = str1.length + 1
       cols = str2.length + 1
 
@@ -455,8 +453,6 @@ class Seq
 
       for j in 1 ... cols do
         for i in 1 ... rows do
-          puts "pos: #{pos}   i: #{i}   j: #{j}   str1: #{str1}   str2: #{str2}   str1[i-1]: #{str1[i-1]}   str2[j-1]: #{str2[j-1]}"
-
           if EQUAL[(str1[i - 1].upcase + str2[j - 1].upcase).to_sym]
             matrix[i, j] = matrix[i - 1, j - 1]
             matches += 1
@@ -478,10 +474,8 @@ class Seq
           end
         end
       end
-      pp matrix
-      puts "match: #{matches}  mis: #{mismatches}   del: #{deletions}   ins: #{insertions}"
 
-      return pos if matrix[rows - 1, cols - 1] == 0
+      return pos if matrix[rows - 1, cols - 1] <= hd
 
       pos += 1
     end
index d8317d370ed95d864fe644f94d56b8fcc2f3a884..f8ae29e10a47d597d6643c558ba0fe92c0fbbded 100755 (executable)
@@ -457,6 +457,14 @@ class TestSeq < Test::Unit::TestCase
     @entry.seq = "atcatc"
     assert_equal(3, @entry.match("aTc", 2))
   end
+
+  def test_Seq_match_with_hamming_dist_returns_correctly
+    @entry.seq = "atcg"
+    assert_equal(0, @entry.match("XTCG", pos=0, hd=1))
+    assert_equal(0, @entry.match("TTCX", pos=0, hd=2))
+    assert_equal(0, @entry.match("XXCX", pos=0, hd=3))
+    assert_equal(0, @entry.match("XXXX", pos=0, hd=4))
+  end
 end