# ------------------------------------------------------------------------------
# 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
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
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
@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