]> git.donarmstrong.com Git - biopieces.git/blob - code_ruby/Maasha/test/test_seq.rb
2482422611ca53fbdf9a5cb5e61b3848011706e7
[biopieces.git] / code_ruby / Maasha / test / test_seq.rb
1 #!/usr/bin/env ruby
2
3 require 'seq'
4 require 'test/unit'
5 require 'pp'
6
7 class TestSeq < Test::Unit::TestCase 
8   def setup
9     @entry = Seq.new
10   end
11
12   #  def test_Seq# autoremoves whitespace, newlines, and carriage returns
13   #    dna = Seq.new
14   #    dna.seq = "A\tT\r\tC\nG  "
15   #    assert_equal(dna.seq, "ATCG")
16   #  end
17
18   def test_Seq_is_dna_with_no_sequence_type_returns_false
19     assert(@entry.is_dna? == false)
20   end
21
22   def test_Seq_is_dna_with_dna_sequence_type_returns_true
23     @entry.type = 'dna'
24     assert(@entry.is_dna? == true)
25   end
26
27   def test_Seq_is_rna_with_no_sequence_type_returns_false
28     assert(@entry.is_rna? == false)
29   end
30
31   def test_Seq_is_rna_with_rna_sequence_type_returns_true
32     @entry.type = 'rna'
33     assert(@entry.is_rna? == true)
34   end
35
36   def test_Seq_is_protein_with_no_sequence_type_returns_false
37     assert(@entry.is_protein? == false)
38   end
39
40   def test_Seq_is_protein_with_protein_sequence_type_returns_true
41     @entry.type = 'protein'
42     assert(@entry.is_protein? == true)
43   end
44
45   def test_Seq_length_is_correct
46     @entry.seq = 'ATCG'
47     assert_equal(4, @entry.length)
48   end
49
50   def test_Seq_indels_is_correct
51     @entry.seq = 'ATCG.-~_'
52     assert_equal(4, @entry.indels)
53   end
54
55   def test_Seq_to_rna_raises_if_no_sequence
56     @entry.type = 'dna'
57     assert_raise(SeqError) { @entry.to_rna }
58   end
59
60   def test_Seq_to_rna_raises_on_bad_type
61     @entry.seq  = 'ATCG'
62     @entry.type = 'rna'
63     assert_raise(SeqError) { @entry.to_rna }
64   end
65
66   def test_Seq_to_rna_transcribes_correctly
67     @entry.seq  = 'ATCGatcg'
68     @entry.type = 'dna'
69     assert_equal("AUCGaucg", @entry.to_rna)
70   end
71
72   def test_Seq_to_rna_changes_entry_type_to_rna
73     @entry.seq  = 'ATCGatcg'
74     @entry.type = 'dna'
75     @entry.to_rna
76     assert_equal("rna", @entry.type)
77   end
78
79   def test_Seq_to_dna_raises_if_no_sequence
80     @entry.type = 'rna'
81     assert_raise(SeqError) { @entry.to_dna }
82   end
83
84   def test_Seq_to_dna_raises_on_bad_type
85     @entry.seq  = 'AUCG'
86     @entry.type = 'dna'
87     assert_raise(SeqError) { @entry.to_dna }
88   end
89
90   def test_Seq_to_dna_transcribes_correctly
91     @entry.seq  = 'AUCGaucg'
92     @entry.type = 'rna'
93     assert_equal("ATCGatcg", @entry.to_dna)
94   end
95
96   def test_Seq_to_dna_changes_entry_type_to_dna
97     @entry.seq  = 'AUCGaucg'
98     @entry.type = 'rna'
99     @entry.to_dna
100     assert_equal("dna", @entry.type)
101   end
102
103   def test_Seq_to_bp_returns_correct_record
104     @entry.seq_name = 'test'
105     @entry.seq      = 'ATCG'
106     assert_equal({:SEQ_NAME=>"test", :SEQ=>"ATCG", :SEQ_LEN=>4}, @entry.to_bp)
107   end
108
109   def test_Seq_to_bp_raises_on_missing_seq_name
110     @entry.seq = 'ATCG'
111     assert_raise(SeqError) { @entry.to_bp }
112   end
113
114   def test_Seq_to_bp_raises_on_missing_sequence
115     @entry.seq_name = 'test'
116     assert_raise(SeqError) { @entry.to_bp }
117   end
118
119   def test_Seq_to_fasta_returns_correct_entry
120     @entry.seq_name = 'test'
121     @entry.seq      = 'ATCG'
122     assert_equal(">test\nATCG\n", @entry.to_fasta)
123   end
124
125   def test_Seq_to_fasta_wraps_correctly
126     entry = Seq.new("test", "ATCG")
127     assert_equal(">test\nAT\nCG\n", entry.to_fasta(2))
128   end
129
130   def test_Seq_to_key_with_bad_residue_raises
131     entry = Seq.new("test", "AUCG")
132     assert_raise(SeqError) { entry.to_key }
133   end
134
135   def test_Seq_to_key_returns_correctly
136     entry = Seq.new("test", "ATCG")
137     assert_equal(54, entry.to_key)
138   end
139
140   def test_Seq_reverse_returns_correctly
141     @entry.seq = "ATCG"
142     assert_equal("GCTA", @entry.reverse)
143   end
144
145   def test_Seq_complement_raises_if_no_sequence
146     @entry.type = 'dna'
147     assert_raise(SeqError) { @entry.complement }
148   end
149
150   def test_Seq_complement_raises_on_bad_type
151     @entry.seq  = 'ATCG'
152     @entry.type = 'protein'
153     assert_raise(SeqError) { @entry.complement }
154   end
155
156   def test_Seq_complement_for_DNA_is_correct
157     @entry.seq  = 'ATCGatcg'
158     @entry.type = 'dna'
159     assert_equal("TAGCtagc", @entry.complement)
160   end
161
162   def test_Seq_complement_for_RNA_is_correct
163     @entry.seq  = 'AUCGaucg'
164     @entry.type = 'rna'
165     assert_equal("UAGCuagc", @entry.complement)
166   end
167
168   def test_Seq_reverse_complement_for_DNA_is_correct
169     @entry.seq  = 'ATCGatcg'
170     @entry.type = 'dna'
171     assert_equal("cgatCGAT", @entry.reverse_complement)
172   end
173
174   def test_Seq_reverse_complement_for_RNA_is_correct
175     @entry.seq  = 'AUCGaucg'
176     @entry.type = 'rna'
177     assert_equal("cgauCGAU", @entry.reverse_complement)
178   end
179
180   def test_Seq_generate_with_length_lt_1_raises
181     assert_raise(SeqError) { @entry.generate(-10, "dna") }
182     assert_raise(SeqError) { @entry.generate(0, "dna") }
183   end
184
185   def test_Seq_generate_with_bad_type_raises
186     assert_raise(SeqError) { @entry.generate(10, "foo") }
187   end
188
189   def test_Seq_generate_with_ok_type_dont_raise
190     %w[dna DNA rna RNA protein Protein].each do |type|
191       assert_nothing_raised { @entry.generate(10, type) }
192     end
193   end
194
195   def test_Seq_subseq_with_start_lt_0_raises
196     @entry.seq = "ATCG"
197     assert_raise(SeqError) { @entry.subseq(-1, 1) }
198   end
199
200   def test_Seq_subseq_with_length_lt_1_raises
201     @entry.seq = "ATCG"
202     assert_raise(SeqError) { @entry.subseq(0, 0) }
203   end
204
205   def test_Seq_subseq_with_start_plus_length_gt_seq_raises
206     @entry.seq = "ATCG"
207     assert_raise(SeqError) { @entry.subseq(0, 5) }
208   end
209
210   def test_Seq_subseq_returns_correct_sequence
211     @entry.seq  = "ATCG"
212     assert_equal("AT", @entry.subseq(0, 2).seq)
213     assert_equal("CG", @entry.subseq(2, 2).seq)
214   end
215
216   def test_Seq_subseq_returns_correct_qual
217     @entry.seq  = "ATCG"
218     @entry.qual = "abcd"
219     assert_equal("ab", @entry.subseq(0, 2).qual)
220     assert_equal("cd", @entry.subseq(2, 2).qual)
221   end
222
223   def test_Seq_subseq_rand_returns_correct_sequence
224     @entry.seq  = "ATCG"
225     assert_equal("ATCG", @entry.subseq_rand(4).seq)
226   end
227
228   def test_Seq_composition_returns_correctly
229     @entry.seq = "AAAATTTCCG"
230     assert_equal(4, @entry.composition["A"])
231     assert_equal(3, @entry.composition["T"])
232     assert_equal(2, @entry.composition["C"])
233     assert_equal(1, @entry.composition["G"])
234     assert_equal(0, @entry.composition["X"])
235   end
236
237   def test_Seq_homopol_max_returns_0_with_empty_sequence
238     @entry.seq = ""
239     assert_equal(0, @entry.homopol_max)
240   end
241
242   def test_Seq_homopol_max_returns_0_with_nil_sequence
243     @entry.seq = nil
244     assert_equal(0, @entry.homopol_max)
245   end
246
247   def test_Seq_homopol_max_returns_0_when_not_found
248     @entry.seq = "AtTcCcGggGnnNnn"
249     assert_equal(0, @entry.homopol_max(6))
250   end
251
252   def test_Seq_homopol_max_returns_correctly
253     @entry.seq = "AtTcCcGggGnnNnn"
254     assert_equal(5, @entry.homopol_max(3))
255   end
256
257   def test_Seq_hard_mask_returns_correctly
258     @entry.seq = "--AAAANn"
259     assert_equal(33.33, @entry.hard_mask)
260   end
261
262   def test_Seq_soft_mask_returns_correctly
263     @entry.seq = "--AAAa"
264     assert_equal(25.00, @entry.soft_mask)
265   end
266
267   def test_Seq_adaptor_locate_right_returns_correctly
268     @entry.seq = "nnnnncgat"
269     assert_equal(-1, @entry.adaptor_locate_right("X"))
270     assert_equal(8,  @entry.adaptor_locate_right("TX"))
271     assert_equal(7,  @entry.adaptor_locate_right("ATX"))
272     assert_equal(6,  @entry.adaptor_locate_right("GATX"))
273     assert_equal(5,  @entry.adaptor_locate_right("CGATX"))
274     assert_equal(0,  @entry.adaptor_locate_right("NNNNNCGAT"))
275   end
276
277   def test_Seq_adaptor_locate_left_returns_correctly
278     @entry.seq = "cgatnnnnn"
279     assert_equal(-1, @entry.adaptor_locate_left("X"))
280     assert_equal(0,  @entry.adaptor_locate_left("XC"))
281     assert_equal(1,  @entry.adaptor_locate_left("XCG"))
282     assert_equal(2,  @entry.adaptor_locate_left("XCGA"))
283     assert_equal(3,  @entry.adaptor_locate_left("XCGAT"))
284     assert_equal(8,  @entry.adaptor_locate_left("CGATNNNNN"))
285   end
286
287   def test_Seq_adaptor_clip_right_returns_correct_sequence
288     @entry.seq = "nnnnncgat"
289     @entry.adaptor_clip_right("cgat")
290     assert_equal( "nnnnn", @entry.seq)
291   end
292
293   def test_Seq_adaptor_clip_right_returns_correct_qual
294     @entry.seq  = "nnnnncgat"
295     @entry.qual = "abcdefghi"
296     @entry.adaptor_clip_right("cgat")
297     assert_equal( "abcde", @entry.qual)
298   end
299
300   def test_Seq_adaptor_clip_left_returns_correct_sequence
301     @entry.seq = "cgatnnnnn"
302     @entry.adaptor_clip_left("cgat")
303     assert_equal( "nnnnn", @entry.seq)
304   end
305
306   def test_Seq_adaptor_clip_left_returns_correct_qual
307     @entry.seq  = "cgatnnnnn"
308     @entry.qual = "abcdefghi"
309     @entry.adaptor_clip_left("cgat")
310     assert_equal( "efghi", @entry.qual)
311   end
312
313   def test_Digest_new_raises_on_bad_pattern_residue
314     assert_raise(DigestError) { Digest.new(@entry, "X", 4) }
315   end
316
317   def test_Digest_new_dont_raise_on_ok_pattern_residue
318     assert_nothing_raised { Digest.new(@entry, "AGCUTRYWSMKHDVBNagcutrywsmkhdvbn", 4) }
319   end
320
321   def test_Digest_each
322     @entry.seq = "aaaaTTTTbbbbTTTT"
323     digest = Digest.new(@entry, "TTNT", 1)
324     assert_equal("aaaaT", digest.first.seq)
325   end
326 end
327
328
329 __END__