]> git.donarmstrong.com Git - biopieces.git/commitdiff
fixed unit tests for patternmatcher
authormartinahansen <martinahansen@74ccb610-7750-0410-82ae-013aeee3265d>
Tue, 4 Dec 2012 10:35:06 +0000 (10:35 +0000)
committermartinahansen <martinahansen@74ccb610-7750-0410-82ae-013aeee3265d>
Tue, 4 Dec 2012 10:35:06 +0000 (10:35 +0000)
git-svn-id: http://biopieces.googlecode.com/svn/trunk@2025 74ccb610-7750-0410-82ae-013aeee3265d

code_ruby/lib/maasha/seq/patternmatcher.rb
code_ruby/test/maasha/seq/test_patternmatcher.rb

index 415c337c186977965928dadc35d4c6733585d310..4c0df650bed52952e933b43a96c61a28f642998f 100644 (file)
 
 require 'inline'
 
+# Module containing code to locate nucleotide patterns in sequences allowing for
+# ambiguity codes and a given maximum edit distance.
+# Insertions are nucleotides found in the pattern but not in the sequence.
+# Deletions are nucleotides found in the sequence but not in the pattern.
+#
+# Inspired by the paper by Bruno Woltzenlogel Paleo (page 197):
+# http://www.logic.at/people/bruno/Papers/2007-GATE-ESSLLI.pdf
 module PatternMatcher
+  def match(pattern, pos = 0, max_edit_distance = 0)
+    self.scan(pattern, pos, max_edit_distance) do |m|
+      return m
+    end
+  end
+
   # ------------------------------------------------------------------------------
   #   str.scan(pattern[, pos[, max_edit_distance]])
   #   -> Array
index d6caf419cd2fdac6a0612267a3b007041c1ae603..42b1f636b7260e6e6bb1751265677a50216244ad 100755 (executable)
@@ -30,9 +30,12 @@ require 'maasha/seq/patternmatcher'
 require 'test/unit'
 require 'pp'
 
-include PatternMatcher
+class Seq
+  include PatternMatcher
+end
 
 class TestPatternMatcher < Test::Unit::TestCase
+
   def setup
     @p = Seq.new("test", "atcg")
   end
@@ -41,99 +44,93 @@ class TestPatternMatcher < Test::Unit::TestCase
     assert_nil(@p.match("gggg"))
   end
 
