-
Notifications
You must be signed in to change notification settings - Fork 16
/
gotoh_local.js
296 lines (244 loc) · 9.79 KB
/
gotoh_local.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
/*
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_LOCAL) { // to avoid self execution on a script import
gotohLocal.startGotohLocal();
loaded = ALGORITHMS.NONE;
}
});
(function () { // namespace
// public methods
namespace("gotohLocal", startGotohLocal, GotohLocal);
// instances
var alignmentInstance;
var gotohLocalInstance;
// shared variables
var inputData = {}; // stores the input of the algorithm
var outputData = {}; // stores the output of the algorithm
/**
* Function managing objects.
*/
function startGotohLocal() {
var subadditiveAlignmentInterface = new interfaces.subadditiveAlignmentInterface.SubadditiveAlignmentInterface();
subadditiveAlignmentInterface.startSubadditiveAlignmentAlgorithm(GotohLocal, ALGORITHMS.GOTOH_LOCAL);
}
/*---- ALGORITHM ----*/
/**
* Computes the optimal, local affine alignment.
* Combination of the Smith-Waterman and Gotoh-Algorithm.
* @constructor
* @augments Alignment
* @see: https://doi.org/10.1016/0022-2836(82)90398-9 and https://doi.org/10.1016/0022-2836(81)90087-5
*
* Gotoh, Osamu.
* "An improved algorithm for matching biological sequences."
* Journal of molecular biology 162.3 (1982): 705-708.
*
* Smith, Temple F., and Michael S. Waterman.
* "Identification of common molecular subsequences."
* Journal of molecular biology 147.1 (1981): 195-197.
*/
function GotohLocal() {
gotohLocalInstance = this;
// variables
this.type = ALGORITHMS.GOTOH_LOCAL;
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) {
alignmentInstance.setIO(inputData, {});
alignmentInstance.setSubadditiveAlignmentInput(inputViewmodel);
}
/**
* Starts the computation.
*/
function compute() {
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() {
outputData.matrix[0][0] = 0;
for (var i = 1; i < inputData.matrixHeight; i++) {
outputData.matrix[i][0] = 0;
outputData.horizontalGaps[i][0] = Number.NEGATIVE_INFINITY;
outputData.verticalGaps[i][0] = SYMBOLS.GAP;
}
for (var j = 1; j < inputData.matrixWidth; j++) {
outputData.matrix[0][j] = 0;
outputData.horizontalGaps[0][j] = SYMBOLS.GAP;
outputData.verticalGaps[0][j] = Number.NEGATIVE_INFINITY;
}
}
/**
* Computes the matrix by using the recursion function and the score.
*/
function computeMatricesAndScore() {
alignmentInstance.setIO(inputData, outputData);
var maxValue = 0;
// 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];
outputData.matrix[i][j] = alignmentInstance.affineRecursionFunction(aChar, bChar, i, j, Math.max, true);
// storing maximum
if (maxValue < outputData.matrix[i][j])
maxValue = outputData.matrix[i][j];
}
}
// score is the maximum value
outputData.score = maxValue;
}
/**
* Initializes the traceback.
* @override Alignment.computeTraceback()
*/
function computeTraceback() {
gotohLocalInstance.numberOfTracebacks = 0;
// computing all traceback start-positions
var backtraceStarts = alignmentInstance.getAllMaxPositions(inputData, outputData);
outputData.tracebackPaths = [];
outputData.moreTracebacks = false;
for (var i = 0; i < backtraceStarts.length; i++) {
var tracebackPaths = alignmentInstance.getLocalTraces([backtraceStarts[i]], inputData, outputData, -1, getNeighboured);
outputData.tracebackPaths = outputData.tracebackPaths.concat(tracebackPaths);
if (alignmentInstance.stopTraceback) break;
}
}
/**
* Returns the neighbours to which you can go from the current cell position used as input.
* @param position {Vector} - 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;
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;
// check
var isMatchMismatch = currentValue === (diagonalValue + matchOrMismatch);
var isChangeToP = currentValue === verticalValue;
var isChangeToQ = currentValue === horizontalValue;
isMatchMismatch = isMatchMismatch || currentValue === 0 && up >= 0 && left >= 0;
var isDeletion = currentValue === 0 && up >= 0; // lower 0 -> cut away
var isInsertion = currentValue === 0 && left >= 0; // lower 0 -> cut away
// 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;
}
}());