]> git.donarmstrong.com Git - biopieces.git/blob - code_ruby/test/maasha/seq/test_backtrack.rb
added unit test to backtrack code
[biopieces.git] / code_ruby / test / maasha / seq / test_backtrack.rb
1 #!/usr/bin/env ruby
2 $:.unshift File.join(File.dirname(__FILE__),'..','lib')
3
4 # Copyright (C) 2007-2011 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 'test/unit'
29 require 'maasha/seq'
30
31 class BackTrackTest < Test::Unit::TestCase
32   def setup
33     #                       0         1
34     #                       01234567890123456789
35     @seq = Seq.new("test", "tacgatgctagcatgcacgg")
36     @seq.extend(BackTrack)
37   end
38
39   def test_BackTrack_patscan_with_bad_pattern_raises
40     ["", "X", "1"].each { |pattern|
41       assert_raise(BackTrackError) { @seq.patscan(pattern) }
42     }
43   end
44
45   def test_BackTrack_patscan_with_OK_pattern_dont_raise
46     ["N", "atcg"].each { |pattern|
47       assert_nothing_raised { @seq.patscan(pattern) }
48     }
49   end
50
51   def test_BackTrack_patscan_with_bad_start_raises
52     [-1, 20].each { |start| 
53       assert_raise(BackTrackError) { @seq.patscan("N", start) }
54     }
55   end
56
57   def test_BackTrack_patscan_with_OK_start_dont_raise
58     [0, 19].each { |start| 
59       assert_nothing_raised { @seq.patscan("N", start) }
60     }
61   end
62
63   def test_BackTrack_patscan_with_bad_stop_raises
64     [-1, 20].each { |stop|
65       assert_raise(BackTrackError) { @seq.patscan("N", [0, stop]) }
66     }
67   end
68
69   def test_BackTrack_patscan_with_OK_stop_dont_raise
70     [0, 19].each { |stop|
71       assert_nothing_raised { @seq.patscan("N", [0, stop]) }
72     }
73   end
74
75   def test_BackTrack_patscan_with_stop_returns_correctly
76     assert_nil(@seq.patmatch("G", [0, 2]))
77     assert_equal("3:1:g", @seq.patmatch("G", [0, 3]).to_s)
78   end
79
80   def test_BackTrack_patscan_with_bad_mis_raises
81     [-1, 6].each { |mis| 
82       assert_raise(BackTrackError) { @seq.patscan("N", 0, mis) }
83     }
84   end
85
86   def test_BackTrack_patscan_with_OK_mis_dont_raise
87     [0, 5].each { |mis| 
88       assert_nothing_raised { @seq.patscan("N", 0, mis) }
89     }
90   end
91
92   def test_BackTrack_patscan_with_bad_ins_raises
93     [-1, 6].each { |ins| 
94       assert_raise(BackTrackError) { @seq.patscan("N", 0, 0, ins) }
95     }
96   end
97
98   def test_BackTrack_patscan_with_OK_ins_dont_raise
99     [0, 5].each { |ins| 
100       assert_nothing_raised { @seq.patscan("N", 0, 0, ins) }
101     }
102   end
103
104   def test_BackTrack_patscan_with_bad_del_raises
105     [-1, 6].each { |del| 
106       assert_raise(BackTrackError) { @seq.patscan("N", 0, 0, 0, del) }
107     }
108   end
109
110   def test_BackTrack_patscan_with_OK_del_dont_raise
111     [0, 5].each { |del| 
112       assert_nothing_raised { @seq.patscan("N", 0, 0, 0, del) }
113     }
114   end
115
116   def test_BackTrack_patscan_perfect_left_is_ok
117     assert_equal("0:7:tacgatg", @seq.patscan("TACGATG").first.to_s)
118   end
119
120   def test_BackTrack_patscan_perfect_right_is_ok
121     assert_equal("13:7:tgcacgg", @seq.patscan("TGCACGG").first.to_s)
122   end
123
124   def test_BackTrack_patscan_ambiguity_is_ok
125     assert_equal("13:7:tgcacgg", @seq.patscan("TGCACNN").first.to_s)
126   end
127
128   def test_BackTrack_patscan_start_is_ok
129     assert_equal("10:1:g", @seq.patscan("N", 10).first.to_s)
130     assert_equal("19:1:g", @seq.patscan("N", 10).last.to_s)
131   end
132
133   def test_BackTrack_patscan_mis_left_is_ok
134     assert_equal("0:7:tacgatg", @seq.patscan("Aacgatg", 0, 1).first.to_s)
135   end
136
137   def test_BackTrack_patscan_mis_right_is_ok
138     assert_equal("13:7:tgcacgg", @seq.patscan("tgcacgA", 0, 1).first.to_s)
139   end
140
141   def test_BackTrack_patscan_ins_left_is_ok
142     assert_equal("0:7:tacgatg", @seq.patscan("Atacgatg", 0, 0, 1).first.to_s)
143   end
144
145   def test_BackTrack_patscan_ins_right_is_ok
146     assert_equal("13:7:tgcacgg", @seq.patscan("tgcacggA", 0, 0, 1).first.to_s)
147   end
148
149   def test_BackTrack_patscan_del_left_is_ok
150     assert_equal("0:7:tacgatg", @seq.patscan("acgatg", 0, 0, 0, 1).first.to_s)
151   end
152
153   def test_BackTrack_patscan_del_right_is_ok
154     assert_equal("12:8:atgcacgg", @seq.patscan("tgcacgg", 0, 0, 0, 1).first.to_s)
155   end
156
157   def test_BackTrack_patscan_ambiguity_mis_ins_del_all_ok
158     assert_equal("0:20:tacgatgctagcatgcacgg", @seq.patscan("tacatgcNagGatgcCacgg", 0, 1, 1, 1).first.to_s)
159   end
160 end
161