# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
-require 'maasha/digest'
require 'maasha/patternmatcher'
require 'maasha/bits'
+require 'maasha/backtrack'
+require 'maasha/digest'
#require 'maasha/patscan'
# Residue alphabets
# 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
class Seq
#include Patscan
include PatternMatcher
+ include BackTrack
+ include Digest
attr_accessor :seq_name, :seq, :type, :qual
# 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
def reverse_complement
self.reverse
self.complement
+ self
end
alias :revcomp :reverse_complement
# 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.
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
# 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
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.