]> git.donarmstrong.com Git - biopieces.git/blob - code_ruby/test/maasha/seq/test_patternmatcher.rb
clean-up of ruby code to remove rake warnings
[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 'test/unit'
30 require 'pp'
31
32 class TestPatternMatcher < Test::Unit::TestCase
33   def setup
34     @p = Seq.new("test", "atcg")
35   end
36
37   def test_PatternMatcher_no_match_returns_nil
38     assert_nil(@p.match("gggg"))
39   end
40
41   def test_PatternMatcher_match_perfect_returns_correctly
42     m = @p.match("atcg")
43     assert_equal(0, m.pos)
44     assert_equal("atcg", m.match)
45     assert_equal(4, m.matches)
46     assert_equal(0, m.mismatches)
47     assert_equal(0, m.insertions)
48     assert_equal(0, m.deletions)
49     assert_equal(4, m.length)
50   end
51
52   def test_PatternMatcher_match_perfect_with_ambiguity_codes_returns_correctly
53     m = @p.match("nnnn")
54     assert_equal(0, m.pos)
55     assert_equal("atcg", m.match)
56     assert_equal(4, m.matches)
57     assert_equal(0, m.mismatches)
58     assert_equal(0, m.insertions)
59     assert_equal(0, m.deletions)
60     assert_equal(4, m.length)
61   end
62
63   def test_PatternMatcher_match_with_one_mismatch_and_edit_dist_zero_returns_nil
64     assert_nil(@p.match("aCcg"))
65   end
66
67   def test_PatternMatcher_match_with_one_mismatch_and_edit_dist_one_returns_correctly
68     m = @p.match("aCcg", 0, 1)
69     assert_equal(0, m.pos)
70     assert_equal("atcg", m.match)
71     assert_equal(3, m.matches)
72     assert_equal(1, m.mismatches)
73     assert_equal(0, m.insertions)
74     assert_equal(0, m.deletions)
75     assert_equal(4, m.length)
76   end
77
78   def test_PatternMatcher_match_with_two_mismatch_and_edit_dist_one_returns_nil
79     assert_nil(@p.match("aGcA", 0, 1))
80   end
81
82   def test_PatternMatcher_match_with_one_insertion_and_edit_dist_zero_returns_nil
83     assert_nil(@p.match("atGcg"))
84   end
85
86   def test_PatternMatcher_match_with_one_insertion_and_edit_dist_one_returns_correctly
87     m = @p.match("atGcg", 0, 1)
88     assert_equal(0, m.pos)
89     assert_equal("atcg", m.match)
90     assert_equal(4, m.matches)
91     assert_equal(0, m.mismatches)
92     assert_equal(1, m.insertions)
93     assert_equal(0, m.deletions)
94     assert_equal(4, m.length)
95   end
96
97   def test_PatternMatcher_match_with_two_insertions_and_edit_dist_one_returns_nil
98     assert_nil(@p.match("atGcTg", 0, 1))
99   end
100
101   def test_PatternMatcher_match_with_two_insertions_and_edit_dist_two_returns_correctly
102     m = @p.match("atGcTg", 0, 2)
103     assert_equal(0, m.pos)
104     assert_equal("atcg", m.match)
105     assert_equal(4, m.matches)
106     assert_equal(0, m.mismatches)
107     assert_equal(2, m.insertions)
108     assert_equal(0, m.deletions)
109     assert_equal(4, m.length)
110   end
111
112   def test_PatternMatcher_match_with_one_deletion_and_edit_distance_zero_returns_nil
113     assert_nil(@p.match("acg"))
114   end
115
116   def test_PatternMatcher_match_with_one_deletion_and_edit_distance_one_returns_correctly
117     m = @p.match("acg", 0, 1)
118     assert_equal(0, m.pos)
119     assert_equal("atcg", m.match)
120     assert_equal(3, m.matches)
121     assert_equal(0, m.mismatches)
122     assert_equal(0, m.insertions)
123     assert_equal(1, m.deletions)
124     assert_equal(4, m.length)
125   end
126
127   def test_PatternMatcher_scan_locates_three_patterns_ok
128     p = Seq.new("test", "ataacgagctagctagctagctgactac")
129     assert_equal(3, p.scan("tag").count)
130   end
131
132   def test_PatternMatcher_scan_with_pos_locates_two_patterns_ok
133     p = Seq.new("test", "ataacgagctagctagctagctgactac")
134     assert_equal(2, p.scan("tag", 10).count)
135   end
136 end