]> git.donarmstrong.com Git - biopieces.git/blobdiff - code_ruby/lib/maasha/seq.rb
added Seq.shuffle method and tests
[biopieces.git] / code_ruby / lib / maasha / seq.rb
index b919a7f31cff1a32555960e4fa60a03f4668eb34..e72250d596cc5d1dd55adac34d3a15b3503048fa 100644 (file)
 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
 
 require 'maasha/bits'
-require 'maasha/seq/backtrack'
 require 'maasha/seq/digest'
-#require 'maasha/seq/patscan'
-require 'maasha/seq/patternmatcher'
 require 'maasha/seq/trim'
 require 'narray'
 
+autoload :BackTrack, 'maasha/seq/backtrack.rb'
+autoload :Dynamic,   'maasha/seq/dynamic.rb'
+
 # Residue alphabets
 DNA     = %w[a t c g]
 RNA     = %w[a u c g]
@@ -67,18 +67,16 @@ TRANS_TAB11 = {
   "GTG" => "V", "GCG" => "A", "GAG" => "E", "GGG" => "G"
 }
 
-# Quality scores bases
-SCORE_BASE = 64
-SCORE_MIN  = 0
-SCORE_MAX  = 40
 
 # Error class for all exceptions to do with Seq.
 class SeqError < StandardError; end
 
 class Seq
-  #include Patscan
-  include PatternMatcher
-  include BackTrack
+  # Quality scores bases
+  SCORE_BASE = 64
+  SCORE_MIN  = 0
+  SCORE_MAX  = 40
+
   include Digest
   include Trim
 
@@ -392,7 +390,12 @@ class Seq
     seq_new
   end
 
-  # Method to shuffle a sequence readomly inline.
+  # Method to return a new Seq object with shuffled sequence.
+  def shuffle
+    Seq.new(self.seq_name, self.seq.split('').shuffle!.join, self.type, self.qual)
+  end
+
+  # Method to shuffle a sequence randomly inline.
   def shuffle!
     self.seq = self.seq.split('').shuffle!.join
     self
@@ -415,25 +418,18 @@ class Seq
       qual = self.qual[start .. stop] unless self.qual.nil?
     end
 
-    Seq.new(self.seq_name.dup, seq, self.type, qual)
+    Seq.new(self.seq_name, seq, self.type, qual)    # TODO changed self.seq_name.dup to self.seq_name -> consequence?
   end
 
   # Method that replaces a sequence with a subsequence from a given start position
   # and of a given length.
   def subseq!(start, length = self.length - start)
-    raise SeqError, "subsequence start: #{start} < 0"                                                if start  < 0
-    raise SeqError, "subsequence length: #{length} < 0"                                              if length < 0
-    raise SeqError, "subsequence start + length > Seq.length: #{start} + #{length} > #{self.length}" if start + length > self.length
-
-    if length == 0
-      self.seq  = ""
-      self.qual = "" unless self.qual.nil?
-    else
-      stop = start + length - 1
+    s = subseq(start, length)
 
-      self.seq  = self.seq[start .. stop]
-      self.qual = self.qual[start .. stop] unless self.qual.nil?
-    end
+    self.seq_name = s.seq_name
+    self.seq      = s.seq
+    self.type     = s.type
+    self.qual     = s.qual
 
     self
   end
@@ -545,11 +541,41 @@ class Seq
     self
   end
 
+  # Method that determines if a quality score string can be
+  # absolutely identified as base 33.
+  def qual_base33?
+    self.qual.match(/[!-:]/) ? true : false
+  end
+
+  # Method that determines if a quality score string can be
+  # absolutely identified as base 64.
+  def qual_base64?
+    self.qual.match(/[K-h]/) ? true : false
+  end
+
+  # Method to determine if a quality score is valid.
+  def qual_valid?(encoding)
+    raise SeqError, "Missing qual" if self.qual.nil?
+
+    case encoding.downcase
+    when "sanger"     then return true if self.qual.match(/^[!-~]*$/)
+    when "454"        then return true if self.qual.match(/^[@-~]*$/)
+    when "solexa"     then return true if self.qual.match(/^[;-~]*$/)
+    when "illumina13" then return true if self.qual.match(/^[@-~]*$/)
+    when "illumina15" then return true if self.qual.match(/^[@-~]*$/)
+    when "illumina18" then return true if self.qual.match(/^[!-~]*$/)
+    else raise SeqError, "unknown quality score encoding: #{encoding}"
+    end
+
+    false
+  end
+
   # Method to convert quality scores inbetween formats.
   # Sanger     base 33, range  0-40 
+  # 454        base 64, range  0-40 
   # Solexa     base 64, range -5-40 
   # Illumina13 base 64, range  0-40 
-  # Illumina15 base 64, range  3-40 
+  # Illumina15 base 64, range  0-40 
   # Illumina18 base 33, range  0-41 
   def convert_scores!(from, to)
     unless from == to
@@ -557,6 +583,7 @@ class Seq
 
       case from.downcase
       when "sanger"     then na_qual -= 33
+      when "454"        then na_qual -= 64
       when "solexa"     then na_qual -= 64
       when "illumina13" then na_qual -= 64
       when "illumina15" then na_qual -= 64
@@ -566,11 +593,12 @@ class Seq
 
       case to.downcase
       when "sanger"     then na_qual += 33
+      when "454"        then na_qual += 64
       when "solexa"     then na_qual += 64
       when "illumina13" then na_qual += 64
       when "illumina15" then na_qual += 64
       when "illumina18" then na_qual += 33
-      else raise SeqError, "unknown quality score encoding: #{from}"
+      else raise SeqError, "unknown quality score encoding: #{to}"
       end
 
       self.qual = na_qual.to_s