-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathHirschberg.html
460 lines (404 loc) · 21.8 KB
/
Hirschberg.html
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
<!--
University of Freiburg WS 2017/2018
Chair for Bioinformatics
Supervisor: Martin Raden
Authors: Alexander Mattheis, Martin Raden
-->
<div class="hirschberg_static_size"> <!-- to avoid that MathJax disrupts the output -->
<div id="algorithm_description">
<div class="description">
<a href="https://dl.acm.org/citation.cfm?doid=360825.360861">Daniel S. Hirschberg</a>
introduced 1975 (submitted 1974) an approach to compute the longest common subsequence of two strings.
This approach can be adapted to compute the global alignment of two sequences
in quadratic time $O(nm)$ and only linear space, instead of quadratic space like in the original
<a href="http://rna.informatik.uni-freiburg.de/Teaching/index.jsp?toolName=Needleman-Wunsch">Needleman-Wunsch (1970)</a> algorithm.
While it offers the optimal alignment <em>score</em> without changing the runtime,
additional efforts are needed to traceback an according alignment, since
the needed data was not stored.
The idea is that the tracebacks you get from a prefix- and a suffix-alignment are congruent
i.e. the traceback-cells are the same.
Because of this, rows $i$ of the prefix dynamic programming matrix $D$
and the suffix matrix $D'$ (when using same coordinate system, see recursions)
can be added to identify the row's cell that is passed by an optimal traceback.
<br />
<br />
In detail, to enable traceback calculation in quadratic time a divide-and-conquer
strategy is applied. Here, we want to identify where the traceback "passes
through" the center row of the matrix (first divide-step on left picture).
<img class="embedded_picture" src="hirschberg_traceback.png" align="left"/>
To this end, first the middle row $i = \lfloor n/2 \rfloor$, where $n = |a|$
has to be computed for both matrices $D$ and $D'$. Given that $D_{i,j}$
holds the best prefix score up to and including $a_i,b_j$ and $D'_{i,j}$
provides the same for the suffixes starting in $a_{i+1},b_{j+1}$, the
optimal overall alignment score can be found as the summation of some column $j$, <br \>
i.e. $\exists_{j} : D_{n,m} = D_{i,j}+D'_{i,j} = D'_{0,0}$.
This position $j$ provides a decomposition of the traceback problem into
the respective prefix and suffix strings for which the procedure is iterated
until (a) one of the substrings is empty or (b) only $a_n$ or $b_j$ is left, respectively.
<br />
<br />
In the following, only the leftmost optimal traceback-cell per row is identified. Note,
the introduced algorithm does not identify <em>all</em> traceback-cells.
Missing horizontal traceback-steps (within a row)
and vertical traceback-steps have to be identified
in an additional postprocessing.
In the following, we provide the series of the computed rows from the
$D$ and $D'$ matrix (optionally along with the computation details) and
the respective traceback subproblems, i.e. the according subsequences.
For rows $i$, the identified traceback-cell $j$ is highlighted.
Furthermore, we visualize all identified cells within a full matrix
representation along with a final complete traceback path and alignment.
<br />
<br />
</div>
<div class="picture">
<img src="Hirschberg-120x90.png" />
</div>
</div>
<h1>Input:</h1>
<div id="algorithm_input">
<div class="row">
<div class="colW100"><label>Sequence $a$:</label></div>
<div class="colW400"><input class="sequence" data-bind="value: input.sequence1" id="sequence_1" placeholder="EXAMPLE 'ATC'"
title="Allowed are A-Z and '-'." type="text"></div>
</div>
<div class="row">
<div class="colW100"><label>Sequence $b$:</label></div>
<div class="colW400"><input class="sequence" data-bind="value: input.sequence2" id="sequence_2" placeholder="EXAMPLE 'AGTC'"
title="Allowed are A-Z and '-'." type="text"></div>
</div>
<div style="display:none;">
<div class="colW100"><label>Optimization of:</label></div>
<div class="colW400">
<span class="group">
Distance <input class="optimization_type" data-bind="checked: input.calculation" id="distance"
name="calculation" type="radio" value="distance" checked="checked">
</span>
</div>
</div>
<div class="row">
<div class="colW100"><label>Scoring in $s$:</label></div>
<div class="colW400">
<span class="group"> <!-- Microsoft Browsers will fallback on text-fields using following input type -->
Match <input class="fx_parameter" data-bind="value: input.match" id="match" type="number">
Mismatch <input class="fx_parameter" data-bind="value: input.mismatch" id="mismatch" type="number">
Gap $\gamma$ <input class="fx_parameter" data-bind="value: input.gap" id="gap" type="number">
</span>
<div class="group_hint">
<b>Hint:</b> <br />
For distance minimization, match scores should be smaller than all other scores.
</div>
</div>
</div>
<div class="row">
<div class="colW100">
<label>
<br />
<br />
Recursions:
</label>
</div>
<div class="colW600">
<span data-bind="text: $root.input.formula"></span>
<br />
<br />
<span data-bind="text: $root.input.formulaBackward"></span>
</div>
</div>
</div>
</div>
<h1>Output</h1>
<div id="algorithm_output">
<div class="row">
Visualization of the final traceback path and the according optimal
alignment.
</div>
<div class="output">
<div class="main_output">
<table class="calculation trace_table">
<thead>
<tr>
<th><small><b>Trace</b></small></th>
<th></th>
<!-- ko foreach: input.sequence2 -->
<th data-bind="drawChar: [$data, undefined]"></th>
<!-- /ko -->
</tr>
</thead>
<tbody>
<!-- ko foreach: $root.output.forwardMatrices()[0] --> <!-- to get i-indexes = $parentContext.$index() -->
<tr>
<!-- ko if: $index() == 0 -->
<th></th>
<!-- /ko -->
<!-- ko if: $index() > 0 -->
<th data-bind="drawChar: [$root.input.sequence1()[$index()-1], undefined]"></th>
<!-- /ko -->
<!-- ko foreach: $root.output.forwardMatrices()[0][0] --> <!-- to get j-indexes = $index() -->
<td class="entry"> </td>
<!-- /ko -->
</tr>
<!-- /ko -->
</tbody>
</table>
</div>
</div>
<div class="outcome">
<div class ="ancillary_output">
<table class="results_header">
<thead>
<tr>
<th>
Final alignment <br />
<small>
The alignment for which the traceback was computed is marked blue.
</small>
</th>
</tr>
</thead>
</table>
<div class="results_with_scrollbar">
<table class="results">
<tbody>
<!-- ko foreach: $root.output.alignments -->
<tr>
<td class="entry entry_start selected">
<code data-bind="text: $data[0]"></code> <br />
<code data-bind="text: $data[1]"></code> <br />
<code data-bind="text: $data[2]"></code>
</td>
</tr>
<!-- /ko -->
</tbody>
</table>
</div>
<table class="results_footer">
<tr>
<th>
<small>
<b>Hint:</b> Only one traceback computed.
</small>
</th>
</tr>
</table>
</div>
</div>
<h2>Computed Traceback-cells</h2>
<div class="output">
<div class="main_output">
<table class="calculation tracecells_table">
<thead>
<tr>
<th><small><b>Trace<br />cells</b></small></th>
<th></th>
<!-- ko foreach: input.sequence2 -->
<th data-bind="drawChar: [$data, undefined]"></th>
<!-- /ko -->
</tr>
</thead>
<tbody>
<!-- ko foreach: $root.output.forwardMatrices()[0] --> <!-- to get i-indexes = $parentContext.$index() -->
<tr>
<!-- ko if: $index() == 0 -->
<th></th>
<!-- /ko -->
<!-- ko if: $index() > 0 -->
<th data-bind="drawChar: [$root.input.sequence1()[$index()-1], undefined]"></th>
<!-- /ko -->
<!-- ko foreach: $root.output.forwardMatrices()[0][0] --> <!-- to get j-indexes = $index() -->
<td class="entry"> </td>
<!-- /ko -->
</tr>
<!-- /ko -->
</tbody>
</table>
</div>
</div>
<div class="outcome">
<div class ="ancillary_output">
<table class="results_header">
<thead>
<tr>
<th>
Cells with min. sum <br />
<small>
The computed minima are marked blue.
Red marks the traceback end, green
marks the traceback start.
</small>
</th>
</tr>
</thead>
</table>
<div class="results_with_scrollbar">
<table class="results">
<tbody>
<tr>
<td class="entry entry_start more_left selected">
<code data-bind="text: $root.output.globalMinima()"></code>
</td>
</tr>
</tbody>
</table>
</div>
<table class="results_footer">
<tr>
<th>
<small>
<b>Hint:</b> In computation-order.
</small>
</th>
</tr>
</table>
</div>
</div>
<h2>Recursions <!-- ko foreach: $data --><span data-bind="text: $data"></span>.<!-- /ko --></h2>
<a href="#" data-bind="click: $root.output.toggleVisibility, text: $root.output.toggleLinkText"></a>
<!-- ko foreach: $root.output.recursionNumbersContainer -->
<div class="recursion">
<h3>Split <!-- ko foreach: $data --><span data-bind="text: $data"></span>.<!-- /ko --></h3>
<span class="trace_function" data-bind="text: $root.output.traceFunctions()[$index()]"></span> <br />
<div class="matrices" data-bind="visible: $root.output.showMatrices">
<h4>Prefix Computation</h4>
<!-- ko foreach: $root.output.prefixTwoRowsMatrices()[$index()] -->
<div class="main_output extra_large">
<table class="calculation">
<tbody>
<tr>
<th>
<span data-bind="text: $root.output.matrixDLatex"></span>
</th>
<th></th>
<!-- ko foreach: $root.output.secondSequences()[$parentContext.$index()] -->
<th>
<span data-bind="text: $data"></span><sub><span
data-bind="text: $root.output.secondSequencePositions()[$parentContext.$parentContext.$index()][$index()]"></span></sub>
</th>
<!-- /ko -->
</tr>
<tr>
<th>
<span data-bind="text: $root.output.prefixTwoRowsCharacters()[$parentContext.$index()][$index()][0]"></span><sub><span
data-bind="text: $root.output.prefixTwoRowsCharactersPositions()[$parentContext.$index()][$index()][0]"></span></sub>
</th>
<!-- ko foreach: $data[0] -->
<td class="entry" data-bind="text: $data"></td>
<!-- /ko -->
</tr>
<tr>
<th>
<span data-bind="text: $root.output.prefixTwoRowsCharacters()[$parentContext.$index()][$index()][1]"></span><sub><span
data-bind="text: $root.output.prefixTwoRowsCharactersPositions()[$parentContext.$index()][$index()][1]"></span></sub>
</th>
<!-- ko if: $index() < $root.output.prefixTwoRowsMatrices()[$parentContext.$index()].length - 1 -->
<!-- ko foreach: $data[1] -->
<td class="entry" data-bind="text: $data"></td>
<!-- /ko -->
<!-- /ko -->
<!-- ko if: $index() >= $root.output.prefixTwoRowsMatrices()[$parentContext.$index()].length - 1 -->
<!-- ko foreach: $data[1] -->
<td class="entry selected_light_blue" data-bind="text: $data"></td>
<!-- /ko -->
<!-- /ko -->
</tr>
</tbody>
</table>
</div>
<!-- /ko -->
<h4>Suffix Computation <small>(Note, row numbers <b>i</b> refer to the full-length Needleman-Wunsch matrix)</small></h4>
<!-- ko foreach: $root.output.suffixTwoRowsMatrices()[$index()] -->
<div class="main_output extra_large">
<table class="calculation">
<tbody>
<tr>
<th>
<span data-bind="text: $root.output.matrixDPrimeLatex"></span>
</th>
<!-- ko foreach: $root.output.secondSequences()[$parentContext.$index()] -->
<th>
<span data-bind="text: $data"></span><sub><span
data-bind="text: $root.output.secondSequencePositions()[$parentContext.$parentContext.$index()][$index()]"></span></sub>
</th>
<!-- /ko -->
<th></th>
</tr>
<tr>
<th>
i=<span data-bind="text: $root.output.suffixTwoRowsCharactersPositions()[$parentContext.$index()][$index()][0]"></span>
</th>
<!-- ko if: $index() < $root.output.suffixTwoRowsMatrices()[$parentContext.$index()].length - 1 -->
<!-- ko foreach: $data[0] -->
<td class="entry" data-bind="text: $data"></td>
<!-- /ko -->
<!-- /ko -->
<!-- ko if: $index() >= $root.output.suffixTwoRowsMatrices()[$parentContext.$index()].length - 1 -->
<!-- ko foreach: $data[0] -->
<td class="entry selected_light_blue" data-bind="text: $data"></td>
<!-- /ko -->
<!-- /ko -->
</tr>
<tr>
<th>
i=<span data-bind="text: $root.output.suffixTwoRowsCharactersPositions()[$parentContext.$index()][$index()][1]"></span>
</th>
<!-- ko foreach: $data[1] -->
<td class="entry" data-bind="text: $data"></td>
<!-- /ko -->
</tr>
</tbody>
</table>
</div>
<!-- /ko -->
</div>
<h4>Addition <small>(Note, row numbers <b>i</b> refer to the full-length Needleman-Wunsch matrix)</small></h4>
<div class="output">
<div class="main_output extra_large">
<table class="calculation">
<tbody>
<tr>
<th>
<small>
<b>Row:</b> <br />
i = <span data-bind="text: $root.output.currentGlobalRow()[$index()]"></span>
</small>
</th>
<th></th>
<!-- ko foreach: $root.output.secondSequences()[$index()] -->
<th>
<span data-bind="text: ($index()+1)"></span>
<!--<span data-bind="text: $root.output.secondSequencePositions()[$parentContext.$index()][$index()]"></span>-->
</th>
<!-- /ko -->
</tr>
<tr>
<th><span data-bind="text: $root.output.matrixDLatex"></span></th>
<!-- ko foreach: $root.output.forwardRows()[$index()] -->
<td class="entry" data-bind="text: $data"></td>
<!-- /ko -->
</tr>
<tr>
<th><span data-bind="text: $root.output.matrixDPrimeLatex"></span></th>
<!-- ko foreach: $root.output.mirroredBackwardRows()[$index()] -->
<td class="entry" data-bind="text: $data"></td>
<!-- /ko -->
</tr>
<tr class="thick_line">
<th><span data-bind="text: $root.output.sumLatex"></span></th>
<!-- ko foreach: $root.output.addedRows()[$index()] -->
<!-- ko if: $root.output.highlightPositions()[$parentContext.$index()][1] == $index() -->
<td class="selected" data-bind="text: $data"></td>
<!-- /ko -->
<!-- ko if: $root.output.highlightPositions()[$parentContext.$index()][1] != $index() -->
<td class="entry" data-bind="text: $data"></td>
<!-- /ko -->
<!-- /ko -->
</tr>
</tbody>
</table>
</div>
</div>
</div>
<br />
<br />
<!-- /ko -->
</div>