X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=code_ruby%2Ftest%2Fmaasha%2Ftest_seq.rb;h=5947ee874666102cf6ef61f3ab8f8ff90516a2ef;hb=b2ea0b5a51a558478af60e9df4c643dd58552086;hp=5e79a0ff4b5a048e1da3c300b45a317743093207;hpb=494dc53ebd515b1e3e9b91bbebf43059899ca4ce;p=biopieces.git diff --git a/code_ruby/test/maasha/test_seq.rb b/code_ruby/test/maasha/test_seq.rb index 5e79a0f..5947ee8 100755 --- a/code_ruby/test/maasha/test_seq.rb +++ b/code_ruby/test/maasha/test_seq.rb @@ -1,254 +1,490 @@ #!/usr/bin/env ruby +$:.unshift File.join(File.dirname(__FILE__), '..', '..') + +# Copyright (C) 2011 Martin A. Hansen. + +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +# http://www.gnu.org/copyleft/gpl.html + +# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< + +# This software is part of the Biopieces framework (www.biopieces.org). + +# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< require 'maasha/seq' require 'test/unit' -require 'pp' +require 'test/helper' class TestSeq < Test::Unit::TestCase def setup @entry = Seq.new end - # def test_Seq# autoremoves whitespace, newlines, and carriage returns - # dna = Seq.new - # dna.seq = "A\tT\r\tC\nG " - # assert_equal(dna.seq, "ATCG") - # end + test "Seq.new_bp returns correctly" do + record = {:SEQ_NAME => "test", :SEQ => "ATCG", :SEQ_TYPE => :dna, :SCORES => "hhhh"} + seq = Seq.new_bp(record) + assert_equal("test", seq.seq_name) + assert_equal("ATCG", seq.seq) + assert_equal(:dna, seq.type) + assert_equal("hhhh", seq.qual) + end - def test_Seq_is_dna_with_no_sequence_type_returns_false + test "#is_dna? with no sequence type returns false" do assert(@entry.is_dna? == false) end - def test_Seq_is_dna_with_dna_sequence_type_returns_true - @entry.type = 'dna' + test "#is_dna? with dna sequence type returns true" do + @entry.type = :dna assert(@entry.is_dna? == true) end - def test_Seq_is_rna_with_no_sequence_type_returns_false + test "#is_rna? with no sequence type returns false" do assert(@entry.is_rna? == false) end - def test_Seq_is_rna_with_rna_sequence_type_returns_true - @entry.type = 'rna' + test "#is_rna? with rna sequence type returns true" do + @entry.type = :rna assert(@entry.is_rna? == true) end - def test_Seq_is_protein_with_no_sequence_type_returns_false + test "#is_protein? with no sequence type returns false" do assert(@entry.is_protein? == false) end - def test_Seq_is_protein_with_protein_sequence_type_returns_true - @entry.type = 'protein' - assert(@entry.is_protein? == true) + test "#is_protein? with protein sequence type returns true" do + @entry.type = :protein + assert_equal(true, @entry.is_protein?) + end + + test "#type_guess without sequence raises" do + assert_raise(SeqError) { @entry.type_guess } + end + + test "#type_guess with protein returns protein" do + @entry.seq = 'atcatcrFgatcg' + assert_equal(:protein, @entry.type_guess) + end + + test "#type_guess with rna returns rna" do + @entry.seq = 'atcatcrUgatcg' + assert_equal(:rna, @entry.type_guess) + end + + test "#type_guess with dna returns dna" do + @entry.seq = 'atcatcgatcg' + assert_equal(:dna, @entry.type_guess) + end + + test "#type_guess! without sequence raises" do + assert_raise(SeqError) { @entry.type_guess! } + end + + test "#type_guess! with protein returns protein" do + @entry.seq = 'atcatcrFgatcg' + @entry.type_guess! + assert_equal(:protein, @entry.type) + end + + test "#type_guess! with rna returns rna" do + @entry.seq = 'atcatcrUgatcg' + @entry.type_guess! + assert_equal(:rna, @entry.type) + end + + test "#type_guess! with dna returns dna" do + @entry.seq = 'atcatcgatcg' + @entry.type_guess! + assert_equal(:dna, @entry.type) end - def test_Seq_length_is_correct + test "#length returns corretly" do @entry.seq = 'ATCG' assert_equal(4, @entry.length) end - def test_Seq_indels_is_correct + test "#indels returns correctly" do @entry.seq = 'ATCG.-~_' assert_equal(4, @entry.indels) end - def test_Seq_to_rna_raises_if_no_sequence - @entry.type = 'dna' + test "#to_rna with no sequence raises" do + @entry.type = :dna assert_raise(SeqError) { @entry.to_rna } end - def test_Seq_to_rna_raises_on_bad_type + test "#to_rna with bad type raises" do @entry.seq = 'ATCG' - @entry.type = 'rna' + @entry.type = :rna assert_raise(SeqError) { @entry.to_rna } end - def test_Seq_to_rna_transcribes_correctly + test "#to_rna transcribes correctly" do @entry.seq = 'ATCGatcg' - @entry.type = 'dna' + @entry.type = :dna assert_equal("AUCGaucg", @entry.to_rna) end - def test_Seq_to_rna_changes_entry_type_to_rna + test "#to_rna changes entry type to rna" do @entry.seq = 'ATCGatcg' - @entry.type = 'dna' + @entry.type = :dna @entry.to_rna - assert_equal("rna", @entry.type) + assert_equal(:rna, @entry.type) end - def test_Seq_to_dna_raises_if_no_sequence - @entry.type = 'rna' + test "#to_dna with no sequence raises" do + @entry.type = :rna assert_raise(SeqError) { @entry.to_dna } end - def test_Seq_to_dna_raises_on_bad_type + test "#to_dna with bad type raises" do @entry.seq = 'AUCG' - @entry.type = 'dna' + @entry.type = :dna assert_raise(SeqError) { @entry.to_dna } end - def test_Seq_to_dna_transcribes_correctly + test "#to_dna transcribes correctly" do @entry.seq = 'AUCGaucg' - @entry.type = 'rna' + @entry.type = :rna assert_equal("ATCGatcg", @entry.to_dna) end - def test_Seq_to_dna_changes_entry_type_to_dna + test "#to_dna changes entry type to dna" do @entry.seq = 'AUCGaucg' - @entry.type = 'rna' + @entry.type = :rna @entry.to_dna - assert_equal("dna", @entry.type) + assert_equal(:dna, @entry.type) end - def test_Seq_to_bp_returns_correct_record + test "#to_bp returns correct record" do @entry.seq_name = 'test' @entry.seq = 'ATCG' assert_equal({:SEQ_NAME=>"test", :SEQ=>"ATCG", :SEQ_LEN=>4}, @entry.to_bp) end - def test_Seq_to_bp_raises_on_missing_seq_name + test "#to_bp with missing seq_name raises" do @entry.seq = 'ATCG' assert_raise(SeqError) { @entry.to_bp } end - def test_Seq_to_bp_raises_on_missing_sequence + test "#to_bp with missing sequence raises" do @entry.seq_name = 'test' assert_raise(SeqError) { @entry.to_bp } end - def test_Seq_to_fasta_returns_correct_entry + test "#to_fasta with missing seq_name raises" do + @entry.seq = 'ATCG' + assert_raise(SeqError) { @entry.to_fasta } + end + + test "#to_fasta with empty seq_name raises" do + @entry.seq_name = '' + @entry.seq = 'ATCG' + assert_raise(SeqError) { @entry.to_fasta } + end + + test "#to_fasta with missing seq raises" do + @entry.seq_name = 'test' + assert_raise(SeqError) { @entry.to_fasta } + end + + test "#to_fasta with empty seq raises" do + @entry.seq_name = 'test' + @entry.seq = '' + assert_raise(SeqError) { @entry.to_fasta } + end + + test "#to_fasta returns correct entry" do @entry.seq_name = 'test' @entry.seq = 'ATCG' assert_equal(">test\nATCG\n", @entry.to_fasta) end - def test_Seq_to_fasta_wraps_correctly + test "#to_fasta wraps correctly" do entry = Seq.new("test", "ATCG") assert_equal(">test\nAT\nCG\n", entry.to_fasta(2)) end - def test_Seq_to_key_with_bad_residue_raises + test "#to_fastq returns correct entry" do + @entry.seq_name = 'test' + @entry.seq = 'ATCG' + @entry.qual = 'hhhh' + assert_equal("@test\nATCG\n+\nhhhh\n", @entry.to_fastq) + end + + test "#to_key with bad residue raises" do entry = Seq.new("test", "AUCG") assert_raise(SeqError) { entry.to_key } end - def test_Seq_to_key_returns_correctly + test "#to_key returns correctly" do entry = Seq.new("test", "ATCG") assert_equal(54, entry.to_key) end - def test_Seq_reverse_returns_correctly + test "#reverse returns correctly" do + @entry.seq = "ATCG" + new_entry = @entry.reverse + assert_equal("GCTA", new_entry.seq) + assert_equal("ATCG", @entry.seq) + end + + test "#reverse! returns correctly" do @entry.seq = "ATCG" - assert_equal("GCTA", @entry.reverse) + @entry.reverse! + assert_equal("GCTA", @entry.seq) end - def test_Seq_complement_raises_if_no_sequence - @entry.type = 'dna' + test "#complement with no sequence raises" do + @entry.type = :dna assert_raise(SeqError) { @entry.complement } end - def test_Seq_complement_raises_on_bad_type + test "#complement with bad type raises" do @entry.seq = 'ATCG' - @entry.type = 'protein' + @entry.type = :protein assert_raise(SeqError) { @entry.complement } end - def test_Seq_complement_for_DNA_is_correct + test "#complement for DNA is correct" do @entry.seq = 'ATCGatcg' - @entry.type = 'dna' - assert_equal("TAGCtagc", @entry.complement) + @entry.type = :dna + comp = @entry.complement + assert_equal("TAGCtagc", comp.seq) + assert_equal("ATCGatcg", @entry.seq) end - def test_Seq_complement_for_RNA_is_correct + test "#complement for RNA is correct" do @entry.seq = 'AUCGaucg' - @entry.type = 'rna' - assert_equal("UAGCuagc", @entry.complement) + @entry.type = :rna + comp = @entry.complement + assert_equal("UAGCuagc", comp.seq) + assert_equal("AUCGaucg", @entry.seq) + end + + test "#complement! with no sequence raises" do + @entry.type = :dna + assert_raise(SeqError) { @entry.complement! } + end + + test "#complement! with bad type raises" do + @entry.seq = 'ATCG' + @entry.type = :protein + assert_raise(SeqError) { @entry.complement! } end - def test_Seq_reverse_complement_for_DNA_is_correct + test "#complement! for DNA is correct" do @entry.seq = 'ATCGatcg' - @entry.type = 'dna' - assert_equal("cgatCGAT", @entry.reverse_complement) + @entry.type = :dna + assert_equal("TAGCtagc", @entry.complement!.seq) end - def test_Seq_reverse_complement_for_RNA_is_correct + test "#complement! for RNA is correct" do @entry.seq = 'AUCGaucg' - @entry.type = 'rna' - assert_equal("cgauCGAU", @entry.reverse_complement) + @entry.type = :rna + assert_equal("UAGCuagc", @entry.complement!.seq) end - def test_Seq_generate_with_length_lt_1_raises - assert_raise(SeqError) { @entry.generate(-10, "dna") } - assert_raise(SeqError) { @entry.generate(0, "dna") } + test "#hamming_distance returns correctly" do + seq1 = Seq.new("test1", "ATCG") + seq2 = Seq.new("test2", "atgg") + assert_equal(1, seq1.hamming_distance(seq2)) end - def test_Seq_generate_with_bad_type_raises + test "#edit_distance returns correctly" do + seq1 = Seq.new("test1", "ATCG") + seq2 = Seq.new("test2", "tgncg") + assert_equal(2, seq1.edit_distance(seq2)) + end + + test "#generate with length < 1 raises" do + assert_raise(SeqError) { @entry.generate(-10, :dna) } + assert_raise(SeqError) { @entry.generate(0, :dna) } + end + + test "#generate with bad type raises" do assert_raise(SeqError) { @entry.generate(10, "foo") } end - def test_Seq_generate_with_ok_type_dont_raise - %w[dna DNA rna RNA protein Protein].each do |type| - assert_nothing_raised { @entry.generate(10, type) } + test "#generate with ok type dont raise" do + %w[dna rna protein].each do |type| + assert_nothing_raised { @entry.generate(10, type.to_sym) } end end - def test_Seq_subseq_with_start_lt_0_raises - @entry.seq = "ATCG" - assert_raise(SeqError) { @entry.subseq(-1, 1) } + test "#shuffle returns correctly" do + orig = "actgactgactgatcgatcgatcgatcgtactg" + @entry.seq = "actgactgactgatcgatcgatcgatcgtactg" + entry_shuf = @entry.shuffle + assert_equal(orig, @entry.seq) + assert_not_equal(@entry.seq, entry_shuf.seq) + end + + test "#shuffle! returns correctly" do + @entry.seq = "actgactgactgatcgatcgatcgatcgtactg" + assert_not_equal(@entry.seq, @entry.shuffle!.seq) + end + + test "#+ without qual returns correctly" do + entry = Seq.new("test1", "at") + Seq.new("test2", "cg") + assert_nil(entry.seq_name) + assert_equal("atcg", entry.seq) + assert_nil(entry.type) + assert_nil(entry.qual) end - def test_Seq_subseq_with_length_lt_1_raises + test "#+ with qual returns correctly" do + entry = Seq.new("test1", "at", :dna, "II") + Seq.new("test2", "cg", :dna, "JJ") + assert_nil(entry.seq_name) + assert_equal("atcg", entry.seq) + assert_equal(:dna, entry.type) + assert_equal("IIJJ", entry.qual) + end + + test "#<< with different types raises" do + @entry.seq = "atcg" + assert_raise(SeqError) { @entry << Seq.new("test", "atcg", :dna) } + end + + test "#<< with missing qual in one entry raises" do + @entry.seq = "atcg" + @entry.type = :dna + assert_raise(SeqError) { @entry << Seq.new("test", "atcg", :dna, "IIII") } + @entry.qual = "IIII" + assert_raise(SeqError) { @entry << Seq.new("test", "atcg", :dna) } + end + + test "#<< with nil qual in both entries dont raise" do + @entry.seq = "atcg" + assert_nothing_raised { @entry << Seq.new("test", "atcg") } + end + + test "#<< with qual in both entries dont raise" do + @entry.seq = "atcg" + @entry.type = :dna + @entry.qual = "IIII" + assert_nothing_raised { @entry << Seq.new("test", "atcg", :dna, "IIII") } + end + + test "#<< without qual returns correctly" do + @entry.seq = "atcg" + @entry << Seq.new("test", "ATCG") + assert_equal("atcgATCG", @entry.seq) + end + + test "#<< with qual returns correctly" do + @entry.seq = "atcg" + @entry.type = :dna + @entry.qual = "HHHH" + @entry << Seq.new("test", "ATCG", :dna, "IIII") + assert_equal("atcgATCG", @entry.seq) + assert_equal("HHHHIIII", @entry.qual) + end + + test "#[] with qual returns correctly" do + entry = Seq.new("test", "atcg", :dna, "FGHI") + + e = entry[2] + + assert_equal("test", e.seq_name) + assert_equal("c", e.seq) + assert_equal(:dna, e.type) + assert_equal("H", e.qual) + assert_equal("atcg", entry.seq) + assert_equal("FGHI", entry.qual) + end + + test "#[] without qual returns correctly" do + entry = Seq.new("test", "atcg") + + e = entry[2] + + assert_equal("test", e.seq_name) + assert_equal("c", e.seq) + assert_nil(e.qual) + assert_equal("atcg", entry.seq) + end + + test "[]= with qual returns correctly" do + entry = Seq.new("test", "atcg", :dna, "FGHI") + + entry[0] = Seq.new("foo", "T", :dna, "I") + + assert_equal("test", entry.seq_name) + assert_equal("Ttcg", entry.seq) + assert_equal(:dna, entry.type) + assert_equal("IGHI", entry.qual) + end + + test "[]= without qual returns correctly" do + entry = Seq.new("test", "atcg") + + entry[0] = Seq.new("foo", "T") + + assert_equal("test", entry.seq_name) + assert_equal("Ttcg", entry.seq) + end + + test "#subseq with start < 0 raises" do @entry.seq = "ATCG" - assert_raise(SeqError) { @entry.subseq(0, 0) } + assert_raise(SeqError) { @entry.subseq(-1, 1) } end - def test_Seq_subseq_with_start_plus_length_gt_seq_raises + test "#subseq with start plus length gt seq raises" do @entry.seq = "ATCG" assert_raise(SeqError) { @entry.subseq(0, 5) } end - def test_Seq_subseq_returns_correct_sequence + test "#subseq returns correct sequence" do @entry.seq = "ATCG" assert_equal("AT", @entry.subseq(0, 2).seq) assert_equal("CG", @entry.subseq(2, 2).seq) end - def test_Seq_subseq_without_len_returns_correct_sequence + test "#subseq without length returns correct sequence" do @entry.seq = "ATCG" assert_equal("ATCG", @entry.subseq(0).seq) assert_equal("CG", @entry.subseq(2).seq) end - def test_Seq_subseq_returns_correct_qual + test "#subseq returns correct qual" do @entry.seq = "ATCG" @entry.qual = "abcd" assert_equal("ab", @entry.subseq(0, 2).qual) assert_equal("cd", @entry.subseq(2, 2).qual) end - def test_Seq_subseq_without_len_returns_correct_qual + test "#subseq without length returns correct qual" do @entry.seq = "ATCG" @entry.qual = "abcd" assert_equal("abcd", @entry.subseq(0).qual) assert_equal("cd", @entry.subseq(2).qual) end - def test_Seq_subseq_bang_with_start_lt_0_raises + test "#subseq! with start < 0 raises" do @entry.seq = "ATCG" assert_raise(SeqError) { @entry.subseq!(-1, 1) } end - def test_Seq_subseq_bang_with_length_lt_1_raises - @entry.seq = "ATCG" - assert_raise(SeqError) { @entry.subseq!(0, 0) } - end - - def test_Seq_subseq_bang_with_start_plus_length_gt_seq_raises + test "#subseq! with start plus length > seq.length raises" do @entry.seq = "ATCG" assert_raise(SeqError) { @entry.subseq!(0, 5) } end - def test_Seq_subseq_bang_returns_correct_sequence + test "#subseq! returns correct sequence" do @entry.seq = "ATCG" @entry.subseq!(0, 2) assert_equal("AT", @entry.seq) @@ -257,7 +493,7 @@ class TestSeq < Test::Unit::TestCase assert_equal("CG", @entry.seq) end - def test_Seq_subseq_bang_without_len_returns_correct_sequence + test "#subseq! without length returns correct sequence" do @entry.seq = "ATCG" @entry.subseq!(0) assert_equal("ATCG", @entry.seq) @@ -266,7 +502,7 @@ class TestSeq < Test::Unit::TestCase assert_equal("CG", @entry.seq) end - def test_Seq_subseq_bang_with_pos_and_len_returns_correct_qual + test "#subseq! with pos and length returns correct qual" do @entry.seq = "ATCG" @entry.qual = "abcd" @entry.subseq!(0, 2) @@ -277,7 +513,7 @@ class TestSeq < Test::Unit::TestCase assert_equal("cd", @entry.qual) end - def test_Seq_subseq_bang_with_pos_returns_correct_qual + test "#subseq! with pos returns correct qual" do @entry.seq = "ATCG" @entry.qual = "abcd" @entry.subseq!(0) @@ -288,12 +524,25 @@ class TestSeq < Test::Unit::TestCase assert_equal("cd", @entry.qual) end - def test_Seq_subseq_rand_returns_correct_sequence + test "#subseq_rand returns correct sequence" do @entry.seq = "ATCG" assert_equal("ATCG", @entry.subseq_rand(4).seq) end - def test_Seq_composition_returns_correctly + test "#indels_remove without qual returns correctly" do + @entry.seq = "A-T.CG~CG" + @entry.qual = nil + assert_equal("ATCGCG", @entry.indels_remove.seq) + end + + test "#indels_remove with qual returns correctly" do + @entry.seq = "A-T.CG~CG" + @entry.qual = "a@b@cd@fg" + assert_equal("ATCGCG", @entry.indels_remove.seq) + assert_equal("abcdfg", @entry.indels_remove.qual) + end + + test "#composition returns correctly" do @entry.seq = "AAAATTTCCG" assert_equal(4, @entry.composition["A"]) assert_equal(3, @entry.composition["T"]) @@ -302,36 +551,171 @@ class TestSeq < Test::Unit::TestCase assert_equal(0, @entry.composition["X"]) end - def test_Seq_homopol_max_returns_0_with_empty_sequence - @entry.seq = "" - assert_equal(0, @entry.homopol_max) + test "#hard_mask returns correctly" do + @entry.seq = "--AAAANn" + assert_equal(33.33, @entry.hard_mask) end - def test_Seq_homopol_max_returns_0_with_nil_sequence - @entry.seq = nil - assert_equal(0, @entry.homopol_max) + test "#soft_mask returns correctly" do + @entry.seq = "--AAAa" + assert_equal(25.00, @entry.soft_mask) end - def test_Seq_homopol_max_returns_0_when_not_found - @entry.seq = "AtTcCcGggGnnNnn" - assert_equal(0, @entry.homopol_max(6)) + test "#mask_seq_hard! with nil seq raises" do + @entry.seq = nil + @entry.qual = "" + + assert_raise(SeqError) { @entry.mask_seq_hard!(20) } end - def test_Seq_homopol_max_returns_correctly - @entry.seq = "AtTcCcGggGnnNnn" - assert_equal(5, @entry.homopol_max(3)) + test "#mask_seq_hard! with nil qual raises" do + @entry.seq = "" + @entry.qual = nil + + assert_raise(SeqError) { @entry.mask_seq_hard!(20) } end - def test_Seq_hard_mask_returns_correctly - @entry.seq = "--AAAANn" - assert_equal(33.33, @entry.hard_mask) + test "#mask_seq_hard! with bad cutoff raises" do + assert_raise(SeqError) { @entry.mask_seq_hard!(-1) } + assert_raise(SeqError) { @entry.mask_seq_hard!(41) } end - def test_Seq_soft_mask_returns_correctly - @entry.seq = "--AAAa" - assert_equal(25.00, @entry.soft_mask) + test "#mask_seq_hard! with OK cutoff dont raise" do + @entry.seq = "ATCG" + @entry.qual = "RSTU" + + assert_nothing_raised { @entry.mask_seq_hard!(0) } + assert_nothing_raised { @entry.mask_seq_hard!(40) } end -end + test "#mask_seq_hard! returns correctly" do + @entry.seq = "-ATCG" + @entry.qual = "33456" + + assert_equal("-NNCG", @entry.mask_seq_hard!(20).seq) + end + + test "#mask_seq_soft! with nil seq raises" do + @entry.seq = nil + @entry.qual = "" + + assert_raise(SeqError) { @entry.mask_seq_soft!(20) } + end + + test "#mask_seq_soft! with nil qual raises" do + @entry.seq = "" + @entry.qual = nil + + assert_raise(SeqError) { @entry.mask_seq_soft!(20) } + end + + test "#mask_seq_soft! with bad cutoff raises" do + assert_raise(SeqError) { @entry.mask_seq_soft!(-1) } + assert_raise(SeqError) { @entry.mask_seq_soft!(41) } + end + + test "#mask_seq_soft! with OK cutoff dont raise" do + @entry.seq = "ATCG" + @entry.qual = "RSTU" + + assert_nothing_raised { @entry.mask_seq_soft!(0) } + assert_nothing_raised { @entry.mask_seq_soft!(40) } + end + + test "#mask_seq_soft! returns correctly" do + @entry.seq = "-ATCG" + @entry.qual = "33456" + + assert_equal("-atCG", @entry.mask_seq_soft!(20).seq) + end -__END__ + # qual score detection + + test "#qual_base33? returns correctly" do + # self.qual.match(/[!-:]/) + @entry.qual = '!"#$%&\'()*+,-./0123456789:' + assert_equal(true, @entry.qual_base33? ) + @entry.qual = 32.chr + assert_equal(false, @entry.qual_base33? ) + @entry.qual = 59.chr + assert_equal(false, @entry.qual_base33? ) + end + + test "#qual_base64? returns correctly" do + # self.qual.match(/[K-h]/) + @entry.qual = 'KLMNOPQRSTUVWXYZ[\]^_`abcdefgh' + assert_equal(true, @entry.qual_base64? ) + @entry.qual = 74.chr + assert_equal(false, @entry.qual_base64? ) + @entry.qual = 105.chr + assert_equal(false, @entry.qual_base64? ) + end + + test "#qual_valid? with nil qual raises" do + assert_raise(SeqError) { @entry.qual_valid?(:base_33) } + assert_raise(SeqError) { @entry.qual_valid?(:base_64) } + end + + test "#qual_valid? with bad encoding raises" do + @entry.qual = "abc" + assert_raise(SeqError) { @entry.qual_valid?("foobar") } + end + + test "#qual_valid? with OK range returns correctly" do + @entry.qual = ((Seq::SCORE_MIN + 33).chr .. (Seq::SCORE_MAX + 33).chr).to_a.join + assert_equal(true, @entry.qual_valid?(:base_33)) + @entry.qual = ((Seq::SCORE_MIN + 64).chr .. (Seq::SCORE_MAX + 64).chr).to_a.join + assert_equal(true, @entry.qual_valid?(:base_64)) + end + + test "#qual_valid? with bad range returns correctly" do + @entry.qual = ((Seq::SCORE_MIN + 33 - 1).chr .. (Seq::SCORE_MAX + 33).chr).to_a.join + assert_equal(false, @entry.qual_valid?(:base_33)) + @entry.qual = ((Seq::SCORE_MIN + 33).chr .. (Seq::SCORE_MAX + 33 + 1).chr).to_a.join + assert_equal(false, @entry.qual_valid?(:base_33)) + + @entry.qual = ((Seq::SCORE_MIN + 64 - 1).chr .. (Seq::SCORE_MAX + 64).chr).to_a.join + assert_equal(false, @entry.qual_valid?(:base_64)) + @entry.qual = ((Seq::SCORE_MIN + 64).chr .. (Seq::SCORE_MAX + 64 + 1).chr).to_a.join + assert_equal(false, @entry.qual_valid?(:base_64)) + end + + # convert sanger to ... + + test "#qual_convert! from base33 to base33 returns OK" do + @entry.qual = 'BCDEFGHI' + assert_equal('BCDEFGHI', @entry.qual_convert!(:base_33, :base_33).qual) + end + + test "#qual_convert! from base33 to base64 returns OK" do + @entry.qual = 'BCDEFGHI' + assert_equal('abcdefgh', @entry.qual_convert!(:base_33, :base_64).qual) + end + + test "#qual_convert! from base64 to base64 returns OK" do + @entry.qual = 'BCDEFGHI' + assert_equal('BCDEFGHI', @entry.qual_convert!(:base_64, :base_64).qual) + end + + test "#qual_convert! from base64 to base33 returns OK" do + @entry.qual = 'abcdefgh' + assert_equal('BCDEFGHI', @entry.qual_convert!(:base_64, :base_33).qual) + end + + test "#qual_coerce! returns correctly" do + @entry.qual = ('!' .. '~').to_a.join + assert_equal("!\"\#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII", @entry.qual_coerce!(:base_33).qual) + @entry.qual = ('!' .. '~').to_a.join + assert_equal("!\"\#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZh\\h^_`abcdefghhhhhhhhhhhhhhhhhhhhhhh", @entry.qual_coerce!(:base_64).qual) + end + + test "#scores_mean without qual raises" do + @entry.qual = nil + assert_raise(SeqError) { @entry.scores_mean } + end + + test "#scores_mean returns correctly" do + @entry.qual = '!!II' + assert_equal(20.0, @entry.scores_mean) + end +end