-
Notifications
You must be signed in to change notification settings - Fork 8
/
SmithWaterman.cpp
184 lines (160 loc) · 4.14 KB
/
SmithWaterman.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
//////////////////////////////////////////////
// Simple Ends-Free Smith-Waterman Algorithm
//
// You will be prompted for input sequences
// Penalties and match scores are hard-coded
//
// Program does not perform multiple tracebacks if
// it finds several alignments with the same score
//
// By Nikhil Gopal
// Similar implementation here: https://wiki.uni-koeln.de/biologicalphysics/index.php/Implementation_of_the_Smith-Waterman_local_alignment_algorithm
//////////////////////////////////////////////
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <cmath>
#include <sys/time.h>
using namespace std;
int ind;
double penalty=-4;
double similarityScore(char a, char b);
double findMax(double array[], int length);
double similarityScore(char a, char b)
{
double result;
if(a==b)
{
result=1;
}
else
{
result=penalty;
}
return result;
}
double findMax(double array[], int length)
{
double max = array[0];
ind = 0;
for(int i=1; i<length; i++)
{
if(array[i] > max)
{
max = array[i];
ind=i;
}
}
return max;
}
int main()
{
string seqA; // sequence A
string seqB; // sequence B
cout << "Sequence A" << endl;
cin >> seqA;
cout << "Sequence B" << endl;
cin >> seqB;
cout << "You typed in " << endl << seqA << endl << seqB << endl;
// initialize some variables
int lengthSeqA = seqA.length();
int lengthSeqB = seqB.length();
// initialize matrix
double matrix[lengthSeqA+1][lengthSeqB+1];
for(int i=0;i<=lengthSeqA;i++)
{
for(int j=0;j<=lengthSeqB;j++)
{
matrix[i][j]=0;
}
}
double traceback[4];
int I_i[lengthSeqA+1][lengthSeqB+1];
int I_j[lengthSeqA+1][lengthSeqB+1];
//start populating matrix
for (int i=1;i<=lengthSeqA;i++)
{
for(int j=0;j<=lengthSeqB;j++)
{
cout << i << " " << j << endl;
traceback[0] = matrix[i-1][j-1]+similarityScore(seqA[i-1],seqB[j-1]);
traceback[1] = matrix[i-1][j]+penalty;
traceback[2] = matrix[i][j-1]+penalty;
traceback[3] = 0;
matrix[i][j] = findMax(traceback,4);
switch(ind)
{
case 0:
I_i[i][j] = i-1;
I_j[i][j] = j-1;
break;
case 1:
I_i[i][j] = i-1;
I_j[i][j] = j;
break;
case 2:
I_i[i][j] = i;
I_j[i][j] = j-1;
break;
case 3:
I_i[i][j] = i;
I_j[i][j] = j;
break;
}
}
}
// print the scoring matrix to console
for(int i=1;i<lengthSeqA;i++)
{
for(int j=1;j<lengthSeqB;j++)
{
cout << matrix[i][j] << " ";
}
cout << endl;
}
// find the max score in the matrix
double matrix_max = 0;
int i_max=0, j_max=0;
for(int i=1;i<lengthSeqA;i++)
{
for(int j=1;j<lengthSeqB;j++)
{
if(matrix[i][j]>matrix_max)
{
matrix_max = matrix[i][j];
i_max=i;
j_max=j;
}
}
}
cout << "Max score in the matrix is " << matrix_max << endl;
// traceback
int current_i=i_max,current_j=j_max;
int next_i=I_i[current_i][current_j];
int next_j=I_j[current_i][current_j];
int tick=0;
char consensus_a[lengthSeqA+lengthSeqB+2],consensus_b[lengthSeqA+lengthSeqB+2];
while(((current_i!=next_i) || (current_j!=next_j)) && (next_j!=0) && (next_i!=0))
{
if(next_i==current_i) consensus_a[tick] = '-'; // deletion in A
else consensus_a[tick] = seqA[current_i-1]; // match/mismatch in A
if(next_j==current_j) consensus_b[tick] = '-'; // deletion in B
else consensus_b[tick] = seqB[current_j-1]; // match/mismatch in B
current_i = next_i;
current_j = next_j;
next_i = I_i[current_i][current_j];
next_j = I_j[current_i][current_j];
tick++;
}
//print the consensus sequences
cout<<endl<<" "<<endl;
cout<<"Alignment:"<<endl<<endl;
for(int i=0;i<lengthSeqA;i++){cout<<seqA[i];}; cout<<" and"<<endl;
for(int i=0;i<lengthSeqB;i++){cout<<seqB[i];}; cout<<endl<<endl;
for(int i=tick-1;i>=0;i--) cout<<consensus_a[i];
cout<<endl;
for(int j=tick-1;j>=0;j--) cout<<consensus_b[j];
cout<<endl;
return 0;
}