]> git.donarmstrong.com Git - biopieces.git/blob - code_ruby/test/maasha/seq/test_patternmatcher.rb
refactor match->patmatch scan->patscan
[biopieces.git] / code_ruby / test / maasha / seq / test_patternmatcher.rb
1 #!/usr/bin/env ruby
2 $:.unshift File.join(File.dirname(__FILE__),'..','lib')
3
4 # Copyright (C) 2007-2010 Martin A. Hansen.
5
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
20 # http://www.gnu.org/copyleft/gpl.html
21
22 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
23
24 # This software is part of the Biopieces framework (www.biopieces.org).
25
26 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
27
28 require 'maasha/seq'
29 require 'maasha/seq/patternmatcher'
30 require 'test/unit'
31 require 'pp'
32
33 class Seq
34   include PatternMatcher
35 end
36
37 class TestPatternMatcher < Test::Unit::TestCase
38
39   def setup
40     @p = Seq.new("test", "atcg")
41   end
42
43   def test_PatternMatcher_no_match_returns_nil
44     assert_nil(@p.patmatch("gggg"))
45   end
46
47   def test_PatternMatcher_patmatch_perfect_returns_correctly
48     m = @p.patmatch("atcg")
49     assert_equal(0, m.beg)
50     assert_equal("atcg", m.match)
51     assert_equal(0, m.mis)
52     assert_equal(0, m.ins)
53     assert_equal(0, m.del)
54     assert_equal(4, m.length)
55   end
56
57   def test_PatternMatcher_patmatch_perfect_with_ambiguity_codes_returns_correctly
58     m = @p.patmatch("nnnn")
59     assert_equal(0, m.beg)
60     assert_equal("atcg", m.match)
61     assert_equal(0, m.mis)
62     assert_equal(0, m.ins)
63     assert_equal(0, m.del)
64     assert_equal(4, m.length)
65   end
66
67   def test_PatternMatcher_patmatch_with_one_mismatch_and_edit_dist_zero_returns_nil
68     assert_nil(@p.patmatch("aCcg"))
69   end
70
71   def test_PatternMatcher_patmatch_with_one_mismatch_and_edit_dist_one_returns_correctly
72     m = @p.patmatch("aCcg", 0, 1)
73     assert_equal(0, m.beg)
74     assert_equal("atcg", m.match)
75     assert_equal(1, m.mis)
76     assert_equal(0, m.ins)
77     assert_equal(0, m.del)
78     assert_equal(4, m.length)
79   end
80
81   def test_PatternMatcher_patmatch_with_two_mismatch_and_edit_dist_one_returns_nil
82     assert_nil(@p.patmatch("aGcA", 0, 1))
83   end
84
85   def test_PatternMatcher_patmatch_with_one_insertion_and_edit_dist_zero_returns_nil
86     assert_nil(@p.patmatch("atGcg"))
87   end
88
89   def test_PatternMatcher_patmatch_with_one_insertion_and_edit_dist_one_returns_correctly
90     m = @p.patmatch("atGcg", 0, 1)
91     assert_equal(0, m.beg)
92     assert_equal("atcg", m.match)
93     assert_equal(0, m.mis)
94     assert_equal(1, m.ins)
95     assert_equal(0, m.del)
96     assert_equal(4, m.length)
97   end
98
99   def test_PatternMatcher_patmatch_with_two_insertions_and_edit_dist_one_returns_nil
100     assert_nil(@p.patmatch("atGcTg", 0, 1))
101   end
102
103   def test_PatternMatcher_patmatch_with_two_insertions_and_edit_dist_two_returns_correctly
104     m = @p.patmatch("atGcTg", 0, 2)
105     assert_equal(0, m.beg)
106     assert_equal("atcg", m.match)
107     assert_equal(0, m.mis)
108     assert_equal(2, m.ins)
109     assert_equal(0, m.del)
110     assert_equal(4, m.length)
111   end
112
113   def test_PatternMatcher_patmatch_with_one_deletion_and_edit_distance_zero_returns_nil
114     assert_nil(@p.patmatch("acg"))
115   end
116
117   def test_PatternMatcher_patmatch_with_one_deletion_and_edit_distance_one_returns_correctly
118     m = @p.patmatch("acg", 0, 1)
119     assert_equal(0, m.beg)
120     assert_equal("atcg", m.match)
121     assert_equal(0, m.mis)
122     assert_equal(0, m.ins)
123     assert_equal(1, m.del)
124     assert_equal(4, m.length)
125   end
126
127   def test_PatternMatcher_patscan_locates_three_patterns_ok
128     p = Seq.new("test", "ataacgagctagctagctagctgactac")
129     assert_equal(3, p.patscan("tag").count)
130   end
131
132   def test_PatternMatcher_patscan_with_pos_locates_two_patterns_ok
133     p = Seq.new("test", "ataacgagctagctagctagctgactac")
134     assert_equal(2, p.patscan("tag", 10).count)
135   end
136 end