require 'maasha/digest'
require 'maasha/patternmatcher'
+require 'maasha/bits'
#require 'maasha/patscan'
# Residue alphabets
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
@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
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
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
end
# Method that complements sequence including ambiguity codes.
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
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)
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