-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdatastructures.cpp
851 lines (756 loc) · 28.4 KB
/
datastructures.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
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
/**
Copyright 2019 Gary K. Chen ([email protected])
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
**/
#include<iostream>
#include<math.h>
#include<time.h>
#include<sstream>
#include "simulator.h"
using namespace std;
ChrPosition::ChrPosition(unsigned long int iGraphIteration,
double position){
this->iGraphIteration = iGraphIteration;
this->position = position;
}
PtrRefCountable::PtrRefCountable(){
references = 0;
}
PtrRefCountable::~PtrRefCountable(){
// cout<<"Parent destructor\n";
}
Mutation::Mutation(double dLocation,double dFreq){
this->dLocation = dLocation;
this->dFreq = dFreq;
this->bPrintOutput = false;
}
Mutation::~Mutation(){
// cout<<"Mutation destructor\n";
}
GeneConversion::GeneConversion(double dEndPos):
PtrRefCountable(){
this ->dEndPos = dEndPos;
}
GeneConversion::~GeneConversion(){
#ifdef DIAG
// cout<<"Gene conversion destructor\n";
#endif
}
HotSpotBin::HotSpotBin(double dStart,
double dEnd,double dRate){
this->dStart = dStart;
this->dEnd = dEnd;
this->dRate = dRate;
}
AlleleFreqBin::AlleleFreqBin(double dStart,
double dEnd, double dFreq){
this->dStart = dStart;
this->dEnd = dEnd;
this->dFreq = dFreq;
this->iObservedCounts = 0;
}
AlleleFreqBin::~AlleleFreqBin(){
#ifdef DIAG
// cout<<"AlleleFreqBin destructor\n";
#endif
}
Population::Population(){
setChrSampled(0);
setPopSize(1);
dGrowthAlpha=0;
dLastTime=0;
}
void Population::setLastTime(double time){
this->dLastTime = time;
#ifdef DIAG
if (time<0) throw "setLastTime, negative time";
#endif
}
double Population::getLastTime(){
return this->dLastTime;
}
void Population::setGrowthAlpha(double alpha){
this->dGrowthAlpha = alpha;
#ifdef DIAG
if (this->dGrowthAlpha<0) throw "setGrowthAlpha, negative alpha";
#endif
}
double Population::getGrowthAlpha(){
return this->dGrowthAlpha;
}
void Population::setChrSampled(int iChrSampled){
this->iChrSampled = iChrSampled;
#ifdef DIAG
if (this->iChrSampled<0) throw "setChrSampled, negative chrs";
#endif
}
void Population::setPopSize(double dPopSize){
this->dPopSize = dPopSize;
#ifdef DIAG
if (this->dPopSize<0) throw "setPopSize, negative pop";
#endif
}
Edge::Edge(NodePtr & topNode,NodePtr & bottomNode):
PtrRefCountable(){
#ifdef DIAG
if (topNode->getHeight()<bottomNode->getHeight()){
cerr<<"Error in creating edge. Top node must be higher or equal\n";
throw "Error in creating edge";
}
if (topNode->getType()==Node::MIGRATION &&
bottomNode->getPopulation()==topNode->getPopulation()){
cerr<<"Error in creating edge. Top node must be different from "<<
"migration node's population\n";
throw "Error in creating edge";
}
cerr<<"edge of constructed.\n";
#endif
this->dLength = topNode->getHeight() - bottomNode->getHeight();
this->topNode = topNode;
this->getBottomNodeRef() = bottomNode;
this->bDeleted = false;
this->bInQueue = false;
this->bInCurrentTree = false;
this->iGraphIteration = 0;
}
Edge::~Edge(){
#ifdef DIAG
// cerr<<"edge of length: "<<dLength<<" destructed.\n";
#endif
}
void Edge::setBottomNode(NodePtr & bottomNode){
#ifdef DIAG
if (topNode->getHeight()<bottomNode->getHeight())
throw "Error in modifying edge. Top node must be higher or equal";
#endif
this->dLength = topNode->getHeight() - bottomNode->getHeight();
this->bottomNode = bottomNode;
}
const char* Node::getTypeStr(){
switch(iType){
case COAL:
return "coal";
case XOVER:
return "xover";
case MIGRATION:
return "migr";
case SAMPLE:
return "sampl";
case QUERY:
return "query";
}
return "undef";
}
void Node::addNewEdge(EdgeLocation iLocation,
EdgePtr & newEdge){
switch (iLocation){
case Node::TOP_EDGE:
if (topEdgeSize) this->topEdge2 = newEdge;
else this->topEdge1 = newEdge;
++topEdgeSize;
break;
case Node::BOTTOM_EDGE:
if (bottomEdgeSize) this->bottomEdge2 = newEdge;
else this->bottomEdge1 = newEdge;
++bottomEdgeSize;
break;
break;
}
#ifdef DIAG
switch(iType){
case Node::COAL:
if (bottomEdgeSize>2||
topEdgeSize>1)
throw "Coal node has too many edges\n";
break;
case Node::XOVER:
if (topEdgeSize>2||
bottomEdgeSize>1)
throw "Xover node has too many edges\n";
break;
case Node::MIGRATION:
if (bottomEdgeSize>1||
topEdgeSize>1)
throw "Migration node has too many edges\n";
break;
default:
break;
}
#endif
}
void Node::replaceOldWithNewEdge(EdgeLocation iLocation,
EdgePtr & oldEdge,EdgePtr & newEdge){
bool found = false;
unsigned int i=0;
if (iLocation==Node::TOP_EDGE){
while(!found && i<this->topEdgeSize){
WeakEdgePtr & topEdge = i?topEdge2:topEdge1;
if (topEdge.lock()==oldEdge){
topEdge=WeakEdgePtr(newEdge);
found = true;
}else{
++i;
}
}
#ifdef DIAG
if (!found) throw "Can't find top edge in replace edge";
#endif
}else if (iLocation==Node::BOTTOM_EDGE){
while(!found && i<this->bottomEdgeSize){
WeakEdgePtr & bottomEdge = i?bottomEdge2:bottomEdge1;
if (bottomEdge.lock()==oldEdge){
bottomEdge = WeakEdgePtr(newEdge);
found = true;
}else{
++i;
}
}
if (!found) throw "Can't find bottom edge in replace edge";
}
}
Node::Node(NodeType iType,short int iPopulation,double dHeight):
PtrRefCountable(){
this->iType = iType;
this->iPopulation = iPopulation;
this->dHeight = dHeight;
this->bEventDefined = false;
this->topEdgeSize=0;
this->bottomEdgeSize=0;
this->bDeleted = false;
#ifdef DIAG
cerr<<"Graph node at "<<dHeight<<" constructed."<<endl;
#endif
}
Node::~Node(){
#ifdef DIAG
// cerr<<"Graph node at "<<dHeight<<" destructed."<<endl;
#endif
}
SampleNode::SampleNode(short int iPopulation,int iId):
Node(Node::SAMPLE,iPopulation,0.0){
this->bAffected = false;
this->iId = iId;
}
Event::~Event(){
#ifdef DIAG
// cerr<<"At time "<<this->dTime<<" Graph event destructor\n";
#endif
}
Event::Event(EventType iType,double dTime):
PtrRefCountable(){
#ifdef DIAG
if (dTime<0) throw "Error in creating event. Time must be positive";
cerr<<"At time "<<this->dTime<<" Graph event constructor\n";
#endif
this->iType = iType;
this->dTime = dTime;
this->bMarkedForDelete = false;
if (dTime>Node::MAX_HEIGHT) throw "Stop here\n";
}
GenericEvent::GenericEvent(EventType iType,double dTime,
double dParameterValue):
Event(iType,dTime){
this->dParameterValue = dParameterValue;
//cerr<<"destroying generic event at height "<<dTime<<endl;
}
double GenericEvent::getParamValue(){
return this->dParameterValue;
}
MigrationEvent::MigrationEvent(EventType iType,double dTime,
short int iPopMigratedTo,short int iPopMigratedFrom):
Event(iType,dTime){
this->iPopMigratedTo = iPopMigratedTo;
this->iPopMigratedFrom = iPopMigratedFrom;
}
MigrationRateEvent::MigrationRateEvent(EventType iType,
double dTime,short int iSourcePop,short int iDestPop,
double dRate):
Event(iType,dTime){
this->iSourcePop=iSourcePop;
this->iDestPop=iDestPop;
this->dRate=dRate;
}
short int MigrationRateEvent::getSourcePop(){
return this->iSourcePop;
}
short int MigrationRateEvent::getDestPop(){
return this->iDestPop;
}
double MigrationRateEvent::getRate(){
return this->dRate;
}
MigrationRateMatrixEvent::MigrationRateMatrixEvent(
EventType iType,double dTime,MatrixDouble dMigrationMatrix):
Event(iType,dTime){
this->dMigrationMatrix = dMigrationMatrix;
}
MatrixDouble MigrationRateMatrixEvent::getMigrationMatrix(){
return this->dMigrationMatrix;
}
CoalEvent::CoalEvent(EventType iType,double dTime,
short int iPopulation):
Event(iType,dTime){
this->iPopulation = iPopulation;
}
XoverEvent::XoverEvent(EventType iType,double dTime,
short int iPopulation):Event(iType,dTime){
this->iPopulation = iPopulation;
}
PopSizeChangeEvent::PopSizeChangeEvent(EventType iType,double dTime,
short int iPopulationIndex,double dPopChangeParam):
Event(iType,dTime){
this->iPopulationIndex=iPopulationIndex;
this->dPopChangeParam=dPopChangeParam;
}
short int PopSizeChangeEvent::getPopulationIndex(){
return iPopulationIndex;
}
double PopSizeChangeEvent::getPopChangeParam(){
return dPopChangeParam;
}
PopJoinEvent::PopJoinEvent(EventType iType,double dTime,
short int iSourcePop,short int iDestPop):
Event(iType,dTime){
this->iSourcePop = iSourcePop;
this->iDestPop = iDestPop;
}
short int PopJoinEvent::getSourcePop(){
return iSourcePop;
}
short int PopJoinEvent::getDestPop(){
return iDestPop;
}
GraphBuilder::~GraphBuilder(){
cerr<<"Graphbuilder destructor\n";
this->pConfig = NULL;
this->pRandNumGenerator = NULL;
delete this->pEdgeListInARG;
delete this->pEdgeVectorByPop;
delete this->pVectorIndicesToRecycle;
delete [] pTreeEdgesToCoalesceArray;
delete this->pEdgeVectorInTree;
delete [] this->pSampleNodeArray;
MutationPtrVector::iterator it;
for(it=pMutationPtrVector->begin();it!=pMutationPtrVector->end();++it){
delete(*it);
}
delete this->pMutationPtrVector;
delete this->pEventList;
delete this->pGeneConversionPtrSet;
delete [] sites;
delete pChrPositionQueue;
}
GraphBuilder::GraphBuilder(Configuration *pConfig,RandNumGenerator * pRG){
this->iGraphIteration = 0;
this->bIncrementHistory = false;
this->iTotalTreeEdges = 0;
this->dArgLength = 0.;
this->pConfig = pConfig;
this->pChrPositionQueue = new ChrPositionQueue;
this->dTrailingGap = pConfig->dBasesToTrack/pConfig->dSeqLength;
this->bEndGeneConversion = false;
this->bBeginGeneConversion = false;
this->dScaledRecombRate = pConfig->dRecombRateRAcrossSites*
(pConfig->dGeneConvRatio+1.);
this->dMigrationMatrix = pConfig->dMigrationMatrix;
this->pRandNumGenerator = pRG;
this->pEdgeVectorByPop = new EdgePtrVectorByPop;
this->pVectorIndicesToRecycle = new EdgeIndexQueueByPop;
this->pEdgeListInARG = new EdgePtrList;
this->pTreeEdgesToCoalesceArray = new EdgePtr[pConfig->iSampleSize];
for (int i=0;i<pConfig->iTotalPops;++i){
this->pEdgeVectorByPop->push_back(EdgePtrVector());
cerr<<"DEBUG: Size at "<<i<<" is "<<this->pEdgeVectorByPop->at(i).size()<<endl;
this->pVectorIndicesToRecycle->push_back(EdgeIndexQueue());
}
this->pSampleNodeArray = new NodePtr[pConfig->iSampleSize];
sites = new bool[pConfig->iSampleSize];
this->pEdgeVectorInTree = new EdgePtrVector;
this->pMutationPtrVector = new MutationPtrVector;
this->pEventList = new EventPtrList;
EventPtrList::iterator it = pConfig->pEventList->begin();
for (it=pConfig->pEventList->begin();it!=pConfig->pEventList->end();++it){
this->pEventList->push_back(*it);
}
this->pGeneConversionPtrSet = new GeneConversionPtrSet;
if (pConfig->pAlleleFreqBinPtrSet!=NULL){
AlleleFreqBinPtrSet::iterator it2;
for (it2=pConfig->pAlleleFreqBinPtrSet->begin();it2!=pConfig->pAlleleFreqBinPtrSet->end();++it2){
AlleleFreqBinPtr ptr = *it2;
ptr->iObservedCounts = 0;
}
}
}
void GraphBuilder::checkPopCountIntegrity(PopVector & pPopList,
double dTime){
int iTotalPops = pPopList.size();
vector<int> iCounts;
vector <vector <int> > edgeIndices;
for (int k=0;k<iTotalPops;++k){
vector<int> temp;
edgeIndices.push_back(temp);
iCounts.push_back(0); }
for (int k=0;k<iTotalPops;++k){
EdgePtrVector & pEdgeVector = this->pEdgeVectorByPop->at(k);
for (unsigned int i=0;i<pEdgeVector.size();++i){
if (!pEdgeVector[i]->bDeleted && pEdgeVector[i]
->getBottomNodeRef()->getHeight()<=dTime &&
pEdgeVector[i]->getTopNodeRef()->getHeight()>dTime){
int iPopulation = pEdgeVector[i]->getBottomNodeRef()->getPopulation();
++iCounts[iPopulation];
edgeIndices[iPopulation].push_back(i);
}
}
if (coalescingEdge->getBottomNodeRef()->getPopulation()==k &&
coalescingEdge->getBottomNodeRef()->getHeight()<=dTime &&
coalescingEdge->getTopNodeRef()->getHeight()>dTime){
++iCounts[k];
}
if (originExtension->getBottomNodeRef()->getPopulation()==k &&
originExtension->getBottomNodeRef()->getHeight()<=dTime &&
originExtension->getTopNodeRef()->getHeight()>dTime){
++iCounts[k];
}
}
for (int j=0;j<iTotalPops;++j){
if (pPopList[j].getChrSampled()!=iCounts[j]){
cerr<<"At time: "<<dTime<<endl;
cerr<<"pop:"<<j<<",size:"<<pPopList[j].getChrSampled()<<endl;
cerr<<"pop:"<<j<<",found:"<<iCounts[j]<<endl;
printDataStructures();
throw "Mismatch in pop counts in CheckPopCountIntegrity" ;
}
}
}
void GraphBuilder::insertNodeInRunningEdge(NodePtr & newNode,EdgePtr & tempEdge){
NodePtr & bottomNode = tempEdge->getBottomNodeRef();
NodePtr & topNode = tempEdge->getTopNodeRef();
#ifdef DIAG
if (newNode->getHeight()<=bottomNode->getHeight() ||
newNode->getHeight() >= topNode->getHeight()){
cerr<<"Edge has heights "<<bottomNode->getHeight()<<
" and "<<topNode->getHeight()<<endl;
cerr<<"New node has height "<<newNode->getHeight()<<endl;
throw "Node to insert does not fit within coal edge ";
}
#endif
EdgePtr tempEdgeCopy = tempEdge;
tempEdge = EdgePtr(new Edge(topNode,newNode));
tempEdge->iGraphIteration = iGraphIteration;
newNode->addNewEdge(Node::TOP_EDGE,tempEdge);
tempEdge->getTopNodeRef()->replaceOldWithNewEdge(
Node::BOTTOM_EDGE,tempEdgeCopy,tempEdge);
EdgePtr newBottomEdge = EdgePtr(new Edge(newNode,bottomNode));
newBottomEdge->iGraphIteration = iGraphIteration;
newNode->addNewEdge(Node::BOTTOM_EDGE,newBottomEdge);
addEdge(newBottomEdge);
bottomNode->replaceOldWithNewEdge(Node::TOP_EDGE,tempEdgeCopy,
newBottomEdge);
}
void GraphBuilder::mergeEdges(EdgePtr & topEdge,EdgePtr & bottomEdge){
#ifdef DIAG
if (topEdge->getBottomNodeRef()->getHeight()!=bottomEdge->getTopNodeRef()->getHeight())
throw "The top edge and bottom edge are mismatched for the join";
#endif
// mark expired node as deleted
bottomEdge->getTopNodeRef()->bDeleted = true;
NodePtr & bottomNode = bottomEdge->getBottomNodeRef();
bottomNode->replaceOldWithNewEdge(Node::TOP_EDGE,bottomEdge,topEdge);
topEdge->setBottomNode(bottomNode);
deleteEdge(bottomEdge);
}
void GraphBuilder::insertNodeInEdge(NodePtr & newNode,
EdgePtr & selectedEdge){
NodePtr bottomNodeCopy = selectedEdge->getBottomNodeRef();
#ifdef DIAG
NodePtr & topNode = selectedEdge->getTopNodeRef();
if (newNode->getHeight()<bottomNodeCopy->getHeight() ||
newNode->getHeight() > topNode->getHeight()){
cerr<<"Edge has heights "<<bottomNodeCopy->getHeight()<<
" and "<<topNode->getHeight()<<endl;
cerr<<"New node has height "<<newNode->getHeight()<<endl;
throw "Node to insert does not fit within edge";
}
#endif
int iGraphIteration = selectedEdge->iGraphIteration;
selectedEdge->setBottomNode(newNode);
newNode->addNewEdge(Node::TOP_EDGE,selectedEdge);
EdgePtr newBottomEdge = EdgePtr(new Edge(newNode,bottomNodeCopy));
newBottomEdge->iGraphIteration = iGraphIteration;
addEdge(newBottomEdge);
bottomNodeCopy->replaceOldWithNewEdge(Node::TOP_EDGE,selectedEdge,
newBottomEdge);
newNode->addNewEdge(Node::BOTTOM_EDGE,newBottomEdge);
}
void GraphBuilder::deleteEdge(EdgePtr & edge){
if (!edge->bDeleted){
edge->bDeleted = true;
if (pConfig->bDebug){
cerr<<"Deleting edge with hts "<<edge->getBottomNodeRef()->getHeight()<<" and "<<
edge->getTopNodeRef()->getHeight()<<endl;
}
}
}
// Insert into EdgeVector, pop refers to bottom node
void GraphBuilder::addEdge(EdgePtr & edge){
unsigned int iPopulation = edge->getBottomNodeRef()->getPopulation();
//if(pConfig->bDebug) cerr<<"DEBUG addEdge: iPopulation: "<<iPopulation<<" and pEdgeVectorByPop size "<<pEdgeVectorByPop->size()<<endl;
this->pEdgeListInARG->push_back(edge);
while (iPopulation>=pEdgeVectorByPop->size()){
this->pEdgeVectorByPop->push_back(EdgePtrVector());
this->pVectorIndicesToRecycle->push_back(EdgeIndexQueue());
if(pConfig->bDebug)cerr<<"DEBUG! addEdge: Adding pop "<<iPopulation<<" has edge vector size: "<<pEdgeVectorByPop->size()<<endl;
// if(pConfig->bDebug)cerr<<"DEBUG! addEdge: Just added. Pop "<<iPopulation<<" has edge vector size: "<<pEdgeVectorByPop->at(iPopulation).size()<<endl;
//cerr<<"Attempting to add edge of population "<<iPopulation<<" when the number of available population edge pools is only "<<pEdgeVectorByPop->size()<<". It is recommended that you increase the migration rates and/or number of sampled chromosomes."<<endl;
//for(uint i=0;i<pEdgeVectorByPop->size();++i){
//cerr<<"Pop "<<i<<" has edge vector size: "<<pEdgeVectorByPop->at(i).size()<<endl;
//}
// throw "Data structure integrity error.";
//}
//}
}
if (iPopulation>=pEdgeVectorByPop->size()){
if(pConfig->bDebug) cerr<<"DEBUG! addEdge: Still not added! iPopulation: "<<iPopulation<<" and pEdgeVectorByPop size "<<pEdgeVectorByPop->size()<<endl;
throw "Something wrong with while loop";
}
// Insert into the vector allowing for random access
EdgePtrVector & pEdgeVector = this->pEdgeVectorByPop->
at(iPopulation);
EdgeIndexQueue & pVectorIndicesToRecycle = this->pVectorIndicesToRecycle->
at(iPopulation);
if (pVectorIndicesToRecycle.empty()){
pEdgeVector.push_back(edge);
//if(pConfig->bDebug) cerr<<"DEBUG: pEdgeVector for pop "<<iPopulation<<" is now size: "<<pEdgeVector.size()<<endl;
}else{
int iIndex = pVectorIndicesToRecycle.front();
pVectorIndicesToRecycle.pop();
pEdgeVector[iIndex] = edge;
}
}
void GraphBuilder::addEdgeToCurrentTree(EdgePtr & edge){
edge->bInCurrentTree = true;
edge->iGraphIteration = this->iGraphIteration;
if (iTotalTreeEdges<pEdgeVectorInTree->size()){
pEdgeVectorInTree->at(iTotalTreeEdges) = edge;
}else{
pEdgeVectorInTree->push_back(edge);
}
++iTotalTreeEdges;
}
void GraphBuilder::initializeCurrentTree(){
// this method does two things, traverses the edges
// to compute the total length, and clears out the status for
// in current tree
dLastTreeLength = 0.0;
dArgLength = 0.;
if (iGraphIteration==0){
EdgePtrList::iterator it;
for (it=pEdgeListInARG->begin();it!=pEdgeListInARG->end();++it){
EdgePtr curEdge=*it;
dLastTreeLength+=curEdge->getLength();
this->addEdgeToCurrentTree(curEdge);
curEdge->bInCurrentTree = false;
}
dArgLength = dLastTreeLength;
}else{
EdgePtrList::iterator it1=pEdgeListInARG->begin();
while(it1!=pEdgeListInARG->end()){
EdgePtr curEdge = *it1;
if (!curEdge->bDeleted){
dArgLength+=curEdge->getLength();
if (curEdge->bInCurrentTree){
dLastTreeLength+=curEdge->getLength();
curEdge->bInCurrentTree = false;
}
++it1;
}else{
it1 = pEdgeListInARG->erase(it1);
}
}
}
}
void GraphBuilder::printHaplotypes(){
unsigned int iTotalSites = pMutationPtrVector->size();
bool bZeroCellCount=false;
if (pConfig->bDebug) cerr<<"Total sites: "<<iTotalSites<<endl;
if (iTotalSites){
int iReducedSites=iTotalSites;
if (pConfig->bSNPAscertainment){
// first see if any expected count exceed actual counts
bool bSufficientObs=false;
do{
bSufficientObs=true;
AlleleFreqBinPtrSet::iterator it=pConfig->pAlleleFreqBinPtrSet->begin();
while(bSufficientObs && !bZeroCellCount && it!=pConfig->pAlleleFreqBinPtrSet->end()){
AlleleFreqBinPtr bin = *it;
int iExpectedCount = static_cast<int>(bin->dFreq * iReducedSites);
if (!iExpectedCount && bin->dFreq>0.){
bZeroCellCount = true;
if (pConfig->bDebug){
cerr<<"Setting zero cell count true because expecting a proportion of "<<
bin->dFreq<<" but found "<<iExpectedCount<<endl;
}
}
else if (bin->iObservedCounts<iExpectedCount){
bSufficientObs = false;
--iReducedSites;
}else{
++it;
}
}
}while(!bSufficientObs && !bZeroCellCount);
if (bZeroCellCount){
cerr<<"Warning: Some observed SNP counts were zero when they should have been positive.\n"<<
"No ascertainment correction was applied.\n"<<
"Try expanding frequency bin sizes and/or increasing mutation rate.\n";
iReducedSites = 0;
}else{
int tally=0;
for (AlleleFreqBinPtrSet::iterator it=pConfig->pAlleleFreqBinPtrSet->begin();
it!=pConfig->pAlleleFreqBinPtrSet->end();++it){
AlleleFreqBinPtr bin = *it;
double dStart = bin->dStart;
double dEnd = bin->dEnd;
int iExpectedCount = static_cast<int>(bin->dFreq * iReducedSites);
tally+=iExpectedCount;
if (pConfig->bDebug) {
cerr<<"Looking for SNPs in range "<<dStart<<" to "<<dEnd<<endl;
cerr<<"iObserved count: "<<bin->iObservedCounts<<endl;
cerr<<"Expecting "<<iExpectedCount<<" SNPS."<<endl;
}
#ifdef DIAG
if (bin->iObservedCounts<iExpectedCount){
throw "Too many expected counts";
}
#endif
while(iExpectedCount>0){
int iRandIndex = static_cast<int>(pRandNumGenerator->unifRV()*iTotalSites);
MutationPtr mutation = pMutationPtrVector->at(iRandIndex);
if (!mutation->bPrintOutput && mutation->dFreq>=dStart && mutation->dFreq<=dEnd){
mutation->bPrintOutput = true;
--iExpectedCount;
}
}
}
iReducedSites = tally;
cerr<<"Total sites reduced from "<<iTotalSites<<" to "<<iReducedSites<<endl;
}
}
if (iReducedSites){
MutationPtrVector::iterator it;
// copy to a temporary vector if ascertained
cout<<TOTALSAMPLES<<FIELD_DELIMITER<<pConfig->iSampleSize<<endl;
cout<<TOTALSITES<<FIELD_DELIMITER<<iReducedSites<<endl;
cout<<SNPBEGIN<<endl;
if (pConfig->bSNPAscertainment && !bZeroCellCount){
int origIndex=0;
bool indexPrinted=false;
for (it = pMutationPtrVector->begin();
it!=pMutationPtrVector->end();++it){
MutationPtr mutation = *it;
if (mutation->bPrintOutput){
if (indexPrinted) cout<<FIELD_DELIMITER;
cout<<origIndex;
indexPrinted=true;
}
++origIndex;
}
}else{
for(int i=0;i<iReducedSites;++i){
if (i) cout<<FIELD_DELIMITER;
cout<<i;
}
}
cout<<endl<<SNPEND<<endl;
}
}
}
void GraphBuilder::printDataStructures(){
cerr<<endl<<"*** Begin printing structures ***"<<endl;
double trueLen = 0.0;
cerr<<"Full ARG (list of edges)\n";
trueLen = 0.0;
for (EdgePtrList::iterator it=pEdgeListInARG->begin();it!=pEdgeListInARG->end();it++){
EdgePtr curEdge = *it;
cerr<<"low:ht:"<<curEdge->getBottomNodeRef()->getHeight()<<
",type:"<<curEdge->getBottomNodeRef()->getTypeStr()<<
",pop:"<<curEdge->getBottomNodeRef()->getPopulation()<<
";high:ht:"<<curEdge->getTopNodeRef()->getHeight()<<
",type:"<<curEdge->getTopNodeRef()->getTypeStr()<<
",pop:"<<curEdge->getTopNodeRef()->getPopulation()<<
",del:"<<curEdge->bDeleted<<
// ",length:"<<curEdge->getLength()<<
";hist:"<<curEdge->iGraphIteration<<endl;
trueLen+=curEdge->getLength();
}
cerr<<"Last tree (list of edges)\n";
// it;
trueLen = 0.0;
EdgePtrVector::iterator it=pEdgeVectorInTree->begin();
unsigned int count=0;
while(count<iTotalTreeEdges){
// for (EdgePtrVector::iterator it=pEdgeVectorInTree->begin();it!=pEdgeVectorInTree->end();++it){
EdgePtr curEdge = *it;
cerr<<"low_ht:"<<curEdge->getBottomNodeRef()->getHeight()<<
",type:"<<curEdge->getBottomNodeRef()->getTypeStr()<<
",pop:"<<curEdge->getBottomNodeRef()->getPopulation()<<
";high_ht:"<<curEdge->getTopNodeRef()->getHeight()<<
",type:"<<curEdge->getTopNodeRef()->getTypeStr()<<
",pop:"<<curEdge->getTopNodeRef()->getPopulation()<<endl;
// ",intree:"<<curEdge->bInCurrentTree<<endl;
// ",length:"<<curEdge->getLength()<<
// ";hist:"<<curEdge->iGraphIteration<<endl;
trueLen+=curEdge->getLength();
++count;
++it;
}
cerr<<"MRCA: "<<localMRCA->getHeight()<<endl;
cerr<<"Graph grandMRCA: "<<grandMRCA->getHeight()<<endl;
// cerr<<"true length:"<<trueLen<<endl;
cerr<<"*** Done printing structures ***"<<endl;
}
string GraphBuilder::getNewickTree(double lastCoalHeight,NodePtr & curNode){
ostringstream oss;
if (curNode->getType()==Node::SAMPLE){
// print the Newick format with the Sample ID followed by length
SampleNode * pSampleNode = static_cast<SampleNode *>(curNode.get());
oss<<pSampleNode->iId<<":"<<lastCoalHeight-curNode->getHeight();
}else if (curNode->getType()==Node::COAL){
// if the two lower branches are in the current tree, then print the
// Newick format, and recursively repeat
if (iGraphIteration==curNode->getBottomEdgeByIndex(0)->iGraphIteration &&
iGraphIteration==curNode->getBottomEdgeByIndex(1)->iGraphIteration){
oss<<"("<<getNewickTree(curNode->getHeight(),
curNode->getBottomEdgeByIndex(0)->getBottomNodeRef())
<<","<<getNewickTree(curNode->getHeight(),
curNode->getBottomEdgeByIndex(1)->getBottomNodeRef())
<<")";
// if we are at the MRCA we don't need to output the branch length since
// it is obviously 0 length
if (curNode!=localMRCA) oss<<":"<<lastCoalHeight-curNode->getHeight();
// cout<<"got here\n";
}else{
for(int i=0;i<2;++i){
if (curNode->getBottomEdgeByIndex(i)->iGraphIteration==iGraphIteration){
oss<<getNewickTree(lastCoalHeight,
curNode->getBottomEdgeByIndex(i)->getBottomNodeRef());
}
}
}
}else {
// this is for everything else(e.g. migration, xover nodes),
// don't print anything, but recursively
// descend down the graph
oss<<getNewickTree(lastCoalHeight,
curNode->getBottomEdgeByIndex(0)->getBottomNodeRef());
}
return oss.str();
}