]> git.donarmstrong.com Git - biopieces.git/blobdiff - code_ruby/lib/maasha/seq.rb
added requirement of RubyInline
[biopieces.git] / code_ruby / lib / maasha / seq.rb
index a3fe78905159ab4edeee58fcd0067ee4aca2988c..598d0a2cd5056cd0786735009ba4eb59571d04fd 100644 (file)
 
 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
 
-require 'maasha/digest'
 require 'maasha/patternmatcher'
+require 'maasha/bits'
+require 'maasha/backtrack'
+require 'maasha/digest'
 #require 'maasha/patscan'
 
 # Residue alphabets
@@ -35,6 +37,8 @@ INDELS  = %w[. - _ ~]
 # Quality scores bases
 SCORE_PHRED    = 33
 SCORE_ILLUMINA = 64
+SCORE_MIN      = 0
+SCORE_MAX      = 40
 
 # Error class for all exceptions to do with Seq.
 class SeqError < StandardError; end
@@ -42,10 +46,23 @@ class SeqError < StandardError; end
 class Seq
   #include Patscan
   include PatternMatcher
+  include BackTrack
+  include Digest
 
   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
 
@@ -164,21 +181,34 @@ class Seq
 
   # Method that given a Seq entry returns a FASTA entry (a string).
   def to_fasta(wrap = nil)
-    raise SeqError, "Missing seq_name" if self.seq_name.nil?
-    raise SeqError, "Missing seq"      if self.seq.nil?
+    raise SeqError, "Missing seq_name" if self.seq_name.nil? or self.seq_name == ''
+    raise SeqError, "Missing seq"      if self.seq.nil?      or self.seq.empty?
 
-    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
@@ -205,6 +235,7 @@ class Seq
   def reverse_complement
     self.reverse
     self.complement
+    self
   end
 
   alias :revcomp :reverse_complement
@@ -212,6 +243,8 @@ class Seq
   # Method to reverse the sequence.
   def reverse
     self.seq.reverse!
+    self.qual.reverse! if self.qual
+    self
   end
 
   # Method that complements sequence including ambiguity codes.
@@ -219,14 +252,20 @@ class Seq
     raise SeqError, "Cannot complement 0 length sequence" if self.length == 0
 
     if self.is_dna?
-      self.seq.tr!( 'AGCUTRYWSMKHDVBNagcutrywsmkhdvbn', 'TCGAAYRWSKMDHBVNtcgaayrwskmdhbvn' )
+      self.seq.tr!('AGCUTRYWSMKHDVBNagcutrywsmkhdvbn', 'TCGAAYRWSKMDHBVNtcgaayrwskmdhbvn')
     elsif self.is_rna?
-      self.seq.tr!( 'AGCUTRYWSMKHDVBNagcutrywsmkhdvbn', 'UCGAAYRWSKMDHBVNucgaayrwskmdhbvn' )
+      self.seq.tr!('AGCUTRYWSMKHDVBNagcutrywsmkhdvbn', 'UCGAAYRWSKMDHBVNucgaayrwskmdhbvn')
     else
       raise SeqError, "Cannot complement sequence type: #{self.type}"
     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
@@ -248,6 +287,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)
@@ -288,6 +333,40 @@ class Seq
     self.subseq(start, length)
   end
 
+  def quality_trim_right(min)
+    raise SeqError, "no sequence"      if self.seq.nil?
+    raise SeqError, "no quality score" if self.qual.nil?
+    raise SeqError, "minimum value: #{min} out of range #{SCORE_MIN} .. #{SCORE_MAX}" unless (SCORE_MIN .. SCORE_MAX).include? min
+
+    regex_right = Regexp.new("[#{(SCORE_ILLUMINA).chr}-#{(SCORE_ILLUMINA + min).chr}]+$")
+
+    self.qual.match(regex_right) do |m|
+      self.subseq!(0, $`.length) if $`.length > 0
+    end
+
+    self
+  end
+
+  def quality_trim_left(min)
+    raise SeqError, "no sequence"      if self.seq.nil?
+    raise SeqError, "no quality score" if self.qual.nil?
+    raise SeqError, "minimum value: #{min} out of range #{SCORE_MIN} .. #{SCORE_MAX}" unless (SCORE_MIN .. SCORE_MAX).include? min
+
+    regex_left  = Regexp.new("^[#{(SCORE_ILLUMINA).chr}-#{(SCORE_ILLUMINA + min).chr}]+")
+
+    self.qual.match(regex_left) do |m|
+      self.subseq!(m.to_s.length, self.length - m.to_s.length) if self.length - m.to_s.length > 0
+    end
+
+    self
+  end
+
+  def quality_trim(min)
+    self.quality_trim_right(min)
+    self.quality_trim_left(min)
+    self
+  end
+
   # Method that returns the residue compositions of a sequence in
   # a hash where the key is the residue and the value is the residue
   # count.
@@ -335,7 +414,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