From: martinahansen Date: Sun, 23 Jan 2011 20:50:58 +0000 (+0000) Subject: added Seq#to_key method X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;h=8d8e3bcd28bd17ce3708f9b1d99f6833571baac7;p=biopieces.git added Seq#to_key method git-svn-id: http://biopieces.googlecode.com/svn/trunk@1218 74ccb610-7750-0410-82ae-013aeee3265d --- diff --git a/code_ruby/Maasha/lib/seq.rb b/code_ruby/Maasha/lib/seq.rb index bac30c4..a61e761 100644 --- a/code_ruby/Maasha/lib/seq.rb +++ b/code_ruby/Maasha/lib/seq.rb @@ -125,6 +125,26 @@ class Seq ">#{seq_name}\n#{seq}\n" end + # Method that generates a unique key for a + # DNA sequence and return this key as a Fixnum. + def to_key + key = 0 + + self.seq.upcase.each_char do |char| + key <<= 2 + + case char + when 'A' then key |= 0 + when 'C' then key |= 1 + when 'G' then key |= 2 + when 'T' then key |= 3 + else raise SeqError, "Bad residue: #{char}" + end + end + + key + end + # Method to reverse complement sequence. def reverse_complement self.reverse diff --git a/code_ruby/Maasha/test/test_seq.rb b/code_ruby/Maasha/test/test_seq.rb index 6e10288..8ab705d 100755 --- a/code_ruby/Maasha/test/test_seq.rb +++ b/code_ruby/Maasha/test/test_seq.rb @@ -127,6 +127,16 @@ class TestSeq < Test::Unit::TestCase assert_equal(">test\nAT\nCG\n", entry.to_fasta(2)) end + def test_Seq_to_key_with_bad_residue_raises + entry = Seq.new("test", "AUCG") + assert_raise(SeqError) { entry.to_key } + end + + def test_Seq_to_key_returns_correctly + entry = Seq.new("test", "ATCG") + assert_equal(54, entry.to_key) + end + def test_Seq_reverse_returns_correctly @entry.seq = "ATCG" assert_equal("GCTA", @entry.reverse)