]> git.donarmstrong.com Git - biopieces.git/blobdiff - code_ruby/lib/maasha/seq.rb
refactoring of ruby code converting sequences types to symbols
[biopieces.git] / code_ruby / lib / maasha / seq.rb
index cf4b6e6802883d5447c7bbad7157ae65bc0c95b1..92ce19edaee4a9176487272a8f1a89c32c7ee806 100644 (file)
@@ -87,7 +87,7 @@ class Seq
   def self.new_bp(record)
     seq_name = record[:SEQ_NAME]
     seq      = record[:SEQ]
-    type     = record[:SEQ_TYPE]
+    type     = record[:SEQ_TYPE].to_sym if record[:SEQ_TYPE]
     qual     = record[:SCORES]
 
     self.new(seq_name, seq, type, qual)
@@ -98,9 +98,9 @@ class Seq
     raise SeqError, "Cannot generate negative oligo length: #{length}" if length <= 0
 
     case type.downcase
-    when /dna/     then alph = DNA
-    when /rna/     then alph = RNA
-    when /protein/ then alph = PROTEIN
+    when :dna     then alph = DNA
+    when :rna     then alph = RNA
+    when :protein then alph = PROTEIN
     else
       raise SeqError, "Unknown sequence type: #{type}"
     end
@@ -140,9 +140,9 @@ class Seq
     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"
+    when /[flpqie]/ then return :protein
+    when /[u]/      then return :rna
+    else                 return :dna
     end
   end
 
@@ -190,24 +190,24 @@ class Seq
 
   # Method that returns true is a given sequence type is DNA.
   def is_dna?
-    self.type == 'dna'
+    self.type == :dna
   end
 
   # Method that returns true is a given sequence type is RNA.
   def is_rna?
-    self.type == 'rna'
+    self.type == :rna
   end
 
   # Method that returns true is a given sequence type is protein.
   def is_protein?
-    self.type == 'protein'
+    self.type == :protein
   end
 
   # Method to transcribe DNA to RNA.
   def to_rna
     raise SeqError, "Cannot transcribe 0 length sequence" if self.length == 0
     raise SeqError, "Cannot transcribe sequence type: #{self.type}" unless self.is_dna?
-    self.type = 'rna'
+    self.type = :rna
     self.seq.tr!('Tt','Uu')
   end
 
@@ -215,14 +215,13 @@ class Seq
   def to_dna
     raise SeqError, "Cannot reverse-transcribe 0 length sequence" if self.length == 0
     raise SeqError, "Cannot reverse-transcribe sequence type: #{self.type}" unless self.is_rna?
-
-    self.type = 'dna'
+    self.type = :dna
     self.seq.tr!('Uu','Tt')
   end
 
   # Method to translate a DNA sequence to protein.
   def translate!(trans_tab = 11)
-    raise SeqError, "Sequence type must be 'dna' - not #{self.type}" unless self.type == 'dna'
+    raise SeqError, "Sequence type must be 'dna' - not #{self.type}" unless self.type == :dna
     raise SeqError, "Sequence length must be a multiplum of 3 - was: #{self.length}" unless (self.length % 3) == 0
 
     case trans_tab
@@ -257,7 +256,7 @@ class Seq
 
     self.seq  = protein
     self.qual = nil
-    self.type = "protein"
+    self.type = :protein
 
     self
   end
@@ -392,20 +391,17 @@ class Seq
   def generate(length, type)
     raise SeqError, "Cannot generate sequence length < 1: #{length}" if length <= 0
 
-    case type.downcase
-    when "dna"
-      alph = DNA
-    when "rna"
-      alph = RNA
-    when "protein"
-      alph = PROTEIN
+    case type
+    when :dna     then alph = DNA
+    when :rna     then alph = RNA
+    when :protein then alph = PROTEIN
     else
       raise SeqError, "Unknown sequence type: #{type}"
     end
 
     seq_new   = Array.new(length) { alph[rand(alph.size)] }.join("")
     self.seq  = seq_new
-    self.type = type.downcase
+    self.type = type
     seq_new
   end