-#  def test_PatternMatcher_match_perfect_returns_correctly
-#    m = @p.match("atcg")
-#    assert_equal(0, m.pos)
-#    assert_equal("atcg", m.match)
-#    assert_equal(4, m.matches)
-#    assert_equal(0, m.mismatches)
-#    assert_equal(0, m.insertions)
-#    assert_equal(0, m.deletions)
-#    assert_equal(4, m.length)
-#  end
-#
-#  def test_PatternMatcher_match_perfect_with_ambiguity_codes_returns_correctly
-#    m = @p.match("nnnn")
-#    assert_equal(0, m.pos)
-#    assert_equal("atcg", m.match)
-#    assert_equal(4, m.matches)
-#    assert_equal(0, m.mismatches)
-#    assert_equal(0, m.insertions)
-#    assert_equal(0, m.deletions)
-#    assert_equal(4, m.length)
-#  end
-#
-#  def test_PatternMatcher_match_with_one_mismatch_and_edit_dist_zero_returns_nil
-#    assert_nil(@p.match("aCcg"))
-#  end
-#
-#  def test_PatternMatcher_match_with_one_mismatch_and_edit_dist_one_returns_correctly
-#    m = @p.match("aCcg", 0, 1)
-#    assert_equal(0, m.pos)
-#    assert_equal("atcg", m.match)
-#    assert_equal(3, m.matches)
-#    assert_equal(1, m.mismatches)
-#    assert_equal(0, m.insertions)
-#    assert_equal(0, m.deletions)
-#    assert_equal(4, m.length)
-#  end
-#
-#  def test_PatternMatcher_match_with_two_mismatch_and_edit_dist_one_returns_nil
-#    assert_nil(@p.match("aGcA", 0, 1))
-#  end
-#
-#  def test_PatternMatcher_match_with_one_insertion_and_edit_dist_zero_returns_nil
-#    assert_nil(@p.match("atGcg"))
-#  end
-#
-#  def test_PatternMatcher_match_with_one_insertion_and_edit_dist_one_returns_correctly
-#    m = @p.match("atGcg", 0, 1)
-#    assert_equal(0, m.pos)
-#    assert_equal("atcg", m.match)
-#    assert_equal(4, m.matches)
-#    assert_equal(0, m.mismatches)
-#    assert_equal(1, m.insertions)
-#    assert_equal(0, m.deletions)
-#    assert_equal(4, m.length)
-#  end
-#
-#  def test_PatternMatcher_match_with_two_insertions_and_edit_dist_one_returns_nil
-#    assert_nil(@p.match("atGcTg", 0, 1))
-#  end
-#
-#  def test_PatternMatcher_match_with_two_insertions_and_edit_dist_two_returns_correctly
-#    m = @p.match("atGcTg", 0, 2)
-#    assert_equal(0, m.pos)
-#    assert_equal("atcg", m.match)
-#    assert_equal(4, m.matches)
-#    assert_equal(0, m.mismatches)
-#    assert_equal(2, m.insertions)
-#    assert_equal(0, m.deletions)
-#    assert_equal(4, m.length)
-#  end
-#
-#  def test_PatternMatcher_match_with_one_deletion_and_edit_distance_zero_returns_nil
-#    assert_nil(@p.match("acg"))
-#  end
-#
-#  def test_PatternMatcher_match_with_one_deletion_and_edit_distance_one_returns_correctly
-#    m = @p.match("acg", 0, 1)
-#    assert_equal(0, m.pos)
-#    assert_equal("atcg", m.match)
-#    assert_equal(3, m.matches)
-#    assert_equal(0, m.mismatches)
-#    assert_equal(0, m.insertions)
-#    assert_equal(1, m.deletions)
-#    assert_equal(4, m.length)
-#  end
-#
-#  def test_PatternMatcher_scan_locates_three_patterns_ok
-#    p = Seq.new("test", "ataacgagctagctagctagctgactac")
-#    assert_equal(3, p.scan("tag").count)
-#  end
-#
-#  def test_PatternMatcher_scan_with_pos_locates_two_patterns_ok
-#    p = Seq.new("test", "ataacgagctagctagctagctgactac")
-#    assert_equal(2, p.scan("tag", 10).count)
-#  end
+  def test_PatternMatcher_match_perfect_returns_correctly
+    m = @p.match("atcg")
+    assert_equal(0, m.beg)
+    assert_equal("atcg", m.match)
+    assert_equal(0, m.mis)
+    assert_equal(0, m.ins)
+    assert_equal(0, m.del)
+    assert_equal(4, m.length)
+  end
+
+  def test_PatternMatcher_match_perfect_with_ambiguity_codes_returns_correctly
+    m = @p.match("nnnn")
+    assert_equal(0, m.beg)
+    assert_equal("atcg", m.match)
+    assert_equal(0, m.mis)
+    assert_equal(0, m.ins)
+    assert_equal(0, m.del)
+    assert_equal(4, m.length)
+  end
+
+  def test_PatternMatcher_match_with_one_mismatch_and_edit_dist_zero_returns_nil
+    assert_nil(@p.match("aCcg"))
+  end
+
+  def test_PatternMatcher_match_with_one_mismatch_and_edit_dist_one_returns_correctly
+    m = @p.match("aCcg", 0, 1)
+    assert_equal(0, m.beg)
+    assert_equal("atcg", m.match)
+    assert_equal(1, m.mis)
+    assert_equal(0, m.ins)
+    assert_equal(0, m.del)
+    assert_equal(4, m.length)
+  end
+
+  def test_PatternMatcher_match_with_two_mismatch_and_edit_dist_one_returns_nil
+    assert_nil(@p.match("aGcA", 0, 1))
+  end
+
+  def test_PatternMatcher_match_with_one_insertion_and_edit_dist_zero_returns_nil
+    assert_nil(@p.match("atGcg"))
+  end
+
+  def test_PatternMatcher_match_with_one_insertion_and_edit_dist_one_returns_correctly
+    m = @p.match("atGcg", 0, 1)
+    assert_equal(0, m.beg)
+    assert_equal("atcg", m.match)
+    assert_equal(0, m.mis)
+    assert_equal(1, m.ins)
+    assert_equal(0, m.del)
+    assert_equal(4, m.length)
+  end
+
+  def test_PatternMatcher_match_with_two_insertions_and_edit_dist_one_returns_nil
+    assert_nil(@p.match("atGcTg", 0, 1))
+  end
+
+  def test_PatternMatcher_match_with_two_insertions_and_edit_dist_two_returns_correctly
+    m = @p.match("atGcTg", 0, 2)
+    assert_equal(0, m.beg)
+    assert_equal("atcg", m.match)
+    assert_equal(0, m.mis)
+    assert_equal(2, m.ins)
+    assert_equal(0, m.del)
+    assert_equal(4, m.length)
+  end
+
+  def test_PatternMatcher_match_with_one_deletion_and_edit_distance_zero_returns_nil
+    assert_nil(@p.match("acg"))
+  end
+
+  def test_PatternMatcher_match_with_one_deletion_and_edit_distance_one_returns_correctly
+    m = @p.match("acg", 0, 1)
+    assert_equal(0, m.beg)
+    assert_equal("atcg", m.match)
+    assert_equal(0, m.mis)
+    assert_equal(0, m.ins)
+    assert_equal(1, m.del)
+    assert_equal(4, m.length)
+  end
+
+  def test_PatternMatcher_scan_locates_three_patterns_ok
+    p = Seq.new("test", "ataacgagctagctagctagctgactac")
+    assert_equal(3, p.scan("tag").count)
+  end
+
+  def test_PatternMatcher_scan_with_pos_locates_two_patterns_ok
+    p = Seq.new("test", "ataacgagctagctagctagctgactac")
+    assert_equal(2, p.scan("tag", 10).count)
+  end
 end