2 $:.unshift File.join(File.dirname(__FILE__), '..', '..')
4 # Copyright (C) 2011 Martin A. Hansen.
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.
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.
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.
20 # http://www.gnu.org/copyleft/gpl.html
22 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
24 # This software is part of the Biopieces framework (www.biopieces.org).
26 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
32 class TestSeq < Test::Unit::TestCase
37 test "Seq.new_bp returns correctly" do
38 record = {:SEQ_NAME => "test", :SEQ => "ATCG", :SEQ_TYPE => :dna, :SCORES => "hhhh"}
39 seq = Seq.new_bp(record)
40 assert_equal("test", seq.seq_name)
41 assert_equal("ATCG", seq.seq)
42 assert_equal(:dna, seq.type)
43 assert_equal("hhhh", seq.qual)
46 test "#is_dna? with no sequence type returns false" do
47 assert(@entry.is_dna? == false)
50 test "#is_dna? with dna sequence type returns true" do
52 assert(@entry.is_dna? == true)
55 test "#is_rna? with no sequence type returns false" do
56 assert(@entry.is_rna? == false)
59 test "#is_rna? with rna sequence type returns true" do
61 assert(@entry.is_rna? == true)
64 test "#is_protein? with no sequence type returns false" do
65 assert(@entry.is_protein? == false)
68 test "#is_protein? with protein sequence type returns true" do
69 @entry.type = :protein
70 assert_equal(true, @entry.is_protein?)
73 test "#type_guess without sequence raises" do
74 assert_raise(SeqError) { @entry.type_guess }
77 test "#type_guess with protein returns protein" do
78 @entry.seq = 'atcatcrFgatcg'
79 assert_equal(:protein, @entry.type_guess)
82 test "#type_guess with rna returns rna" do
83 @entry.seq = 'atcatcrUgatcg'
84 assert_equal(:rna, @entry.type_guess)
87 test "#type_guess with dna returns dna" do
88 @entry.seq = 'atcatcgatcg'
89 assert_equal(:dna, @entry.type_guess)
92 test "#type_guess! without sequence raises" do
93 assert_raise(SeqError) { @entry.type_guess! }
96 test "#type_guess! with protein returns protein" do
97 @entry.seq = 'atcatcrFgatcg'
99 assert_equal(:protein, @entry.type)
102 test "#type_guess! with rna returns rna" do
103 @entry.seq = 'atcatcrUgatcg'
105 assert_equal(:rna, @entry.type)
108 test "#type_guess! with dna returns dna" do
109 @entry.seq = 'atcatcgatcg'
111 assert_equal(:dna, @entry.type)
114 test "#length returns corretly" do
116 assert_equal(4, @entry.length)
119 test "#indels returns correctly" do
120 @entry.seq = 'ATCG.-~_'
121 assert_equal(4, @entry.indels)
124 test "#to_rna with no sequence raises" do
126 assert_raise(SeqError) { @entry.to_rna }
129 test "#to_rna with bad type raises" do
132 assert_raise(SeqError) { @entry.to_rna }
135 test "#to_rna transcribes correctly" do
136 @entry.seq = 'ATCGatcg'
138 assert_equal("AUCGaucg", @entry.to_rna)
141 test "#to_rna changes entry type to rna" do
142 @entry.seq = 'ATCGatcg'
145 assert_equal(:rna, @entry.type)
148 test "#to_dna with no sequence raises" do
150 assert_raise(SeqError) { @entry.to_dna }
153 test "#to_dna with bad type raises" do
156 assert_raise(SeqError) { @entry.to_dna }
159 test "#to_dna transcribes correctly" do
160 @entry.seq = 'AUCGaucg'
162 assert_equal("ATCGatcg", @entry.to_dna)
165 test "#to_dna changes entry type to dna" do
166 @entry.seq = 'AUCGaucg'
169 assert_equal(:dna, @entry.type)
172 test "#to_bp returns correct record" do
173 @entry.seq_name = 'test'
175 assert_equal({:SEQ_NAME=>"test", :SEQ=>"ATCG", :SEQ_LEN=>4}, @entry.to_bp)
178 test "#to_bp with missing seq_name raises" do
180 assert_raise(SeqError) { @entry.to_bp }
183 test "#to_bp with missing sequence raises" do
184 @entry.seq_name = 'test'
185 assert_raise(SeqError) { @entry.to_bp }
188 test "#to_fasta with missing seq_name raises" do
190 assert_raise(SeqError) { @entry.to_fasta }
193 test "#to_fasta with empty seq_name raises" do
196 assert_raise(SeqError) { @entry.to_fasta }
199 test "#to_fasta with missing seq raises" do
200 @entry.seq_name = 'test'
201 assert_raise(SeqError) { @entry.to_fasta }
204 test "#to_fasta with empty seq raises" do
205 @entry.seq_name = 'test'
207 assert_raise(SeqError) { @entry.to_fasta }
210 test "#to_fasta returns correct entry" do
211 @entry.seq_name = 'test'
213 assert_equal(">test\nATCG\n", @entry.to_fasta)
216 test "#to_fasta wraps correctly" do
217 entry = Seq.new("test", "ATCG")
218 assert_equal(">test\nAT\nCG\n", entry.to_fasta(2))
221 test "#to_fastq returns correct entry" do
222 @entry.seq_name = 'test'
225 assert_equal("@test\nATCG\n+\nhhhh\n", @entry.to_fastq)
228 test "#to_key with bad residue raises" do
229 entry = Seq.new("test", "AUCG")
230 assert_raise(SeqError) { entry.to_key }
233 test "#to_key returns correctly" do
234 entry = Seq.new("test", "ATCG")
235 assert_equal(54, entry.to_key)
238 test "#reverse returns correctly" do
240 new_entry = @entry.reverse
241 assert_equal("GCTA", new_entry.seq)
242 assert_equal("ATCG", @entry.seq)
245 test "#reverse! returns correctly" do
248 assert_equal("GCTA", @entry.seq)
251 test "#complement with no sequence raises" do
253 assert_raise(SeqError) { @entry.complement }
256 test "#complement with bad type raises" do
258 @entry.type = :protein
259 assert_raise(SeqError) { @entry.complement }
262 test "#complement for DNA is correct" do
263 @entry.seq = 'ATCGatcg'
265 comp = @entry.complement
266 assert_equal("TAGCtagc", comp.seq)
267 assert_equal("ATCGatcg", @entry.seq)
270 test "#complement for RNA is correct" do
271 @entry.seq = 'AUCGaucg'
273 comp = @entry.complement
274 assert_equal("UAGCuagc", comp.seq)
275 assert_equal("AUCGaucg", @entry.seq)
278 test "#complement! with no sequence raises" do
280 assert_raise(SeqError) { @entry.complement! }
283 test "#complement! with bad type raises" do
285 @entry.type = :protein
286 assert_raise(SeqError) { @entry.complement! }
289 test "#complement! for DNA is correct" do
290 @entry.seq = 'ATCGatcg'
292 assert_equal("TAGCtagc", @entry.complement!.seq)
295 test "#complement! for RNA is correct" do
296 @entry.seq = 'AUCGaucg'
298 assert_equal("UAGCuagc", @entry.complement!.seq)
301 test "#hamming_distance returns correctly" do
302 seq1 = Seq.new("test1", "ATCG")
303 seq2 = Seq.new("test2", "atgg")
304 assert_equal(1, seq1.hamming_distance(seq2))
307 test "#edit_distance returns correctly" do
308 seq1 = Seq.new("test1", "ATCG")
309 seq2 = Seq.new("test2", "tgncg")
310 assert_equal(2, seq1.edit_distance(seq2))
313 test "#generate with length < 1 raises" do
314 assert_raise(SeqError) { @entry.generate(-10, :dna) }
315 assert_raise(SeqError) { @entry.generate(0, :dna) }
318 test "#generate with bad type raises" do
319 assert_raise(SeqError) { @entry.generate(10, "foo") }
322 test "#generate with ok type dont raise" do
323 %w[dna rna protein].each do |type|
324 assert_nothing_raised { @entry.generate(10, type.to_sym) }
328 test "#shuffle returns correctly" do
329 orig = "actgactgactgatcgatcgatcgatcgtactg"
330 @entry.seq = "actgactgactgatcgatcgatcgatcgtactg"
331 entry_shuf = @entry.shuffle
332 assert_equal(orig, @entry.seq)
333 assert_not_equal(@entry.seq, entry_shuf.seq)
336 test "#shuffle! returns correctly" do
337 @entry.seq = "actgactgactgatcgatcgatcgatcgtactg"
338 assert_not_equal(@entry.seq, @entry.shuffle!.seq)
341 test "#+ without qual returns correctly" do
342 entry = Seq.new("test1", "at") + Seq.new("test2", "cg")
343 assert_nil(entry.seq_name)
344 assert_equal("atcg", entry.seq)
345 assert_nil(entry.type)
346 assert_nil(entry.qual)
349 test "#+ with qual returns correctly" do
350 entry = Seq.new("test1", "at", :dna, "II") + Seq.new("test2", "cg", :dna, "JJ")
351 assert_nil(entry.seq_name)
352 assert_equal("atcg", entry.seq)
353 assert_equal(:dna, entry.type)
354 assert_equal("IIJJ", entry.qual)
357 test "#<< with different types raises" do
359 assert_raise(SeqError) { @entry << Seq.new("test", "atcg", :dna) }
362 test "#<< with missing qual in one entry raises" do
365 assert_raise(SeqError) { @entry << Seq.new("test", "atcg", :dna, "IIII") }
367 assert_raise(SeqError) { @entry << Seq.new("test", "atcg", :dna) }
370 test "#<< with nil qual in both entries dont raise" do
372 assert_nothing_raised { @entry << Seq.new("test", "atcg") }
375 test "#<< with qual in both entries dont raise" do
379 assert_nothing_raised { @entry << Seq.new("test", "atcg", :dna, "IIII") }
382 test "#<< without qual returns correctly" do
384 @entry << Seq.new("test", "ATCG")
385 assert_equal("atcgATCG", @entry.seq)
388 test "#<< with qual returns correctly" do
392 @entry << Seq.new("test", "ATCG", :dna, "IIII")
393 assert_equal("atcgATCG", @entry.seq)
394 assert_equal("HHHHIIII", @entry.qual)
397 test "#[] with qual returns correctly" do
398 entry = Seq.new("test", "atcg", :dna, "FGHI")
402 assert_equal("test", e.seq_name)
403 assert_equal("c", e.seq)
404 assert_equal(:dna, e.type)
405 assert_equal("H", e.qual)
406 assert_equal("atcg", entry.seq)
407 assert_equal("FGHI", entry.qual)
410 test "#[] without qual returns correctly" do
411 entry = Seq.new("test", "atcg")
415 assert_equal("test", e.seq_name)
416 assert_equal("c", e.seq)
418 assert_equal("atcg", entry.seq)
421 test "[]= with qual returns correctly" do
422 entry = Seq.new("test", "atcg", :dna, "FGHI")
424 entry[0] = Seq.new("foo", "T", :dna, "I")
426 assert_equal("test", entry.seq_name)
427 assert_equal("Ttcg", entry.seq)
428 assert_equal(:dna, entry.type)
429 assert_equal("IGHI", entry.qual)
432 test "[]= without qual returns correctly" do
433 entry = Seq.new("test", "atcg")
435 entry[0] = Seq.new("foo", "T")
437 assert_equal("test", entry.seq_name)
438 assert_equal("Ttcg", entry.seq)
441 test "#subseq with start < 0 raises" do
443 assert_raise(SeqError) { @entry.subseq(-1, 1) }
446 test "#subseq with start plus length gt seq raises" do
448 assert_raise(SeqError) { @entry.subseq(0, 5) }
451 test "#subseq returns correct sequence" do
453 assert_equal("AT", @entry.subseq(0, 2).seq)
454 assert_equal("CG", @entry.subseq(2, 2).seq)
457 test "#subseq without length returns correct sequence" do
459 assert_equal("ATCG", @entry.subseq(0).seq)
460 assert_equal("CG", @entry.subseq(2).seq)
463 test "#subseq returns correct qual" do
466 assert_equal("ab", @entry.subseq(0, 2).qual)
467 assert_equal("cd", @entry.subseq(2, 2).qual)
470 test "#subseq without length returns correct qual" do
473 assert_equal("abcd", @entry.subseq(0).qual)
474 assert_equal("cd", @entry.subseq(2).qual)
477 test "#subseq! with start < 0 raises" do
479 assert_raise(SeqError) { @entry.subseq!(-1, 1) }
482 test "#subseq! with start plus length > seq.length raises" do
484 assert_raise(SeqError) { @entry.subseq!(0, 5) }
487 test "#subseq! returns correct sequence" do
490 assert_equal("AT", @entry.seq)
493 assert_equal("CG", @entry.seq)
496 test "#subseq! without length returns correct sequence" do
499 assert_equal("ATCG", @entry.seq)
502 assert_equal("CG", @entry.seq)
505 test "#subseq! with pos and length returns correct qual" do
509 assert_equal("ab", @entry.qual)
513 assert_equal("cd", @entry.qual)
516 test "#subseq! with pos returns correct qual" do
520 assert_equal("abcd", @entry.qual)
524 assert_equal("cd", @entry.qual)
527 test "#subseq_rand returns correct sequence" do
529 assert_equal("ATCG", @entry.subseq_rand(4).seq)
532 test "#indels_remove without qual returns correctly" do
533 @entry.seq = "A-T.CG~CG"
535 assert_equal("ATCGCG", @entry.indels_remove.seq)
538 test "#indels_remove with qual returns correctly" do
539 @entry.seq = "A-T.CG~CG"
540 @entry.qual = "a@b@cd@fg"
541 assert_equal("ATCGCG", @entry.indels_remove.seq)
542 assert_equal("abcdfg", @entry.indels_remove.qual)
545 test "#composition returns correctly" do
546 @entry.seq = "AAAATTTCCG"
547 assert_equal(4, @entry.composition["A"])
548 assert_equal(3, @entry.composition["T"])
549 assert_equal(2, @entry.composition["C"])
550 assert_equal(1, @entry.composition["G"])
551 assert_equal(0, @entry.composition["X"])
554 test "#hard_mask returns correctly" do
555 @entry.seq = "--AAAANn"
556 assert_equal(33.33, @entry.hard_mask)
559 test "#soft_mask returns correctly" do
560 @entry.seq = "--AAAa"
561 assert_equal(25.00, @entry.soft_mask)
564 test "#mask_seq_hard! with nil seq raises" do
568 assert_raise(SeqError) { @entry.mask_seq_hard!(20) }
571 test "#mask_seq_hard! with nil qual raises" do
575 assert_raise(SeqError) { @entry.mask_seq_hard!(20) }
578 test "#mask_seq_hard! with bad cutoff raises" do
579 assert_raise(SeqError) { @entry.mask_seq_hard!(-1) }
580 assert_raise(SeqError) { @entry.mask_seq_hard!(41) }
583 test "#mask_seq_hard! with OK cutoff dont raise" do
587 assert_nothing_raised { @entry.mask_seq_hard!(0) }
588 assert_nothing_raised { @entry.mask_seq_hard!(40) }
591 test "#mask_seq_hard! returns correctly" do
593 @entry.qual = "33456"
595 assert_equal("-NNCG", @entry.mask_seq_hard!(20).seq)
598 test "#mask_seq_soft! with nil seq raises" do
602 assert_raise(SeqError) { @entry.mask_seq_soft!(20) }
605 test "#mask_seq_soft! with nil qual raises" do
609 assert_raise(SeqError) { @entry.mask_seq_soft!(20) }
612 test "#mask_seq_soft! with bad cutoff raises" do
613 assert_raise(SeqError) { @entry.mask_seq_soft!(-1) }
614 assert_raise(SeqError) { @entry.mask_seq_soft!(41) }
617 test "#mask_seq_soft! with OK cutoff dont raise" do
621 assert_nothing_raised { @entry.mask_seq_soft!(0) }
622 assert_nothing_raised { @entry.mask_seq_soft!(40) }
625 test "#mask_seq_soft! returns correctly" do
627 @entry.qual = "33456"
629 assert_equal("-atCG", @entry.mask_seq_soft!(20).seq)
632 # qual score detection
634 test "#qual_base33? returns correctly" do
635 # self.qual.match(/[!-:]/)
636 @entry.qual = '!"#$%&\'()*+,-./0123456789:'
637 assert_equal(true, @entry.qual_base33? )
639 assert_equal(false, @entry.qual_base33? )
641 assert_equal(false, @entry.qual_base33? )
644 test "#qual_base64? returns correctly" do
645 # self.qual.match(/[K-h]/)
646 @entry.qual = 'KLMNOPQRSTUVWXYZ[\]^_`abcdefgh'
647 assert_equal(true, @entry.qual_base64? )
649 assert_equal(false, @entry.qual_base64? )
650 @entry.qual = 105.chr
651 assert_equal(false, @entry.qual_base64? )
654 test "#qual_valid? with nil qual raises" do
655 assert_raise(SeqError) { @entry.qual_valid?(:base_33) }
656 assert_raise(SeqError) { @entry.qual_valid?(:base_64) }
659 test "#qual_valid? with bad encoding raises" do
661 assert_raise(SeqError) { @entry.qual_valid?("foobar") }
664 test "#qual_valid? with OK range returns correctly" do
665 @entry.qual = ((Seq::SCORE_MIN + 33).chr .. (Seq::SCORE_MAX + 33).chr).to_a.join
666 assert_equal(true, @entry.qual_valid?(:base_33))
667 @entry.qual = ((Seq::SCORE_MIN + 64).chr .. (Seq::SCORE_MAX + 64).chr).to_a.join
668 assert_equal(true, @entry.qual_valid?(:base_64))
671 test "#qual_valid? with bad range returns correctly" do
672 @entry.qual = ((Seq::SCORE_MIN + 33 - 1).chr .. (Seq::SCORE_MAX + 33).chr).to_a.join
673 assert_equal(false, @entry.qual_valid?(:base_33))
674 @entry.qual = ((Seq::SCORE_MIN + 33).chr .. (Seq::SCORE_MAX + 33 + 1).chr).to_a.join
675 assert_equal(false, @entry.qual_valid?(:base_33))
677 @entry.qual = ((Seq::SCORE_MIN + 64 - 1).chr .. (Seq::SCORE_MAX + 64).chr).to_a.join
678 assert_equal(false, @entry.qual_valid?(:base_64))
679 @entry.qual = ((Seq::SCORE_MIN + 64).chr .. (Seq::SCORE_MAX + 64 + 1).chr).to_a.join
680 assert_equal(false, @entry.qual_valid?(:base_64))
683 # convert sanger to ...
685 test "#qual_convert! from base33 to base33 returns OK" do
686 @entry.qual = 'BCDEFGHI'
687 assert_equal('BCDEFGHI', @entry.qual_convert!(:base_33, :base_33).qual)
690 test "#qual_convert! from base33 to base64 returns OK" do
691 @entry.qual = 'BCDEFGHI'
692 assert_equal('abcdefgh', @entry.qual_convert!(:base_33, :base_64).qual)
695 test "#qual_convert! from base64 to base64 returns OK" do
696 @entry.qual = 'BCDEFGHI'
697 assert_equal('BCDEFGHI', @entry.qual_convert!(:base_64, :base_64).qual)
700 test "#qual_convert! from base64 to base33 returns OK" do
701 @entry.qual = 'abcdefgh'
702 assert_equal('BCDEFGHI', @entry.qual_convert!(:base_64, :base_33).qual)
705 test "#qual_coerce! returns correctly" do
706 @entry.qual = ('!' .. '~').to_a.join
707 assert_equal("!\"\#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII", @entry.qual_coerce!(:base_33).qual)
708 @entry.qual = ('!' .. '~').to_a.join
709 assert_equal("!\"\#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZh\\h^_`abcdefghhhhhhhhhhhhhhhhhhhhhhh", @entry.qual_coerce!(:base_64).qual)
712 test "#scores_mean without qual raises" do
714 assert_raise(SeqError) { @entry.scores_mean }
717 test "#scores_mean returns correctly" do
719 assert_equal(20.0, @entry.scores_mean)