# 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"
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.
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"])