]> git.donarmstrong.com Git - biopieces.git/blobdiff - code_ruby/lib/maasha/seq.rb
fixed return on reverse_complement in seq.rb
[biopieces.git] / code_ruby / lib / maasha / seq.rb
index 21e6e079f0c60a031aa2fb97ef2621c95fff7fb9..037f99dd9317a8c4057aae478cbcaefa2e9c9263 100644 (file)
@@ -24,6 +24,7 @@
 
 require 'maasha/digest'
 require 'maasha/patternmatcher'
+require 'maasha/bits'
 #require 'maasha/patscan'
 
 # Residue alphabets
@@ -45,7 +46,18 @@ class Seq
 
   attr_accessor :seq_name, :seq, :type, :qual
 
-  # Method that generates all possible oligos of a specifed length and type.
+  # Class method to instantiate a new Sequence object given
+  # a Biopiece record.
+  def self.new_bp(record)
+    seq_name = record[:SEQ_NAME]
+    seq      = record[:SEQ]
+    type     = record[:SEQ_TYPE]
+    qual     = record[:SCORES]
+
+    self.new(seq_name, seq, type, qual)
+  end
+
+  # Class method that generates all possible oligos of a specifed length and type.
   def self.generate_oligos(length, type)
     raise SeqError, "Cannot generate negative oligo length: #{length}" if length <= 0
 
@@ -86,6 +98,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
@@ -149,18 +179,31 @@ class Seq
     raise SeqError, "Missing seq_name" if self.seq_name.nil?
     raise SeqError, "Missing seq"      if self.seq.nil?
 
-    seq_name = self.seq_name
-    seq      = self.seq
+    seq_name = self.seq_name.to_s
+    seq      = self.seq.to_s
 
     unless wrap.nil?
       seq.gsub!(/(.{#{wrap}})/) do |match|
-        match << "\n"
+        match << $/
       end
 
       seq.chomp!
     end
 
-    ">#{seq_name}\n#{seq}\n"
+    ">" + seq_name + $/ + seq + $/
+  end
+
+  # Method that given a Seq entry returns a FASTQ entry (a string).
+  def to_fastq
+    raise SeqError, "Missing seq_name" if self.seq_name.nil?
+    raise SeqError, "Missing seq"      if self.seq.nil?
+    raise SeqError, "Missing qual"     if self.qual.nil?
+
+    seq_name = self.seq_name.to_s
+    seq      = self.seq.to_s
+    qual     = self.qual.to_s
+
+    "@" + seq_name + $/ + seq + $/ + "+" + $/ + qual + $/
   end
 
   # Method that generates a unique key for a
@@ -187,6 +230,7 @@ class Seq
   def reverse_complement
     self.reverse
     self.complement
+    self
   end
 
   alias :revcomp :reverse_complement
@@ -194,6 +238,7 @@ class Seq
   # Method to reverse the sequence.
   def reverse
     self.seq.reverse!
+    self.qual.reverse! if self.qual
   end
 
   # Method that complements sequence including ambiguity codes.
@@ -209,6 +254,12 @@ class Seq
     end
   end
 
+  # Method to determine the Hamming Distance between
+  # two Sequence objects (case insensitive).
+  def hamming_distance(seq)
+    self.seq.upcase.hamming_distance(seq.seq.upcase)
+  end
+
   # Method that generates a random sequence of a given length and type.
   def generate(length, type)
     raise SeqError, "Cannot generate sequence length < 1: #{length}" if length <= 0
@@ -230,6 +281,12 @@ class Seq
     seq_new
   end
 
+  # Method to shuffle a sequence readomly inline.
+  def shuffle!
+    self.seq = self.seq.split('').shuffle!.join
+    self
+  end
+
   # Method that returns a subsequence of from a given start position
   # and of a given length.
   def subseq(start, length = self.length - start)
@@ -317,7 +374,7 @@ class Seq
   def convert_phred2illumina!
     self.qual.gsub!(/./) do |score|
       score_phred  = score.ord - SCORE_PHRED
-      raise SeqError, "Bad Phred score: #{score} (#{score_phred})" unless (0 .. 40).include? score_phred
+      raise SeqError, "Bad Phred score: #{score} (#{score_phred})" unless (0 .. 41).include? score_phred
       score_illumina = score_phred + SCORE_ILLUMINA
       score          = score_illumina.chr
     end