1 # Copyright (C) 2007-2013 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 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
26 require 'maasha/math_aux'
28 # Class for describing a match between two sequences q and s.
30 attr_accessor :q_beg, :s_beg, :length, :score
32 def initialize(q_beg, s_beg, length)
47 # Method to expand a match as far as possible to the left and right
48 # within a given search space.
49 def expand(q_seq, s_seq, q_space_beg, s_space_beg, q_space_end, s_space_end)
50 length = expand_left_C(q_seq, s_seq, @q_beg, @s_beg, q_space_beg, s_space_beg)
55 length = expand_right_C(q_seq, s_seq, q_end, s_end, q_space_end, s_space_end)
61 def redundant?(matches)
62 # Speed-up with binary search
65 if Math.dist_point2line(self.q_beg, self.s_beg, m.q_beg, m.s_beg, m.q_end, m.s_end) == 0
74 s = "q: #{@q_beg}, s: #{@s_beg}, l: #{@length}, s: #{@score}"
75 s << " #{seq[@q_beg .. q_end]}" if seq
83 # Method to expand a match as far as possible to the left within a given
95 unsigned char *q_seq = (unsigned char *) StringValuePtr(_q_seq);
96 unsigned char *s_seq = (unsigned char *) StringValuePtr(_s_seq);
97 unsigned int q_beg = NUM2UINT(_q_beg);
98 unsigned int s_beg = NUM2UINT(_s_beg);
99 unsigned int q_space_beg = NUM2UINT(_q_space_beg);
100 unsigned int s_space_beg = NUM2UINT(_s_space_beg);
102 unsigned int len = 0;
104 while (q_beg > q_space_beg && s_beg > s_space_beg && q_seq[q_beg - 1] == s_seq[s_beg - 1])
111 return UINT2NUM(len);
116 VALUE expand_right_C(
125 unsigned char *q_seq = (unsigned char *) StringValuePtr(_q_seq);
126 unsigned char *s_seq = (unsigned char *) StringValuePtr(_s_seq);
127 unsigned int q_end = NUM2UINT(_q_end);
128 unsigned int s_end = NUM2UINT(_s_end);
129 unsigned int q_space_end = NUM2UINT(_q_space_end);
130 unsigned int s_space_end = NUM2UINT(_s_space_end);
132 unsigned int len = 0;
134 while (q_end + 1 <= q_space_end && s_end + 1 <= s_space_end && q_seq[q_end + 1] == s_seq[s_end + 1])
141 return UINT2NUM(len);