]> 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 394d6fb3ca205b9ee5b86757efdc88ff4ca9ab30..598d0a2cd5056cd0786735009ba4eb59571d04fd 100644 (file)
 
 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
 
-require 'maasha/digest'
 require 'maasha/patternmatcher'
 require 'maasha/bits'
+require 'maasha/backtrack'
+require 'maasha/digest'
 #require 'maasha/patscan'
 
 # Residue alphabets
@@ -36,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
@@ -43,6 +46,8 @@ class SeqError < StandardError; end
 class Seq
   #include Patscan
   include PatternMatcher
+  include BackTrack
+  include Digest
 
   attr_accessor :seq_name, :seq, :type, :qual
 
@@ -176,8 +181,8 @@ 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.to_s
     seq      = self.seq.to_s
@@ -230,6 +235,7 @@ class Seq
   def reverse_complement
     self.reverse
     self.complement
+    self
   end
 
   alias :revcomp :reverse_complement
@@ -237,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.
@@ -244,9 +252,9 @@ 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
@@ -282,6 +290,7 @@ class Seq
   # 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
@@ -324,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.