]> git.donarmstrong.com Git - biopieces.git/commitdiff
added subseq and subseq_rand methods to seq.rb
authormartinahansen <martinahansen@74ccb610-7750-0410-82ae-013aeee3265d>
Thu, 3 Feb 2011 18:44:14 +0000 (18:44 +0000)
committermartinahansen <martinahansen@74ccb610-7750-0410-82ae-013aeee3265d>
Thu, 3 Feb 2011 18:44:14 +0000 (18:44 +0000)
git-svn-id: http://biopieces.googlecode.com/svn/trunk@1252 74ccb610-7750-0410-82ae-013aeee3265d

code_ruby/Maasha/lib/seq.rb
code_ruby/Maasha/test/test_seq.rb

index 08c90ea538e9963808903476631324caa7afc788..a8b4a84f1dc896675ad51026607f461b43fc7406 100644 (file)
@@ -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.
index 8ab705da791bb4c3aaffe04344afb689d9702d5b..e1dfd87807c09816c07cf12bae633a6c3aad7a6b 100755 (executable)
@@ -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"])