1
+ #include < iostream>
2
+ #include < string>
3
+
4
+ using namespace std ;
5
+ int writeToFile (string sequence1Name, string sequence1, string sequence2Name, string sequence2, int score, string fileName);
6
+
7
+ int needleman_wunsch (string sequence1Name, string sequence1, string sequence2Name, string sequence2,
8
+ int gapopen, int gapext, int matchScore, int mismatchPenalty) {
9
+
10
+ int text1Length = sequence1.size ();
11
+ int text2Length = sequence2.size ();
12
+
13
+ int table[text1Length + 1 ][text2Length + 1 ];
14
+ char directionTable[text1Length + 1 ][text2Length + 1 ];
15
+
16
+ // Basic values in the tables.
17
+ table[0 ][0 ] = 0 ;
18
+ table[1 ][0 ] = gapopen + gapext;
19
+ table[0 ][1 ] = gapopen + gapext;
20
+ directionTable[1 ][0 ] = ' u' ;
21
+ directionTable[0 ][1 ] = ' l' ;
22
+
23
+ // Fill in the 0th columns
24
+ for (int i = 2 ; i < text1Length + 1 ; i++){
25
+ table[i][0 ] = table[i-1 ][0 ] + gapext;
26
+ directionTable[i][0 ] = ' u' ;
27
+ }
28
+
29
+ // Fill in the 0th rows
30
+ for (int j = 2 ; j < text2Length + 1 ; j++){
31
+ table[0 ][j] = table[0 ][j - 1 ] + gapext;
32
+ directionTable[0 ][j] = ' l' ;
33
+ }
34
+
35
+ // Fill in the tables
36
+ for (int i = 1 ; i < text1Length + 1 ; i++){
37
+ for (int j = 1 ; j < text2Length + 1 ; j++){
38
+ int diagonalExtra = sequence1.at (i - 1 ) == sequence2.at (j - 1 ) ? matchScore : mismatchPenalty;
39
+ int leftExtra = directionTable[i][j-1 ] == ' l' ? (gapext) : (gapext + gapopen);
40
+ int upExtra = directionTable[i-1 ][j] == ' u' ? (gapext) : (gapext + gapopen);
41
+
42
+ if (table[i-1 ][j-1 ] + diagonalExtra >= table[i][j-1 ] + leftExtra && table[i-1 ][j-1 ] + diagonalExtra >= table[i-1 ][j] + upExtra){
43
+ table[i][j] = table[i-1 ][j-1 ] + diagonalExtra;
44
+ directionTable[i][j] = ' d' ;
45
+ } else if (table[i][j-1 ] + leftExtra >= table[i-1 ][j-1 ] + diagonalExtra && table[i][j-1 ] + leftExtra >= table[i-1 ][j] + upExtra){
46
+ table[i][j] = table[i][j-1 ] + leftExtra;
47
+ directionTable[i][j] = ' l' ;
48
+ } else {
49
+ table[i][j] = table[i-1 ][j] + upExtra;
50
+ directionTable[i][j] = ' u' ;
51
+ }
52
+ }
53
+ }
54
+
55
+ // Backtracking
56
+ string matchedSequence1;
57
+ string matchedSequence2;
58
+ int i = text1Length, j = text2Length;
59
+ while (i >= 1 || j >= 1 ){
60
+ if (directionTable[i][j] == ' d' ){
61
+ matchedSequence1.insert (0 , 1 , sequence1.at (i - 1 ));
62
+ matchedSequence2.insert (0 , 1 , sequence2.at (j - 1 ));
63
+ i--; j--;
64
+ } else if (directionTable[i][j] == ' l' ){
65
+ matchedSequence1.insert (0 , 1 , ' -' );
66
+ matchedSequence2.insert (0 , 1 , sequence2.at (j - 1 ));
67
+ j--;
68
+ } else if (directionTable[i][j] == ' u' ){
69
+ matchedSequence1.insert (0 , 1 , sequence1.at (i - 1 ));
70
+ matchedSequence2.insert (0 , 1 , ' -' );
71
+ i--;
72
+ } else {
73
+ cout << " Error while backtracking." << endl;
74
+ return -1 ;
75
+ }
76
+ }
77
+
78
+ /*
79
+ // Print tables to console
80
+ for(int i = 0; i < text1Length + 1; i++){
81
+ for(int j = 0; j < text2Length + 1; j++)
82
+ printf("%*c",4,directionTable[i][j]);
83
+ printf("\n");
84
+ }
85
+ printf("\n");
86
+ for(int i = 0; i < text1Length + 1; i++){
87
+ for(int j = 0; j < text2Length + 1; j++)
88
+ printf("%*d",4,table[i][j]);
89
+ printf("\n");
90
+ }
91
+ */
92
+
93
+ // Print results to file
94
+ if (gapopen == 0 )
95
+ writeToFile (sequence1Name, matchedSequence1, sequence2Name, matchedSequence2, table[text1Length][text2Length], " global-naiveGap.aln" );
96
+ else
97
+ writeToFile (sequence1Name, matchedSequence1, sequence2Name, matchedSequence2, table[text1Length][text2Length], " global-affineGap.aln" );
98
+
99
+ return 0 ;
100
+ }
0 commit comments