-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathIndelRefine.h
787 lines (702 loc) · 23.6 KB
/
IndelRefine.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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
#ifndef INDEL_REFINE_H_
#define INDEL_REFINE_H_
#include "Alignment.h"
#include "Genome.h"
#include "Read.h"
#include "Options.h"
#include "AffineOneGapAlign.h"
#include <algorithm>
void FlatPrintMat(vector <int> &mat, vector<int> &qS, vector<int> &qE) {
int mi=0;
for (int r=0; r< qS.size(); r++) {
cout << r << "\t" << qS[r] << "\t" << qE[r] << "\t";
for (int c=qS[r]; c <= qE[r]; c++, mi++) {
cout << setw(3) << mat[mi];
}
cout << endl;
}
}
void PrintMat(vector<int> &mat, vector<int> &qS, vector<int> &qE) {
int m=0;
int s=qS[0];
int w=4;
cout << setw(w) << ".";
for (int i=0; i < qE[qE.size()-1]; i++) {
cout << setw(w) << i;
}
cout << endl;
for (int i=0; i < qS.size(); i++) {
cout << setw(w) << i;
for (int j=0; j < qS[i] - s; j++) { cout << setw(w) << "."; }
for (int j=0; j < qE[i] - qS[i] + 1; j++) { cout << setw(w) << mat[m]; m++;} cout << endl;
}
}
class IndelRefineBuffers {
public:
vector<int> cuMatSize;
vector<int> qS, qE;
vector<int> insScoreMat, insPathMat, insIndexMat;
vector<int> delScoreMat, delPathMat, delIndexMat;
vector<int> scoreMat;
vector<int> pathMat;
vector<int> indexMat;
};
void IndelRefineAlignment(Read &read,
Genome &genome,
Alignment &alignment,
const Options &opts,
IndelRefineBuffers &buffers, bool endAlign=false) {
int startBlock=0, endBlock=0;
int k=opts.refineBand;
int maxGap=k-1;
long qPos,tPos;
vector<Block> refined;
vector<int> &cuMatSize=buffers.cuMatSize;
vector<int> &qS=buffers.qS;
vector<int> &qE=buffers.qE;
vector<int> &insScoreMat=buffers.insScoreMat, &insPathMat=buffers.insPathMat, &insIndexMat=buffers.insIndexMat;
vector<int> &delScoreMat=buffers.delScoreMat, & delPathMat=buffers.delPathMat, & delIndexMat=buffers.delIndexMat;
vector<int> &scoreMat=buffers.scoreMat;
vector<int> &pathMat=buffers.pathMat;
vector<int> &indexMat=buffers.indexMat;
//
// No modification on empty or ungapped alignment
//
if (alignment.blocks.size() == 0 or alignment.blocks.size() == 1) { return; }
int st=0,sq=0;
int startMatch=0;
int endMatch=0;
int prevQGap=0;
int prevTGap=0;
//
// qPos and tPos are at the
if ( endAlign and alignment.blocks.size() > 0 ) {
int nExtra=0;
long qStart=alignment.blocks[0].qPos;
long tStart=alignment.blocks[0].tPos;
int minStart=min(qStart,tStart);
int addStart=0;
if (minStart < 40) {
tStart=tStart-minStart;
qStart=qStart-minStart;
startMatch=minStart;
addStart=1;
}
int e=alignment.blocks.size();
long qAlnEnd=alignment.blocks[e-1].qPos+alignment.blocks[e-1].length;
long tAlnEnd=alignment.blocks[e-1].tPos+alignment.blocks[e-1].length;
int minEnd=min(read.length-qAlnEnd, genome.lengths[alignment.chromIndex]-tAlnEnd);
int addEnd=0;
if (minEnd < 40) {
endMatch=minEnd;
qPos+=minEnd;
tPos+=minEnd;
addEnd++;
}
vector<Block> newBlocks(alignment.blocks.size() + addStart+addEnd);
long origTEnd=tAlnEnd;
long origQEnd=qAlnEnd;
if (addStart) {
newBlocks[0].qPos = qStart;
newBlocks[0].tPos = tStart;
newBlocks[0].length = startMatch;
}
copy(alignment.blocks.begin(), alignment.blocks.end(), newBlocks.begin()+addStart);
if (addEnd) {
int e=newBlocks.size()-1;
newBlocks[e].tPos = origTEnd;
newBlocks[e].qPos = origQEnd;
newBlocks[e].length=endMatch;
}
alignment.blocks=newBlocks;
}
while (endBlock < alignment.blocks.size()) {
//
// Indel is measured from next qpos/tpos to current position
//
long qStart = alignment.blocks[startBlock].qPos;
long tStart = alignment.blocks[startBlock].tPos;
int blockLen=alignment.blocks[startBlock].length;
qPos=alignment.blocks[startBlock].qPos + blockLen;
tPos=alignment.blocks[startBlock].tPos + blockLen;
int tGap=0, qGap=0;
if (endBlock < alignment.blocks.size()-1) {
tGap=alignment.blocks[endBlock+1].tPos - tPos;
qGap=alignment.blocks[endBlock+1].qPos - qPos;
}
while (endBlock < alignment.blocks.size() - 1 and
qGap < maxGap and
tGap < maxGap and
(startBlock == endBlock or alignment.blocks[endBlock].length < 100)) {
endBlock++;
int blockLen=alignment.blocks[endBlock].length;
qPos=alignment.blocks[endBlock].qPos + blockLen;
tPos=alignment.blocks[endBlock].tPos + blockLen;
if (endBlock+1 < alignment.blocks.size()-1) {
assert(endBlock < alignment.blocks.size()-1);
tGap=alignment.blocks[endBlock+1].tPos - tPos;
qGap=alignment.blocks[endBlock+1].qPos - qPos;
}
}
//
// Just one block, do not refine.
//
Block altEndBlock;
bool usedAltEndBlock=false;
if (endBlock == startBlock) {
refined.push_back(alignment.blocks[startBlock]);
}
else {
//
// Multiple blocks, need to do a banded alignment
//
if ( alignment.blocks[startBlock].length > maxGap ) {
// //
// // Start block is large, don't bother refining the whole thing.
// //
// //
// // First trim current back a bit, and add to refined.
int advanced=alignment.blocks[startBlock].length - maxGap;
alignment.blocks[startBlock].length -= maxGap;
refined.push_back(alignment.blocks[startBlock]);
//
// Now advance block forward by added match.
//
alignment.blocks[startBlock].qPos += advanced;
alignment.blocks[startBlock].tPos += advanced;
alignment.blocks[startBlock].length=maxGap;
qStart+=advanced;
tStart+=advanced;
}
if (alignment.blocks[endBlock].length > maxGap) {
usedAltEndBlock=true;
altEndBlock=alignment.blocks[endBlock];
altEndBlock.qPos+=maxGap;
altEndBlock.tPos+=maxGap;
altEndBlock.length-=maxGap;
alignment.blocks[endBlock].length = maxGap;
qPos=alignment.blocks[endBlock].qPos + maxGap;
tPos=alignment.blocks[endBlock].tPos + maxGap;
}
long qEnd = alignment.blocks[endBlock].qPos + alignment.blocks[endBlock].length;
long tEnd = alignment.blocks[endBlock].tPos + alignment.blocks[endBlock].length;
long qLen=qPos-qStart;
long tLen=tPos-tStart;
qS.resize(tLen, -1);
qE.resize(tLen, -1);
fill(qS.begin(), qS.end(),-1);
fill(qE.begin(), qE.end(), -1);
long t, q;
int tOff=0, qOff=0;
t=alignment.blocks[startBlock].tPos;
q=alignment.blocks[startBlock].qPos;
// cout << "Block boundaries " << endl;
// cerr << " start block " << startBlock << " length " << alignment.blocks[startBlock].length << "\tend:\t" << endBlock << "\t" << alignment.blocks[endBlock].tPos - alignment.blocks[startBlock].tPos << endl;
for (int b=startBlock; b <= endBlock; b++) {
int qGap=0, tGap=0;
int commonGap = 0;
int blockLength = alignment.blocks[b].length;
if (b < endBlock) {
qGap = alignment.blocks[b+1].qPos -
(alignment.blocks[b].qPos + blockLength);
tGap = alignment.blocks[b+1].tPos -
(alignment.blocks[b].tPos + blockLength);
if (qGap > 0 and tGap > 0) {
commonGap=min(qGap,tGap);
qGap-=commonGap;
tGap-=commonGap;
blockLength+=commonGap;
}
}
//
// Process contiguous alignment.
//
for (int bi = 0; bi < blockLength; tOff++, bi++, q++, t++) {
if (qS[tOff] == -1) {
qS[tOff] = max(q-k, qStart);
// cout << "START " << startBlock << "\tROW " << tOff << "\tqS " << qS[tOff];
}
else {
qS[tOff] = min((long)qS[tOff], max(q-k, (long)qStart));
// cout << "START " << startBlock << "\tROW " << tOff << "\tqS " << qS[tOff] << endl;
assert(qS[tOff] >= 0);
}
if (qE[tOff] == -1 or qE[tOff] < q+k) {
qE[tOff] = min(qEnd-1, (long)(q+k));
// cout << "START " << startBlock << "\tROW " << tOff << "\tqE " << qE[tOff] << endl;
assert(qE[tOff] < qEnd);
}
for (int ki=0; ki < k; ki++) {
//
// ensure future entries process to this point k above and below.
if (tOff - ki >= 0) {
if (qE[tOff - ki] < q) {
// cout << "START " << startBlock << "\tROW " << tOff-ki << "\tResetting qE " << tOff-ki << "\t" << qE[tOff-ki] << "\t" << q << endl;
qE[tOff-ki] = q;
}
}
if (tOff + ki < qS.size()) {
if (qS[tOff+ki] == -1 or qS[tOff+ki] > q) {
// cout << "START " << startBlock << "\tROW " << tOff+ki << "\tResetting qS " << tOff+ki << "\t" << qS[tOff+ki] << "\t" << q << endl;
qS[tOff+ki] = q;
}
}
}
}
//
// Advance gap
//
if (qGap > tGap) {
assert(tGap == 0);
for (int qi=0; qi < qGap; qi++, q++) {
for (int ki=0; ki < k; ki++) {
if (tOff - ki >= 0) {
if (qE[tOff - ki] < q) {
// cout << "START " << startBlock << "\tROW " << tOff-ki << "\tgap qe " << tOff-ki << "\t" << qE[tOff-ki] << "\t" << q << endl;
qE[tOff-ki] = q;
}
}
if (tOff + ki < qS.size()) {
if (qS[tOff+ki] == 0 or qS[tOff+ki] > q) {
// cout << "START " << startBlock << "\tROW " << tOff+ki << "\tgap qs " << tOff+ki << "\t" << qE[tOff+ki] << "\t" << q << endl;
qS[tOff+ki] = q;
}
}
}
}
}
if (tGap > qGap) {
assert(qGap == 0);
for (int ti=0; ti < tGap; tOff++, ti++, t++) {
// cout << "START " << startBlock << "\tROW " << tOff << " qs gap " << qS[tOff] << "\t" << q-k << "\t" << qStart << endl;
qS[tOff] = max(q-k, qStart);
// cout << "START " << startBlock << "\tROW " << tOff << " qe gap " << qE[tOff] << "\t" << q+k << "\t" << qEnd -1 << endl;
qE[tOff] = min(qEnd-1,q+k);
}
}
}
long matSize=0;
for (int qi=qS.size(); qi >1; qi--) {
if (qS[qi-1] < qS[qi-2]) {
qS[qi-2] = qS[qi-1];
}
}
for (int qi=0; qi < qS.size()-1; qi++) {
if (qE[qi] > qE[qi+1]) { qE[qi+1] = qE[qi]; }
}
for (int qi=0; qi < qS.size(); qi++) {
matSize+=qE[qi]-qS[qi]+1;
}
for (int qi=1; qi < qS.size(); qi++) {
assert(qS[qi] >= qS[qi-1]);
assert(qE[qi] >= qE[qi-1]);
assert(qS[qi] < qE[qi]);
}
char *tSeq=genome.seqs[alignment.chromIndex];
char *qSeq=alignment.read;
long tSeqLen = tEnd-tStart;
long qSeqLen = qEnd-qStart;
int gap=opts.localIndel;
int gapOpen=opts.localIndel*2+1;
int gapExtend=0;//opts.localMismatch+1;
int match=opts.localMatch;
int mismatch=opts.localMismatch;
if (tSeqLen < k or qSeqLen < k) {
Alignment aln;
AffineAlignBuffers buff;
string qStr(&qSeq[qStart], qSeqLen);
string tStr(&tSeq[tStart], tSeqLen);
AffineOneGapAlign(qStr, qSeqLen, tStr, tSeqLen,
match, mismatch, gap, k, aln, buff);
for (int afb=0; afb < aln.blocks.size(); afb++) {
aln.blocks[afb].qPos += qStart;
aln.blocks[afb].tPos += tStart;
}
refined.insert(refined.end(), aln.blocks.begin(), aln.blocks.end());
}
else {
cuMatSize.resize(qS.size(), 0);
fill(cuMatSize.begin(), cuMatSize.end(), 0);
for (int qi=1; qi < qS.size(); qi++) {
cuMatSize[qi] = cuMatSize[qi-1] + qE[qi-1] - qS[qi-1] + 1;
}
//
// The first base is always aligned here.
int rowStart=0, rowEnd=-1;
int BAD=-999999999;
int diag=0;
int left=1;
int down=2;
int bound=3;
int done=20;
int delOpen=4;
int delExtend=5;
int delClose=6;
int insOpen=7;
int insExtend=8;
int insClose=9;
scoreMat.resize(matSize, 0);
fill(scoreMat.begin(), scoreMat.end(), 0);
pathMat.resize(matSize, bound);
fill(pathMat.begin(), pathMat.end(), bound);
indexMat.resize(matSize, -1);
fill(indexMat.begin(), indexMat.end(), -1);
delScoreMat.resize(matSize, BAD);
fill(delScoreMat.begin(), delScoreMat.end(), BAD);
delPathMat.resize(matSize, 0);
fill(delPathMat.begin(), delPathMat.end(), bound);
delIndexMat.resize(matSize, -1);
fill(delIndexMat.begin(), delIndexMat.end(), -1);
insScoreMat.resize(matSize, BAD);
fill(insScoreMat.begin(), insScoreMat.end(), BAD);
insPathMat.resize(matSize, 0);
fill(insPathMat.begin(), insPathMat.end(), bound);
insIndexMat.resize(matSize, -1);
fill(insIndexMat.begin(), insIndexMat.end(), -1);
indexMat[0] = 0;
pathMat[0] = done;
// cerr << startBlock << "/" << alignment.blocks.size() << " memory is " << insIndexMat.size() << "\t" << sizeof(int)*9*insIndexMat.size() << "\t" << alignment.blocks[endBlock].length << endl;
for (int ti=0; ti < tLen; ti++) {
int rowLen=qE[ti]-qS[ti]+1;
rowEnd=rowStart + rowLen-1;
if (rowStart > 0) {
scoreMat[rowStart] = BAD;
pathMat[rowStart] = bound;
insPathMat[rowStart] = bound;
}
else {
for (int qi = 1; qi < rowEnd; qi++ ){
scoreMat[qi] = scoreMat[qi-1] + gap;
pathMat[qi] = left;
indexMat[qi] = qi-1;
}
}
if (ti < tLen-1) {
scoreMat[rowEnd] = BAD;
pathMat[rowEnd] = bound;
}
rowStart+=rowLen;
}
//
// Now run dp
//
int curRowStart = qE[0]-qS[0]+1;
int prevRowStart = 0;
int prevRowLen = qE[0]-qS[0]+1;
for (int ti=1; ti < tLen; ti++) {
int curRowLen = qE[ti] - qS[ti] + 1;
assert(qS[ti-1] <= qS[ti]);
int curRowOffset = qS[ti] - qS[ti-1];
// Iterate on current row, skipping boundaries.
// prevRowPos is set to the cell immediately below the current cell, so the diagonal cell is prevRowPos-1
// The +1 offset is to skip past boundary cells.
int curRowPos = curRowStart + 1;
int prevRowPos = prevRowStart + curRowOffset +1;
int rowEnd;
// Last row is solved to final cell which should end on a match.
if (ti == tLen-1) {
rowEnd = curRowLen;
}
else {
rowEnd = curRowLen - 1;
}
int *scoreMatPrevRowPosPtr=&scoreMat[prevRowPos];
int *scoreMatCurRowPosPtr=&scoreMat[curRowPos];
int *scoreMatDiagPosPtr=&scoreMat[prevRowPos-1];
int *scoreMatInsPosPtr=&scoreMat[curRowPos-1];
int *pathMatPrevRowPosPtr=&pathMat[prevRowPos];
int *pathMatCurRowPosPtr=&pathMat[curRowPos];
int *pathMatDiagPosPtr=&pathMat[prevRowPos-1];
int *indexMatCurRowPosPtr = &indexMat[curRowPos];
int *delScoreMatPrevRowPosPtr=&delScoreMat[prevRowPos];
int *delScoreMatCurRowPosPtr=&delScoreMat[curRowPos];
int *delIndexMatCurRowPosPtr=&delIndexMat[curRowPos];
int *delPathMatCurRowPosPtr=&delPathMat[curRowPos];
int *insScoreMatInsPosPtr=&insScoreMat[curRowPos-1];
int *insScoreMatCurRowPosPtr=&insScoreMat[curRowPos];
int *insIndexMatCurRowPosPtr=&insIndexMat[curRowPos];
int *insPathMatCurRowPosPtr=&insPathMat[curRowPos];
char tChar=tSeq[ti+tStart];
char *qCharPtr=&qSeq[1+qS[ti]];
int qEPrev = qE[ti-1];
int qECur = qE[ti];
int qSCur = qS[ti];
for (int qi=1; qi < rowEnd; qi++, curRowPos++, prevRowPos++, ++scoreMatPrevRowPosPtr, ++scoreMatCurRowPosPtr, ++scoreMatDiagPosPtr, ++scoreMatInsPosPtr, ++pathMatPrevRowPosPtr, ++pathMatCurRowPosPtr, ++pathMatDiagPosPtr, ++delScoreMatPrevRowPosPtr,++delScoreMatCurRowPosPtr, ++insScoreMatInsPosPtr, ++insScoreMatCurRowPosPtr, ++delIndexMatCurRowPosPtr, ++insIndexMatCurRowPosPtr, ++qCharPtr, ++delPathMatCurRowPosPtr, ++insPathMatCurRowPosPtr, ++indexMatCurRowPosPtr) {
int matchScore, insScore, delScore;
int matchIndex, insIndex, delIndex;
int delOpenScore, delExtendScore;
int insOpenScore, insExtendScore;
//
// Del matrix (down)
//
// if (qE[ti-1] >= qi+qS[ti] && pathMat[prevRowPos] != bound) {
if (qE[ti-1] >= qi+qS[ti] && *pathMatPrevRowPosPtr != bound) {
// delOpenScore = scoreMat[prevRowPos] + gapOpen;
delOpenScore = *scoreMatPrevRowPosPtr + gapOpen;
// delExtendScore = delScoreMat[prevRowPos] + gapExtend;
delExtendScore = *delScoreMatPrevRowPosPtr + gapExtend;
// assert(pathMat[prevRowPos] != bound);
}
else {
delOpenScore = BAD;
delExtendScore = BAD;
}
int maxScore=max(delOpenScore, delExtendScore);
if (maxScore == delOpenScore) {
// delPathMat[curRowPos] = delOpen;
*delPathMatCurRowPosPtr = delOpen;
// delIndexMat[curRowPos] = prevRowPos;
*delIndexMatCurRowPosPtr = prevRowPos;
}
else {
// delPathMat[curRowPos] = delExtend;
*delPathMatCurRowPosPtr = delExtend;
// delIndexMat[curRowPos] = prevRowPos;
*delIndexMatCurRowPosPtr = prevRowPos;
}
// delScoreMat[curRowPos]=maxScore;
*delScoreMatCurRowPosPtr = maxScore;
//
// Insertion matrix.
//
// insOpenScore=scoreMat[curRowPos-1] + gapOpen;
insOpenScore = *scoreMatInsPosPtr + gapOpen;
// insExtendScore=insScoreMat[curRowPos-1] + gapExtend;
insExtendScore = *insScoreMatInsPosPtr + gapExtend;
maxScore=max(insOpenScore, insExtendScore);
if (maxScore == insOpenScore) {
// insPathMat[curRowPos] = insOpen;
*insPathMatCurRowPosPtr = insOpen;
// insIndexMat[curRowPos]= curRowPos-1;
*insIndexMatCurRowPosPtr = curRowPos-1;
}
else {
// insPathMat[curRowPos] = insExtend;
*insPathMatCurRowPosPtr = insExtend;
// insIndexMat[curRowPos] = curRowPos-1;
*insIndexMatCurRowPosPtr = curRowPos-1;
}
assert(insIndexMat[curRowPos] >= 0);
// insScoreMat[curRowPos] = maxScore;
*insScoreMatCurRowPosPtr = maxScore;
if (qE[ti-1] >= qi + qS[ti] && pathMat[prevRowPos-1] != bound) {
// if (tSeq[ti+tStart] == qSeq[qi+qS[ti]]) {
if (tChar == *qCharPtr) {
// matchScore = scoreMat[prevRowPos-1] + match;
matchScore = *scoreMatDiagPosPtr + match;
// assert(pathMat[prevRowPos-1] != bound);
}
else {
// matchScore = scoreMat[prevRowPos-1] + mismatch;
matchScore = *scoreMatDiagPosPtr + mismatch;
// assert(pathMat[prevRowPos-1] != bound);
}
}
else {
matchScore = BAD;
}
// insScore = scoreMat[curRowPos-1] + gap;
insScore = *scoreMatInsPosPtr + gap;
// if (qE[ti-1] >= qi+qS[ti] && pathMat[prevRowPos] != bound) {
if (qEPrev >= qi+qSCur && *pathMatPrevRowPosPtr != bound) {
// delScore = scoreMat[prevRowPos] + gap;
delScore = *scoreMatPrevRowPosPtr + gap;
// assert(pathMat[prevRowPos] != bound);
}
else {
delScore = BAD;
}
// int delCloseScore = delScoreMat[curRowPos];
int delCloseScore = *delScoreMatCurRowPosPtr;
// int insCloseScore = insScoreMat[curRowPos];
int insCloseScore = *insScoreMatCurRowPosPtr;
maxScore = max(matchScore,max(insScore, max(delScore, max(delCloseScore, insCloseScore))));
assert(curRowPos < scoreMat.size());
// scoreMat[curRowPos] = maxScore;
*scoreMatCurRowPosPtr = maxScore;
if (maxScore == matchScore) {
// pathMat[curRowPos] = diag;
*pathMatCurRowPosPtr = diag;
// assert(prevRowPos-1 < cuMatSize[ti]);
// indexMat[curRowPos] = prevRowPos-1;
*indexMatCurRowPosPtr = prevRowPos-1;
}
else if (maxScore == insScore) {
// pathMat[curRowPos] = left;
*pathMatCurRowPosPtr = left;
// indexMat[curRowPos] = curRowPos-1;
*indexMatCurRowPosPtr = curRowPos-1;
// assert(curRowPos-1 > cuMatSize[ti]);
}
else if (maxScore == delScore) {
// pathMat[curRowPos] = down;
*pathMatCurRowPosPtr = down;
// assert(prevRowPos <= cuMatSize[ti]);
// indexMat[curRowPos] = prevRowPos;
*indexMatCurRowPosPtr = prevRowPos;
}
else if (maxScore == delCloseScore) {
// pathMat[curRowPos] = delClose;
*pathMatCurRowPosPtr = delClose;
// indexMat[curRowPos] = curRowPos;
*indexMatCurRowPosPtr = curRowPos;
}
else if (maxScore == insCloseScore) {
// pathMat[curRowPos] = insClose;
*pathMatCurRowPosPtr = insClose;
//indexMat[curRowPos] = curRowPos;
*indexMatCurRowPosPtr = curRowPos;
}
}
prevRowStart += prevRowLen;
curRowStart += curRowLen;
prevRowLen = curRowLen;
}
//
// Trace back.
//
vector<int> path;
int matchMat=0, delMat=1, insMat=2;
int curMat=0;
int curMatPos=pathMat.size()-1;
while (curMatPos > 0) {
if (curMat == matchMat) {
if (pathMat[curMatPos] == delClose) {
// cout << "Hopping to del " << endl;
curMat=delMat;
}
else if (pathMat[curMatPos] == insClose) {
// cout << "Hopping to ins" << endl;
curMat=insMat;
}
else {
assert(curMatPos < pathMat.size());
path.push_back(pathMat[curMatPos]);
pathMat[curMatPos] = 90+pathMat[curMatPos];
}
curMatPos=indexMat[curMatPos];
}
else if (curMat == delMat) {
path.push_back(down);
if (delPathMat[curMatPos] == delOpen) {
curMat=matchMat;
}
else {
curMat=delMat;
}
curMatPos=delIndexMat[curMatPos];
assert(curMatPos >= 0);
}
else {
assert(curMat == insMat);
path.push_back(left);
if (insPathMat[curMatPos] == insOpen) {
curMat = matchMat;
}
else {
curMat=insMat;
}
curMatPos=insIndexMat[curMatPos];
assert(curMatPos >= 0);
}
assert(curMatPos >= 0);
}
path.push_back(diag);
int ci=0;
int totalI=0;
// if (startBlock == 0) {
// }
for (int r=0; r < qS.size(); r++) {
int np=0;
for (int ri=0; ri < qE[r] - qS[r] + 1; ri++) {
if (pathMat[ci] == 90 || pathMat[ci] == 92) { np++; totalI++;}
++ci;
}
/*
if (r > 0 and np != 1) {
PrintMat(pathMat, qS, qE);
cout << "ERROR at " << r << "\t" << np << endl;
}
assert(r == 0 || np == 1);
*/
}
// cout<< "Start " << startBlock << " end " << endBlock << endl;
// FlatPrintMat(pathMat, qS, qE);
// PrintMat(scoreMat, qS, qE);
long qPath=qStart;
long tPath=tStart;
int pi=path.size()-1;
int nm=0, ni=0,nd=0;
for (int p=0;p<path.size();p++) {
assert(path[p] >= 0&& path[p] <= 2);
if (path[p] == 0) { nm++;}
if (path[p] == 1) { ni++;}
if (path[p] == 2) { nd++;}
}
reverse(path.begin(), path.end());
assert(path[0] == diag);
// assert(path[pi] == diag);
pi=0;
while (pi < path.size()) {
int blockLen=0;
while (pi < path.size() and path[pi] == diag) {
blockLen++;
pi++;
}
int tgapLen=0;
int qgapLen=0;
if (pi < path.size()) {
if (path[pi] == left) {
while (pi < path.size() and path[pi] == left) {
qgapLen++;
pi++;
}
}
else if (path[pi] == down) {
while (pi < path.size() and path[pi] == down) {
tgapLen++;
pi++;
}
}
}
refined.push_back(Block(qPath,tPath, blockLen));
qPath+=blockLen+qgapLen;
tPath+=blockLen+tgapLen;
}
if (endBlock < alignment.blocks.size()-1) {
assert(tPath <= alignment.blocks[endBlock+1].tPos);
assert(qPath <= alignment.blocks[endBlock+1].qPos);
}
if (qPath != qSeqLen + qStart || tPath != tSeqLen + tStart) {
// cout << "ERROR on " << read.name << endl;
assert(qPath == qSeqLen + qStart);
assert(tPath == tSeqLen + tStart);
}
}
//cerr << "ratio " << scoreMat.size() / (float)qS.size() << endl;
}
if (usedAltEndBlock==false) {
endBlock++;
}
else {
alignment.blocks[endBlock]=altEndBlock;
}
startBlock=endBlock;
prevQGap=qGap;
prevTGap=tGap;
}
alignment.blocks = refined;
if (alignment.blocks.size() > 1) {
for (int b=0; b < alignment.blocks.size()-1; b++) {
if (alignment.blocks[b].qPos + alignment.blocks[b].length > alignment.blocks[b+1].qPos or
alignment.blocks[b].tPos + alignment.blocks[b].length > alignment.blocks[b+1].tPos ) {
cout << "ERROR with alignment consistency of " << read.name << endl;
cout << "block " << b << endl;
cout << "q: " << alignment.blocks[b].qPos + alignment.blocks[b].length << "\t" << alignment.blocks[b+1].qPos << endl;
cout << "t: " << alignment.blocks[b].tPos + alignment.blocks[b].length << "\t" << alignment.blocks[b+1].tPos << endl;
assert(0);
}
}
}
}
#endif