-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathneedleman_wunsch.cpp
100 lines (87 loc) · 3.17 KB
/
needleman_wunsch.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <iostream>
#include <string>
using namespace std;
int writeToFile(string sequence1Name, string sequence1, string sequence2Name, string sequence2, int score, string fileName);
int needleman_wunsch(string sequence1Name, string sequence1, string sequence2Name, string sequence2,
int gapopen, int gapext, int matchScore, int mismatchPenalty) {
int text1Length = sequence1.size();
int text2Length = sequence2.size();
int table[text1Length + 1][text2Length + 1];
char directionTable[text1Length + 1][text2Length + 1];
// Basic values in the tables.
table[0][0] = 0;
table[1][0] = gapopen + gapext;
table[0][1] = gapopen + gapext;
directionTable[1][0] = 'u';
directionTable[0][1] = 'l';
// Fill in the 0th columns
for(int i = 2; i < text1Length + 1; i++){
table[i][0] = table[i-1][0] + gapext;
directionTable[i][0] = 'u';
}
// Fill in the 0th rows
for(int j = 2; j < text2Length + 1; j++){
table[0][j] = table[0][j - 1] + gapext;
directionTable[0][j] = 'l';
}
// Fill in the tables
for(int i = 1; i < text1Length + 1; i++){
for(int j = 1; j < text2Length + 1; j++){
int diagonalExtra = sequence1.at(i - 1) == sequence2.at(j - 1) ? matchScore : mismatchPenalty;
int leftExtra = directionTable[i][j-1] == 'l' ? (gapext) : (gapext + gapopen);
int upExtra = directionTable[i-1][j] == 'u' ? (gapext) : (gapext + gapopen);
if(table[i-1][j-1] + diagonalExtra >= table[i][j-1] + leftExtra && table[i-1][j-1] + diagonalExtra >= table[i-1][j] + upExtra){
table[i][j] = table[i-1][j-1] + diagonalExtra;
directionTable[i][j] = 'd';
} else if(table[i][j-1] + leftExtra >= table[i-1][j-1] + diagonalExtra && table[i][j-1] + leftExtra >= table[i-1][j] + upExtra){
table[i][j] = table[i][j-1] + leftExtra;
directionTable[i][j] = 'l';
} else{
table[i][j] = table[i-1][j] + upExtra;
directionTable[i][j] = 'u';
}
}
}
// Backtracking
string matchedSequence1;
string matchedSequence2;
int i = text1Length, j = text2Length;
while(i >= 1 || j >= 1){
if(directionTable[i][j] == 'd'){
matchedSequence1.insert (0, 1, sequence1.at(i - 1));
matchedSequence2.insert (0, 1, sequence2.at(j - 1));
i--; j--;
} else if(directionTable[i][j] == 'l'){
matchedSequence1.insert (0, 1, '-');
matchedSequence2.insert (0, 1, sequence2.at(j - 1));
j--;
} else if(directionTable[i][j] == 'u'){
matchedSequence1.insert (0, 1, sequence1.at(i - 1));
matchedSequence2.insert (0, 1, '-');
i--;
} else{
cout << "Error while backtracking." << endl;
return -1;
}
}
/*
// Print tables to console
for(int i = 0; i < text1Length + 1; i++){
for(int j = 0; j < text2Length + 1; j++)
printf("%*c",4,directionTable[i][j]);
printf("\n");
}
printf("\n");
for(int i = 0; i < text1Length + 1; i++){
for(int j = 0; j < text2Length + 1; j++)
printf("%*d",4,table[i][j]);
printf("\n");
}
*/
// Print results to file
if(gapopen == 0)
writeToFile(sequence1Name, matchedSequence1, sequence2Name, matchedSequence2, table[text1Length][text2Length], "global-naiveGap.aln");
else
writeToFile(sequence1Name, matchedSequence1, sequence2Name, matchedSequence2, table[text1Length][text2Length], "global-affineGap.aln");
return 0;
}