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
29 # Method to get the next digestion product from a sequence.
30 def each_digest(pattern, cut_pos)
31 pattern = disambiguate(pattern)
35 self.seq.upcase.scan pattern do
36 pos = $`.length + cut_pos
38 if pos >= 0 and pos < self.length - 2
39 subseq = self.subseq(offset, pos - offset)
40 subseq.seq_name = "#{self.seq_name}[#{offset}-#{pos - offset - 1}]"
42 block_given? ? (yield subseq) : (results << subseq)
48 if offset < 0 or offset > self.length
52 subseq = self.subseq(offset)
53 subseq.seq_name = "#{self.seq_name}[#{offset}-#{self.length - 1}]"
55 block_given? ? (yield subseq) : (results << subseq)
57 return results unless block_given?
62 # Method that returns a regexp object with a restriction
63 # enzyme pattern with ambiguity codes substituted to the
65 def disambiguate(pattern)
87 pattern.upcase.each_char do |char|
89 new_pattern << ambiguity[char]
91 raise DigestError, "Could not disambiguate residue: #{char}"
95 Regexp.new(new_pattern)