-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathLevenshtDP.h
548 lines (475 loc) · 15.6 KB
/
LevenshtDP.h
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
// Metal - A fast methylation alignment and calling tool for WGBS data.
// Copyright (C) 2017 Jonas Fischer
//
// 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 <http://www.gnu.org/licenses/>.
//
// Jonas Fischer [email protected]
#ifndef LEVENSHTDP_H
#define LEVENSHTDP_H
#include <string>
#include <limits> // numeric limits (max)
#include <iostream> // cerr
#include "BandedMatrix.h"
// types of errors allowed
enum ERROR_T {MATCHING, MISMATCH, INSERTION, DELETION};
// class for dynamic programming approach for computing Levenshtein
// distance and the corresponding alignment for two strings
// it is REQUIRED that the first given string (rowStr in Ctor) must be fully
// matched!
//
// template parameter is the size type T of the underlying matrix
// i.e. should be the minimal size type that fits the max of the two string sizes
// and the bandwidth of the banded alignment
// i.e. the number of errors allowed (hence we only fill some diagonals in the matrix)
template <typename T, size_t band>
class LevenshtDP
{
public:
// ------ Ctors ------
LevenshtDP() = delete;
LevenshtDP(const std::string& rowStr, const char* colStr);
// -------------------
// compute the levenshtein distance
//
// ARGUMENTS:
// comp comparison function for the letters
//
template <typename C>
void runDPFill(C& comp);
// for querying from right to left
template <typename C>
void runDPFillRev(C& comp);
// return edit distance
// undefined behaviour if runDPFill was not run beforehand
T getEditDist()
{
T minimum = std::numeric_limits<T>::max();
for (long offset = -static_cast<long>(band); offset <= static_cast<long>(band); ++offset)
minimum = std::min(minimum, dpMatrix(rowPat.size(), rowPat.size() + offset));
return minimum;
}
// backtrack the result to obtain alignment
//
// ARGUMENTS:
// comp comparison function for the letters
// align gives the alignment using error types as flags SHOULD BE EMPTY ON CALL
//
// MODIFICATIONS:
// alignment will bevector of size of rowPat containing a value of type ERROR_T
// the value represents the type of transition made from i-th to
// i+1 th character of rowPat to match with colPat
template <typename C>
void backtrackDP(C& comp, std::vector<ERROR_T>& align);
template <typename C>
int backtrackDPRev(C& comp, std::vector<ERROR_T>& align);
// Print the DP matrix to the given stream
inline void printMatrix(std::ostream& os)
{
os << "\t\t";
for (int i = 0; i < 13; ++i)
{
// os << c << "\t";
os << *(colPat - rowPat.size() + i + 1) << "\t";
}
os << "\n\t";
// for (int row = 0; row <= rowPat.size(); ++row)
for (int row = 0; row <= 13; ++row)
{
if (row > 0)
{
os << rowPat[row] << "\t";
}
for (int off = 0; off < row - (int)band; ++off)
{
os << "\t";
}
for (long offset = -band; offset <= static_cast<long>(band); ++offset)
{
if (row + offset < 0)
continue;
os << dpMatrix(row, row + offset) << "\t";
}
os << "\n";
}
}
void printAlignment(std::ostream& os, std::vector<ERROR_T>& alignment)
{
for (const auto al : alignment)
{
switch (al)
{
case (MATCHING):
os << "=";
break;
case (MISMATCH):
os << "!";
break;
case (DELETION):
os << "-";
break;
case(INSERTION):
os << "+";
}
}
}
private:
// recursive formulation of the levenshtein distance
// DP drops in here
//
// Formula:
//
// LevRec(i,j) = min {LevRec(i-1,j) + 1, LevRec(i,j-1) + 1,
// LevRec(i-1,j-1) + isEqual(rowPat(i),colPat(j)) }
//
// ARGUMENTS:
// indices of positions in strings
// i position in rowPat
// j position in colPat
// comp comparison function for the letters
//
//
// RETURN:
// solution to recurrence
template <typename C>
inline T LevRec(long i, long j, C& comp)
{
T mismatchTest = comp(rowPat[rowPat.size() - i], *(colPat-j+1));
return std::min({dpMatrix(i-1,j) + 1, dpMatrix(i,j-1) + 1, dpMatrix(i-1,j-1) + mismatchTest});
}
// if we want to align reverse sequence
template <typename C>
inline T LevRecRev(long i, long j, C& comp)
{
T mismatchTest = comp(rowPat[i-1], *(colPat-j+1));
return std::min({dpMatrix(i-1,j) + 1, dpMatrix(i,j-1) + 1, dpMatrix(i-1,j-1) + mismatchTest});
}
// the two strings that are compared
// rowPat is represented by rows of the DP matrix
// colPat is represented by columns in DP matrix
const std::string& rowPat;
const char* colPat;
// underlying dp matrix
// note that bandwidth is extended by one for the edge cases
BandedMatrix<T, band + 1> dpMatrix;
};
template <typename T, size_t band>
LevenshtDP<T, band>::LevenshtDP(const std::string& rowStr, const char* colStr) :
rowPat(rowStr)
, colPat(colStr)
, dpMatrix(rowStr.size() + 1, rowStr.size() + 1 + band, static_cast<T>(std::numeric_limits<T>::max()))
{
// check if template param is correct size type
if (!std::is_integral<T>::value)
{
std::cerr << "\n\n(LevenshtDP) Template parameter is not a valid integral type! Terminating...\n\n";
exit(1);
}
}
template <typename T, size_t band>
template <typename C>
void LevenshtDP<T, band>::runDPFill(C& comp)
{
// INIT BORDERS
dpMatrix(0,0) = 0;
// initialize first row
for (long col = 1; col <= static_cast<long>(band); ++col)
{
dpMatrix(0, col) = col;
}
// init first column
for (long row = 1; row <= static_cast<long>(band); ++row)
{
dpMatrix(row, 0) = row;
}
// init outermost lefthanded band
for (long col = 0; col < static_cast<long>(rowPat.size() - band); ++col)
{
dpMatrix(col + (band + 1), col) = std::numeric_limits<T>::max() - col - band - 1;
}
// init outermost righthanded band
for (long row = 0; row < static_cast<long>(rowPat.size()); ++row)
{
dpMatrix(row, row + (band + 1)) = std::numeric_limits<T>::max() - row - band - 1;
}
// FILL MATRIX
for (long row = 1; row <= static_cast<long>(rowPat.size()); ++row)
{
for (long offset = -band; offset <= static_cast<long>(band); ++offset)
{
// skip borders
if (row + offset <= 0)
continue;
dpMatrix(row, row + offset) = LevRec<C>(row, row + offset, comp);
}
}
// // TEST PRINTOUT
// for (long row = 0; row <= static_cast<long>(rowPat.size()); ++row)
// {
// for (long offset = -band - 1; offset <= static_cast<long>(band + 1); ++offset)
// {
// // skip borders
// if (row + offset < 0)
// continue;
// if (row + offset > rowPat.size() + band)
// continue;
// std::cout << dpMatrix(row, row + offset) << "\t";
// }
// std::cout << "\n";
// }
}
template <typename T, size_t band>
template <typename C>
void LevenshtDP<T, band>::runDPFillRev(C& comp)
{
// INIT BORDERS
dpMatrix(0,0) = 0;
// initialize first row
for (long col = 1; col <= static_cast<long>(band); ++col)
{
dpMatrix(0, col) = col;
}
// init first column
for (long row = 1; row <= static_cast<long>(band); ++row)
{
dpMatrix(row, 0) = row;
}
// init outermost lefthanded band
for (long col = 0; col < static_cast<long>(rowPat.size() - band); ++col)
{
dpMatrix(col + (band + 1), col) = std::numeric_limits<T>::max() - col - band - 1;
}
// init outermost righthanded band
for (long row = 0; row < static_cast<long>(rowPat.size()); ++row)
{
dpMatrix(row, row + (band + 1)) = std::numeric_limits<T>::max() - row - band - 1;
}
// FILL MATRIX
for (long row = 1; row <= static_cast<long>(rowPat.size()); ++row)
{
for (long offset = -band; offset <= static_cast<long>(band); ++offset)
{
// skip borders
if (row + offset <= 0)
continue;
dpMatrix(row, row + offset) = LevRecRev<C>(row, row + offset, comp);
}
}
// // TEST PRINTOUT
// for (long row = 0; row <= static_cast<long>(rowPat.size()); ++row)
// {
// for (long offset = -band - 1; offset <= static_cast<long>(band + 1); ++offset)
// {
// // skip borders
// if (row + offset < 0)
// continue;
// if (row + offset > rowPat.size() + band)
// continue;
// std::cout << dpMatrix(row, row + offset) << "\t";
// }
// std::cout << "\n";
// }
}
template <typename T, size_t band>
template <typename C>
void LevenshtDP<T, band>::backtrackDP(C& comp, std::vector<ERROR_T>& alignment)
{
// the trace of errors
// note that insertion means we have an additional character in the PATTERN (rowPat)
// and deletion analogously
// we start indexing for the character in the back
// that is alignment[0] is the error type of the last character
alignment.reserve(rowPat.size() + band);
// find minimum in the last part of table
T minimum = std::numeric_limits<T>::max();
// stores the index of the cell of the minimum
size_t col = 0;
for (long offset = -static_cast<long>(band); offset <= static_cast<long>(band); ++offset)
{
const T& valRef = dpMatrix(rowPat.size(), rowPat.size() + offset);
if (valRef < minimum)
{
col = rowPat.size() + offset;
minimum = valRef;
}
}
// BACKTRACK
// starting from the minimum cell in the last column
for (long row = rowPat.size(); row > 0;)
{
// check if characters match
T mismatchFlag = comp(rowPat[rowPat.size() - row], *(colPat-col+1));
// see where does it came from
if (dpMatrix(row-1,col) < dpMatrix(row,col-1))
{
// we have an insertion in the pattern
if (dpMatrix(row-1,col) + 1 < dpMatrix(row-1,col-1) + mismatchFlag)
{
alignment.emplace_back(INSERTION);
--row;
} else {
// test if mismatch
if (mismatchFlag)
alignment.emplace_back(MISMATCH);
else
alignment.emplace_back(MATCHING);
--row;
--col;
}
} else {
// we have a deletion in the pattern
if (dpMatrix(row,col-1) + 1 < dpMatrix(row-1,col-1) + mismatchFlag)
{
alignment.emplace_back(DELETION);
--col;
} else {
// test if mismatch
if (mismatchFlag)
alignment.emplace_back(MISMATCH);
else
alignment.emplace_back(MATCHING);
--row;
--col;
}
}
}
// shift appropriately (biggest mindfuck in history because of choise of orientation for strings along matrix)
while (col > 0)
{
alignment.emplace_back(DELETION);
--col;
}
}
template <typename T, size_t band>
template <typename C>
int LevenshtDP<T, band>::backtrackDPRev(C& comp, std::vector<ERROR_T>& alignment)
{
// the trace of errors
// note that insertion means we have an additional character in the PATTERN (rowPat)
// and deletion analogously
// we start indexing for the character in the back
// that is alignment[0] is the error type of the last character
alignment.reserve(rowPat.size() + band);
// find minimum in the last part of table
T minimum = std::numeric_limits<T>::max();
// stores the index of the cell of the minimum
// size_t col = 0;
// // ERROR_T l = MATCHING;
// for (long offset = -static_cast<long>(band); offset <= static_cast<long>(band); ++offset)
// {
// const T& valRef = dpMatrix(rowPat.size(), rowPat.size() + offset);
// if (valRef < minimum)
// {
//
// // if (offset > 0)
// // {
// // l = DELETION;
// // } else if (offset < 0)
// // {
// // l = INSERTION;
// // } else {
// // l = MATCHING;
// // }
// //
// col = rowPat.size() + offset;
// minimum = valRef;
// }
// }
// // if (l != MATCHING)
// // alignment.emplace_back(l);
size_t col = 0;
for (long offset = -static_cast<long>(band); offset <= static_cast<long>(band); ++offset)
{
const T& valRef = dpMatrix(rowPat.size(), rowPat.size() + offset);
if (valRef < minimum)
{
col = rowPat.size() + offset;
minimum = valRef;
}
}
// if (col < rowPat.size())
// {
// for (long i = rowPat.size() - col; i > 0; --i)
// {
// alignment.emplace_back(INSERTION);
// }
// } else if (col > rowPat.size())
// {
// for (long i = col - rowPat.size(); i > 0; --i)
// {
// alignment.emplace_back(DELETION);
// }
// }
int varshift = 0;
// BACKTRACK
// starting from the minimum cell in the last column
for (long row = rowPat.size(); row > 0;)
{
// check if characters match
T mismatchFlag = comp(rowPat[row - 1], *(colPat -col + 1));
// see where does it came from
if (dpMatrix(row-1,col) < dpMatrix(row,col-1))
{
// we have an insertion in the pattern
if (dpMatrix(row-1,col) + 1 < dpMatrix(row-1,col-1) + mismatchFlag)
{
alignment.emplace_back(INSERTION);
// ++varshift;
--row;
} else {
// test if mismatch
if (mismatchFlag)
alignment.emplace_back(MISMATCH);
else
alignment.emplace_back(MATCHING);
--row;
--col;
}
} else {
// we have a deletion in the pattern
if (dpMatrix(row,col-1) + 1 < dpMatrix(row-1,col-1) + mismatchFlag)
{
alignment.emplace_back(DELETION);
// --varshift;
--col;
} else {
// test if mismatch
if (mismatchFlag)
alignment.emplace_back(MISMATCH);
else
alignment.emplace_back(MATCHING);
--row;
--col;
}
}
}
while (col > 0)
{
alignment.emplace_back(DELETION);
--col;
--varshift;
}
// auto it = alignment.end() - 1;
// for (; it != alignment.begin(); --it)
// {
// if (*it == INSERTION)
// {
// ++varshift;
// it = alignment.erase(it) - 1;
// } else {
// break;
// }
// }
return varshift;
}
#endif /* LEVENSHTDP_H */