]> git.donarmstrong.com Git - biopieces.git/blob - code_ruby/Maasha/test/test_seq.rb
2e003e2e17c6dc21d83441f4580383a134b5c13d
[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_Sequence_length_is_correct
46     @entry.seq = 'ATCG'
47     assert_equal(4, @entry.length)
48   end
49
50   def test_Seq_to_rna_raises_if_no_sequence
51     @entry.type = 'dna'
52     assert_raise(SeqError) { @entry.to_rna }
53   end
54
55   def test_Seq_to_rna_raises_on_bad_type
56     @entry.seq  = 'ATCG'
57     @entry.type = 'rna'
58     assert_raise(SeqError) { @entry.to_rna }
59   end
60
61   def test_Seq_to_rna_transcribes_correctly
62     @entry.seq  = 'ATCGatcg'
63     @entry.type = 'dna'
64     assert_equal("AUCGaucg", @entry.to_rna)
65   end
66
67   def test_Seq_to_rna_changes_entry_type_to_rna
68     @entry.seq  = 'ATCGatcg'
69     @entry.type = 'dna'
70     @entry.to_rna
71     assert_equal("rna", @entry.type)
72   end
73
74   def test_Seq_to_dna_raises_if_no_sequence
75     @entry.type = 'rna'
76     assert_raise(SeqError) { @entry.to_dna }
77   end
78
79   def test_Seq_to_dna_raises_on_bad_type
80     @entry.seq  = 'AUCG'
81     @entry.type = 'dna'
82     assert_raise(SeqError) { @entry.to_dna }
83   end
84
85   def test_Seq_to_dna_transcribes_correctly
86     @entry.seq  = 'AUCGaucg'
87     @entry.type = 'rna'
88     assert_equal("ATCGatcg", @entry.to_dna)
89   end
90
91   def test_Seq_to_dna_changes_entry_type_to_dna
92     @entry.seq  = 'AUCGaucg'
93     @entry.type = 'rna'
94     @entry.to_dna
95     assert_equal("dna", @entry.type)
96   end
97
98   def test_Seq_to_bp_returns_correct_record
99     @entry.seq_name = 'test'
100     @entry.seq      = 'ATCG'
101     assert_equal({:SEQ_NAME=>"test", :SEQ=>"ATCG", :SEQ_LEN=>4}, @entry.to_bp)
102   end
103
104   def test_Seq_to_bp_raises_on_missing_seq_name
105     @entry.seq = 'ATCG'
106     assert_raise(SeqError) { @entry.to_bp }
107   end
108
109   def test_Seq_to_bp_raises_on_missing_sequence
110     @entry.seq_name = 'test'
111     assert_raise(SeqError) { @entry.to_bp }
112   end
113
114   def test_Seq_to_fasta_returns_correct_entry
115     @entry.seq_name = 'test'
116     @entry.seq      = 'ATCG'
117     assert_equal(">test\nATCG\n", @entry.to_fasta)
118   end
119
120   def test_Seq_to_fasta_wraps_correctly
121     entry = Seq.new("test", "ATCG")
122     assert_equal(">test\nAT\nCG\n", entry.to_fasta(2))
123   end
124
125   def test_Seq_reverse_returns_correctly
126     @entry.seq = "ATCG"
127     assert_equal("GCTA", @entry.reverse)
128   end
129
130   def test_Seq_complement_raises_if_no_sequence
131     @entry.type = 'dna'
132     assert_raise(SeqError) { @entry.complement }
133   end
134
135   def test_Seq_complement_raises_on_bad_type
136     @entry.seq  = 'ATCG'
137     @entry.type = 'protein'
138     assert_raise(SeqError) { @entry.complement }
139   end
140
141   def test_Seq_complement_for_DNA_is_correct
142     @entry.seq  = 'ATCGatcg'
143     @entry.type = 'dna'
144     assert_equal("TAGCtagc", @entry.complement)
145   end
146
147   def test_Seq_complement_for_RNA_is_correct
148     @entry.seq  = 'AUCGaucg'
149     @entry.type = 'rna'
150     assert_equal("UAGCuagc", @entry.complement)
151   end
152
153   def test_Seq_reverse_complement_for_DNA_is_correct
154     @entry.seq  = 'ATCGatcg'
155     @entry.type = 'dna'
156     assert_equal("cgatCGAT", @entry.reverse_complement)
157   end
158
159   def test_Seq_reverse_complement_for_RNA_is_correct
160     @entry.seq  = 'AUCGaucg'
161     @entry.type = 'rna'
162     assert_equal("cgauCGAU", @entry.reverse_complement)
163   end
164
165   def test_Seq_generate_raises_if_length_lt_0
166     assert_raise(SeqError) { @entry.generate(-10, "dna") }
167     assert_raise(SeqError) { @entry.generate(0, "dna") }
168   end
169
170   def test_Seq_generate_raises_on_bad_type
171     assert_raise(SeqError) { @entry.generate(10, "foo") }
172   end
173
174   def test_Seq_generate_dont_raise_on_ok_type
175     %w[ dna DNA rna RNA protein Protein ].each do |type|
176       assert_nothing_raised { @entry.generate(10, type) }
177     end
178   end
179
180   def test_Digest_new_raises_on_bad_pattern_residue
181     assert_raise(DigestError) { Digest.new(@entry, "X", 4) }
182   end
183
184   def test_Digest_new_dont_raise_on_ok_pattern_residue
185     assert_nothing_raised { Digest.new(@entry, "AGCUTRYWSMKHDVBNagcutrywsmkhdvbn", 4) }
186   end
187
188   def test_Digest_positions_return_correctly_at_begin
189     @entry.seq = "TTTTaaa"
190     digest = Digest.new(@entry, "TTTT", 0)
191     assert_equal([], digest.positions)
192   end
193
194   def test_Digest_positions_return_correctly
195     @entry.seq = "TTTTaaa"
196     digest = Digest.new(@entry, "TTTT", 1)
197     assert_equal([0], digest.positions)
198   end
199
200   def test_Digest_positions_return_correctly_at_end
201     @entry.seq = "aaaTTTT"
202     digest = Digest.new(@entry, "TTTT", 3)
203     assert_equal([], digest.positions)
204   end
205
206   def test_Digest_positions_return_correctly_with_ambibuity_pattern
207     @entry.seq = "aaaTTTT"
208     digest = Digest.new(@entry, "TTNT", 1)
209     assert_equal([3], digest.positions)
210   end
211
212   def test_Digest_products_return_correctly
213     @entry.seq = "aaaaTTTTbbbbTTTT"
214     digest = Digest.new(@entry, "TTNT", 1)
215     assert_equal(["aaaaT", "TTTbbbbT", "TTT"], digest.products)
216   end
217 end
218
219
220 __END__
221
222 class TestSeq < Test::Unit::TestCase 
223   # Testing Seq#guess_type
224
225   def test_guess_type_raise_if_no_sequence
226     s = Seq.new
227
228     assert_raise( ArgumentError ) { s.guess_type }
229   end
230
231   def test_guess_type_AA_uppercase
232     s1 = Seq.new( "SEQ" )
233     s2 = Seq::AA.new( "SEQ" )
234     assert_equal( s1.guess_type.class, s2.class )
235   end
236
237   def test_guess_type_AA_lowercase
238     s1 = Seq.new( "seq" )
239     s2 = Seq::AA.new( "seq" )
240
241     assert_equal( s1.guess_type.class, s2.class )
242   end
243
244   def test_guess_type_DNA_uppercase
245     s1 = Seq.new( "ATCG" )
246     s2 = Seq::NA::DNA.new( "ATCG" )
247
248     assert_equal( s1.guess_type.class, s2.class )
249   end
250
251   def test_guess_type_DNA_lowercase
252     s1 = Seq.new( "atcg" )
253     s2 = Seq::NA::DNA.new( "atcg" )
254
255     assert_equal( s1.guess_type.class, s2.class )
256   end
257
258   def test_guess_type_RNA_uppercase
259     s1 = Seq.new( "AUCG" )
260     s2 = Seq::NA::RNA.new( "AUCG" )
261
262     assert_equal( s1.guess_type.class, s2.class )
263   end
264
265   def test_guess_type_RNA_lowercase
266     s1 = Seq.new( "aucg" )
267     s2 = Seq::NA::RNA.new( "aucg" )
268
269     assert_equal( s1.guess_type.class, s2.class )
270   end
271
272   # Testing Seq#wrap
273
274   def test_wrap_arg_is_a_positive_number
275     s = Seq.new
276
277     assert_raise( ArgumentError ) { s.wrap( 0 ) }
278     assert_raise( ArgumentError ) { s.wrap( -10 ) }
279   end
280
281   def test_wrap_with_0_args
282     s = Seq.new( "ACTGACTAGCATCGACTACGACTGACACGACGACGACGACCGAACGATCGATCGCAGACGACGCAGCATGACGACGTACGACTACGACT" )
283
284     assert_equal( "ACTGACTAGCATCGACTACGACTGACACGACGACGACGACCGAACGATCGATCGCAGACGACGCAGCATGACGACGTACG\nACTACGACT", s.wrap.to_s )
285   end
286
287   def test_wrap_with_1_args
288     s = Seq.new( "ATCG" )
289
290     assert_equal( "AT\nCG", s.wrap( 2 ).to_s )
291   end
292
293   def test_wrap_with_2_args
294     s = Seq.new( "ATCG" )
295
296     assert_equal( "AT\rCG", s.wrap( 2, "\r" ).to_s )
297   end
298
299   def test_wrap_dont_change_instance_var
300     s = Seq.new( "ATCG" )
301
302     s.wrap( 2 )
303
304     assert_equal( "ATCG", s.to_s )
305   end
306
307   # Testing Seq#wrap!
308
309   def test_wrap_with_0_args!
310     s = Seq.new( "ACTGACTAGCATCGACTACGACTGACACGACGACGACGACCGAACGATCGATCGCAGACGACGCAGCATGACGACGTACGACTACGACT" )
311
312     s.wrap!
313
314     assert_equal( "ACTGACTAGCATCGACTACGACTGACACGACGACGACGACCGAACGATCGATCGCAGACGACGCAGCATGACGACGTACG\nACTACGACT", s.to_s )
315   end
316
317   def test_wrap_with_1_args!
318     s = Seq.new( "ATCG" )
319
320     s.wrap!( 2 )
321
322     assert_equal( "AT\nCG", s.to_s )
323   end
324
325   def test_wrap_with_2_args!
326     s = Seq.new( "ATCG" )
327
328     s.wrap!( 2, "\r" )
329
330     assert_equal( "AT\rCG", s.to_s )
331   end
332
333   # Testing Seq#generate
334
335   def test_generate_arg_is_a_positive_number
336     s = Seq.new
337
338     assert_raise( ArgumentError ) { s.generate( 0 ) }
339     assert_raise( ArgumentError ) { s.generate( -10 ) }
340   end
341
342   def test_generate
343     s = Seq::AA.new
344
345     seq = s.generate( 40 )
346
347     assert_equal( 40, seq.length )
348   end
349
350   def test_generate_dont_change_instance_var
351     s = Seq::AA.new
352
353     seq = s.generate( 40 )
354
355     assert_equal( "", s.to_s )
356   end
357
358   # Testing Seq#generate!
359
360   def test_generate!
361     s = Seq::AA.new
362
363     s.generate!( 40 )
364
365     assert_equal( 40, s.length )
366   end
367
368   # Testing Seq::AA#residues
369
370   def test_Seq_AA_residues
371     s = Seq::AA.new
372
373     assert_equal( %w{ F L S Y C W P H Q R I M T N K V A D E G }, s.residues )
374   end
375
376   # Testing Seq::AA#mol_weight
377
378   def test_Seq_aa_mol_weight_bad_residue
379     s = Seq::AA.new( "7" )
380     assert_raise( ArgumentError ) { s.mol_weight }
381   end
382
383   def test_Seq_aa_mol_wight_return_correct_uppercase
384     s = Seq::AA.new( "SEQ" )
385     assert_equal( 398.0, s.mol_weight )
386   end
387
388   def test_Seq_aa_mol_wight_return_correct_lowercase
389     s = Seq::AA.new( "seq" )
390     assert_equal( 398.0, s.mol_weight )
391   end
392
393   # Testing Seq::NA::DNA#residues
394
395   def test_Seq_NA_DNA_residues
396     s = Seq::NA::DNA.new
397
398     assert_equal( %w{ A T C G }, s.residues )
399   end
400
401   # Testing Seq::NA::DNA#complement
402
403   def test_Seq_NA_DNA_complement_correct
404     s = Seq::NA::DNA.new( "ATCGatcg" )
405     assert_equal( "TAGCtagc", s.complement.to_s )
406   end
407
408   # Testing Seq::NA::DNA#to_RNA
409
410   def test_Seq_NA_DNA_to_RNA_returns_RNA_object
411     dna = Seq::NA::DNA.new( "ATCGatcg" )
412     rna = Seq::NA::RNA.new
413
414     new_rna = dna.to_RNA
415
416     assert_equal( rna.class, new_rna.class )
417   end
418
419   def test_Seq_NA_DNA_to_RNA_is_correct
420     dna = Seq::NA::DNA.new( "ATCGatcg" )
421     rna = dna.to_RNA
422
423     assert_equal( "AUCGaucg", rna.to_s )
424   end
425
426   # Testing Seq::NA::RNA#residues
427
428   def test_Seq_NA_RNA_residues
429     s = Seq::NA::RNA.new
430
431     assert_equal( %w{ A U C G }, s.residues )
432   end
433
434   # Testing Seq::NA::RNA#complement
435
436   def test_Seq_NA_RNA_complement_correct
437     s = Seq::NA::RNA.new( "AUCGaucg" )
438     assert_equal( "UAGCuagc", s.complement.to_s )
439   end
440
441   # Testing Seq::NA::RNA#to_DNA
442
443   def test_Seq_NA_RNA_to_DNA_returns_DNA_object
444     rna = Seq::NA::RNA.new( "AUCGaucg" )
445     dna = Seq::NA::DNA.new
446
447     new_dna = rna.to_DNA
448
449     assert_equal( dna.class, new_dna.class )
450   end
451
452   def test_Seq_NA_RNA_to_DNA_is_correct
453     rna = Seq::NA::RNA.new( "AUCGaucg" )
454     dna = rna.to_DNA
455
456     assert_equal( "ATCGatcg", dna.to_s )
457   end
458 end
459