1 # Copyright (C) 2007-2011 Martin A. Hansen.
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 # http://www.gnu.org/copyleft/gpl.html
19 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
21 # This software is part of the Biopieces framework (www.biopieces.org).
23 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
25 # Error class for all exceptions to do with Digest.
26 class DigestError < StandardError; end
31 # Initialize a digest object with the following arguments:
32 # - seq: A sequence object.
33 # - pattern: A restriction enzyme recognition pattern.
34 # - cut_pos: Offset from match position where the enzyme cuts.
35 def initialize(seq, pattern, cut_pos)
37 @pattern = disambiguate(pattern)
42 # Method to get the next digestion product from a sequence.
44 @seq.seq.upcase.scan @pattern do
45 pos = $`.length + @cut_pos - 1
47 if pos >= 0 and pos < @seq.length - 2
48 seq = Seq.new("#{@seq.seq_name}[#{@offset}-#{pos}]", @seq.seq[@offset .. pos], @seq.type)
58 elsif @offset > @seq.length
62 seq = Seq.new("#{@seq.seq_name}[#{@offset}-#{@seq.length - 1}]", @seq.seq[@offset .. @seq.length], @seq.type)
71 # Method that returns a regexp object with a restriction
72 # enzyme pattern with ambiguity codes substituted to the
74 def disambiguate(pattern)
96 pattern.upcase.each_char do |char|
97 if ambiguity.has_key? char
98 new_pattern << ambiguity[char]
100 raise DigestError, "Could not disambiguate residue: #{char}"
104 Regexp.new(new_pattern)