-
Notifications
You must be signed in to change notification settings - Fork 16
/
gotoh.js
336 lines (276 loc) · 11.8 KB
/
gotoh.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
/*
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.GOTOH) { // to avoid self execution on a script import
gotoh.startGotoh();
loaded = ALGORITHMS.NONE;
}
});
(function () { // namespace
// public methods
namespace("gotoh", startGotoh, Gotoh);
// instances
var alignmentInstance;
var gotohInstance;
// shared variables
var inputData = {}; // stores the input of the algorithm
var outputData = {}; // stores the output of the algorithm
/**
* Function managing objects.
*/
function startGotoh() {
var subadditiveAlignmentInterface = new interfaces.subadditiveAlignmentInterface.SubadditiveAlignmentInterface();
subadditiveAlignmentInterface.startSubadditiveAlignmentAlgorithm(Gotoh, ALGORITHMS.GOTOH);
}
/*---- ALGORITHM ----*/
/**
* Computes the optimal, global affine alignment.
* @constructor
* @augments Alignment
* @see https://doi.org/10.1016/0022-2836(82)90398-9
*
* Gotoh, Osamu.
* "An improved algorithm for matching biological sequences."
* Journal of molecular biology 162.3 (1982): 705-708.
*/
function Gotoh() {
gotohInstance = this;
// variables
this.type = ALGORITHMS.GOTOH;
this.numberOfTracebacks = 0;
// instances
alignmentInstance = new bases.alignment.Alignment(this);
// public class methods
this.getInput = getInput;
this.setInput = setInput;
this.compute = compute;
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) {
inputData.computeOneAlignment = false; // extension to speed up Feng-Doolittle, default value is false
inputData.recomputeTraceback = true; // extension to speed up T-Coffee
alignmentInstance.setIO(inputData, {});
alignmentInstance.setSubadditiveAlignmentInput(inputViewmodel);
}
/**
* Starts the computation.
*/
function compute() {
if (inputData.recomputeTraceback) { // T-Coffee optimization: reuse traceback paths which were computed before (look into Unit-Test PDF)
initializeMatrices();
computeMatricesAndScore();
computeTraceback();
}
createAlignments();
return [inputData, outputData];
}
/**
* Initializes and creates the matrices.
*/
function initializeMatrices() {
createMatrices();
initMatrices();
}
/**
* Creates the matrices without initializing them.
*/
function createMatrices() {
outputData.matrix = new Array(inputData.matrixHeight);
outputData.horizontalGaps = new Array(inputData.matrixHeight);
outputData.verticalGaps = new Array(inputData.matrixHeight);
for (var i = 0; i < inputData.matrixHeight; i++) {
outputData.matrix[i] = new Array(inputData.matrixWidth);
outputData.horizontalGaps[i] = new Array(inputData.matrixWidth);
outputData.verticalGaps[i] = new Array(inputData.matrixWidth);
}
}
/**
* Initializes the default matrix and the gap matrices.
*/
function initMatrices() {
initComputationMatrix();
initHorizontalGapCostMatrix();
initVerticalGapCostMatrix();
}
/**
* Initializes the default matrix.
*/
function initComputationMatrix() {
outputData.matrix[0][0] = 0;
var currentVerticalPosition = 1;
for (var i = 1; i < inputData.matrixHeight; i++) {
if (inputData.sequenceA[i - 1] === SYMBOLS.NONE) // extension for Feng-Doolittle
outputData.matrix[i][0] = outputData.matrix[i - 1][0];
else {
outputData.matrix[i][0] = inputData.baseCosts + currentVerticalPosition * inputData.enlargement;
currentVerticalPosition++;
}
}
var currentHorizontalPosition = 1;
for (var j = 1; j < inputData.matrixWidth; j++) {
if (inputData.sequenceB[j - 1] === SYMBOLS.NONE) // extension for Feng-Doolittle
outputData.matrix[0][j] = outputData.matrix[0][j - 1];
else {
outputData.matrix[0][j] = inputData.baseCosts + currentHorizontalPosition * inputData.enlargement;
currentHorizontalPosition++;
}
}
}
/**
* Initializes the horizontal gap cost matrix.
*/
function initHorizontalGapCostMatrix() {
for (var i = 1; i < inputData.matrixHeight; i++)
outputData.horizontalGaps[i][0] = (ALIGNMENT_TYPES.SIMILARITY === inputData.calculationType)
? Number.NEGATIVE_INFINITY : Number.POSITIVE_INFINITY;
for (var j = 1; j < inputData.matrixWidth; j++)
outputData.horizontalGaps[0][j] = SYMBOLS.GAP;
}
/**
* Initializes the vertical gap cost matrix.
*/
function initVerticalGapCostMatrix() {
for (var i = 1; i < inputData.matrixHeight; i++)
outputData.verticalGaps[i][0] = SYMBOLS.GAP;
for (var j = 1; j < inputData.matrixWidth; j++)
outputData.verticalGaps[0][j] = (ALIGNMENT_TYPES.SIMILARITY === inputData.calculationType)
? Number.NEGATIVE_INFINITY : Number.POSITIVE_INFINITY;
}
/**
* Computes the matrix by using the recursion function and the score.
*/
function computeMatricesAndScore() {
alignmentInstance.setIO(inputData, outputData);
// 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] = alignmentInstance.affineRecursionFunction(aChar, bChar, i, j, Math.min, false);
else // inputData.calculationType === ALIGNMENT_TYPES.SIMILARITY
outputData.matrix[i][j] = alignmentInstance.affineRecursionFunction(aChar, bChar, i, j, Math.max, false);
}
}
// score is stored in the right bottom cell
outputData.score = outputData.matrix[inputData.matrixHeight - 1][inputData.matrixWidth - 1];
}
/**
* Initializes the traceback.
* @override Alignment.computeTraceback()
*/
function computeTraceback() {
gotohInstance.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.
* @see Hint: The parameter algorithm is needed!
* It is based on the code of Alexander Mattheis in project Algorithms for Bioninformatics.
*/
function getNeighboured(position, inputData, outputData, algorithm) {
var neighboured = [];
if (position.label === MATRICES.VERTICAL)
return alignmentInstance.getVerticalNeighboured(position, inputData, outputData);
else if (position.label === MATRICES.HORIZONTAL)
return alignmentInstance.getHorizontalNeighboured(position, inputData, outputData);
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;
if (aChar === SYMBOLS.NONE || bChar === SYMBOLS.NONE) matchOrMismatch = 0; // extension for Feng-Doolittle
if (inputData.substitutionFunction !== undefined) // extension for T-Coffee
matchOrMismatch = inputData.substitutionFunction(position.i, position.j);
var diagonalValue = left >= 0 && up >= 0 ? outputData.matrix[up][left] : Number.NaN;
var verticalValue = up >= 0 ? outputData.verticalGaps[position.i][position.j] : Number.NaN;
var horizontalValue = left >= 0 ? outputData.horizontalGaps[position.i][position.j] : 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); // extension for T-Coffee
var isChangeToP = currentValue === verticalValue;
var isChangeToQ = currentValue === horizontalValue;
var isDeletion = currentValue === upValue + (aChar === SYMBOLS.NONE ? 0 : inputData.enlargement); // extension for Feng-Doolittle
var isInsertion = currentValue === leftValue + (bChar === SYMBOLS.NONE ? 0 : inputData.enlargement);
// add
if (isMatchMismatch)
neighboured.push(new bases.alignment.Vector(up, left));
if (isChangeToP)
neighboured.push(bases.alignment.create(new bases.alignment.Vector(position.i, position.j), MATRICES.VERTICAL));
if (isChangeToQ)
neighboured.push(bases.alignment.create(new bases.alignment.Vector(position.i, position.j), MATRICES.HORIZONTAL));
if (isInsertion)
neighboured.push(new bases.alignment.Vector(position.i, left));
if (isDeletion)
neighboured.push(new bases.alignment.Vector(up, position.j));
if (!(isMatchMismatch || isChangeToP || isChangeToQ || isInsertion || isDeletion)
&& (position.i !== 0 || position.j !== 0))
neighboured.push(new bases.alignment.Vector(0, 0));
return neighboured;
}
/**
* 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;
}
}());