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