]> git.donarmstrong.com Git - biopieces.git/blob - code_ruby/lib/maasha/seq/homopolymer.rb
added missing files
[biopieces.git] / code_ruby / lib / maasha / seq / homopolymer.rb
1 # Copyright (C) 2007-2013 Martin A. Hansen.
2
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.
7
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.
12
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.
16
17 # http://www.gnu.org/copyleft/gpl.html
18
19 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
20
21 # This software is part of the Biopieces framework (www.biopieces.org).
22
23 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
24
25
26 module Homopolymer
27   # Error class for all exceptions to do with Homopolymer.
28   class HomopolymerError < StandardError; end
29
30   def each_homopolymer(min = 1)
31     list = []
32
33     self.seq.upcase.scan(/A{#{min},}|T{#{min},}|G{#{min},}|C{#{min},}|N{#{min},}/) do |match|
34       hp = Homopolymer.new(match, match.length, $`.length)
35
36       if block_given?
37         yield hp
38       else
39         list << hp
40       end
41     end
42
43     block_given? ? self : list
44   end
45
46   class Homopolymer
47     attr_reader :pattern, :length, :pos
48
49     def initialize(pattern, length, pos)
50       @pattern = pattern
51       @length  = length
52       @pos     = pos
53     end
54   end
55 end