-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLevenshtein.cpp
More file actions
60 lines (55 loc) · 1.7 KB
/
Copy pathLevenshtein.cpp
File metadata and controls
60 lines (55 loc) · 1.7 KB
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
using namespace std;
// Levenshtein edit distance function modified to break calculation when SNP limit met
// Was slowly morphed from smith waterman to needleman wunsch to levenshtein to maximize the alignment speed
int levenshtein(char *SeqA, int sizeA, char *SeqB, int sizeB, int maxSNP)
{
unsigned int left, up, diag;
int i, j;
int gap = 1, mismatch = 1, match = 0;
unsigned int DScore[sizeA+1][sizeB+1];
for (i=0; i<sizeA+1; i++) {DScore[i][0] = i*gap;}
for (j=0; j<sizeB+1; j++) {DScore[0][j] = j*gap;}
for (i=1; i <= sizeA; i++)
{
for (j=1; j <= sizeB; j++)
{
left = DScore[i][j-1] + gap;
up = DScore[i-1][j] + gap;
if (SeqA[i-1] == SeqB[j-1]){
diag = DScore[i-1][j-1] + match;
} else {
diag = DScore[i-1][j-1] + mismatch;
}
if (left > up)
DScore[i][j] = up;
else
DScore[i][j] = left;
if (DScore[i][j] >= diag)
DScore[i][j] = diag;
if (i==j && DScore[i][j] > maxSNP)
return DScore[i][j];
}
}
return DScore[sizeA][sizeB];
}
// The reverse complement funtion for the genome since DNa is double stranded
string rev_comp(string genome){
string rev_comp;
rev_comp.reserve(genome.length());
long i;
for(i = genome.length()-1; i >= 0; i--){
if (genome[i] == 'A'){
rev_comp += "T";
}
else if (genome[i] == 'C'){
rev_comp += "G";
}
else if (genome[i] == 'G'){
rev_comp += "C";
}
else{
rev_comp += "A";
}
}
return rev_comp;
}