-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathwaterman_smith_beyer.js
407 lines (339 loc) · 14.1 KB
/
waterman_smith_beyer.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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/*
University of Freiburg WS 2017/2018
Chair for Bioinformatics
Supervisor: Martin Raden
Author: Alexander Mattheis
*/
"use strict";
/**
* Defines tasks after page-loading.
*/
$(document).ready(function () {
if (loaded === ALGORITHMS.WATERMAN_SMITH_BEYER) { // to avoid self execution on a script import
watermanSmithBeyer.startWatermanSmithBeyer();
loaded = ALGORITHMS.NONE;
}
});
(function () { // namespace
// public methods
namespace("watermanSmithBeyer", startWatermanSmithBeyer, WatermanSmithBeyer);
// instances
var alignmentInstance;
var watermanSmithBeyerInstance;
// shared variables
var inputData = {}; // stores the input of the algorithm
var outputData = {}; // stores the output of the algorithm
/**
* Function managing objects.
*/
function startWatermanSmithBeyer() {
var subadditiveAlignmentInterface = new interfaces.subadditiveAlignmentInterface.SubadditiveAlignmentInterface();
subadditiveAlignmentInterface.startSubadditiveAlignmentAlgorithm(WatermanSmithBeyer, ALGORITHMS.WATERMAN_SMITH_BEYER);
}
/*---- ALGORITHM ----*/
/**
* Computes the optimal, global subadditive alignment.
* @constructor
* @see https://doi.org/10.1016/0001-8708(76)90202-4
*
* Waterman, Michael S., Temple F. Smith, and William A. Beyer.
* "Some biological sequence metrics."
* Advances in Mathematics 20.3 (1976): 367-387.
*/
function WatermanSmithBeyer() {
watermanSmithBeyerInstance = this;
// variables
this.type = ALGORITHMS.WATERMAN_SMITH_BEYER;
this.numberOfTracebacks = 0;
// instances
alignmentInstance = new bases.alignment.Alignment(this);
// public class methods
this.getInput = getInput;
this.setInput = setInput;
this.compute = compute;
this.gapFunction = gapFunction;
this.getNeighboured = getNeighboured;
this.getOutput = getOutput;
this.setIO = setIO;
this.getSuperclass = getSuperclass;
}
/**
* Returns the input data of the algorithm.
* @return {Object} - Contains all input data.
*/
function getInput() {
return inputData;
}
/**
* Sets the algorithm input for an appropriate algorithm
* which is using the inputViewmodel properties in its computations.
* @param inputViewmodel {Object} - The InputViewmodel of an appropriate algorithm.
*/
function setInput(inputViewmodel) {
alignmentInstance.setIO(inputData, {});
alignmentInstance.setSubadditiveAlignmentInput(inputViewmodel);
inputData.subadditiveFunction = inputViewmodel.subadditiveFunction();
}
/**
* Starts the computation.
*/
function compute() {
initializeMatrix();
computeMatrixAndScore();
computeTraceback();
createAlignments();
return [inputData, outputData];
}
/**
* Initializes and creates the matrices.
*/
function initializeMatrix() {
createMatrix();
initMatrix();
}
/**
* Creates the matrix without initializing them.
*/
function createMatrix() {
outputData.matrix = new Array(inputData.matrixHeight);
for (var i = 0; i < inputData.matrixHeight; i++)
outputData.matrix[i] = new Array(inputData.matrixWidth);
}
/**
* Initializes the matrix by distinguishing between three possible subadditive functions.
*/
function initMatrix() {
outputData.matrix[0][0] = 0;
for (var i = 1; i < inputData.matrixHeight; i++)
outputData.matrix[i][0] = gapFunction(i);
for (var j = 1; j < inputData.matrixWidth; j++)
outputData.matrix[0][j] = gapFunction(j);
}
/**
* The gap function used to compute values.
* @param k {number} - The integer for which the gap function-value should be computed.
* @return {number} - The gap costs.
*/
function gapFunction(k) {
switch (inputData.subadditiveFunction) {
case SUBADDITIVE_FUNCTIONS.AFFINE:
return inputData.baseCosts + inputData.enlargement * k;
case SUBADDITIVE_FUNCTIONS.LOGARITHMIC:
return inputData.baseCosts + inputData.enlargement * Math.log(k);
case SUBADDITIVE_FUNCTIONS.QUADRATIC:
return inputData.baseCosts + inputData.enlargement * Math.pow(k, 2);
}
}
/**
* Computes the matrix by using the recursion function and the score.
*/
function computeMatrixAndScore() {
// going through every matrix cell
for (var i = 1; i < inputData.matrixHeight; i++) {
var aChar = inputData.sequenceA[i - 1];
for (var j = 1; j < inputData.matrixWidth; j++) {
var bChar = inputData.sequenceB[j - 1];
if (inputData.calculationType === ALIGNMENT_TYPES.DISTANCE)
outputData.matrix[i][j] = recursionFunction(aChar, bChar, i, j, Math.min);
else
outputData.matrix[i][j] = recursionFunction(aChar, bChar, i, j, Math.max);
}
}
// score is stored in the right bottom cell
outputData.score = outputData.matrix[inputData.matrixHeight - 1][inputData.matrixWidth - 1];
}
/**
* Computes the cell score.
* @param aChar {string} - The current char from the first string.
* @param bChar {string} - The current char from the second string.
* @param i {number} - The current vertical position in the matrix.
* @param j {number} - The current horizontal position in the matrix.
* @param optimum {Function} - The function which should be used for optimization {Math.min, Math.max}.
* @return {number} - The value for the cell at position (i,j).
*/
function recursionFunction(aChar, bChar, i, j, optimum) {
var matchOrMismatch = aChar === bChar ? inputData.match : inputData.mismatch;
// recursion function
return optimum(
horizontalOptimum(optimum, i, j),
outputData.matrix[i - 1][j - 1] + matchOrMismatch,
verticalOptimum(optimum, i, j));
}
/**
* Computes horizontal gap score.
* @param optimum {Function} - The function which should be used for optimization {Math.min, Math.max}.
* @param i {number} - The current vertical position in the matrix.
* @param j {number} - The current horizontal position in the matrix.
* @return {number} - The optimal value.
*/
function horizontalOptimum(optimum, i, j) {
var optimumValue;
var value;
if (optimum === Math.min) {
optimumValue = Number.POSITIVE_INFINITY;
for (var k = 1; k <= j; k++) {
value = outputData.matrix[i][j - k] + gapFunction(k);
if (value < optimumValue)
optimumValue = value;
}
}
else {
optimumValue = Number.NEGATIVE_INFINITY;
for (var k = 1; k <= j; k++) {
value = outputData.matrix[i][j - k] + gapFunction(k);
if (value > optimumValue)
optimumValue = value;
}
}
return optimumValue;
}
/**
* Computes vertical gap score.
* @param optimum {Function} - The function which should be used for optimization {Math.min, Math.max}.
* @param i {number} - The current vertical position in the matrix.
* @param j {number} - The current horizontal position in the matrix.
* @return {number} - The optimal value.
*/
function verticalOptimum(optimum, i, j) {
var optimumValue;
var value;
if (optimum === Math.min) {
optimumValue = Number.POSITIVE_INFINITY;
for (var k = 1; k <= i; k++) {
value = outputData.matrix[i - k][j] + gapFunction(k);
if (value < optimumValue)
optimumValue = value;
}
}
else {
optimumValue = Number.NEGATIVE_INFINITY;
for (var k = 1; k <= i; k++) {
value = outputData.matrix[i - k][j] + gapFunction(k);
if (value > optimumValue)
optimumValue = value;
}
}
return optimumValue;
}
/**
* Initializes the traceback.
* @override Alignment.computeTraceback()
*/
function computeTraceback() {
watermanSmithBeyerInstance.numberOfTracebacks = 0;
var lowerRightCorner = new bases.alignment.Vector(inputData.matrixHeight - 1, inputData.matrixWidth - 1);
outputData.moreTracebacks = false;
outputData.tracebackPaths =
alignmentInstance.getGlobalTraces([lowerRightCorner], inputData, outputData, -1, getNeighboured);
}
/**
* Returns the neighbours to which you can go from the current cell position used as input.
* @param position {Object} - Current cell position in matrix.
* @param inputData {Object} - Contains all input data.
* @param outputData {Object} - Contains all output data.
* @param algorithm {Object} - Contains an alignment algorithm.
* @return {Array} - Contains neighboured positions as Vector-objects.
*/
function getNeighboured(position, inputData, outputData, algorithm) {
var neighboured = [];
var left = position.j - 1;
var up = position.i - 1;
// retrieve values
var aChar = left >= 0 ? inputData.sequenceB[left] : SYMBOLS.EMPTY;
var bChar = up >= 0 ? inputData.sequenceA[up] : SYMBOLS.EMPTY;
var currentValue = outputData.matrix[position.i][position.j];
var matchOrMismatch = aChar === bChar ? inputData.match : inputData.mismatch;
var horizontalK = searchHorizontalMatchPosition(algorithm, currentValue, position, outputData);
var verticalK = searchVerticalMatchPosition(algorithm, currentValue, position, outputData);
var diagonalValue = left >= 0 && up >= 0 ? outputData.matrix[up][left] : Number.NaN;
var upValue = up >= 0 && position.j === 0 ? outputData.matrix[up][position.j] : Number.NaN;
var leftValue = left >= 0 && position.i === 0 ? outputData.matrix[position.i][left] : Number.NaN;
// check
var isMatchMismatch = alignmentInstance.differenceLowerEpsilon(currentValue, diagonalValue + matchOrMismatch, EPSILON);
var isHorizontal = !isNaN(horizontalK); // if a position exists to which we can horizontally jump
var isVertical = !isNaN(verticalK); // if a position exists to which we can vertically jump
var isDeletion = currentValue === upValue + inputData.enlargement;
var isInsertion = currentValue === leftValue + inputData.enlargement;
// add
if (isMatchMismatch)
neighboured.push(new bases.alignment.Vector(up, left));
if (isHorizontal)
neighboured.push(new bases.alignment.Vector(position.i, horizontalK));
if (isVertical)
neighboured.push(new bases.alignment.Vector(verticalK, position.j));
if (isInsertion)
neighboured.push(new bases.alignment.Vector(position.i, left));
if (isDeletion)
neighboured.push(new bases.alignment.Vector(up, position.j));
if (!(isMatchMismatch || isHorizontal || isVertical || isInsertion || isDeletion)
&& (position.i !== 0 || position.j !== 0))
neighboured.push(new bases.alignment.Vector(0, 0));
return neighboured;
}
/**
* Computes the vertical position from which you get to the currentValue.
* @param algorithm {Object} - Contains an alignment algorithm.
* @param currentValue {number} - The value from the current cell.
* @param position {Object} - Current cell position in matrix.
* @param outputData {Object} - Contains all output data.
* @return {number} - The matching position. You get back NaN if such position does not exists.
*/
function searchVerticalMatchPosition(algorithm, currentValue, position, outputData) {
if (position.j > 0) {
for (var k = 1; k <= position.i; k++) {
if (alignmentInstance.differenceLowerEpsilon(outputData.matrix[position.i - k][position.j] + algorithm.gapFunction(k), currentValue, EPSILON))
return position.i - k;
}
}
return Number.NaN;
}
/**
* Computes the horizontal position from which you get to the currentValue.
* @param algorithm {Object} - Contains an alignment algorithm.
* @param currentValue {number} - The value from the current cell.
* @param position {Object} - Current cell position in matrix.
* @param outputData {Object} - Contains all output data.
* @return {number} - The matching position. You get back NaN if such position does not exists.
*/
function searchHorizontalMatchPosition(algorithm, currentValue, position, outputData) {
if (position.i > 0) {
for (var k = 1; k <= position.j; k++) {
if (alignmentInstance.differenceLowerEpsilon(outputData.matrix[position.i][position.j - k] + algorithm.gapFunction(k), currentValue, EPSILON))
return position.j - k;
}
}
return Number.NaN;
}
/**
* Creates the alignments.
* @augments Alignment.createAlignments()
*/
function createAlignments() {
alignmentInstance.setIO(inputData, outputData);
alignmentInstance.createAlignments();
outputData = alignmentInstance.getOutput();
}
/**
* Returns all algorithm output.
* @return {Object} - Contains all output of the algorithm.
*/
function getOutput() {
return outputData;
}
/**
* Sets the input and output of an algorithm.
* @param input {Object} - Contains all input data.
* @param output {Object} - Contains all output data.
*/
function setIO(input, output) {
inputData = input;
outputData = output;
}
/**
* Returns the superclass instance.
* @return {Object} - Superclass instance.
*/
function getSuperclass() {
return alignmentInstance;
}
}());