-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathneedleman-wunsch.js
150 lines (117 loc) · 3.42 KB
/
needleman-wunsch.js
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
/**
* Created by mar on 18-2-16.
*/
function initialize(seq1, seq2) {
seq1 = seq1.toUpperCase();
seq2 = seq2.toUpperCase();
var table = makeTable(seq1, seq2);
fillOuterTable(table, seq1, seq2);
return table;
}
function fill(table, seq1, seq2) {
fillInnerTable(table, seq1, seq2);
return table;
}
function traceback(table, seq1, seq2) {
var alignment1 = "";
var alignment2 = "";
var matches = "";
var i = table.matrix.length-1;
var j = table.matrix[0].length-1;
var selectedCells = [[i,j]];
while (i > 0 || j > 0) {
var bestNeighbor = maximumNeighbor(table, i, j);
selectedCells.push(bestNeighbor);
if (bestNeighbor[0] === i-1 && bestNeighbor[1] === j-1) {
alignment1 = seq1[i-1] + alignment1;
alignment2 = seq2[j-1] + alignment2;
i--;
j--;
}
else if (bestNeighbor[0] === i-1 && bestNeighbor[1] === j) {
alignment1 = seq1[i-1] + alignment1;
alignment2 = "-" + alignment2;
i--;
}
else {
alignment1 = "-" + alignment1;
alignment2 = seq2[j-1] + alignment2;
j--;
}
if (alignment1[0] === alignment2[0])
matches = "|" + matches;
else
matches = " " + matches;
}
return [alignment1, alignment2, matches, selectedCells];
}
function sequenceCharacter(seq, i) {
if (seq[i] === undefined)
return "_";
else
return seq[i];
}
function maximumNeighbor(table, i, j) {
var values = [];
var cells = [];
if (i > 0 && j > 0) {
cells.push([i-1, j-1]);
values.push(table.matrix[i-1][j-1]);
}
if (i > 0) {
cells.push([i-1, j]);
values.push(table.matrix[i-1][j]);
}
if (j > 0) {
cells.push([i, j-1]);
values.push(table.matrix[i][j-1]);
}
var max = values[0];
var best = cells[0];
for (var n = 0; n < values.length; n++) {
if (values[n] > max) {
max = values[n];
best = cells[n];
}
}
return best;
}
function scoringFunction(a, b) {
if (a === undefined || b === undefined) {
console.log("a or b undefined");
return -1;
}
else if (a.length === 0 || b.length === 0)
return -1;
else if (a !== b)
return -1;
else
return 1;
}
// stating from the top left, compute each entry using the recursive relation
function fillInnerTable(table, seq1, seq2) {
for (var i = 1; i < table.rows; i++) {
for (var j = 1; j < table.cols; j++) {
table.matrix[i][j] = recursiveRelation(table, seq1, seq2, i, j);
}
}
}
function recursiveRelation(table, seq1, seq2, i, j) {
return Math.max(table.matrix[i-1][j-1] + scoringFunction(seq1[i-1], seq2[j-1]), table.matrix[i-1][j] + scoringFunction(seq1[i-1], ""), table.matrix[i][j-1] + scoringFunction("", seq2[j-1]));
}
// fill table entries (m:1) and (1:n)
function fillOuterTable(table, seq1, seq2) {
for (var i = 0; i < table.rows; i++) {
for (var k = 0; k < i; k++) {
table.matrix[i][0] += scoringFunction(seq1[k], "");
}
}
for (var j = 0; j < table.cols; j++) {
for (var k = 0; k < j; k++) {
table.matrix[0][j] += scoringFunction("", seq2[k]);
}
}
}
function makeTable(seq1, seq2) {
return new Matrix(seq1.length+1, seq2.length+1);
}