]> git.donarmstrong.com Git - biopieces.git/blob - code_ruby/lib/maasha/align/match.rb
added align/match.rb
[biopieces.git] / code_ruby / lib / maasha / align / match.rb
1 # Copyright (C) 2007-2012 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 class Matches
26   def self.find(q_seq, s_seq, q_min, s_min, q_max, s_max, kmer)
27     m = self.new
28     m.matches_find(q_seq, s_seq, q_min, s_min, q_max, s_max, kmer)
29   end
30
31   # Method that finds all maximally expanded non-redundant matches shared
32   # between two sequences inside a given search space.
33   def matches_find(q_seq, s_seq, q_min, s_min, q_max, s_max, kmer)
34     matches   = []
35     redundant = Hash.new { |h, k| h[k] = [] }
36
37     s_index = index_seq(s_seq, s_min, s_max, kmer)
38
39     q_pos = q_min
40
41     while q_pos <= q_max - kmer + 1
42       q_oligo = q_seq[q_pos ... q_pos + kmer]
43
44       s_index[q_oligo].each do |s_pos|
45         match = Match.new(q_pos, s_pos, kmer)
46         unless match_redundant?(redundant, match)
47           match_expand(match, q_seq, s_seq, q_min, s_min, q_max, s_max)
48           matches << match
49
50           match_redundant_add(redundant, match)
51         end
52       end
53
54       q_pos += 1
55     end
56
57     matches
58   end
59
60   # Method that indexes a sequence within a given interval such that the
61   # index contains all oligos of a given kmer size and the positions where
62   # this oligo was located.
63   def index_seq(seq, min, max, kmer)
64     index_hash = Hash.new { |h, k| h[k] = [] }
65
66     pos = min
67
68     while pos <= max - kmer + 1
69       oligo = seq[pos ... pos + kmer]
70       index_hash[oligo] << pos
71
72       pos += 1
73     end
74
75     index_hash
76   end
77
78   # Method to check if a match is redundant.
79   def match_redundant?(redundant, match)
80     redundant[match.q_beg].each do |s_interval|
81       if s_interval.include? match.s_beg and s_interval.include? match.s_end
82         return true
83       end
84     end
85
86     false
87   end
88
89   # Method that adds a match to the redundancy index.
90   def match_redundant_add(redundant, match)
91     (match.q_beg .. match.q_end).each do |q|
92       redundant[q] << (match.s_beg .. match.s_end)
93     end
94   end
95
96   # Method that expands a match as far as possible to the left and right.
97   def match_expand(match, q_seq, s_seq, q_min, s_min, q_max, s_max)
98     match_expand_left(match, q_seq, s_seq, q_min, s_min, q_max, s_max)
99     match_expand_right(match, q_seq, s_seq, q_min, s_min, q_max, s_max)
100
101     match
102   end
103
104   # Method that expands a match as far as possible to the left.
105   def match_expand_left(match, q_seq, s_seq, q_min, s_min, q_max, s_max)
106     while match.q_beg > q_min and
107           match.s_beg > s_min and 
108           q_seq[match.q_beg - 1] == s_seq[match.s_beg - 1]
109       match.q_beg  -= 1
110       match.s_beg  -= 1
111       match.length += 1
112     end
113
114     match
115   end
116
117   # Method that expands a match as far as possible to the right.
118   def match_expand_right(match, q_seq, s_seq, q_min, s_min, q_max, s_max)
119     while match.q_end < q_max and
120           match.s_end < s_max and
121           q_seq[match.q_end + 1] == s_seq[match.s_end + 1]
122       match.length += 1
123     end
124
125     match
126   end
127
128   # Class for containing a match between two sequences q and s.
129   class Match
130     attr_accessor :q_beg, :s_beg, :length, :score
131
132     def initialize(q_beg, s_beg, length, score = 0.0)
133       @q_beg  = q_beg
134       @s_beg  = s_beg
135       @length = length
136       @score  = score
137     end
138
139     def q_end
140       @q_beg + @length - 1
141     end
142
143     def s_end
144       @s_beg + @length - 1
145     end
146
147     def to_s(seq = nil)
148       s = "q: #{@q_beg} #{q_end} s: #{@s_beg} #{s_end} l: #{@length} s: #{@score}"
149       s << " seq: #{seq[@q_beg .. q_end]}" if seq
150       s
151     end
152   end
153 end