-
Notifications
You must be signed in to change notification settings - Fork 14
/
string_alignment.cpp
80 lines (70 loc) · 1.89 KB
/
string_alignment.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
#define MAX_LEN 20
char A[MAX_LEN] = "ACAATCC";
char B[MAX_LEN] = "AGCATGC";
int table[MAX_LEN][MAX_LEN]; //DP table
vector<char> needleman_wunsch(int table[MAX_LEN][MAX_LEN], int n, int m)
{
//init DP table (bottom-up)
//base case: insert/delete = -1 point
for (int i = 1; i <= n; ++i){table[i][0] = -1*i;}
for (int j = 1; j <= m; ++j){table[0][j] = -1*j;}
//recursion
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= m; ++j)
{
//match = 2 points, mismatch = -1 point
table[i][j] = table[i-1][j-1] + (A[i-1] == B[j-1] ? 2 : -1);
//insert / delete = - 1 point
table[i][j] = max(table[i][j], table[i-1][j] - 1); //delete
table[i][j] = max(table[i][j], table[i][j-1] - 1); //insert
}
}
//backtrace
int i = n, j = m;
vector<char> result;
while (i > 0 && j > 0)
{
if (A[i-1] == B[j-1])
{
result.push_back(A[i-1]);
--i; --j;
}
else if (table[i-1][j] > table[i][j-1])
{
--i;
}
else
{
--j;
}
}
reverse(result.begin(), result.end());
return result;
}
int main()
{
int n = (int)strlen(A), m = (int)strlen(B);
memset(table, 0, sizeof(table));
printf("String Alignment (Needleman-Wunsch)\n");
printf("1: %s\n", A); printf("2: %s\n", B);
vector<char> result;
result = needleman_wunsch(table, n, m);
printf("DP table:\n");
for (int i=0; i <= n; ++i)
{
for (int j=0; j <= m; ++j)
{
printf("%3d", table[i][j]);
}
printf("\n");
}
printf("Max alignment score: %d\n", table[n][m]);
for (auto it = result.begin(); it != result.end(); ++it){cout << *it << " ";}
printf("\n");
return 0;
}