1 # Copyright (C) 2007-2012 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 # Extending Math module with a couple of useful methods.
27 # Method for calculating the distance between a point and a line.
28 def self.dist_point2line(px, py, x1, y1, x2, y2)
29 a = (y2.to_f - y1) / (x2.to_f - x1)
33 (a * px + b - py ).abs / Math.sqrt(a ** 2 + 1 )
36 # Method for calculating the distance between two points.
37 def self.dist_point2point(x1, y1, x2, y2)
38 Math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
42 # Module with stuff to create a pairwise aligment.
44 # Class for creating a pairwise alignment.
46 # Class method to create a pairwise alignment of two given Seq objects.
47 def self.align(q_entry, s_entry)
48 self.new(q_entry, s_entry)
51 # Method to inialize a pairwise alignment given two Seq objects.
52 def initialize(q_entry, s_entry)
57 @q_entry.seq.downcase!
58 @s_entry.seq.downcase!
60 align_recurse(@q_entry.seq, @s_entry.seq, 0, 0, @q_entry.length - 1, @s_entry.length - 1, 16, [])
67 # Method that creates an alignment by chaining matches, which are
68 # subsequences shared between two sequences. This recursive method
69 # functions by considering only matches within a given search space. If no
70 # matches are given these will be located and matches will be included
71 # depending on a calculated score. New search spaces spanning the spaces
72 # between the best scoring matches and the search space boundaries will be
73 # cast and recursed into.
74 def align_recurse(q_seq, s_seq, q_min, s_min, q_max, s_max, kmer, matches)
75 matches = matches_select_by_space(matches, q_min, s_min, q_max, s_max)
77 while (matches.size == 0 and kmer > 0)
78 matches = matches_find(q_seq, s_seq, q_min, s_min, q_max, s_max, kmer)
82 matches = matches_select_by_score(matches, q_min, s_min, q_max, s_max)
84 if best_match = matches.pop
85 @matches << best_match
89 l_q_max = best_match.q_beg - 1
90 l_s_max = best_match.s_beg - 1
92 r_q_min = best_match.q_end + 1
93 r_s_min = best_match.s_end + 1
97 if l_q_max - l_q_min > 0 and l_s_max - l_s_min > 0
98 align_recurse(q_seq, s_seq, l_q_min, l_s_min, l_q_max, l_s_max, kmer, matches)
101 if r_q_max - r_q_min > 0 and r_s_max - r_s_min > 0
102 align_recurse(q_seq, s_seq, r_q_min, r_s_min, r_q_max, r_s_max, kmer, matches)
107 # Method to select matches that lies within the search space.
108 def matches_select_by_space(matches, q_min, s_min, q_max, s_max)
109 new_matches = matches.select do |match|
110 match.q_beg >= q_min and
111 match.s_beg >= s_min and
112 match.q_end <= q_max and
119 # Method to select the best scoring matches and return these sorted
120 # according to score.
121 def matches_select_by_score(matches, q_min, s_min, q_max, s_max)
124 matches.each do |match|
125 score_length = match_score_by_length(match)
126 score_diag = match_score_by_diagonal_dist(match, q_min, s_min, q_max, s_max)
128 match.score = score_length - score_diag
130 new_matches << match if match.score > 0
133 new_matches.sort_by! { |match| match.score }
138 # Method to calculate score based on match length.
139 def match_score_by_length(match)
143 # Method to calculate score based on the distance to the closest
144 # diagonal. The smaller the distance the better the score.
145 def match_score_by_diagonal_dist(match, q_min, s_min, q_max, s_max)
146 q_dim = q_max - q_min + 1
147 s_dim = s_max - s_min + 1
149 if q_dim >= s_dim # s_dim is the narrow end
150 beg_dist = Math.dist_point2line(match.q_beg, match.s_beg, q_min, s_min, q_min + s_dim, s_min + s_dim)
151 end_dist = Math.dist_point2line(match.q_beg, match.s_beg, q_max - s_dim, s_max - s_dim, q_max, s_max)
153 beg_dist = Math.dist_point2line(match.q_beg, match.s_beg, q_min, s_min, q_min + q_dim, s_min + q_dim)
154 end_dist = Math.dist_point2line(match.q_beg, match.s_beg, q_max - q_dim, s_max - q_dim, q_max, s_max)
157 min_dist = (beg_dist < end_dist) ? beg_dist : end_dist
161 # Method that finds all maximally expanded non-redundant matches shared
162 # between two sequences inside a given search space.
163 def matches_find(q_seq, s_seq, q_min, s_min, q_max, s_max, kmer)
165 redundant = Hash.new { |h, k| h[k] = [] }
167 s_index = index_seq(s_seq, s_min, s_max, kmer)
171 while q_pos <= q_max - kmer + 1
172 q_oligo = q_seq[q_pos ... q_pos + kmer]
174 s_index[q_oligo].each do |s_pos|
175 match = Match.new(q_pos, s_pos, kmer)
177 unless match_redundant?(redundant, match)
178 match_expand(match, q_seq, s_seq, q_min, s_min, q_max, s_max)
181 match_redundant_add(redundant, match)
191 # Method that indexes a seuquence within a given interval such that the
192 # index contains all oligos of a given kmer size and the positions where
193 # this oligo was located.
194 def index_seq(seq, min, max, kmer)
195 index_hash = Hash.new { |h, k| h[k] = [] }
199 while pos <= max - kmer + 1
200 oligo = seq[pos ... pos + kmer]
201 index_hash[oligo] << pos
209 # Method to check if a match is redundant.
210 def match_redundant?(redundant, match)
211 redundant[match.q_beg].each do |s_interval|
212 if s_interval.include? match.s_beg and s_interval.include? match.s_end
220 # Method that adds a match to the redundancy index.
221 def match_redundant_add(redundant, match)
222 (match.q_beg .. match.q_end).each do |q|
223 redundant[q] << (match.s_beg .. match.s_end)
227 # Method that expands a match as far as possible to the left and right.
228 def match_expand(match, q_seq, s_seq, q_min, s_min, q_max, s_max)
229 match_expand_left(match, q_seq, s_seq, q_min, s_min)
230 match_expand_right(match, q_seq, s_seq, q_max, s_max)
235 # Method that expands a match as far as possible to the left.
236 def match_expand_left(match, q_seq, s_seq, q_min, s_min)
237 while match.q_beg > q_min and
238 match.s_beg > s_min and
239 q_seq[match.q_beg - 1] == s_seq[match.s_beg - 1]
248 # Method that expands a match as far as possible to the right.
249 def match_expand_right(match, q_seq, s_seq, q_max, s_max)
250 while match.q_end < q_max and
251 match.s_end < s_max and
252 q_seq[match.q_end + 1] == s_seq[match.s_end + 1]
259 # Method for debugging purposes that upcase matching sequence while non-matches
260 # sequence is kept in lower case.
262 @matches.each do |match|
263 @q_entry.seq[match.q_beg .. match.q_end] = @q_entry.seq[match.q_beg .. match.q_end].upcase
264 @s_entry.seq[match.s_beg .. match.s_end] = @s_entry.seq[match.s_beg .. match.s_end].upcase
268 # Method that insert gaps in sequences based on a list of matches and thus
269 # creating an alignment.
270 # TODO check boundaries!
272 @matches.sort_by! { |m| m.q_beg }
277 @matches.each do |match|
278 diff = (q_gaps + match.q_beg) - (s_gaps + match.s_beg)
280 #pp "q_gaps #{q_gaps} s_gaps #{s_gaps} diff #{diff}"
283 @q_entry.seq.insert(match.q_beg + q_gaps, "-" * diff.abs)
286 @s_entry.seq.insert(match.s_beg + s_gaps, "-" * diff.abs)
291 diff = @q_entry.length - @s_entry.length
294 @q_entry.seq << ("-" * diff.abs)
296 @s_entry.seq << ("-" * diff.abs)
301 # Class for containing a match between two sequences q and s.
303 attr_accessor :q_beg, :s_beg, :length, :score
305 def initialize(q_beg, s_beg, length, score = 0.0)
321 s = "q: #{@q_beg} #{q_end} s: #{@s_beg} #{s_end} l: #{@length} s: #{@score}"
322 s << " seq: #{seq[@q_beg .. q_end]}" if seq