class DigestError < StandardError; end
class Digest
+ include Enumerable
+
# Initialize a digest object with the following arguments:
# - seq: A sequence object.
# - pattern: A restriction enzyme recognition pattern.
@seq = seq
@pattern = disambiguate(pattern)
@cut_pos = cut_pos
+ @offset = 0
end
- # Method that returns a list of positions where
- # a specified restriction enzyme will cut the sequence.
- def positions
- positions = []
-
- @seq.seq.scan @pattern do |match|
+ # Method to get the next digestion product from a sequence.
+ def each
+ @seq.seq.scan @pattern do
pos = $`.length + @cut_pos - 1
-
+
if pos >= 0 and pos < @seq.seq.length - 2
- positions << pos
- end
- end
+ seq = Seq.new("#{@seq.seq_name}[#{@offset + 1}-#{pos + 1}]", @seq.seq[@offset .. pos], @seq.type)
- positions
- end
-
- # Method that returns a list of strings with digestion produts
- # from the digestion of a specified sequence with a specified
- # restriction enzyme.
- def products
- products = []
-
- beg = 0
+ yield seq
+ end
- self.positions.each do |pos|
- products << @seq.seq[beg .. pos]
- beg = pos + 1
+ @offset = pos + 1
end
- if beg < @seq.seq.length
- products << @seq.seq[beg .. @seq.seq.length]
+ # Handle remaining sequence after last cut.
+ if @offset > 0 and @offset < @seq.seq.length
+ seq = Seq.new("#{@seq.seq_name}[#{@offset + 1}-#{@seq.seq.length}]", @seq.seq[@offset .. @seq.seq.length], @seq.type)
+
+ yield seq
end
- products
+ self # conventionally
end
private