]> git.donarmstrong.com Git - biopieces.git/commitdiff
added Seq#to_key method
authormartinahansen <martinahansen@74ccb610-7750-0410-82ae-013aeee3265d>
Sun, 23 Jan 2011 20:50:58 +0000 (20:50 +0000)
committermartinahansen <martinahansen@74ccb610-7750-0410-82ae-013aeee3265d>
Sun, 23 Jan 2011 20:50:58 +0000 (20:50 +0000)
git-svn-id: http://biopieces.googlecode.com/svn/trunk@1218 74ccb610-7750-0410-82ae-013aeee3265d

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

index bac30c416884c271e8d8a912d636095920c9fb4a..a61e761eb01aefba1eb7b45009a204e645689008 100644 (file)
@@ -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
index 6e102881a377391cf13c1e9109bf42ea4915bcb1..8ab705da791bb4c3aaffe04344afb689d9702d5b 100755 (executable)
@@ -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)