1 | // $Id: Alignment.cc 831 2007-03-27 13:22:09Z peter $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2004 Jari Häkkinen |
---|
5 | Copyright (C) 2005 Peter Johansson |
---|
6 | Copyright (C) 2006 Jari Häkkinen |
---|
7 | |
---|
8 | This file is part of the yat library, http://lev.thep.lu.se/trac/yat |
---|
9 | |
---|
10 | The yat library is free software; you can redistribute it and/or |
---|
11 | modify it under the terms of the GNU General Public License as |
---|
12 | published by the Free Software Foundation; either version 2 of the |
---|
13 | License, or (at your option) any later version. |
---|
14 | |
---|
15 | The yat library is distributed in the hope that it will be useful, |
---|
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
18 | General Public License for more details. |
---|
19 | |
---|
20 | You should have received a copy of the GNU General Public License |
---|
21 | along with this program; if not, write to the Free Software |
---|
22 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
23 | 02111-1307, USA. |
---|
24 | */ |
---|
25 | |
---|
26 | #include "Alignment.h" |
---|
27 | #include "matrix.h" |
---|
28 | |
---|
29 | #include <utility> |
---|
30 | #include <vector> |
---|
31 | |
---|
32 | namespace theplu { |
---|
33 | namespace yat { |
---|
34 | namespace utility { |
---|
35 | |
---|
36 | double NeedlemanWunsch(const utility::matrix& s, |
---|
37 | std::vector<std::pair<size_t, size_t> >& path, |
---|
38 | const double gap) |
---|
39 | { |
---|
40 | utility::matrix m(s.rows()+1,s.columns()+1); |
---|
41 | // Init upper and left border of matrix |
---|
42 | for (size_t i=1; i<m.rows(); i++) |
---|
43 | m(i,0)=-i*gap; |
---|
44 | for (size_t i=1; i<m.columns(); i++) |
---|
45 | m(0,i)=-i*gap; |
---|
46 | // choice(i,j) tells us how we came to s(i,j). 1 is diagonal, 2 |
---|
47 | // vertical, and 3 horizontal, |
---|
48 | utility::matrix choice(m.rows(),m.columns()); |
---|
49 | |
---|
50 | // Calculating NeedlemanWunsch matrix |
---|
51 | for (size_t i=1; i<m.rows(); i++) |
---|
52 | for (size_t j=1; j<m.columns(); j++){ |
---|
53 | if (m(i-1,j-1) + s(i-1,j-1) > m(i-1,j)-gap && |
---|
54 | m(i-1,j-1) + s(i-1,j-1) > m(i,j-1)-gap){ |
---|
55 | m(i,j)=m(i-1,j-1) + s(i-1,j-1); |
---|
56 | choice(i,j)=1; |
---|
57 | } |
---|
58 | else if (m(i-1,j) > m(i,j-1)){ |
---|
59 | m(i,j)=m(i-1,j)-gap; |
---|
60 | choice(i,j)=2; |
---|
61 | } |
---|
62 | else{ |
---|
63 | m(i,j)=m(i,j-1)-gap; |
---|
64 | choice(i,j)=3; |
---|
65 | } |
---|
66 | } |
---|
67 | // Going backwards to find best path |
---|
68 | size_t i = m.rows()-1; |
---|
69 | size_t j= m.columns()-1; |
---|
70 | path.resize(0); |
---|
71 | while (i && j){ |
---|
72 | if (choice(i,j)==1){ |
---|
73 | i--; |
---|
74 | j--; |
---|
75 | path.push_back(std::make_pair(i,j)); |
---|
76 | } |
---|
77 | else if (choice(i,j)==2) |
---|
78 | i--; |
---|
79 | else |
---|
80 | j--; |
---|
81 | } |
---|
82 | |
---|
83 | return m(m.rows()-1,m.columns()-1); |
---|
84 | } |
---|
85 | |
---|
86 | }}} // of namespace utility, yat, and theplu |
---|