-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree.cpp
More file actions
1384 lines (1282 loc) · 39.4 KB
/
Copy pathtree.cpp
File metadata and controls
1384 lines (1282 loc) · 39.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
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
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "tree.h"
// randomly generate a rooted tree
// numTips <= 1
void UnlabelTree::randRootedTree(int numTips, vector<int>& topology, MyRand& myrand) {
int rootID;
vector<int> top;
subrandRootedTree(numTips, top, myrand, rootID);
topology.clear();
tidyUpFormat(top, topology);;
}
void UnlabelTree::subrandRootedTree(int numTips, vector<int>& topology, MyRand& myrand, int& rootID) {
int leftRootID, rightRootID;
int i;
if (numTips==1) {
topology.push_back(-1);
topology.push_back(-1);
} else {
i = myrand.iunif(1, numTips-1);
// left child has i tips and right child has numTips-i tips
subrandRootedTree(i, topology, myrand, leftRootID);
subrandRootedTree(numTips-i, topology, myrand, rightRootID);
topology.push_back(leftRootID);
topology.push_back(rightRootID);
}
rootID = (topology.size() - 2) / 2;
}
// generate all possible rooted trees
// output format: topologies[i] = the i-th topology (in integer format)
void UnlabelTree::genRootedTrees(int numTips, vector<vector<int> >& topologies) {
int i,j,k,l,s,m,p,q;
int numTipLeftTree, numTipRightTree;
vector<vector<vector<int> > > treeList;
vector<int> topology;
vector<int> tidyTop;
treeList.resize(numTips);
// initalize the first two cases
// tree with zero tip, which is empty
treeList[0].clear();
// tree with one tip
treeList[1].resize(1);
treeList[1].at(0).clear();
treeList[1].at(0).push_back(-1);
treeList[1].at(0).push_back(-1);
for (i=2; i<numTips; i++) {
m=0;
for (j=1; j<=i/2; j++) {
numTipLeftTree = 2 * j - 1;
numTipRightTree = 2 * (i-j) - 1;
for (k=0; k<treeList[j].size(); k++) {
if (i-j==j)
s = k;
else
s = 0;
for (l=s; l<treeList[i-j].size(); l++) {
topology.clear();
// insert the left part of the tree
topology.insert(topology.begin(), treeList[j].at(k).begin(), treeList[j].at(k).end());
// insert the right part of the tree
for (p=0; p<treeList[i-j].at(l).size(); p++) {
q = treeList[i-j].at(l).at(p);
if (q == -1)
topology.push_back(q);
else
topology.push_back(q + numTipLeftTree);
}
// insert the root
topology.push_back(numTipLeftTree-1);
topology.push_back(numTipLeftTree+numTipRightTree-1);
// put into treeList[i]
treeList[i].resize(m+1);
treeList[i].at(m).clear();
treeList[i].at(m).insert(treeList[i].at(m).begin(),topology.begin(),topology.end());
m++;
}
}
}
cerr << i << " tips: " << treeList[i].size() << " rooted unlabeled trees" << endl;
}
// for i = numTips
i = numTips;
m=0;
topologies.clear();
for (j=1; j<=i/2; j++) {
numTipLeftTree = 2 * j - 1;
numTipRightTree = 2 * (i-j) - 1;
for (k=0; k<treeList[j].size(); k++) {
if (i-j==j)
s = k;
else
s = 0;
for (l=s; l<treeList[i-j].size(); l++) {
topology.clear();
// insert the left part of the tree
topology.insert(topology.begin(), treeList[j].at(k).begin(), treeList[j].at(k).end());
// insert the right part of the tree
for (p=0; p<treeList[i-j].at(l).size(); p++) {
q = treeList[i-j].at(l).at(p);
if (q == -1)
topology.push_back(q);
else
topology.push_back(q + numTipLeftTree);
}
// insert the root
topology.push_back(numTipLeftTree-1);
topology.push_back(numTipLeftTree+numTipRightTree-1);
// tidy up the topology
tidyUpFormat(topology, tidyTop);
// put into treeList[i]
topologies.resize(m+1);
topologies.at(m).clear();
topologies.at(m).insert(topologies.at(m).begin(),tidyTop.begin(),tidyTop.end());
m++;
}
}
}
cerr << i << " tips: " << topologies.size() << " unlabeled trees" << endl;
}
// generate all possible unrooted trees
// output format: topologies[i] = the i-th topology (in integer format)
void UnlabelTree::genUnrootedTrees(int numTips, vector<vector<int> >& topologies) {
int i,j,k,l,s,m,p,q;
int numTipLeftTree, numTipRightTree;
set<string> unroot_trees;
set<string>::iterator itr;
vector<vector<vector<int> > > treeList;
vector<int> topology;
vector<int> tidyTop;
string treeStr;
treeList.resize(numTips);
// initalize the first two cases
// tree with zero tip, which is empty
treeList[0].clear();
// tree with one tip
treeList[1].resize(1);
treeList[1].at(0).clear();
treeList[1].at(0).push_back(-1);
treeList[1].at(0).push_back(-1);
for (i=2; i<=numTips; i++) {
m=0;
for (j=1; j<=i/2; j++) {
numTipLeftTree = 2 * j - 1;
numTipRightTree = 2 * (i-j) - 1;
for (k=0; k<treeList[j].size(); k++) {
if (i-j==j)
s = k;
else
s = 0;
for (l=s; l<treeList[i-j].size(); l++) {
topology.clear();
// insert the left part of the tree
topology.insert(topology.begin(), treeList[j].at(k).begin(), treeList[j].at(k).end());
// insert the right part of the tree
for (p=0; p<treeList[i-j].at(l).size(); p++) {
q = treeList[i-j].at(l).at(p);
if (q == -1)
topology.push_back(q);
else
topology.push_back(q + numTipLeftTree);
}
// insert the root
topology.push_back(numTipLeftTree-1);
topology.push_back(numTipLeftTree+numTipRightTree-1);
// put into treeList[i]
if (i < numTips) {
treeList[i].resize(m+1);
treeList[i].at(m).clear();
treeList[i].at(m).insert(treeList[i].at(m).begin(),topology.begin(),topology.end());
m++;
} else {
tidyUpFormat(topology, tidyTop);
treeStr = getRepresent(tidyTop,topology);
itr = unroot_trees.find(treeStr);
if (itr == unroot_trees.end()) {
unroot_trees.insert(treeStr);
topologies.resize(m+1);
topologies.at(m).clear();
topologies.at(m).insert(topologies.at(m).begin(),tidyTop.begin(),tidyTop.end());
m++;
}
}
}
}
}
}
}
// only for the number of haplotypes between 2 and 20
// quickly retrieve all possible unrooted trees
// output format: topologies[i] = the i-th topology (in integer format)
void UnlabelTree::getUnrootedTrees(int numHap, vector<vector<int> >& topologies) {
int numMatrix[] = {0, 1, 1, 1, 1, 1, 2, 2, 4, 6, 11, 18, 37, 66, 135, 265, 552, 1132, 2410, 5098, 11020};
int i, j, k;
if (numHap < 2 || numHap > 20) {
cerr << "Number of haplotypes has to be between 2 and 20" << endl;
exit(1);
}
topologies.resize(numMatrix[numHap]);
k=0;
for (i=2; i<numHap; i++) {
k += numMatrix[i] * (i-1) * 2;
}
for (i=0; i<numMatrix[numHap]; i++) {
topologies[i].clear();
for (j=0; j<2*numHap; j++) {
topologies[i].push_back(-1);
}
for (j=0; j<(numHap-1)*2; j++) {
topologies[i].push_back(rawMatrix[k++]);
}
}
}
// tidy up the topology format such that all the leaf nodes appear in the beginning
void UnlabelTree::tidyUpFormat(vector<int>& topInt, vector<int>& updateTop) {
vector<int> newID;
int nodeNum;
int i,k;
k=0;
nodeNum = ((int)topInt.size()) / 2;
newID.resize(nodeNum);
updateTop.clear();
// assigning a new ID for all the leaves
for (i=0; i<nodeNum; i++) {
if (topInt[2*i]==-1 || topInt[2*i+1]==-1) {
// node i is a leaf
// its new ID is k
newID[i] = k;
k++;
}
}
for (i=0; i<k; i++) {
updateTop.push_back(-1);
updateTop.push_back(-1);
}
// now consider the internal nodes
for (i=0; i<nodeNum; i++) {
if (!(topInt[2*i]==-1 || topInt[2*i+1]==-1)) {
// an internal node
newID[i] = k;
k++;
updateTop.push_back(newID[topInt[2*i]]);
updateTop.push_back(newID[topInt[2*i+1]]);
}
}
}
// convert the toplogy from integer format into text format
string UnlabelTree::topInt2Txt(vector<int>& topInt) {
vector<string> nodeStr;
int leftID, rightID;
string leftTxt, rightTxt;
int k;
nodeStr.clear();
for (k=0; k<topInt.size()/2; k++) {
leftID = topInt[2*k];
rightID = topInt[2*k+1];
if (leftID==-1 || rightID==-1) {
// a leaf
nodeStr.push_back("x");
} else {
// an internal node
leftTxt = nodeStr[leftID];
rightTxt = nodeStr[rightID];
if (leftTxt <= rightTxt)
nodeStr.push_back("(" + leftTxt + "," + rightTxt + ")");
else
nodeStr.push_back("(" + rightTxt + "," + leftTxt + ")");
}
}
return nodeStr[nodeStr.size()-1];
}
// convert the toplogy from integer format into text format
// for labelled tree
string UnlabelTree::topInt2TxtLabelTree(vector<int>& topInt) {
vector<string> nodeStr;
int leftID, rightID;
string leftTxt, rightTxt;
int k;
nodeStr.clear();
for (k=0; k<topInt.size()/2; k++) {
leftID = topInt[2*k];
rightID = topInt[2*k+1];
if (leftID==-1 || rightID==-1) {
// a leaf
nodeStr.push_back(int2str(k));
} else {
// an internal node
leftTxt = nodeStr[leftID];
rightTxt = nodeStr[rightID];
if (leftTxt <= rightTxt)
nodeStr.push_back("(" + leftTxt + "," + rightTxt + ")");
else
nodeStr.push_back("(" + rightTxt + "," + leftTxt + ")");
}
}
return nodeStr[nodeStr.size()-1];
}
// convert the toplogy from integer format into text format
// the topology begins with leaves
string UnlabelTree::topInt2Txt(vector<int>& topInt, vector<double>& haploFreq, vector<double>& edgeLen, int numDigits) {
vector<string> nodeStr;
int leftID, rightID;
string leftTxt, rightTxt;
int k;
double leftLen, rightLen;
string s;
// cout << "[enter UnlabelTree::topInt2Txt]" << endl << flush;
nodeStr.clear();
for (k=0; k<topInt.size()/2; k++) {
leftID = topInt[2*k];
rightID = topInt[2*k+1];
if (leftID==-1 || rightID==-1) {
// a leaf
nodeStr.push_back("f=" + doublToStr(haploFreq[k], numDigits));
} else {
// an internal node
leftTxt = nodeStr[leftID];
rightTxt = nodeStr[rightID];
if (leftID >= edgeLen.size())
leftLen = 0.0;
else
leftLen = edgeLen[leftID];
if (rightID >= edgeLen.size())
rightLen = 0.0;
else
rightLen = edgeLen[rightID];
if (leftTxt <= rightTxt) {
nodeStr.push_back("(" + leftTxt + ":" + doublToStr(leftLen, numDigits) + "," + rightTxt + ":" + doublToStr(rightLen, numDigits) + ")");
} else {
nodeStr.push_back("(" + rightTxt + ":" + doublToStr(rightLen, numDigits) + "," + leftTxt + ":" + doublToStr(leftLen, numDigits) + ")");
}
}
}
s = nodeStr[nodeStr.size()-1];
// cout << "[leave UnlabelTree::topInt2Txt]" << endl << flush;
return s;
}
// convert the toplogy from integer format into text format
// the topology begins with leaves
string UnlabelTree::topInt2Txt(vector<int>& topInt, vector<double>& haploFreq, vector<long double>& edgeLen, int numDigits) {
vector<string> nodeStr;
int leftID, rightID;
string leftTxt, rightTxt;
int k;
double leftLen, rightLen;
string s;
nodeStr.clear();
for (k=0; k<topInt.size()/2; k++) {
leftID = topInt[2*k];
rightID = topInt[2*k+1];
if (leftID==-1 || rightID==-1) {
// a leaf
nodeStr.push_back("f=" + doublToStr(haploFreq[k], numDigits));
} else {
// an internal node
leftTxt = nodeStr[leftID];
rightTxt = nodeStr[rightID];
if (leftID >= edgeLen.size())
leftLen = 0.0;
else
leftLen = edgeLen[leftID];
if (rightID >= edgeLen.size())
rightLen = 0.0;
else
rightLen = edgeLen[rightID];
if (leftTxt <= rightTxt) {
nodeStr.push_back("(" + leftTxt + ":" + doublToStr(leftLen, numDigits) + "," + rightTxt + ":" + doublToStr(rightLen, numDigits) + ")");
} else {
nodeStr.push_back("(" + rightTxt + ":" + doublToStr(rightLen, numDigits) + "," + leftTxt + ":" + doublToStr(leftLen, numDigits) + ")");
}
}
}
s = nodeStr[nodeStr.size()-1];
return s;
}
// convert the toplogy from integer format into text format
string UnlabelTree::topInt2Txt(vector<int>& topInt, vector<int>& nodeOrder) {
vector<string> nodeStr;
int leftID, rightID;
string leftTxt, rightTxt;
int numNodes;
int i,k;
numNodes = ((int)topInt.size()) / 2;
nodeStr.resize(numNodes);
for (i=0; i<numNodes; i++) {
k = nodeOrder[i];
leftID = topInt[2*k];
rightID = topInt[2*k+1];
if (leftID==-1 || rightID==-1) {
// a leaf
nodeStr[k]="x";
} else {
// an internal node
leftTxt = nodeStr[leftID];
rightTxt = nodeStr[rightID];
if (leftTxt <= rightTxt)
nodeStr[k]="(" + leftTxt + "," + rightTxt + ")";
else
nodeStr[k]="(" + rightTxt + "," + leftTxt + ")";
}
}
return nodeStr[nodeStr.size()-1];
}
// convert the toplogy from integer format into text format (for labeled tree)
string UnlabelTree::topInt2TxtLabelTree(vector<int>& topInt, vector<int>& tipOrder) {
vector<string> nodeStr;
int leftID, rightID;
string leftTxt, rightTxt;
int numNodes;
int i;
numNodes = ((int)topInt.size()) / 2;
nodeStr.resize(numNodes);
for (i=0; i<numNodes; i++) {
leftID = topInt[2*i];
rightID = topInt[2*i+1];
if (leftID==-1 || rightID==-1) {
// a leaf
// the array begins with the leaves
nodeStr[i]=int2str(tipOrder[i]);
} else {
// an internal node
leftTxt = nodeStr[leftID];
rightTxt = nodeStr[rightID];
if (leftTxt <= rightTxt)
nodeStr[i]="(" + leftTxt + "," + rightTxt + ")";
else
nodeStr[i]="(" + rightTxt + "," + leftTxt + ")";
}
}
return nodeStr[nodeStr.size()-1];
}
// get the corresponding pairs of starting and ending positions from topology sequence
// assume no space in the topStr
void getPosPair(string& topStr, vector<pair<int,int> >& ipos, vector<int>& iparents, vector<pair<int,int> >& ichildren) {
int p; // position on the sequence
int k; // k-th bracket
int t,n;
string tStr;
ipos.clear();
iparents.clear();
ichildren.clear();
vector<pair<int,int> > tmpPos;
vector<int> tmpParents;
vector<bool> isLeaf;
vector<int> changeTo;
vector<int> changeFr;
vector<pair<int,int> > tpos;
vector<int> tparents;
vector<pair<int,int> > tchildren;
k=-1;
for (p=0; p<(int)topStr.length(); p++) {
if (topStr[p]=='(' || (p-1>=0 && topStr[p-1]=='(') || (p-1>=0 && topStr[p-1]==',')) {
k = (int)tmpPos.size();
tmpPos.push_back(pair<int,int>(p,-1));
tmpParents.push_back(-1);
} else if ((p+1<topStr.length() && topStr[p+1] == ')')|| (p+1<topStr.length() && topStr[p+1] == ',')) {
if (k==-1) {
cerr << "[getBracketPair] Error! The parentheses are not balanced" << endl;
exit(1);
}
tmpPos[k].second = p;
t = k;
k--;
while (k>=0 && tmpPos[k].second != -1)
k--;
tmpParents[t] = k;
}
}
if (k==0) {
tmpPos[k].second = topStr.length();
k--;
}
if (k!=-1) {
cerr << "[getBracketPair] Error! The parentheses are not balanced" << endl;
exit(1);
}
// output the information reversely
n = (int)tmpPos.size();
t = n - 1;
for (k=t; k>=0; k--) {
tpos.push_back(pair<int,int>(tmpPos[k].first,tmpPos[k].second));
if (tmpParents[k]==-1)
tparents.push_back(-1);
else
tparents.push_back(t - tmpParents[k]);
}
// set the children
for (k=0; k<n; k++) {
tchildren.push_back(pair<int,int>(-1,-1));
}
for (k=0; k<n; k++) {
t = tparents[k];
if (t != -1) {
if (tchildren[t].first == -1)
tchildren[t].first = k;
else if (tchildren[t].second == -1)
tchildren[t].second = k;
else {
cerr << "[getBracketPair] Error! The input newick tree is not binary" << endl;
exit(1);
}
}
}
// identify which are leaves
for (k=0; k<n; k++) {
if (tchildren[k].first == -1 && tchildren[k].second == -1) {
isLeaf.push_back(true);
} else {
isLeaf.push_back(false);
}
}
// reorder the nodes such that all leaves appear first
changeTo.resize(n);
changeFr.resize(n);
t=0;
// for all leaves
for (k=0; k<n; k++) {
if (isLeaf[k]) {
changeTo[t]=k;
changeFr[k]=t;
t++;
}
}
// for all internal nodes
for (k=0; k<n; k++) {
if (!isLeaf[k]) {
changeTo[t]=k;
changeFr[k]=t;
t++;
}
}
// update the arrays
for (k=0; k<n; k++) {
t = changeTo[k];
ipos.push_back(tpos[t]);
if (tparents[t] != -1)
iparents.push_back(changeFr[tparents[t]]);
else
iparents.push_back(-1);
if (tchildren[t].first != -1 && tchildren[t].second != -1)
ichildren.push_back(pair<int,int>(changeFr[tchildren[t].first],changeFr[tchildren[t].second]));
else
ichildren.push_back(pair<int,int>(-1,-1));
}
/*
for (k=0; k<n; k++) {
cout << k << "\t" << tpos[k].first << "\t" << tpos[k].second << "\t" << tparents[k] << "\t" << tchildren[k].first << "\t" << tchildren[k].second << "\t" << isLeaf[k];
if (tchildren[k].first == -1 || tchildren[k].second==-1)
cout << "\t" << topStr.substr(tpos[k].first, tpos[k].second-tpos[k].first+1);
cout << endl;
}
cout << "changeTo:" << endl;
for (k=0; k<changeTo.size(); k++) {
cout << k << " -> " << changeTo[k] << endl;
}
for (k=0; k<n; k++) {
cout << k << "\t" << ipos[k].first << "\t" << ipos[k].second << "\t" << iparents[k] << "\t" << ichildren[k].first << "\t" << ichildren[k].second;
if (ichildren[k].first == -1 || ichildren[k].second==-1)
cout << "\t" << topStr.substr(ipos[k].first, ipos[k].second-ipos[k].first+1);
cout << endl;
}
*/
}
// convert the binary toplogy from text format into integer format
void UnlabelTree::topTxt2Int(string& topStr, vector<int>& topInt, vector<double>& haploFreq, vector<double>& edgeLen) {
// get corresponding pairs of brackets
vector<pair<int,int> > pos;
vector<int> parents;
vector<pair<int,int> > children;
vector<string> token;
int k,s,t;
string seq;
double l;
topInt.clear();
haploFreq.clear();
edgeLen.clear();
getPosPair(topStr, pos, parents, children);
for (k=0; k<(int)pos.size(); k++) {
topInt.push_back(children[k].first);
topInt.push_back(children[k].second);
s = pos[k].first;
t = pos[k].second;
seq = topStr.substr(s,t-s+1);
if (seq[seq.length()-1] == ')') {
// root
l = 0.0;
} else {
tokenizer(topStr.substr(s,t-s+1), "=:", &token);
l = atof(token[token.size()-1].c_str());
if (children[k].first == -1 && children[k].second==-1) {
haploFreq.push_back(atof(token[1].c_str()));
}
}
edgeLen.push_back(l);
}
}
// show the topologies
void UnlabelTree::showTopologies(vector<vector<int> >& topologies) {
int i;
for (i=0; i<topologies.size(); i++) {
cout << topInt2Txt(topologies.at(i)) << endl;
}
}
// get the tree depth
int UnlabelTree::treeDepth(vector<int>& topInt) {
int numNodes = (int)topInt.size() / 2;
int* nodeDepths = new int[numNodes];
int lnode, rnode;
int treeDepth;
int i;
for (i=0; i<numNodes; i++) {
lnode = topInt[i*2];
rnode = topInt[i*2+1];
if (lnode == -1 || rnode == -1) {
nodeDepths[i] = 0;
} else {
nodeDepths[i] = maxInt(nodeDepths[lnode], nodeDepths[rnode])+1;
}
}
treeDepth = nodeDepths[numNodes-1];
delete[] nodeDepths;
return treeDepth;
}
// get the tree depth
int UnlabelTree::treeDepth(vector<int>& topInt, vector<int>& nodeOrder) {
int numNodes = (int)topInt.size() / 2;
int* nodeDepths = new int[numNodes];
int lnode, rnode;
int treeDepth;
int i,k;
for (k=0; k<numNodes; k++) {
i = nodeOrder[k];
lnode = topInt[i*2];
rnode = topInt[i*2+1];
if (lnode == -1 || rnode == -1) {
nodeDepths[i] = 0;
} else {
nodeDepths[i] = maxInt(nodeDepths[lnode], nodeDepths[rnode])+1;
}
}
treeDepth = nodeDepths[numNodes-1];
delete[] nodeDepths;
return treeDepth;
}
// change the root position to the edge connecting to the node x (i.e. newRoot)
void UnlabelTree::changeRoot(vector<int>& topInt, int newRoot, vector<int>& newTop, vector<int>& newEdge, vector<int>& newNodeOrder) {
int numNodes = (int)topInt.size() / 2;
int numTips = (numNodes + 1) / 2;
int* parents = new int[numNodes-1];
int* newParents = new int[numNodes-1];
int lnode, rnode, cnode;
vector<int> nodesToProcess;
int i,k;
// check the value of newRoot
if (newRoot >= numNodes-1) {
cerr << "[UnlabelTree::changeRoot] Error! The value of newRoot >= the number of nodes - 1 (i.e. " << numNodes-1 << ")" << endl;
exit(1);
}
// for all the nodes except root
for (i=0; i<numNodes; i++) {
lnode = topInt[2*i];
rnode = topInt[2*i+1];
if (lnode == -1 || rnode == -1)
continue;
if (i < numNodes-1) {
// not a root
parents[lnode] = i;
parents[rnode] = i;
} else {
// a root
parents[lnode] = rnode;
parents[rnode] = lnode;
}
}
// initialize the new topology
newTop.clear();
newTop.insert(newTop.begin(), topInt.begin(), topInt.end());
// initialize the new node order
newNodeOrder.resize(numNodes);
for (i=0; i<numTips; i++) {
newNodeOrder[i] = i;
}
// initialize the new edge
newEdge.resize(numNodes-1);
// build the new topology according to the new position of the root
lnode = newRoot;
rnode = parents[newRoot];
cnode = numNodes-1;
newTop[cnode*2] = lnode;
newTop[cnode*2+1] = rnode;
newParents[lnode] = rnode;
newParents[rnode] = lnode;
nodesToProcess.push_back(lnode);
nodesToProcess.push_back(rnode);
newNodeOrder[numNodes-1] = cnode;
newEdge[lnode] = -1; // the edge length of the new root's left child is always ZERO
newEdge[rnode] = minInt(lnode, rnode);
i=0;k=1;
while (i<nodesToProcess.size()) {
cnode = nodesToProcess[i];
if (cnode >= numTips) {
// not a leaf
if (newParents[cnode] != parents[cnode]) {
// build the new subtree
// get the new children
if (topInt[cnode*2] == newParents[cnode]) {
lnode = topInt[cnode*2+1];
rnode = parents[cnode];
} else {
lnode = topInt[cnode*2];
rnode = parents[cnode];
}
newTop[cnode*2] = lnode;
newTop[cnode*2+1] = rnode;
} else {
lnode = topInt[cnode*2];
rnode = topInt[cnode*2+1];
}
newParents[lnode] = cnode;
newParents[rnode] = cnode;
nodesToProcess.push_back(lnode);
nodesToProcess.push_back(rnode);
// set the new node order
k++;
newNodeOrder[numNodes-k] = cnode;
// set the new edge
newEdge[lnode] = minInt(lnode, cnode);
newEdge[rnode] = minInt(rnode, cnode);
}
i++;
}
// clear the memory
delete[] parents;
delete[] newParents;
}
// obtain a reprsentative for the rooted topologies
// only consider the topologies when rooted on the leaf edge
// and pick the smallest one (w.r.t. the alphabetical order)
string UnlabelTree::getRepresent(vector<int>& topInt, vector<int>& representTop) {
int i, besti;
int numNodes = (int)topInt.size() / 2;
int minTreeDepth = numNodes;
int cTreeDepth;
string represent, ctxt;
vector<int> newTop;
vector<int> newNodeOrder;
vector<int> newEdge;
represent = "";
besti = 0;
// cout << "input tree: " << topInt2Txt(topInt) << endl;
for (i=0; i<numNodes-1; i++) {
changeRoot(topInt, i, newTop, newEdge, newNodeOrder);
// showTopology(newTop);
cTreeDepth = treeDepth(newTop, newNodeOrder);
if (cTreeDepth > minTreeDepth)
continue;
ctxt = topInt2Txt(newTop, newNodeOrder);
// cout << ctxt << endl;
if (cTreeDepth<minTreeDepth || ctxt<represent) {
represent = ctxt;
minTreeDepth = cTreeDepth;
besti = i;
}
}
changeRoot(topInt, besti, newTop, newEdge, newNodeOrder);
updateTopology(newTop, newNodeOrder, representTop);
return represent;
}
// update the topology so that the node order becomes 0,1,2,3,....
void UnlabelTree::updateTopology(vector<int>& topInt, vector<int>& nodeOrder, vector<int>& newTop) {
vector<int> changeTo;
int i,k;
int left,right;
newTop.clear();
changeTo.resize(nodeOrder.size());
for (i=0; i<nodeOrder.size(); i++) {
changeTo[nodeOrder[i]] = i;
}
for (k=0; k<topInt.size()/2; k++) {
i = nodeOrder[k];
left = topInt[2*i];
right = topInt[2*i+1];
if (left != -1)
left = changeTo[left];
if (right != -1)
right = changeTo[right];
if (left <= right) {
newTop.push_back(left);
newTop.push_back(right);
} else {
newTop.push_back(right);
newTop.push_back(left);
}
}
}
// show the topology
void UnlabelTree::showTopology(vector<int>& topInt) {
int i;
for (i=0; i<topInt.size(); i+=2) {
cout << topInt[i] << "," << topInt[i+1] << endl;
}
}
// show the topology
void UnlabelTree::showTopology(vector<int>& topInt, vector<int>& newNodeOrder) {
int i,k;
for (i=0; i<topInt.size()/2; i++) {
k = newNodeOrder[i];
cout << k << "," << topInt[2*k] << "," << topInt[2*k+1] << endl;
}
}
// show the topology
void UnlabelTree::showTopology(vector<int>& topInt, vector<int>& newEdge, vector<int>& newNodeOrder) {
int i,k;
for (i=0; i<topInt.size()/2; i++) {
k = newNodeOrder[i];
cout << k << "," << topInt[2*k];
if (topInt[2*k] >= 0)
cout << "(edge:" << newEdge[topInt[2*k]] << ")";
cout << "," << topInt[2*k+1];
if (topInt[2*k+1] >= 0)
cout << "(edge:" << newEdge[topInt[2*k+1]] << ")";
cout << endl;
}
}
// show the topology in one line
void UnlabelTree::showTopologyInOneLine(vector<int>& topInt) {
int i;
for (i=0; i<topInt.size(); i++) {
if (i > 0)
cout << ",";
cout << topInt[i];
}
cout << endl;
}
// get the sister node
// return -1 if not found
int UnlabelTree::getSisterNode(int node1, vector<int>& topInt) {
int i;
for (i=0; i<topInt.size(); i++) {
if (topInt[i] == node1) {
if (i%2==0)
return topInt[i+1];
else
return topInt[i-1];
}
}
return -1;
}
// NNI
// update: topology is always changed
bool UnlabelTree::doNNI(vector<int>& topInt, int numTips, MyRand& myrand, vector<pair<int,int> >& changeNodeIDs) {
int i;
changeNodeIDs.clear();
int which = myrand.iunif(0,1);
if (which==2) {
// no change, exit
return false;
}
int line = myrand.iunif(numTips, topInt.size()/2-3);
int selectNode1Idx = 2*line + which;
int selectNode2Idx = -1;
for (i=(line+1)*2; i<topInt.size(); i++) {
if (topInt[i] == line) {
if (i%2==0)
selectNode2Idx = i+1;
else
selectNode2Idx = i-1;
break;
}
}
if (selectNode2Idx == -1) {
cerr << "[UnlabelTree::doNNI] Error! selectNode2Idx == -1" << endl;
exit(1);
}
// swap between "topInt[selectNode1Idx]" and "topInt[selectNode2Idx]"
i = topInt[selectNode1Idx];
topInt[selectNode1Idx] = topInt[selectNode2Idx];
topInt[selectNode2Idx] = i;
/*
cout << "line = " << line << endl;
cout << "which = " << which << endl;
cout << "selectNode1Idx = " << selectNode1Idx << endl;
cout << "selectNode2Idx = " << selectNode2Idx << endl;
// show the topology
cout << "topology before tidy up:" << endl;
showTopology(topInt);
*/
// tidy up the messy topology
tidyUpMessyTopology(numTips, topInt, changeNodeIDs);
return true;
}
// subtree swapping
// isDescendant[i*numNodes+j] = true if node j is descendant of node i (relationship between two nodes)
// return true if the topology is changed
bool UnlabelTree::swapSubTree(vector<int>& topInt, int numTips, int numNodes, MyRand& myrand, vector<bool>& isDescendant, vector<pair<int,int> >& changeNodeIDs) {
vector<int> availableToChoose;
int node1, node2, sister_node1;
int i;
node1 =myrand.iunif(0, 2*numTips-2);
sister_node1 = getSisterNode(node1, topInt);
changeNodeIDs.clear();
if (sister_node1==-1)
return false;
for (i=0; i<=2*numTips-2; i++) {
if ((i!=node1) && (i!=sister_node1) && (!isDescendant[node1*numNodes+i]) && (!isDescendant[i*numNodes+node1])) {
availableToChoose.push_back(i);
}
}
if (availableToChoose.size()==0)
return false;
node2 = availableToChoose[myrand.iunif(0,availableToChoose.size()-1)];
// swap between node1 and node2
for (i=0; i<topInt.size(); i++) {
if (topInt[i] == node1)
topInt[i] = node2;
else if (topInt[i] == node2)
topInt[i] = node1;
}
// tidy up the messy topology
tidyUpMessyTopology(numTips, topInt, changeNodeIDs);
return true;
}
// tidy up the messy topology
void UnlabelTree::tidyUpMessyTopology(int numTips, vector<int>& topIntMessy, vector<pair<int,int> >& changeNodeIDs) {
int numLines = topIntMessy.size() / 2;
if (numLines != (numTips*2-1)) {
cerr << "[UnlabelTree::tidyUpMessyTopology] Error! The size of 'topIntMessy' does not match with the value of 'numTips'" << endl;
exit(1);
}
vector<int> old2newNodes;
vector<bool> lineExamined;
vector<int> topInt;