-
Notifications
You must be signed in to change notification settings - Fork 9
/
PhasingGraph.cpp
766 lines (659 loc) · 30.6 KB
/
PhasingGraph.cpp
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
#include "PhasingGraph.h"
//SubEdge
SubEdge::SubEdge():readCount(0){
refRead = new std::map<int, std::vector<std::string> >;
altRead = new std::map<int, std::vector<std::string> >;
refQuality = new std::map<int, int>;
altQuality = new std::map<int, int>;
refReadCount = new std::map<int, float>;
altReadCount = new std::map<int, float>;
}
SubEdge::~SubEdge(){
}
void SubEdge::destroy(){
delete refRead;
delete altRead;
delete refQuality;
delete altQuality;
delete refReadCount;
delete altReadCount;
}
void SubEdge::addSubEdge(int currentQuality, Variant connectNode, std::string readName, int baseQuality, double edgeWeight){
// target noded is REF allele
if(connectNode.allele == 0 ){
// debug, this parameter will record the names of all reads between two points
//(*refRead)[connectNode.position].push_back(readName);
/*// quality sum
std::map<int, int>::iterator rqIter = refQuality->find(connectNode.position);
if( rqIter == refQuality->end() ){
(*refQuality)[connectNode.position] = currentQuality + connectNode.quality;
}
else{
(*refQuality)[connectNode.position] += currentQuality + connectNode.quality;
}*/
if ( currentQuality >= baseQuality && connectNode.quality >= baseQuality )
(*refReadCount)[connectNode.position]++;
else {
(*refReadCount)[connectNode.position] = (*refReadCount)[connectNode.position] + edgeWeight ;
}
//(*refReadCount)[connectNode.position]++;
}
// target noded is ALT allele
else if(connectNode.allele == 1 ){
// debug, this parameter will record the names of all reads between two points
// (*altRead)[connectNode.position].push_back(readName);
/*// quality sum
std::map<int, int>::iterator aqIter = altQuality->find(connectNode.position);
if( aqIter == altQuality->end() ){
(*altQuality)[connectNode.position] = currentQuality + connectNode.quality;
}
else{
(*altQuality)[connectNode.position] += currentQuality + connectNode.quality;
}*/
if ( currentQuality >= baseQuality && connectNode.quality >= baseQuality )
(*altReadCount)[connectNode.position]++;
else {
(*altReadCount)[connectNode.position] = (*altReadCount)[connectNode.position] + edgeWeight ;
}
//(*altReadCount)[connectNode.position]++;
}
readCount++;
}
std::pair<float,float> SubEdge::BestPair(int targetPos){
return std::make_pair( getRefReadCount(targetPos), getAltReadCount(targetPos) );
}
float SubEdge::getRefReadCount(int targetPos){
std::map<int, float>::iterator posIter = refReadCount->find(targetPos);
if( posIter != refReadCount->end() ){
return (*refReadCount)[targetPos];
}
return 0;
}
float SubEdge::getAltReadCount(int targetPos){
std::map<int, float>::iterator posIter = altReadCount->find(targetPos);
if( posIter != altReadCount->end() ){
return (*altReadCount)[targetPos];
}
return 0;
}
std::vector<std::string> SubEdge::showEdge(std::string message){
std::vector<std::string> result;
for(std::map<int, float >::iterator edgeIter = refReadCount->begin() ; edgeIter != refReadCount->end() ; edgeIter++ ){
result.push_back(message +" -> ref_" + std::to_string((*edgeIter).first) + "[label=" + std::to_string((*edgeIter).second) + "];");
}
for(std::map<int, float >::iterator edgeIter = altReadCount->begin() ; edgeIter != altReadCount->end() ; edgeIter++ ){
result.push_back(message +" -> alt_" + std::to_string((*edgeIter).first) + "[label=" + std::to_string((*edgeIter).second) + "];");
}
return result;
}
std::vector<std::pair<int,int>> SubEdge::getConnectPos(){
std::vector<std::pair<int,int>> result;
for(std::map<int, float >::iterator edgeIter = refReadCount->begin() ; edgeIter != refReadCount->end() ; edgeIter++ ){
result.push_back( std::make_pair( (*edgeIter).first, 0 ) );
}
for(std::map<int, float >::iterator edgeIter = altReadCount->begin() ; edgeIter != altReadCount->end() ; edgeIter++ ){
result.push_back( std::make_pair( (*edgeIter).first, 1 ) );
}
return result;
}
int SubEdge::getQuality(PosAllele targetPos){
// target is Ref allele
if( targetPos.second == 1 ){
std::map<int, int>::iterator qIter = refQuality->find(targetPos.first);
if( qIter == refQuality->end() )
return 0;
else
return (*refQuality)[targetPos.first];
}
// target is Alt allele
if( targetPos.second == 2 ){
std::map<int, int>::iterator qIter = altQuality->find(targetPos.first);
if( qIter == altQuality->end() )
return 0;
else
return (*altQuality)[targetPos.first];
}
return 0;
}
int SubEdge::getAvgQuality(PosAllele targetPos){
// target is Ref allele
if( targetPos.second == 1 ){
std::map<int, int>::iterator qIter = refQuality->find(targetPos.first);
if( qIter == refQuality->end() )
return 0;
else
return (*refQuality)[targetPos.first]/(*refReadCount)[targetPos.first];
}
// target is Alt allele
if( targetPos.second == 2 ){
std::map<int, int>::iterator qIter = altQuality->find(targetPos.first);
if( qIter == altQuality->end() )
return 0;
else
return (*altQuality)[targetPos.first]/(*altReadCount)[targetPos.first];
}
return 0;
}
VariantEdge::VariantEdge(int inCurrPos){
currPos = inCurrPos;
alt = new SubEdge();
ref = new SubEdge();
}
//VariantEdge
std::pair<PosAllele,PosAllele> VariantEdge::findBestEdgePair(int targetPos, bool isONT, double edgeThreshold, bool debug){
std::pair<float,float> refBestPair = ref->BestPair(targetPos);
std::pair<float,float> altBestPair = alt->BestPair(targetPos);
// get the weight of each pair
float rr = refBestPair.first;
float ra = refBestPair.second;
float ar = altBestPair.first;
float aa = altBestPair.second;
// initialize the edge connection
// -1 : not connect
int refAllele = -1;
int altAllele = -1;
double edgeSimilarRatio = (double)std::min((rr+aa),(ar+ra)) / (double)std::max((rr+aa),(ar+ra));
if( rr + aa > ra + ar ){
// RR conect
refAllele = 1;
altAllele = 2;
}
else if( rr + aa < ra + ar ){
// RA connect
refAllele = 2;
altAllele = 1;
}
else if( rr + aa == ra + ar ){
// no connect
// not sure which is better
}
if( edgeSimilarRatio > edgeThreshold ){
refAllele = -1;
altAllele = -1;
}
if(debug){
std::cout<< currPos << "\t->\t" << targetPos << "\t|rr aa | ra ar\t" << "\t" << rr << "\t" << aa << "\t" << ra << "\t" << ar << "\n";
}
// create edge pairs
PosAllele refEdge = std::make_pair( targetPos, refAllele );
PosAllele altEdge = std::make_pair( targetPos, altAllele );
// return edge pair
return std::make_pair( refEdge, altEdge );
}
std::pair<int,int> VariantEdge::findNumberOfRead(int targetPos){
std::pair<int,int> refBestPair = ref->BestPair(targetPos);
std::pair<int,int> altBestPair = alt->BestPair(targetPos);
// get the weight of each pair
int rr = refBestPair.first;
int ra = refBestPair.second;
int ar = altBestPair.first;
int aa = altBestPair.second;
return std::make_pair( rr + aa , ra +ar );
}
//BlockRead
void BlockRead::recordRead(std::string readName){
std::map<std::string,int>::iterator readIter = readVec.find(readName);
if( readIter == readVec.end() )
readVec[readName] = 1;
else
readVec[readName]++;
}
//VairiantGraph
void VairiantGraph::edgeConnectResult(){
// current snp, haplotype (1 or 2), support snp
std::map<int, std::map<int,std::vector<int> > > *hpCountMap = new std::map<int, std::map<int,std::vector<int> > >;
// current snp, result haplotype (1 or 2)
std::map<int,int> *hpResult = new std::map<int,int>;
// < block start, <snp in this block> >
std::map<int,std::vector<int> > *phasedBlocks = new std::map<int,std::vector<int> >;
int blockStart = -1;
int currPos = -1;
int nextPos = -1;
int lastConnectPos = -1;
// Visit all position and assign SNPs to haplotype.
// Avoid recording duplicate information,
// only one of the two alleles needs to be used for each SNP
for(std::map<int,ReadBaseMap*>::iterator variantIter = totalVariantInfo->begin() ; variantIter != totalVariantInfo->end() ; variantIter++ ){
// check next position
std::map<int,ReadBaseMap*>::iterator nextNodeIter = std::next(variantIter, 1);
if( nextNodeIter == totalVariantInfo->end() ){
break;
}
currPos = variantIter->first;
nextPos = nextNodeIter->first;
// There should not be a large distance between any two variants,
// with the default being a distance of 300000bp, equivalent to one centromere length.
if(std::abs(nextPos-currPos) > params->distance ){
continue;
}
// get the number of HP1 and HP2 supported reference allele
int h1 = (*hpCountMap)[currPos][1].size();
int h2 = (*hpCountMap)[currPos][2].size();
// new block, set this position as block start
if( h1 == 0 && h2 == 0 ){
// No new blocks should be created if the next SNP has already been picked up
if( currPos < lastConnectPos ){
continue;
}
blockStart = currPos;
(*phasedBlocks)[blockStart].push_back(currPos);
(*hpResult)[currPos] = 1;
}
else{
if( h1 > h2 || h1 < h2 ){
int currHP = ( h1 > h2 ? 1 : 2 );
(*hpResult)[currPos] = currHP;
(*phasedBlocks)[blockStart].push_back(currPos);
}
}
// Check if there is no edge from current node
std::map<int,VariantEdge*>::iterator edgeIter = edgeList->find( currPos );
if( edgeIter==edgeList->end() ){
continue;
}
// check connect between surrent SNP and next n SNPs
for(int i = 0 ; i < params->connectAdjacent ; i++ ){
// consider reads from the currnt SNP and the next (i+1)'s SNP
std::pair<PosAllele,PosAllele> tmp = edgeIter->second->findBestEdgePair(nextNodeIter->first, params->isONT, params->edgeThreshold, false);
// -1 : no connect
// 1 : the haplotype of next (i+1)'s SNP are same as previous
// 2 : the haplotype of next (i+1)'s SNP are different as previous
if( tmp.first.second != -1 ){
// record the haplotype resut of next (i+1)'s SNP
if( (*hpResult)[currPos] == 1 ){
if( tmp.first.second == 1 ){
(*hpCountMap)[nextNodeIter->first][1].push_back(currPos);
}
if( tmp.first.second == 2 ){
(*hpCountMap)[nextNodeIter->first][2].push_back(currPos);
}
}
if( (*hpResult)[currPos]==2 ){
if( tmp.first.second == 1 ){
(*hpCountMap)[nextNodeIter->first][2].push_back(currPos);
}
if( tmp.first.second == 2 ){
(*hpCountMap)[nextNodeIter->first][1].push_back(currPos);
}
}
if( params->generateDot ){
std::string e1 = std::to_string(currPos+1) + ".1\t->\t" + std::to_string(tmp.first.first+1) + "." + std::to_string(tmp.first.second);
std::string e2 = std::to_string(currPos+1) + ".2\t->\t" + std::to_string(tmp.second.first+1) + "." + std::to_string(tmp.second.second);
dotResult.push_back(e1);
dotResult.push_back(e2);
}
lastConnectPos = nextNodeIter->first;
}
nextNodeIter++;
if( nextNodeIter == totalVariantInfo->end() ){
break;
}
}
}
// loop all block and construct graph
// Record the phase set(PS) for each variant on the graph and record the haplotype to each variant's allele belongs.
for(auto blockIter = phasedBlocks->begin() ; blockIter != phasedBlocks->end() ; blockIter++ ){
// check block size to skip one node island
if( (*blockIter).second.size()<=1 ){
continue;
}
// loop block's node
// store phasing results include PS and HP
for(auto currIter = (*blockIter).second.begin() ; currIter != (*blockIter).second.end() ; currIter++ ){
// check next node
auto nextIter = std::next(currIter,1);
if( nextIter == (*blockIter).second.end() ){
continue;
}
PosAllele refStart = std::make_pair((*currIter), 1);
PosAllele altStart = std::make_pair((*currIter), 2);
PosAllele refEnd = std::make_pair((*nextIter), 1);
PosAllele altEnd = std::make_pair((*nextIter), 2);
// store PS results
(*bkResult)[refStart] = (*blockIter).first + 1;
(*bkResult)[refEnd] = (*blockIter).first + 1;
(*bkResult)[altStart] = (*blockIter).first + 1;
(*bkResult)[altEnd] = (*blockIter).first + 1;
// store HP results
if( currIter == (*blockIter).second.begin() ){
(*subNodeHP)[refStart] = 0;
(*subNodeHP)[altStart] = 1;
}
if( (*hpResult)[(*currIter)] == 0 || (*hpResult)[(*nextIter)] == 0 ){
}
else if( (*hpResult)[(*currIter)] == (*hpResult)[(*nextIter)] ){
(*subNodeHP)[refEnd] = (*subNodeHP)[refStart];
(*subNodeHP)[altEnd] = (*subNodeHP)[altStart];
}
else{
(*subNodeHP)[refEnd] = (*subNodeHP)[altStart];
(*subNodeHP)[altEnd] = (*subNodeHP)[refStart];
}
}
}
delete hpCountMap;
delete hpResult;
delete phasedBlocks;
}
VairiantGraph::VairiantGraph(std::string &in_ref, PhasingParameters &in_params){
params=&in_params;
ref=&in_ref;
totalVariantInfo = new std::map<int,ReadBaseMap*>;
edgeList = new std::map<int,VariantEdge*>;
bkResult = new std::map<PosAllele,int>;
subNodeHP = new std::map<PosAllele,int>;
variantType = new std::map<int,int>;
readHpMap = new std::map<std::string,int>;
}
VairiantGraph::~VairiantGraph(){
}
void VairiantGraph::destroy(){
dotResult.clear();
dotResult.shrink_to_fit();
for( auto edgeIter = edgeList->begin() ; edgeIter != edgeList->end() ; edgeIter++ ){
edgeIter->second->ref->destroy();
edgeIter->second->alt->destroy();
delete edgeIter->second->ref;
delete edgeIter->second->alt;
}
for( auto variantIter = totalVariantInfo->begin() ; variantIter != totalVariantInfo->end() ; variantIter++ ){
delete variantIter->second;
}
delete totalVariantInfo;
delete edgeList;
delete bkResult;
delete subNodeHP;
delete variantType;
delete readHpMap;
}
void VairiantGraph::addEdge(std::vector<ReadVariant> &in_readVariant){
readVariant = &in_readVariant;
std::map<std::string,ReadVariant> mergeReadMap;
// each read will record fist and list variant posistion
std::map<std::string, std::pair<int,int>> alignRange;
// record an iterator for all alignments of a read.
std::map<std::string, std::vector<int>> readIdxVec;
// record need del read index
std::vector<int> delReadIdx;
// Check for overlaps among different alignments of a read and filter out the shorter overlapping alignments.
for(int readIter = 0 ; readIter < (int)in_readVariant.size() ; readIter++ ){
std::string readName = in_readVariant[readIter].read_name;
int firstVariantPos = in_readVariant[readIter].variantVec[0].position;
int lastVariantPos = in_readVariant[readIter].variantVec[in_readVariant[readIter].variantVec.size()-1].position;
auto rangeIter = alignRange.find(readName);
// this read name appears for the first time
if( rangeIter == alignRange.end() ){
alignRange[readName]=std::make_pair(firstVariantPos,lastVariantPos);
}
// the read appears more than once, check if the alignments overlap
else{
// overlap
if( alignRange[readName].first <= firstVariantPos && firstVariantPos <= alignRange[readName].second ){
double alignStart = std::min(alignRange[readName].first, firstVariantPos);
double alignEnd = std::max(alignRange[readName].second, lastVariantPos);
double alignSpan = alignEnd - alignStart + 1;
double overlapStart = std::max(alignRange[readName].first, firstVariantPos);
double overlapEnd = std::min(alignRange[readName].second, lastVariantPos);
double overlapLen = overlapEnd - overlapStart + 1;
double overlapRatio = overlapLen / alignSpan;
//filtering highly overlapping alignments.
if( overlapRatio >= params->overlapThreshold ){
int alignLen1 = alignRange[readName].second - alignRange[readName].first + 1;
int alignLen2 = lastVariantPos - firstVariantPos + 1;
// filter shorter alignment
// current alignment is shorter
if( alignLen2 <= alignLen1 ){
delReadIdx.push_back(readIter);
}
// previous alignment is shorter
else{
// iterate all previous alignments
for(int iter = 0 ; iter < (int)readIdxVec[readName].size() ; iter++ ){
delReadIdx.push_back(readIdxVec[readName][iter]);
}
// update range
alignRange[readName].first = firstVariantPos;
alignRange[readName].second = lastVariantPos;
readIdxVec[readName].clear();
readIdxVec[readName].push_back(readIter);
}
continue;
}
}
// update range
alignRange[readName].second = lastVariantPos;
}
readIdxVec[readName].push_back(readIter);
}
// sort read index
std::sort(delReadIdx.begin(), delReadIdx.end());
// remove overlap alignment
for( int idx = delReadIdx.size() -1 ; idx > 0 ; idx-- ){
in_readVariant.erase( in_readVariant.begin() + delReadIdx[idx] );
}
int readCount=0;
// merge alignment
for(std::vector<ReadVariant>::iterator readIter = in_readVariant.begin() ; readIter != in_readVariant.end() ; readIter++ ){
// Creating a pseudo read which allows filtering out variants that should not be phased
//ReadVariant tmpRead;
// Visiting all the variants on the read
for( auto variant : (*readIter).variantVec ){
readCount++;
// modification
if( variant.quality == -2 || variant.quality == -3 ){
(*variantType)[variant.position] = 2;
variant.quality = 60;
}
// structure variation
else if( variant.quality == -1 ){
(*variantType)[variant.position] = 1;
if( variant.allele == 1 ){
// SVcaller calling
variant.quality = 60;
}
else{
// In SVcaller, unmarked reads are assumed to be REF
variant.quality = 30;
}
}
// indel
else if( variant.quality == -4 ){
(*variantType)[variant.position] = 3;
variant.quality = 60;
}
// The remaining variants will be labeled as SNPs
else{
(*variantType)[variant.position] = 0;
}
mergeReadMap[(*readIter).read_name].variantVec.push_back(variant);
//tmpRead.variantVec.push_back(variant);
// Each position will record the included reads and their corresponding base qualities.
auto variantIter = totalVariantInfo->find(variant.position);
if( variantIter == totalVariantInfo->end() ){
(*totalVariantInfo)[variant.position] = new ReadBaseMap();
}
(*(*totalVariantInfo)[variant.position])[(*readIter).read_name] = variant.quality;
}
}
for(std::map<std::string,ReadVariant>::iterator readIter = mergeReadMap.begin() ; readIter != mergeReadMap.end() ; readIter++){
(*readIter).second.sort();
// iter all pair of snp and construct initial graph
std::vector<Variant>::iterator variant1Iter = (*readIter).second.variantVec.begin();
std::vector<Variant>::iterator variant2Iter = std::next(variant1Iter,1);
while(variant1Iter != (*readIter).second.variantVec.end() && variant2Iter != (*readIter).second.variantVec.end() ){
// create new edge if not exist
std::map<int,VariantEdge*>::iterator posIter = edgeList->find((*variant1Iter).position);
if( posIter == edgeList->end() )
(*edgeList)[(*variant1Iter).position] = new VariantEdge((*variant1Iter).position);
// add edge process
for(int nextNode = 0 ; nextNode < params->connectAdjacent; nextNode++){
// this allele support ref
if( (*variant1Iter).allele == 0 )
(*edgeList)[(*variant1Iter).position]->ref->addSubEdge((*variant1Iter).quality, (*variant2Iter),(*readIter).first,params->baseQuality,params->edgeWeight);
// this allele support alt
if( (*variant1Iter).allele == 1 )
(*edgeList)[(*variant1Iter).position]->alt->addSubEdge((*variant1Iter).quality, (*variant2Iter),(*readIter).first,params->baseQuality,params->edgeWeight);
// next snp
variant2Iter++;
if( variant2Iter == (*readIter).second.variantVec.end() ){
break;
}
}
variant1Iter++;
variant2Iter = std::next(variant1Iter,1);
}
}
}
void VairiantGraph::readCorrection(){
std::map<std::string,std::map<int,std::map<int,int>>> readBlockHP;
//
std::map<std::string,std::map<int,std::map<int,int>>> readBlockHPcount;
// haplotype, <position <allele, base count>>
std::map<int,std::map<int,std::map<int,int>>> *hpAlleleCountMap = new std::map<int,std::map<int,std::map<int,int>>>;
// iter all read, determine the haplotype of the read
for(std::vector<ReadVariant>::iterator readIter = (*readVariant).begin() ; readIter != (*readVariant).end() ; readIter++ ){
double refCount = 0;
double altCount = 0;
//int block;
// loop all variant
for( auto variant : (*readIter).variantVec ){
PosAllele refAllele = std::make_pair( variant.position , variant.allele+1);
std::map<PosAllele,int>::iterator nodePS = bkResult->find(refAllele);
//block = nodePS->second;
if( nodePS != bkResult->end() ){
if( (*bkResult)[refAllele] != 0 ){
if((*subNodeHP)[refAllele]==0)refCount++;
else altCount++;
}
}
}
// tag high confident reads
if( std::max(refCount,altCount)/(refCount+altCount) > params->readConfidence && (refCount + altCount) > 1 ){
// tag read with the corresponding haplotype
int belongHP = ( refCount > altCount ? 0 : 1 );
(*readHpMap)[(*readIter).read_name] = belongHP;
//readBlockHP[(*readIter).read_name][(*readIter).reference_start][block]=belongHP;
//readBlockHPcount[(*readIter).read_name][block][belongHP]++;
for(auto variantIter = (*readIter).variantVec.begin() ; variantIter != (*readIter).variantVec.end() ; variantIter++ ){
if( (*variantIter).allele == 0 || (*variantIter).allele == 1){
(*hpAlleleCountMap)[belongHP][(*variantIter).position][(*variantIter).allele]++;
}
}
}
else{
(*readHpMap)[(*readIter).read_name] = -1;
}
}
/*
for(auto readIter = readBlockHP.begin() ; readIter != readBlockHP.end() ; readIter++ ){
int max = 0;
for(auto blockIter = readBlockHPcount[readIter->first].begin() ; blockIter != readBlockHPcount[readIter->first].end() ; blockIter++ ){
if( (*blockIter).second.size() > max ){
max = (*blockIter).second.size();
}
}
std::cout<< readIter->first << "\t" << max;
for(auto startIter = readIter->second.begin() ; startIter != readIter->second.end() ; startIter++ ){
std::cout<< "\t" << startIter->first << ",";
for(auto blockIter = startIter->second.begin() ; blockIter != startIter->second.end() ; blockIter++ ){
std::cout<< blockIter->first << "," << blockIter->second;
}
}
std::cout<< "\n";
}
*/
double snpConfidenceThreshold = params->snpConfidence;
subNodeHP->clear();
std::map<int,std::map<int,int>> hpAllele;
// reassign allele result
for(auto variantIter = totalVariantInfo->begin() ; variantIter != totalVariantInfo->end() ; variantIter++ ){
int position = variantIter->first;
PosAllele refAllele = std::make_pair(position, 1);
PosAllele altAllele = std::make_pair(position, 2);
double hp1Ref = (*hpAlleleCountMap)[0][position][0];
double hp1Alt = (*hpAlleleCountMap)[0][position][1];
double hp2Ref = (*hpAlleleCountMap)[1][position][0];
double hp2Alt = (*hpAlleleCountMap)[1][position][1];
double result1reads = hp1Ref + hp2Alt;
double result2reads = hp2Ref + hp1Alt;
double resultConfidence = std::max(result1reads, result2reads) / (result1reads + result2reads);
int hp1Result = -1;
int hp2Result = -1;
if( resultConfidence > snpConfidenceThreshold ){
if( result1reads > result2reads ){
hp1Result = 0;
hp2Result = 1;
}
else if( result1reads < result2reads ){
hp1Result = 1;
hp2Result = 0;
}
}
if( hp1Result != -1 && hp2Result != -1 ){
(*subNodeHP)[refAllele] = hp1Result;
(*subNodeHP)[altAllele] = hp2Result;
}
else{
bkResult->erase(refAllele);
bkResult->erase(altAllele);
}
}
delete hpAlleleCountMap;
}
void VairiantGraph::writingDotFile(std::string dotPrefix){
std::ofstream resultVcf(dotPrefix+".dot");
if(!resultVcf.is_open()){
std::cerr<< "Fail to open write file: " << dotPrefix+".vcf" << "\n";
}
else{
resultVcf << "digraph G {\n";
for(auto edge : dotResult){
resultVcf << edge << "\n";
}
resultVcf << "}\n";
}
return;
}
void VairiantGraph::exportResult(std::string chrName, PhasingResult &result){
// loop all position
for( std::map<int,ReadBaseMap*>::iterator variantIter = totalVariantInfo->begin() ; variantIter != totalVariantInfo->end() ; variantIter++ ){
PhasingElement tmp;
PosAllele ref = std::make_pair( variantIter->first , 1);
PosAllele alt = std::make_pair( variantIter->first , 2);
std::map<PosAllele,int>::iterator psRefIter = bkResult->find(ref);
std::map<PosAllele,int>::iterator psAltIter = bkResult->find(alt);
if( psRefIter != bkResult->end() || psAltIter != bkResult->end() ){
if( psRefIter != bkResult->end() )
tmp.block = (*psRefIter).second;
else
tmp.block = (*psAltIter).second;
tmp.RAstatus = std::to_string((*subNodeHP)[ref]) + "|" + std::to_string((*subNodeHP)[alt]);
}
else
continue;
if( tmp.block != 0){
std::string key = chrName + "_" + std::to_string( variantIter->first );
result[key] = tmp;
}
}
}
std::map<std::string,int>* VairiantGraph::getReadHP(){
return readHpMap;
}
int VairiantGraph::totalNode(){
return totalVariantInfo->size();
}
void VairiantGraph::phasingProcess(){
// This step involves converting all reads into a graph structure, which will be stored as an edge list
// in a two-layer map. The first layer of the map uses the starting coordinate as the key and contains
// a second layer map as the value. The second layer map uses the destination coordinate as the key and
// stores the number of support read as values. (There is another map used for debugging purposes that
// treats the read name vector as a value.) The method begins by visiting the coordinates covered by each
// read and recording this information in 'totalVariantInfo.' Subsequently, it connects the coordinates contained
// in each read on the graph. Specifically, each coordinate is connected to the next N coordinates in a
// linear fashion.
this->edgeConnectResult();
// This step will utilize the results of graph phasing to attempt to separate all the reads into two
// haplotypes and then identify high-confidence SNPs using reads from the two distinct haplotypes.
this->readCorrection();
}