From 2d1f69b1d1205a9dc4433f243f8787f0f26147e0 Mon Sep 17 00:00:00 2001
From: martinahansen <martinahansen@74ccb610-7750-0410-82ae-013aeee3265d>
Date: Mon, 13 Jun 2011 20:43:28 +0000
Subject: [PATCH] added Seq.type_guess to ruby code

git-svn-id: http://biopieces.googlecode.com/svn/trunk@1468 74ccb610-7750-0410-82ae-013aeee3265d
---
 code_ruby/lib/maasha/seq.rb       | 18 +++++++++++++
 code_ruby/test/maasha/test_seq.rb | 43 ++++++++++++++++++++++++++++++-
 2 files changed, 60 insertions(+), 1 deletion(-)

diff --git a/code_ruby/lib/maasha/seq.rb b/code_ruby/lib/maasha/seq.rb
index 21e6e07..a3fe789 100644
--- a/code_ruby/lib/maasha/seq.rb
+++ b/code_ruby/lib/maasha/seq.rb
@@ -86,6 +86,24 @@ class Seq
     @qual     = qual
   end
 
+  # Method that guesses and returns the sequence type
+  # by inspecting the first 100 residues.
+  def type_guess
+    raise SeqError, "Guess failed: sequence is nil" if self.seq.nil?
+
+    case self.seq[0 ... 100].downcase
+    when /[flpqie]/ then return "protein"
+    when /[u]/      then return "rna"
+    else                 return "dna"
+    end
+  end
+
+  # Method that guesses and sets the sequence type
+  # by inspecting the first 100 residues.
+  def type_guess!
+    self.type = self.type_guess
+  end
+
   # Returns the length of a sequence.
   def length
     self.seq.nil? ? 0 : self.seq.length
diff --git a/code_ruby/test/maasha/test_seq.rb b/code_ruby/test/maasha/test_seq.rb
index 5e79a0f..93737d4 100755
--- a/code_ruby/test/maasha/test_seq.rb
+++ b/code_ruby/test/maasha/test_seq.rb
@@ -39,7 +39,48 @@ class TestSeq < Test::Unit::TestCase
 
   def test_Seq_is_protein_with_protein_sequence_type_returns_true
     @entry.type = 'protein'
-    assert(@entry.is_protein? == true)
+    assert_equal(true, @entry.is_protein?)
+  end
+
+  def test_Seq_type_guess_without_sequence_raises
+    assert_raise(SeqError) { @entry.type_guess }
+  end
+
+  def test_Seq_type_guess_with_protein_returns_protein
+    @entry.seq = 'atcatcrFgatcg'
+    assert_equal('protein', @entry.type_guess)
+  end
+
+  def test_Seq_type_guess_with_rna_returns_rna
+    @entry.seq = 'atcatcrUgatcg'
+    assert_equal('rna', @entry.type_guess)
+  end
+
+  def test_Seq_type_guess_with_dna_returns_dna
+    @entry.seq = 'atcatcgatcg'
+    assert_equal('dna', @entry.type_guess)
+  end
+
+  def test_Seq_type_guess_EM_without_sequence_raises
+    assert_raise(SeqError) { @entry.type_guess! }
+  end
+
+  def test_Seq_type_guess_EM_with_protein_returns_protein
+    @entry.seq = 'atcatcrFgatcg'
+    @entry.type_guess!
+    assert_equal('protein', @entry.type)
+  end
+
+  def test_Seq_type_guess_EM_with_rna_returns_rna
+    @entry.seq = 'atcatcrUgatcg'
+    @entry.type_guess!
+    assert_equal('rna', @entry.type)
+  end
+
+  def test_Seq_type_guess_EM_with_dna_returns_dna
+    @entry.seq = 'atcatcgatcg'
+    @entry.type_guess!
+    assert_equal('dna', @entry.type)
   end
 
   def test_Seq_length_is_correct
-- 
2.39.5