source: trunk/yat/utility/Aligner.cc @ 3194

Last change on this file since 3194 was 3194, checked in by Peter, 9 years ago

add missing file. see r3173

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.2 KB
Line 
1// $Id: Aligner.cc 3194 2014-04-22 09:24:56Z peter $
2
3/*
4  Copyright (C) 2012 Peter Johansson
5
6  This file is part of the yat library, http://dev.thep.lu.se/yat
7
8  The yat library is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License as
10  published by the Free Software Foundation; either version 3 of the
11  License, or (at your option) any later version.
12
13  The yat library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with yat. If not, see <http://www.gnu.org/licenses/>.
20*/
21
22#include <config.h>
23
24#include "Aligner.h"
25
26#include "Matrix.h"
27
28#include <cassert>
29#include <limits>
30
31namespace theplu {
32namespace yat {
33namespace utility {
34
35  Aligner::Aligner(double gap, double open_gap)
36    : vertical_gap_(gap), open_vertical_gap_(open_gap), horizon_gap_(gap),
37      open_horizon_gap_(open_gap)
38  {
39  }
40
41
42  Aligner::Aligner(double vertical_gap, double open_vertical_gap,
43                   double horizon_gap, double open_horizon_gap)
44    : vertical_gap_(vertical_gap), open_vertical_gap_(open_vertical_gap),
45      horizon_gap_(horizon_gap), open_horizon_gap_(open_horizon_gap)
46  {
47  }
48
49
50  double Aligner::needleman_wunsch(const Matrix& s)
51  {
52    Matrix m(s.rows()+1,s.columns()+1, -std::numeric_limits<double>::max());
53    m(0, 0) = 0.0;
54    // Init upper and left border of matrix
55    for (size_t i=1; i<m.rows(); i++)
56      m(i,0) = -i * vertical_gap_ - open_vertical_gap_;
57    for (size_t i=1; i<m.columns(); i++)
58      m(0,i) = -i * horizon_gap_ - open_horizon_gap_;
59    (*this)(s, m);
60    // return lower right corner element
61    return m(s.rows(), s.columns());
62  }
63
64
65  double Aligner::smith_waterman(const Matrix& s)
66  {
67    Matrix m(s.rows()+1,s.columns()+1);
68    (*this)(s, m);
69    return max(m);
70  }
71
72
73  void Aligner::operator()(const Matrix& score, Matrix& x)
74  {
75    assert(x.columns() == score.columns()+1);
76    assert(x.rows() == score.rows()+1);
77
78    columns_ = x.columns();
79    alignment_.resize(columns_*x.rows(), none);
80
81    for (size_t i=1; i<x.rows(); ++i)
82      for (size_t j=1; j<x.columns(); ++j) {
83        // match (or mismatch)
84        if (x(i-1, j-1) + score(i-1, j-1) > x(i,j)) {
85          x(i,j) = x(i-1, j-1) + score(i-1, j-1);
86          directions(i, j) = diagonal;
87        }
88
89        if (directions(i, j-1) == right) {
90          if (x(i, j-1)-horizon_gap_ > x(i,j)) {
91            x(i,j) = x(i,j-1) - horizon_gap_;
92            directions(i,j) = right;
93          }
94        }
95        else if(x(i,j-1)-horizon_gap_-open_horizon_gap_ > x(i,j)) {
96          x(i,j) = x(i,j-1) - horizon_gap_ - open_horizon_gap_;
97          directions(i,j) = right;
98        }
99
100        if (directions(i-1,j) == down) {
101          if (x(i-1,j) - vertical_gap_ > x(i,j)) {
102            x(i,j) = x(i-1,j) - vertical_gap_;
103            directions(i,j) = down;
104          }
105        }
106        else if (x(i-1,j) - vertical_gap_ - open_vertical_gap_ > x(i,j)) {
107          x(i,j) = x(i-1,j) - vertical_gap_ - open_vertical_gap_;
108          directions(i,j) = down;
109        }
110      }
111  }
112
113
114  const Aligner::direction& Aligner::alignment(size_t i, size_t j) const
115  {
116    return alignment_[i*columns_ + j];
117  }
118
119
120  Aligner::direction& Aligner::directions(size_t i, size_t j)
121  {
122    return alignment_[i*columns_ + j];
123  }
124
125}}} // of namespace utility, yat, and theplu
Note: See TracBrowser for help on using the repository browser.