From: martinahansen Date: Thu, 3 Feb 2011 18:44:14 +0000 (+0000) Subject: added subseq and subseq_rand methods to seq.rb X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;h=7638797da2a747352fe82cfe3076807c558fe826;p=biopieces.git added subseq and subseq_rand methods to seq.rb git-svn-id: http://biopieces.googlecode.com/svn/trunk@1252 74ccb610-7750-0410-82ae-013aeee3265d --- diff --git a/code_ruby/Maasha/lib/seq.rb b/code_ruby/Maasha/lib/seq.rb index 08c90ea..a8b4a84 100644 --- a/code_ruby/Maasha/lib/seq.rb +++ b/code_ruby/Maasha/lib/seq.rb @@ -202,7 +202,7 @@ class Seq # Method that generates a random sequence of a given length and type. def generate(length,type) - raise SeqError, "Cannot generate negative sequence length: #{length}" if length <= 0 + raise SeqError, "Cannot generate sequence length < 1: #{length}" if length <= 0 case type.downcase when "dna" @@ -221,6 +221,33 @@ class Seq seq_new end + # Method that returns a subsequence of from a given start position + # and of a given length. + def subseq(start, length) + raise SeqError, "sunsequence start: #{start} < 0" if start < 0 + raise SeqError, "subsequence length: #{length} < 1" if length <= 0 + raise SeqError, "subsequence start + length > Seq.length: #{start} + #{length} > #{self.length}" if start + length > self.length + + stop = start + length - 1 + + seq = self.seq[start .. stop] + qual = self.qual[start .. stop] unless self.qual.nil? + + Seq.new(self.seq_name, seq, self.type, qual) + end + + # Method that returns a subsequence of a given length + # beginning at a random position. + def subseq_rand(length) + if self.seq.length - length + 1 == 0 + start = 0 + else + start = rand(self.seq.length - length + 1) + end + + self.subseq(start, length) + end + # Method that returns the residue compositions of a sequence in # a hash where the key is the residue and the value is the residue # count. diff --git a/code_ruby/Maasha/test/test_seq.rb b/code_ruby/Maasha/test/test_seq.rb index 8ab705d..e1dfd87 100755 --- a/code_ruby/Maasha/test/test_seq.rb +++ b/code_ruby/Maasha/test/test_seq.rb @@ -177,21 +177,54 @@ class TestSeq < Test::Unit::TestCase assert_equal("cgauCGAU", @entry.reverse_complement) end - def test_Seq_generate_raises_if_length_lt_0 + def test_Seq_generate_with_length_lt_1_raises assert_raise(SeqError) { @entry.generate(-10, "dna") } assert_raise(SeqError) { @entry.generate(0, "dna") } end - def test_Seq_generate_raises_on_bad_type + def test_Seq_generate_with_bad_type_raises assert_raise(SeqError) { @entry.generate(10, "foo") } end - def test_Seq_generate_dont_raise_on_ok_type + 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) } end end + def test_Seq_subseq_with_start_lt_0_raises + @entry.seq = "ATCG" + assert_raise(SeqError) { @entry.subseq(-1, 1) } + end + + def test_Seq_subseq_with_length_lt_1_raises + @entry.seq = "ATCG" + assert_raise(SeqError) { @entry.subseq(0, 0) } + end + + def test_Seq_subseq_with_start_plus_length_gt_seq_raises + @entry.seq = "ATCG" + assert_raise(SeqError) { @entry.subseq(0, 5) } + end + + def test_Seq_subseq_returns_correct_sequence + @entry.seq = "ATCG" + assert_equal("AT", @entry.subseq(0, 2).seq) + assert_equal("CG", @entry.subseq(2, 2).seq) + end + + def test_Seq_subseq_returns_correct_qual + @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_rand_returns_correct_sequence + @entry.seq = "ATCG" + assert_equal("ATCG", @entry.subseq_rand(4).seq) + end + def test_Seq_composition_returns_correctly @entry.seq = "AAAATTTCCG" assert_equal(4, @entry.composition["A"])