-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathneedlemanWunsch.js
110 lines (100 loc) · 2.84 KB
/
needlemanWunsch.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
/*
* An adaption of the Needleman-Wunsch-Algorithm for arrays of objects
* adapted from the code of Shin Suzuki (https://gist.github.com/shinout/f19da7720d130f3925ac)
*
* Copyright (C) 2022 Nikolaus Altmann
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
* */
const UP = 1;
const LEFT = 2;
const UL = 4;
function nw(s1, s2) {
op = {};
const G = op.G || 2;
const P = op.P || 1;
const M = op.M || -1;
var mat = {};
var direc = {};
// initialization
for(var i=0; i<s1.length+1; i++) {
mat[i] = {0:0};
direc[i] = {0:[]};
for(var j=1; j<s2.length+1; j++) {
mat[i][j] = (i == 0)
? 0
: isEqual(s1[i-1].content,s2[j-1].content) ? P : M
direc[i][j] = [];
}
}
// calculate each value
for(var i=0; i<s1.length+1; i++) {
for(var j=0; j<s2.length+1; j++) {
var newval = (i == 0 || j == 0)
? -G * (i + j)
: Math.max(mat[i-1][j] - G, mat[i-1][j-1] + mat[i][j], mat[i][j-1] -G);
if (i > 0 && j > 0) {
if( newval == mat[i-1][j] - G) direc[i][j].push(UP);
if( newval == mat[i][j-1] - G) direc[i][j].push(LEFT);
if( newval == mat[i-1][j-1] + mat[i][j]) direc[i][j].push(UL);
}
else {
direc[i][j].push((j == 0) ? UP : LEFT);
}
mat[i][j] = newval;
}
}
// get result
var chars = [[],[]];
var I = s1.length;
var J = s2.length;
const max = Math.max(I, J);
while(I > 0 || J > 0) {
switch (direc[I][J][0]) {
case UP:
I--;
s1[I].nwType = 'UP';
chars[0].push(s1[I]);
chars[1].push({content : '-', nwType: 'UP'});
break;
case LEFT:
J--;
s2[J].nwType = 'LEFT';
chars[0].push({content : '-', nwType: 'LEFT'});
chars[1].push(s2[J]);
break;
case UL:
I--;
J--;
s1[I].nwType = 'UL';
s2[J].nwType = 'UL';
chars[0].push(s1[I]);
chars[1].push(s2[J]);
break;
default: break;
}
}
return [chars[0].reverse(),chars[1].reverse()]
}
function isEqual(a,b){
// This is trivial, but you could make your own:)
// For example:
// String Similarity
// Levenshtein Distance
if(a == b){
return 1.0
}else{
return 0.0
}
}
module.exports = nw;