]> git.donarmstrong.com Git - biopieces.git/blob - code_ruby/Maasha/test/test_seq.rb
6e102881a377391cf13c1e9109bf42ea4915bcb1
[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_reverse_returns_correctly
131     @entry.seq = "ATCG"
132     assert_equal("GCTA", @entry.reverse)
133   end
134
135   def test_Seq_complement_raises_if_no_sequence
136     @entry.type = 'dna'
137     assert_raise(SeqError) { @entry.complement }
138   end
139
140   def test_Seq_complement_raises_on_bad_type
141     @entry.seq  = 'ATCG'
142     @entry.type = 'protein'
143     assert_raise(SeqError) { @entry.complement }
144   end
145
146   def test_Seq_complement_for_DNA_is_correct
147     @entry.seq  = 'ATCGatcg'
148     @entry.type = 'dna'
149     assert_equal("TAGCtagc", @entry.complement)
150   end
151
152   def test_Seq_complement_for_RNA_is_correct
153     @entry.seq  = 'AUCGaucg'
154     @entry.type = 'rna'
155     assert_equal("UAGCuagc", @entry.complement)
156   end
157
158   def test_Seq_reverse_complement_for_DNA_is_correct
159     @entry.seq  = 'ATCGatcg'
160     @entry.type = 'dna'
161     assert_equal("cgatCGAT", @entry.reverse_complement)
162   end
163
164   def test_Seq_reverse_complement_for_RNA_is_correct
165     @entry.seq  = 'AUCGaucg'
166     @entry.type = 'rna'
167     assert_equal("cgauCGAU", @entry.reverse_complement)
168   end
169
170   def test_Seq_generate_raises_if_length_lt_0
171     assert_raise(SeqError) { @entry.generate(-10, "dna") }
172     assert_raise(SeqError) { @entry.generate(0, "dna") }
173   end
174
175   def test_Seq_generate_raises_on_bad_type
176     assert_raise(SeqError) { @entry.generate(10, "foo") }
177   end
178
179   def test_Seq_generate_dont_raise_on_ok_type
180     %w[ dna DNA rna RNA protein Protein ].each do |type|
181       assert_nothing_raised { @entry.generate(10, type) }
182     end
183   end
184
185   def test_Seq_composition_returns_correctly
186     @entry.seq = "AAAATTTCCG"
187     assert_equal(4, @entry.composition["A"])
188     assert_equal(3, @entry.composition["T"])
189     assert_equal(2, @entry.composition["C"])
190     assert_equal(1, @entry.composition["G"])
191     assert_equal(0, @entry.composition["X"])
192   end
193
194   def test_Seq_homopol_max_returns_0_with_empty_sequence
195     @entry.seq = ""
196     assert_equal(0, @entry.homopol_max)
197   end
198
199   def test_Seq_homopol_max_returns_0_with_nil_sequence
200     @entry.seq = nil
201     assert_equal(0, @entry.homopol_max)
202   end
203
204   def test_Seq_homopol_max_returns_0_when_not_found
205     @entry.seq = "AtTcCcGggGnnNnn"
206     assert_equal(0, @entry.homopol_max(6))
207   end
208
209   def test_Seq_homopol_max_returns_correctly
210     @entry.seq = "AtTcCcGggGnnNnn"
211     assert_equal(5, @entry.homopol_max(3))
212   end
213
214   def test_Seq_hard_mask_returns_correctly
215     @entry.seq = "--AAAANn"
216     assert_equal(33.33, @entry.hard_mask)
217   end
218
219   def test_Seq_soft_mask_returns_correctly
220     @entry.seq = "--AAAa"
221     assert_equal(25.00, @entry.soft_mask)
222   end
223
224   def test_Digest_new_raises_on_bad_pattern_residue
225     assert_raise(DigestError) { Digest.new(@entry, "X", 4) }
226   end
227
228   def test_Digest_new_dont_raise_on_ok_pattern_residue
229     assert_nothing_raised { Digest.new(@entry, "AGCUTRYWSMKHDVBNagcutrywsmkhdvbn", 4) }
230   end
231
232   def test_Digest_each
233     @entry.seq = "aaaaTTTTbbbbTTTT"
234     digest = Digest.new(@entry, "TTNT", 1)
235     assert_equal("aaaaT", digest.first.seq)
236   end
237 end
238
239
240 __END__
241
242 class TestSeq < Test::Unit::TestCase 
243   # Testing Seq#guess_type
244
245   def test_guess_type_raise_if_no_sequence
246     s = Seq.new
247
248     assert_raise( ArgumentError ) { s.guess_type }
249   end
250
251   def test_guess_type_AA_uppercase
252     s1 = Seq.new( "SEQ" )
253     s2 = Seq::AA.new( "SEQ" )
254     assert_equal( s1.guess_type.class, s2.class )
255   end
256
257   def test_guess_type_AA_lowercase
258     s1 = Seq.new( "seq" )
259     s2 = Seq::AA.new( "seq" )
260
261     assert_equal( s1.guess_type.class, s2.class )
262   end
263
264   def test_guess_type_DNA_uppercase
265     s1 = Seq.new( "ATCG" )
266     s2 = Seq::NA::DNA.new( "ATCG" )
267
268     assert_equal( s1.guess_type.class, s2.class )
269   end
270
271   def test_guess_type_DNA_lowercase
272     s1 = Seq.new( "atcg" )
273     s2 = Seq::NA::DNA.new( "atcg" )
274
275     assert_equal( s1.guess_type.class, s2.class )
276   end
277
278   def test_guess_type_RNA_uppercase
279     s1 = Seq.new( "AUCG" )
280     s2 = Seq::NA::RNA.new( "AUCG" )
281
282     assert_equal( s1.guess_type.class, s2.class )
283   end
284
285   def test_guess_type_RNA_lowercase
286     s1 = Seq.new( "aucg" )
287     s2 = Seq::NA::RNA.new( "aucg" )
288
289     assert_equal( s1.guess_type.class, s2.class )
290   end
291
292   # Testing Seq#wrap
293
294   def test_wrap_arg_is_a_positive_number
295     s = Seq.new
296
297     assert_raise( ArgumentError ) { s.wrap( 0 ) }
298     assert_raise( ArgumentError ) { s.wrap( -10 ) }
299   end
300
301   def test_wrap_with_0_args
302     s = Seq.new( "ACTGACTAGCATCGACTACGACTGACACGACGACGACGACCGAACGATCGATCGCAGACGACGCAGCATGACGACGTACGACTACGACT" )
303
304     assert_equal( "ACTGACTAGCATCGACTACGACTGACACGACGACGACGACCGAACGATCGATCGCAGACGACGCAGCATGACGACGTACG\nACTACGACT", s.wrap.to_s )
305   end
306
307   def test_wrap_with_1_args
308     s = Seq.new( "ATCG" )
309
310     assert_equal( "AT\nCG", s.wrap( 2 ).to_s )
311   end
312
313   def test_wrap_with_2_args
314     s = Seq.new( "ATCG" )
315
316     assert_equal( "AT\rCG", s.wrap( 2, "\r" ).to_s )
317   end
318
319   def test_wrap_dont_change_instance_var
320     s = Seq.new( "ATCG" )
321
322     s.wrap( 2 )
323
324     assert_equal( "ATCG", s.to_s )
325   end
326
327   # Testing Seq#wrap!
328
329   def test_wrap_with_0_args!
330     s = Seq.new( "ACTGACTAGCATCGACTACGACTGACACGACGACGACGACCGAACGATCGATCGCAGACGACGCAGCATGACGACGTACGACTACGACT" )
331
332     s.wrap!
333
334     assert_equal( "ACTGACTAGCATCGACTACGACTGACACGACGACGACGACCGAACGATCGATCGCAGACGACGCAGCATGACGACGTACG\nACTACGACT", s.to_s )
335   end
336
337   def test_wrap_with_1_args!
338     s = Seq.new( "ATCG" )
339
340     s.wrap!( 2 )
341
342     assert_equal( "AT\nCG", s.to_s )
343   end
344
345   def test_wrap_with_2_args!
346     s = Seq.new( "ATCG" )
347
348     s.wrap!( 2, "\r" )
349
350     assert_equal( "AT\rCG", s.to_s )
351   end
352
353   # Testing Seq#generate
354
355   def test_generate_arg_is_a_positive_number
356     s = Seq.new
357
358     assert_raise( ArgumentError ) { s.generate( 0 ) }
359     assert_raise( ArgumentError ) { s.generate( -10 ) }
360   end
361
362   def test_generate
363     s = Seq::AA.new
364
365     seq = s.generate( 40 )
366
367     assert_equal( 40, seq.length )
368   end
369
370   def test_generate_dont_change_instance_var
371     s = Seq::AA.new
372
373     seq = s.generate( 40 )
374
375     assert_equal( "", s.to_s )
376   end
377
378   # Testing Seq#generate!
379
380   def test_generate!
381     s = Seq::AA.new
382
383     s.generate!( 40 )
384
385     assert_equal( 40, s.length )
386   end
387
388   # Testing Seq::AA#residues
389
390   def test_Seq_AA_residues
391     s = Seq::AA.new
392
393     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 )
394   end
395
396   # Testing Seq::AA#mol_weight
397
398   def test_Seq_aa_mol_weight_bad_residue
399     s = Seq::AA.new( "7" )
400     assert_raise( ArgumentError ) { s.mol_weight }
401   end
402
403   def test_Seq_aa_mol_wight_return_correct_uppercase
404     s = Seq::AA.new( "SEQ" )
405     assert_equal( 398.0, s.mol_weight )
406   end
407
408   def test_Seq_aa_mol_wight_return_correct_lowercase
409     s = Seq::AA.new( "seq" )
410     assert_equal( 398.0, s.mol_weight )
411   end
412
413   # Testing Seq::NA::DNA#residues
414
415   def test_Seq_NA_DNA_residues
416     s = Seq::NA::DNA.new
417
418     assert_equal( %w{ A T C G }, s.residues )
419   end
420
421   # Testing Seq::NA::DNA#complement
422
423   def test_Seq_NA_DNA_complement_correct
424     s = Seq::NA::DNA.new( "ATCGatcg" )
425     assert_equal( "TAGCtagc", s.complement.to_s )
426   end
427
428   # Testing Seq::NA::DNA#to_RNA
429
430   def test_Seq_NA_DNA_to_RNA_returns_RNA_object
431     dna = Seq::NA::DNA.new( "ATCGatcg" )
432     rna = Seq::NA::RNA.new
433
434     new_rna = dna.to_RNA
435
436     assert_equal( rna.class, new_rna.class )
437   end
438
439   def test_Seq_NA_DNA_to_RNA_is_correct
440     dna = Seq::NA::DNA.new( "ATCGatcg" )
441     rna = dna.to_RNA
442
443     assert_equal( "AUCGaucg", rna.to_s )
444   end
445
446   # Testing Seq::NA::RNA#residues
447
448   def test_Seq_NA_RNA_residues
449     s = Seq::NA::RNA.new
450
451     assert_equal( %w{ A U C G }, s.residues )
452   end
453
454   # Testing Seq::NA::RNA#complement
455
456   def test_Seq_NA_RNA_complement_correct
457     s = Seq::NA::RNA.new( "AUCGaucg" )
458     assert_equal( "UAGCuagc", s.complement.to_s )
459   end
460
461   # Testing Seq::NA::RNA#to_DNA
462
463   def test_Seq_NA_RNA_to_DNA_returns_DNA_object
464     rna = Seq::NA::RNA.new( "AUCGaucg" )
465     dna = Seq::NA::DNA.new
466
467     new_dna = rna.to_DNA
468
469     assert_equal( dna.class, new_dna.class )
470   end
471
472   def test_Seq_NA_RNA_to_DNA_is_correct
473     rna = Seq::NA::RNA.new( "AUCGaucg" )
474     dna = rna.to_DNA
475
476     assert_equal( "ATCGatcg", dna.to_s )
477   end
478 end
479