]> git.donarmstrong.com Git - biopieces.git/blob - code_ruby/test/maasha/seq/test_dynamic.rb
renamed patternmatcher to dynamic
[biopieces.git] / code_ruby / test / maasha / seq / test_dynamic.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/dynamic'
30 require 'test/unit'
31 require 'pp'
32
33 class Seq
34   include Dynamic
35 end
36
37 class TestDynamic < Test::Unit::TestCase
38   def setup
39     @p = Seq.new("test", "atcg")
40   end
41
42   def test_Dynamic_no_match_returns_nil
43     assert_nil(@p.patmatch("gggg"))
44   end
45
46   def test_Dynamic_patmatch_perfect_returns_correctly
47     m = @p.patmatch("atcg")
48     assert_equal(0, m.beg)
49     assert_equal("atcg", m.match)
50     assert_equal(0, m.mis)
51     assert_equal(0, m.ins)
52     assert_equal(0, m.del)
53     assert_equal(4, m.length)
54   end
55
56   def test_Dynamic_patmatch_perfect_with_ambiguity_codes_returns_correctly
57     m = @p.patmatch("nnnn")
58     assert_equal(0, m.beg)
59     assert_equal("atcg", m.match)
60     assert_equal(0, m.mis)
61     assert_equal(0, m.ins)
62     assert_equal(0, m.del)
63     assert_equal(4, m.length)
64   end
65
66   def test_Dynamic_patmatch_with_one_mismatch_and_edit_dist_zero_returns_nil
67     assert_nil(@p.patmatch("aCcg"))
68   end
69
70   def test_Dynamic_patmatch_with_one_mismatch_and_edit_dist_one_returns_correctly
71     m = @p.patmatch("aCcg", 0, 1)
72     assert_equal(0, m.beg)
73     assert_equal("atcg", m.match)
74     assert_equal(1, m.mis)
75     assert_equal(0, m.ins)
76     assert_equal(0, m.del)
77     assert_equal(4, m.length)
78   end
79
80   def test_Dynamic_patmatch_with_two_mismatch_and_edit_dist_one_returns_nil
81     assert_nil(@p.patmatch("aGcA", 0, 1))
82   end
83
84   def test_Dynamic_patmatch_with_one_insertion_and_edit_dist_zero_returns_nil
85     assert_nil(@p.patmatch("atGcg"))
86   end
87
88   def test_Dynamic_patmatch_with_one_insertion_and_edit_dist_one_returns_correctly
89     m = @p.patmatch("atGcg", 0, 1)
90     assert_equal(0, m.beg)
91     assert_equal("atcg", m.match)
92     assert_equal(0, m.mis)
93     assert_equal(1, m.ins)
94     assert_equal(0, m.del)
95     assert_equal(4, m.length)
96   end
97
98   def test_Dynamic_patmatch_with_two_insertions_and_edit_dist_one_returns_nil
99     assert_nil(@p.patmatch("atGcTg", 0, 1))
100   end
101
102   def test_Dynamic_patmatch_with_two_insertions_and_edit_dist_two_returns_correctly
103     m = @p.patmatch("atGcTg", 0, 2)
104     assert_equal(0, m.beg)
105     assert_equal("atcg", m.match)
106     assert_equal(0, m.mis)
107     assert_equal(2, m.ins)
108     assert_equal(0, m.del)
109     assert_equal(4, m.length)
110   end
111
112   def test_Dynamic_patmatch_with_one_deletion_and_edit_distance_zero_returns_nil
113     assert_nil(@p.patmatch("acg"))
114   end
115
116   def test_Dynamic_patmatch_with_one_deletion_and_edit_distance_one_returns_correctly
117     m = @p.patmatch("acg", 0, 1)
118     assert_equal(0, m.beg)
119     assert_equal("atcg", m.match)
120     assert_equal(0, m.mis)
121     assert_equal(0, m.ins)
122     assert_equal(1, m.del)
123     assert_equal(4, m.length)
124   end
125
126   def test_Dynamic_patscan_locates_three_patterns_ok
127     p = Seq.new("test", "ataacgagctagctagctagctgactac")
128     assert_equal(3, p.patscan("tag").count)
129   end
130
131   def test_Dynamic_patscan_with_pos_locates_two_patterns_ok
132     p = Seq.new("test", "ataacgagctagctagctagctgactac")
133     assert_equal(2, p.patscan("tag", 10).count)
134   end
135 end