## KusanoNEU的博客

No day but today.

# Sequence Alignment

</pre><p class="p1"><pre name="code" class="cpp">#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

// Edit penalty:
// gap penalty: theta
// mismatch penalty: alpha
int optimalAlignment(string s1, string s2, int theta, int alpha, vector<string> &align)
{
int m = s1.length();
int n = s2.length();
vector<vector<int>> M(m + 1, vector<int>(n + 1, 0));
for (int i = 0; i <= m; ++i)
M[i][0] = i * theta;
for (int j = 0; j <= n; ++j)
M[0][j] = j * theta;

for (int i = 1; i <= m; ++i)
{
for (int j = 1; j <= n; ++j)
{
int cost = 0; int kind = 1;
if (s1[i - 1] == s2[j - 1])
cost = M[i - 1][j - 1];
else
cost = M[i - 1][j - 1] + alpha;
cost = min(cost, theta + M[i - 1][j]);
cost = min(cost, theta + M[i][j - 1]);
M[i][j] = cost;

}
}

int i = m, j = n;
// backtracking to get the solution
while (i >= 1 || j >= 1)
{
if ((i > 0 && M[i][j] == theta + M[i - 1][j]) || j == 0)
{
align[0].push_back(s1[i - 1]);
align[1].push_back('-');
--i;
}
else if ((j > 0 && M[i][j] == theta + M[i][j - 1]) || i == 0)
{
align[0].push_back('-');
align[1].push_back(s2[j - 1]);
--j;
}
else if (M[i][j] == M[i - 1][j - 1] + alpha || M[i][j] == M[i - 1][j - 1] )
{
align[0].push_back(s1[i - 1]);
align[1].push_back(s2[j - 1]);
--i, --j;
}
}
reverse(align[0].begin(), align[0].end());
reverse(align[1].begin(), align[1].end());

return M[m][n];
}

// x   y  cost
// ------------
// A   T   1
// A   A   0
// C   -   2
// A   A   0
// G   G   0
// T   G   1
// T   T   0
// A   -   2
// C   C   0
// C   A   1
//        ---
//		   7

int main()
{
string x = "";
string y = "TAAGGTCA";
string x1 = "CTACCG";
string y1 = "TACATG";
vector<string> ret;
ret.resize(2);
cout << "s1: " << x << endl;
cout << "s2: " << y << endl;
cout << "The minimum cost of alignment: " << optimalAlignment(x, y, 2, 1, ret) << endl;
cout << ret[0] << endl;
cout << ret[1] << endl;
ret.clear();
ret.resize(2);
cout << "s1: " << x1 << endl;
cout << "s2: " << y1 << endl;
cout << "The minimum cost of alignment: " << optimalAlignment(x1, y1, 2, 1, ret) << endl;
cout << ret[0] << endl;
cout << ret[1] << endl;

return 0;
}

Reference:

http://www.cs.princeton.edu/courses/archive/fall14/cos126/assignments/sequence.html

#### Sequence alignment

2017-02-20 16:54:14

#### sequence alignment

2017-10-20 06:01:48

#### 菜鸟学算法——动态规划（一）

2016-10-10 20:53:49

#### DNA Sequence Alignment Using Dynamic Programming

2013年05月29日 527KB 下载

#### Kalign – an accurate and fast multiple sequence alignment algorithm

2011年04月15日 266KB 下载

#### UVA 1593 Alignment of Code

2015-08-08 10:44:58

#### 全局子序列比对 Global Sequence Alignment

2014年08月22日 3KB 下载

#### bowtie a Next generation sequence alignment software for windows

2011年01月14日 12.01MB 下载

#### Sequence Modeling With CTC

2017-12-11 00:22:58

#### mega6.06说明书

2017年12月27日 7.9MB 下载