-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgapCloseOLC.cpp
More file actions
786 lines (658 loc) · 29.4 KB
/
Copy pathgapCloseOLC.cpp
File metadata and controls
786 lines (658 loc) · 29.4 KB
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
/*
Copyright (C) 2018 Itoh Laboratory, Tokyo Institute of Technology
This file is part of Platanus_B.
Platanus_B 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 2 of the License, or
(at your option) any later version.
Platanus_B 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 Platanus_B; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "gapCloseOLC.h"
#include "mapper.h"
#include <string>
#include <vector>
#include <unordered_map>
#include <list>
using std::string;
using std::vector;
//////////////////////////////////////////////////////////////////////////////////////
// static constant
//////////////////////////////////////////////////////////////////////////////////////
// not change value below parameters!!
const unsigned OverlapLayoutConsensus::HEADID = 0;
const unsigned OverlapLayoutConsensus::TAILID = 1;
//////////////////////////////////////////////////////////////////////////////////////
// make kmer table to approxiate overlap reads
//////////////////////////////////////////////////////////////////////////////////////
void OverlapLayoutConsensus::makeKmerTable(void)
{
for (unsigned long long i = 0; i < numSeq; ++i) {
string kmer;
if (gapInfo->seq[i].size() < kmerLength) continue;
for (unsigned j = 0; j < kmerLength; ++j) {
char tmp = gapInfo->seq[i][j] + static_cast<char>(1);
kmer += tmp;
}
kmerList[kmer].emplace_back(i);
for (unsigned j = kmerLength, n = gapInfo->seq[i].size(); j < n; ++j) {
kmer.erase(0, 1);
kmer += (gapInfo->seq[i][j] + static_cast<char>(1));
kmerList[kmer].emplace_back(i);
}
}
}
//////////////////////////////////////////////////////////////////////////////////////
// Check table is written whether tow reads overlap or not
//////////////////////////////////////////////////////////////////////////////////////
inline void OverlapLayoutConsensus::updateCheckTable(vector<vector<ALIGN_STATUS> > &checkTable, const unsigned long long readID1, const unsigned long long readID2, ALIGN_STATUS align)
{
checkTable[readID1][readID2 - readID1 - 1] = align;
}
//////////////////////////////////////////////////////////////////////////////////////
// check overlap between reads using pairwise alignment
//////////////////////////////////////////////////////////////////////////////////////
void OverlapLayoutConsensus::makeOverlapGraph(const unsigned minOverlap, const double maxMissRate)
{
this->initSeqLink();
vector<vector<ALIGN_STATUS> > checkTable(numSeq);
for (unsigned long long i = 0; i < numSeq - 1; ++i)
checkTable[i].resize(numSeq - 1 - i, YET);
// check seqs overlap which have same kmer
for (auto kmerListIterator = kmerList.begin(), kmerListEnd = kmerList.end(); kmerListIterator != kmerListEnd; ++kmerListIterator) {
for (auto pair1 = kmerListIterator->second.begin(), listEnd = kmerListIterator->second.end(); pair1 != listEnd; ++pair1) {
auto pair2 = pair1;
for (++pair2; pair2 != listEnd; ++pair2) {
if (*pair1 != *pair2 && checkAlreadyAlign(checkTable, *pair1, *pair2)) {
const ALIGN_STATUS aligned = pairwiseAlignment(*pair1, *pair2, minOverlap, maxMissRate);
updateCheckTable(checkTable, *pair1, *pair2, aligned);
}
}
}
}
// check seq overkap ownselef
for (unsigned long long i = 0; i < numSeq; ++i) {
pairwiseAlignmentOwnself(i, minOverlap, maxMissRate);
seqLink[i].tailLink.sort();
}
deleteKmerList();
}
//////////////////////////////////////////////////////////////////////////////////////
// set parent seq information in seqLink
//////////////////////////////////////////////////////////////////////////////////////
void OverlapLayoutConsensus::initSeqLink(void)
{
unsigned id = 0;
auto linkIt = seqLink.begin();
auto linkEnd = seqLink.end();
for(; linkIt != linkEnd; ++linkIt) {
linkIt->seqID = id;
linkIt->seqLength = gapInfo->seq[id].size();
++id;
}
}
//////////////////////////////////////////////////////////////////////////////////////
// exec pairwise alignment (wrapper function)
//////////////////////////////////////////////////////////////////////////////////////
inline OverlapLayoutConsensus::ALIGN_STATUS OverlapLayoutConsensus::pairwiseAlignment(const unsigned long long readID1, const unsigned long long readID2, const unsigned minOverlap, const double maxMissRate)
{
return pairwiseAlignmentOLC(readID1, readID2, minOverlap, maxMissRate, false);
}
inline void OverlapLayoutConsensus::pairwiseAlignmentOwnself(const unsigned long long readID, const unsigned minOverlap, const double maxMissRate)
{
if (seqLink[readID].seqLength >= minOverlap)
pairwiseAlignmentOLC(readID, readID, minOverlap, maxMissRate, true);
}
//////////////////////////////////////////////////////////////////////////////////////
// exec pairwise alignment
//////////////////////////////////////////////////////////////////////////////////////
OverlapLayoutConsensus::ALIGN_STATUS OverlapLayoutConsensus::pairwiseAlignmentOLC(const unsigned long long readID1, const unsigned long long readID2, const unsigned minOverlap, const double maxMissRate, const bool ownself)
{
/*
std::cout << ">seq1" << std::endl;
for (auto it = gapInfo->seq[readID1].begin(); it != gapInfo->seq[readID1].end(); ++it)
std::cout << platanus::Bin2Char(*it);
std::cout << std::endl;
std::cout << ">seq2" << std::endl;
for (auto it = gapInfo->seq[readID2].begin(); it != gapInfo->seq[readID2].end(); ++it)
std::cout << platanus::Bin2Char(*it);
std::cout << std::endl;
*/
vector<vector<Score> > alignmentTable(seqLink[readID1].seqLength + 1, vector<Score>(seqLink[readID2].seqLength + 1));
unsigned start = 1;
unsigned end = seqLink[readID1].seqLength - minOverlap + 1;
for (unsigned j = 1; j < seqLink[readID2].seqLength + 1; ++j) {
if (seqLink[readID2].seqLength - j + 1 < minOverlap) ++start;
if (end != seqLink[readID1].seqLength + 1) ++end;
for (unsigned i = start; i < end; ++i) {
// fill alignment table
// calculate each score
if (gapInfo->seq[readID1][i-1] == gapInfo->seq[readID2][j-1] && gapInfo->seq[readID1][i-1] != 4) {
alignmentTable[i][j].score = alignmentTable[i-1][j-1].score + 1;
alignmentTable[i][j].miss = alignmentTable[i-1][j-1].miss;
} else {
alignmentTable[i][j].score = alignmentTable[i-1][j-1].score;
alignmentTable[i][j].miss = alignmentTable[i-1][j-1].miss + 1;
}
}
}
// chekc align proper or not
return checkAlignmentMissRate(alignmentTable, readID1, readID2, minOverlap, maxMissRate, ownself);
// return checkAlignment(alignmentTable, readID1, readID2, minOverlap, maxEditDistance, ownself);
}
//////////////////////////////////////////////////////////////////////////////////////
// check alignment result is satisfied enougph aligned
// enough aligned means much overlapped and few missmatch
//////////////////////////////////////////////////////////////////////////////////////
OverlapLayoutConsensus::ALIGN_STATUS OverlapLayoutConsensus::checkAlignment(vector<vector<Score> > &table, const unsigned long long readID1, const unsigned long long readID2, const unsigned minOverlap, const unsigned maxEditDistance, const bool ownself)
{
Score maxScoreI, maxScoreJ;
ALIGN_STATUS align = NOT_ALIGNED;
std::pair<unsigned, unsigned> positionI, positionJ;
unsigned lengthI = seqLink[readID1].seqLength;
unsigned lengthJ = seqLink[readID2].seqLength;
unsigned i, j;
unsigned addTerminal = ownself ? 0 : 1;
// check overlap from j-i
// seq i -------------------
// seq j -----------------------
j = lengthJ;
for (i = minOverlap; i < lengthI + addTerminal; ++i) {
if (table[i][j].score >= maxScoreI.score && table[i][j].miss <= maxEditDistance) {
maxScoreI.score = table[i][j].score;
maxScoreI.miss = table[i][j].miss;
positionI = std::pair<unsigned, unsigned>(i, j);
}
}
// check overlap from i-j
// seq i ----------------------
// seq j -----------------------
i = lengthI;
for (j = minOverlap; j < lengthJ + addTerminal; ++j) {
if (table[i][j].score >= maxScoreJ.score && table[i][j].miss <= maxEditDistance) {
maxScoreJ.score = table[i][j].score;
maxScoreJ.miss = table[i][j].miss;
positionJ = std::pair<unsigned, unsigned>(i, j);
}
}
/*
std::cout << "ownself = " << (int)ownself << std::endl;
std::cout << "scoreI = " << (int)maxScoreI.score << std::endl;
std::cout << "positionI = " << (int)positionI.first << ", " << (int)positionI.second << std::endl;
std::cout << "scoreJ = " << (int)maxScoreJ.score << std::endl;
std::cout << "positionJ = " << (int)positionJ.first << ", " << (int)positionJ.second << std::endl;
*/
short scoreSub = maxScoreI.score - maxScoreJ.score;
if (std::max(maxScoreI.score, maxScoreJ.score) >= minOverlap) {
align = ALIGNED;
if ((scoreSub > 0 && positionI.first >= lengthJ)
|| (scoreSub == 0 && positionI.first == lengthI)) { }
else if (scoreSub < 0 && positionJ.second >= lengthI) { }
else {
if (ownself)
seqLink[readID1].ownOverlap = true;
else {
if (scoreSub >= 0) updateSeqLink(readID1, readID2, positionI, maxScoreI.miss, TO1);
if (scoreSub <= 0) updateSeqLink(readID1, readID2, positionJ, maxScoreJ.miss, TO2);
}
}
}
return align;
}
OverlapLayoutConsensus::ALIGN_STATUS OverlapLayoutConsensus::checkAlignmentMissRate(vector<vector<Score> > &table, const unsigned long long readID1, const unsigned long long readID2, const unsigned minOverlap, const double maxMissRate, const bool ownself)
{
Score maxScoreI, maxScoreJ;
ALIGN_STATUS align = NOT_ALIGNED;
std::pair<unsigned, unsigned> positionI, positionJ;
unsigned lengthI = seqLink[readID1].seqLength;
unsigned lengthJ = seqLink[readID2].seqLength;
unsigned i, j;
unsigned addTerminal = ownself ? 0 : 1;
// check overlap from j-i
// seq i -------------------
// seq j -----------------------
j = lengthJ;
for (i = minOverlap; i < lengthI + addTerminal; ++i) {
if (table[i][j].score >= maxScoreI.score && static_cast<double>(table[i][j].miss) / i <= maxMissRate) {
maxScoreI.score = table[i][j].score;
maxScoreI.miss = table[i][j].miss;
positionI = std::pair<unsigned, unsigned>(i, j);
}
}
// check overlap from i-j
// seq i ----------------------
// seq j -----------------------
i = lengthI;
for (j = minOverlap; j < lengthJ + addTerminal; ++j) {
if (table[i][j].score >= maxScoreJ.score && static_cast<double>(table[i][j].miss) / j <= maxMissRate) {
maxScoreJ.score = table[i][j].score;
maxScoreJ.miss = table[i][j].miss;
positionJ = std::pair<unsigned, unsigned>(i, j);
}
}
/*
std::cout << "ownself = " << (int)ownself << std::endl;
std::cout << "scoreI = " << (int)maxScoreI.score << std::endl;
std::cout << "positionI = " << (int)positionI.first << ", " << (int)positionI.second << std::endl;
std::cout << "scoreJ = " << (int)maxScoreJ.score << std::endl;
std::cout << "positionJ = " << (int)positionJ.first << ", " << (int)positionJ.second << std::endl;
*/
short scoreSub = maxScoreI.score - maxScoreJ.score;
if (std::max(maxScoreI.score, maxScoreJ.score) >= minOverlap) {
align = ALIGNED;
if ((scoreSub > 0 && positionI.first >= lengthJ)
|| (scoreSub == 0 && positionI.first == lengthI)) { }
else if (scoreSub < 0 && positionJ.second >= lengthI) { }
else {
if (ownself)
seqLink[readID1].ownOverlap = true;
else {
if (scoreSub >= 0) updateSeqLink(readID1, readID2, positionI, maxScoreI.miss, TO1);
if (scoreSub <= 0) updateSeqLink(readID1, readID2, positionJ, maxScoreJ.miss, TO2);
}
}
}
return align;
}
//////////////////////////////////////////////////////////////////////////////////////
// update link information
// seq information has omly tail(right) side
//////////////////////////////////////////////////////////////////////////////////////
void OverlapLayoutConsensus::updateSeqLink(const unsigned long long readID1, const unsigned long long readID2, const std::pair<unsigned, unsigned> &position, const unsigned miss, const OverlapLayoutConsensus::OVERLAP pattern)
{
unsigned difference = std::min(position.first, position.second);
LinkInformation link(difference, miss);
switch (pattern) {
case TO1:
{
link.seqID = readID1;
link.seqLength = seqLink[readID1].seqLength;
seqLink[readID2].tailLink.emplace_back(link);
break;
}
case TO2:
{
link.seqID = readID2;
link.seqLength = seqLink[readID2].seqLength;
seqLink[readID1].tailLink.emplace_back(link);
break;
}
case SAME:
{
if (position.first > position.second) {
link.seqID = readID2;
link.seqLength = seqLink[readID2].seqLength;
link.overlapLength = seqLink[readID1].seqLength - position.first - difference;
seqLink[readID1].tailLink.emplace_back(link);
} else {
link.seqID = readID1;
link.seqLength = seqLink[readID1].seqLength;
link.overlapLength = seqLink[readID2].seqLength - position.second - difference;
seqLink[readID2].tailLink.emplace_back(link);
}
break;
}
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// layout and consensus phase
//////////////////////////////////////////////////////////////////////////////////////
// do greedy extension
//////////////////////////////////////////////////////////////////////////////////////
bool OverlapLayoutConsensus::greedyExtension(const double threshold)
{
this->clearObject();
const unsigned topID = getMostOverlapHeadSeqID();
bool closed = false;
seqLayoutInfo[HEADID].startPosition = -1 * GapClose::HEAD_TAIL_SEQ_LEN;
seqLayoutInfo[topID].startPosition = -1 * seqLink[HEADID].tailLink.front().overlapLength;
unsigned numSeqUsed = 0;
// REASON reason;
// if topID equals tail seq, closed gap
if (isTailSeq(topID)) {
// reason = CLOSED;
seqLength = seqLayoutInfo[topID].startPosition - (seqLayoutInfo[topID].startPosition > 0 ? 0 : 1);
seq.resize(std::max(seqLength, 0));
closed = true;
return closed;
}
// exec consensus first
greedyFirst(topID, numSeqUsed);
ObservedSeq observed(topID, gapInfo->seq[topID], 0);
observedSeqInfo.emplace_back(std::move(observed));
long endPoint = seqLength + seqLayoutInfo[topID].startPosition;
seq.resize(endPoint);
for (long i = seqLayoutInfo[topID].startPosition; i < endPoint; ++i) {
vector<unsigned> newSeq = popCandidate(i);
for (long j = 0; j < static_cast<long>(newSeq.size()); ++j) {
// update and check layout confliction
ACTION action = updateSeqLayoutInfo(newSeq[j], numSeqUsed, i);
moveFromCandidateToObserved(newSeq[j]);
switch (action) {
// stop means conflict (duplication) is found in layout
case STOP:
{
// reason = DUPLICATION;
closed = false;
return closed;
break;
}
// redo means redo layout
case REDO:
{
if (this->hasHeadSeqLink()) {
bool comp = this->greedyExtension(threshold);
return comp;
} else {
closed = false;
return closed;
}
break;
}
case CONTINUE:
{
if (isTailSeq(newSeq[j])) {
// reason = CLOSED;
seqLength = i > 0 ? i : i - 1;
seq.resize(std::max(seqLength, 0));
closed = true;
return closed;
} else if (isOwnOverlap(newSeq[j])) {
// reason = OWN_OVERLAP;
closed = false;
return closed;
}
break;
}
}
}
// consensus
long nowSeqPosition = i > 0 ? i : 0;
seq[nowSeqPosition] = consensusBase(threshold);
if (notConsensus(seq[nowSeqPosition])) {
if (i > 0) {
seqLength = i;
// reason = UNKNOWN_BASE;
closed = false;
return closed;
}
}
updateObservedSeqInfo();
if (notExtension()) {
seqLength = i > 0 ? i : 0;
// reason = NO_SEQ;
closed = false;
return closed;
}
}
return closed;
}
//////////////////////////////////////////////////////////////////////////////////////
// consensus
//////////////////////////////////////////////////////////////////////////////////////
char OverlapLayoutConsensus::consensusBase(const double threshold) const
{
vector<unsigned> base(5, 0);
unsigned total = 0;
unsigned max = 0;
char maxBase = 0;
auto listIt = observedSeqInfo.begin();
auto listEnd = observedSeqInfo.end();
for (; listIt != listEnd; ++listIt) {
if (listIt->seq[listIt->nowPosition] > 4 || listIt->seq[listIt->nowPosition] < 0) {
throw platanus::BaseError();
}
++base[listIt->seq[listIt->nowPosition]];
++total;
}
for (char i = 0; i < 4; ++i) {
if (base[i] > max) {
max = base[i];
maxBase = i;
}
}
return (static_cast<double>(max) / total) >= threshold ? maxBase : 4;
}
//////////////////////////////////////////////////////////////////////////////////////
// update observedSeqInfo
// plus 1 seq position
// if seq position reaches seq end, erase this seq
//////////////////////////////////////////////////////////////////////////////////////
void OverlapLayoutConsensus::updateObservedSeqInfo(void)
{
auto listIt = observedSeqInfo.begin();
auto listEnd = observedSeqInfo.end();
for (; listIt != listEnd; ) {
++listIt->nowPosition;
if (listIt->nowPosition == listIt->seq.size()) {
listIt = observedSeqInfo.erase(listIt);
continue;
}
++listIt;
}
}
//////////////////////////////////////////////////////////////////////////////////////
// set first candidate seqs used greedy extension
//////////////////////////////////////////////////////////////////////////////////////
void OverlapLayoutConsensus::greedyFirst(const unsigned topID, unsigned &numSeqUsed)
{
auto listIt = seqLink[topID].tailLink.begin();
auto listEnd = seqLink[topID].tailLink.end();
seqLayoutInfo[topID].used = true;
for (; listIt != listEnd; ++listIt) {
addCandidateSeq(*listIt, seqLink[topID].seqLength, seqLayoutInfo[topID].startPosition);
++numSeqUsed;
}
}
//////////////////////////////////////////////////////////////////////////////////////
// set candidate seq actually
//////////////////////////////////////////////////////////////////////////////////////
void OverlapLayoutConsensus::addCandidateSeq(const LinkInformation &link, const unsigned seqLength, const unsigned startPosition)
{
seqLayoutInfo[link.seqID].startPosition = startPosition + seqLength - link.overlapLength;
seqLayoutInfo[link.seqID].used = true;
candidateSeqList.emplace_back(link.seqID);
}
//////////////////////////////////////////////////////////////////////////////////////
// update seqLayoutInfo (add new child seq in candidate)
//////////////////////////////////////////////////////////////////////////////////////
OverlapLayoutConsensus::ACTION OverlapLayoutConsensus::updateSeqLayoutInfo(const unsigned parentID, unsigned &num, const long nowDistance)
{
ACTION action = CONTINUE;
auto listIt = seqLink[parentID].tailLink.begin();
auto listEnd = seqLink[parentID].tailLink.end();
// check whether position has contradiction
for (; listIt != listEnd; ++listIt) {
POSITION_CHECK check = checkDistance(listIt->seqID, listIt->overlapLength, parentID, nowDistance);
if (check == INCORRECT) {
// decide which position is proper (parents or child)
PROPER_LINK which = selectProperLink(parentID, listIt->seqID);
if (checkLinkFar(seqLayoutInfo[listIt->seqID].startPosition, nowDistance + seqLink[parentID].seqLength - listIt->overlapLength, seqLink[parentID].seqLength)) which = UNKNOWN;
switch (which) {
case OWN:
{
auto observedIt = observedSeqInfo.begin();
auto observedEnd = observedSeqInfo.end();
for (; observedIt != observedEnd; ++observedIt) {
deleteLink(observedIt->seqID, listIt->seqID);
}
action = REDO;
break;
}
case UNKNOWN:
{
return STOP;
break;
}
case OTHERS:
{
break;
}
}
} else if (check == NOT_USED) {
updateStartPosition(listIt->seqID, listIt->overlapLength, nowDistance, seqLink[parentID].seqLength);
candidateSeqList.emplace_back(listIt->seqID);
++num;
}
}
return action;
}
//////////////////////////////////////////////////////////////////////////////////////
// check child seq start position is appropriate
//////////////////////////////////////////////////////////////////////////////////////
OverlapLayoutConsensus::POSITION_CHECK OverlapLayoutConsensus::checkDistance(const unsigned childID, const unsigned overlapLength, const unsigned parentID, const long nowDistance) const
{
if (seqLayoutInfo[childID].used == false)
return NOT_USED;
else if (isSameStartPositionAmongDifferentParents(childID, overlapLength, parentID, nowDistance))
return CORRECT;
else
return INCORRECT;
}
//////////////////////////////////////////////////////////////////////////////////////
// judge child seq start position is same or not among different parents which has link the child.
//////////////////////////////////////////////////////////////////////////////////////
inline bool OverlapLayoutConsensus::isSameStartPositionAmongDifferentParents(const unsigned childID, const unsigned overlapLength, const unsigned parentID, const long nowDistance) const
{
return seqLayoutInfo[childID].startPosition == static_cast<int>(nowDistance + seqLink[parentID].seqLength - overlapLength);
}
//////////////////////////////////////////////////////////////////////////////////////
// decide which parent link is more proper
// child ------------------- -|
// parent 1 ---------------------- |- which link better ?
// child ------------- -|
// parent 2 ----------------------
// the lowest missmathcs link is chosen
//////////////////////////////////////////////////////////////////////////////////////
OverlapLayoutConsensus::PROPER_LINK OverlapLayoutConsensus::selectProperLink(const unsigned parentID, const unsigned childID) const
{
auto linkIt = seqLink[parentID].tailLink.begin();
auto linkEnd = seqLink[parentID].tailLink.end();
unsigned parentMiss = 0;
bool win = false;
for (; linkIt != linkEnd; ++linkIt) {
if (linkIt->seqID == childID) {
parentMiss = linkIt->miss;
break;
}
}
auto infoIt = observedSeqInfo.begin();
auto infoEnd = observedSeqInfo.end();
for (; infoIt != infoEnd; ++infoIt) {
linkIt = seqLink[infoIt->seqID].tailLink.begin();
linkEnd = seqLink[infoIt->seqID].tailLink.end();
for (; linkIt != linkEnd; ++linkIt) {
if (linkIt->seqID == childID) {
if (linkIt->miss < parentMiss) {
return OTHERS;
} else if (linkIt->miss == parentMiss) {
return UNKNOWN;
} else {
win = true;
break;
}
}
}
}
return win ? OWN : OTHERS;
}
//////////////////////////////////////////////////////////////////////////////////////
// check parent links are too far
// child -------------------
// parent 1 ---------------------
// child -------------
// parent 2 ----------------------
//////////////////////////////////////////////////////////////////////////////////////
bool OverlapLayoutConsensus::checkLinkFar(const int othersLinkStartPositionToChild, const int ownLinkStartPositionToChild, const unsigned ownLength) const
{
return abs(othersLinkStartPositionToChild - ownLinkStartPositionToChild) >= ownLength;
}
//////////////////////////////////////////////////////////////////////////////////////
// pop new observed seq into candidates
//////////////////////////////////////////////////////////////////////////////////////
vector<unsigned> OverlapLayoutConsensus::popCandidate(const long seqPosition)
{
vector<unsigned> newSeq;
auto listIt = candidateSeqList.begin();
auto listEnd = candidateSeqList.end();
for (; listIt != listEnd; ) {
if (seqPosition == seqLayoutInfo[*listIt].startPosition) {
newSeq.emplace_back(*listIt);
listIt = candidateSeqList.erase(listIt);
continue;
}
++listIt;
}
return newSeq;
}
//////////////////////////////////////////////////////////////////////////////////////
// update child start position
//////////////////////////////////////////////////////////////////////////////////////
void OverlapLayoutConsensus::updateStartPosition(const unsigned childID, const int overlap, const int distance, const unsigned seqLength)
{
seqLayoutInfo[childID].startPosition = distance + seqLength - overlap;
seqLayoutInfo[childID].used = true;
}
//////////////////////////////////////////////////////////////////////////////////////
// move child seq form candidate to observed
//////////////////////////////////////////////////////////////////////////////////////
void OverlapLayoutConsensus::moveFromCandidateToObserved(const unsigned newSeqID)
{
ObservedSeq newObserved(newSeqID, gapInfo->seq[newSeqID], 0);
observedSeqInfo.emplace_back(std::move(newObserved));
auto listIt = candidateSeqList.begin();
auto listEnd = candidateSeqList.end();
for (; listIt != listEnd; ++listIt) {
if (*listIt == newSeqID) {
candidateSeqList.erase(listIt);
break;
}
}
}
//////////////////////////////////////////////////////////////////////////////////////
// delete link
//////////////////////////////////////////////////////////////////////////////////////
void OverlapLayoutConsensus::deleteLink(const unsigned parentID, const unsigned childID)
{
auto linkIt = seqLink[parentID].tailLink.begin();
auto linkEnd = seqLink[parentID].tailLink.end();
for (; linkIt != linkEnd; ++linkIt) {
if (linkIt->seqID == childID) {
seqLink[parentID].tailLink.erase(linkIt);
break;
}
}
}
//////////////////////////////////////////////////////////////////////////////////////
// check newSeq is tail
//////////////////////////////////////////////////////////////////////////////////////
inline bool OverlapLayoutConsensus::isTailSeq(const unsigned newSeq) const
{
return newSeq == TAILID;
}
//////////////////////////////////////////////////////////////////////////////////////
// check newSeq is overlapeed ownself
//////////////////////////////////////////////////////////////////////////////////////
inline bool OverlapLayoutConsensus::isOwnOverlap(const unsigned newSeq) const
{
return seqLink[newSeq].ownOverlap;
}
//////////////////////////////////////////////////////////////////////////////////////
// check newSeq is overlapeed ownself
//////////////////////////////////////////////////////////////////////////////////////
inline bool OverlapLayoutConsensus::notConsensus(const char base) const
{
return base == 4;
}
//////////////////////////////////////////////////////////////////////////////////////
// check observed seq is empty
//////////////////////////////////////////////////////////////////////////////////////
inline bool OverlapLayoutConsensus::notExtension(void) const
{
return observedSeqInfo.empty();
}