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
|
/*
* mrca.c (included by pstep.c)
*
*
* Part of TREE-PUZZLE 5.2 (July 2004)
*
* (c) 2003-2004 by Heiko A. Schmidt, Korbinian Strimmer, and Arndt von Haeseler
* (c) 1999-2003 by Heiko A. Schmidt, Korbinian Strimmer,
* M. Vingron, and Arndt von Haeseler
* (c) 1995-1999 by Korbinian Strimmer and Arndt von Haeseler
*
* All parts of the source except where indicated are distributed under
* the GNU public licence. See http://www.opensource.org for details.
*
* ($Id$)
*
*/
/* initialize tree with the following starting configuration (see pstep.h) */
void inittree_mrca(ONEEDGE **edge, /* out: new array of edges */
int **edgeofleaf,/* out: array of external edge ptrs */
int *rootleaf, /* out: rooting leaf (=trueID[0]) */
int Maxspc, /* in: Number of species (n) */
int Maxbrnch, /* in: Number of branches (2n-3) */
int *nextedge, /* out: next free edge index (=3) */
int *nextleaf, /* out: next free leaf index (=3) */
int *trueID,
imatrix mrcamatrix)
{
int i, j;
ONEEDGE *tmpedge;
int *tmpedgeofleaf;
/* allocate the memory for the whole tree */
/* allocate memory for vector with all the edges of the tree */
tmpedge = (ONEEDGE *) calloc((size_t) Maxbrnch, sizeof(ONEEDGE) );
if (tmpedge == NULL) maerror("edge in inittree");
*edge = tmpedge;
/* allocate memory for tmpvector with edge numbers of leaves */
tmpedgeofleaf = (int *) calloc((size_t) Maxspc, sizeof(int) );
if (tmpedgeofleaf == NULL) maerror("edgeofleaf in inittree");
for (j = 0; j < Maxspc; j++) tmpedgeofleaf[j]=-1;
*edgeofleaf = tmpedgeofleaf;
/* allocate memory for all the edges the split map */
for (i = 0; i < Maxbrnch; i++) {
(tmpedge)[i].cluster = (int *) calloc((size_t) Maxspc, sizeof(int) );
if (tmpedge[i].cluster == NULL) maerror("edge cluster array in inittree");
(tmpedge)[i].penaltyprop = (uli *) calloc((size_t) Maxspc, sizeof(uli) );
if (tmpedge[i].penaltyprop == NULL) maerror("edge penalty propagation array in inittree");
(tmpedge)[i].taxon = -1;
for (j = 0; j < Maxspc; j++) {
(tmpedge)[i].cluster[j]=-1;
(tmpedge)[i].penaltyprop[j]=0;
}
/* number all edges */
tmpedge[i].numedge = i;
}
/* initialize tree */
*nextedge = 3;
*nextleaf = 3;
/* uppest edge =: root edge*/
*rootleaf = trueID[0];
/* init cluster vectors: */
/* for edge 0 */
(tmpedge[0].cluster)[0] = trueID[1]; /* leaf 1 in edge cluster */
(tmpedge[0].cluster)[1] = trueID[2]; /* leaf 2 in edge cluster */
tmpedge[0].clsize = 2;
/* for edge 1 */
(tmpedge[1].cluster)[0] = trueID[1]; /* leaf 0 in edge cluster */
tmpedge[1].clsize = 1;
/* for edge 2 */
(tmpedge[2].cluster)[0] = trueID[2]; /* leaf 0 in edge cluster */
tmpedge[2].clsize = 1;
/* initializing mrca marix */
for (i=0; i < Maxspc; i++) {
mrcamatrix[*rootleaf][i] = 0;
mrcamatrix[i][*rootleaf] = 0;
}
mrcamatrix[trueID[1]][trueID[2]] = 0;
mrcamatrix[trueID[2]][trueID[1]] = 0;
/* debug */
mrcamatrix[trueID[1]][trueID[1]] = -8;
mrcamatrix[trueID[2]][trueID[2]] = -8;
/* interconnection */
tmpedge[0].up = NULL;
tmpedge[0].downleft = &tmpedge[1];
tmpedge[0].downright = &tmpedge[2];
tmpedge[1].up = &tmpedge[0];
tmpedge[1].downleft = NULL;
tmpedge[1].downright = NULL;
tmpedge[2].up = &tmpedge[0];
tmpedge[2].downleft = NULL;
tmpedge[2].downright = NULL;
/* taxon IDs of leaves */
tmpedge[0].taxon = trueID[0];
tmpedge[1].taxon = trueID[1];
tmpedge[2].taxon = trueID[2];
/* edges of leaves */
tmpedgeofleaf[trueID[0]] = 0;
tmpedgeofleaf[trueID[1]] = 1;
tmpedgeofleaf[trueID[2]] = 2;
} /* inittree_mrca */
/******************/
/* free memory (to be called after inittree) */
void freetree_mrca(ONEEDGE *edge, /* edge array */
int *edgeofleaf, /* ext. edge idx array */
int Maxspc) /* No. of species */
{
int i;
/* free vectors */
for (i = 0; i < 2 * Maxspc - 3; i++) {
free(edge[i].cluster);
free(edge[i].penaltyprop);
}
/* free tree topology */
free(edge);
/* free external edge lookup vector */
free(edgeofleaf);
} /* freetree_mrca */
/******************/
void updatecluster_mrca(ONEEDGE *curredge, /* current edge */
int wherefrom, /* direction to last curr. edge */
int newleaf, /* leaf to add to clusters */
ONEEDGE *edge, /* edge array */
int Maxspc, /* No. of species */
int Maxbrnch, /* No. of branches */
int *edgeofleaf, /* ext. edge idx array */
int in_nextedge, /* next free edge idx */
int in_nextleaf, /* next free leaf idx */
imatrix mrcamatrix)
{
int n, edgenum, clsize, leaf;
ONEEDGE *branchedge;
if (wherefrom == DOWNLEFT)
branchedge = curredge->downright;
else
branchedge = curredge->downleft;
edgenum = curredge->numedge;
clsize = branchedge->clsize;
for (n=0; n<clsize; n++) {
leaf = branchedge->cluster[n];
mrcamatrix[newleaf][leaf] = edgenum;
mrcamatrix[leaf][newleaf] = edgenum;
}
curredge->cluster[(curredge->clsize)++] = newleaf; /* newleaf is down */
if ((curredge->up) != NULL) { /* not root */
if (((curredge->up)->downright) == curredge) {
wherefrom = DOWNRIGHT;
} else {
wherefrom = DOWNLEFT;
}
updatecluster_mrca(curredge->up, wherefrom, newleaf,
edge, Maxspc, Maxbrnch, edgeofleaf,
in_nextedge, in_nextleaf, mrcamatrix);
}
} /* updatecluster_mrca */
/******************/
/* add next leaf on the specified edge */
void updatetreeclusters_mrca(int dockedge, /* dockedge */
int newleaf, /* leaf to add to clusters */
ONEEDGE *edge, /* edge array */
int rootleaf, /* root leaf */
int Maxspc, /* No. of species */
int *edgeofleaf, /* ext. edge idx array */
int in_nextedge, /* next free edge idx */
int in_nextleaf, /* next free leaf idx */
ivector permut,
imatrix mrcamatrix)
{
int n; /* counter */
int wherefrom; /* direction to last edge */
ONEEDGE *curredge; /* edge array */
/* copy cluster dockedge -> nextedge */
for (n=0; n < edge[dockedge].clsize; n++) {
(edge[in_nextedge]).cluster[n] = (edge[dockedge]).cluster[n];
}
if (&(edge[in_nextedge]) != &(edge[edgeofleaf[rootleaf]])) {
#if 0
mrcamatrix[rootleaf][newleaf] = in_nextedge;
mrcamatrix[newleaf][rootleaf] = in_nextedge;
#endif
} else {
for (n=0; n < Maxspc; n++) {
mrcamatrix[rootleaf][n] = in_nextedge;
mrcamatrix[n][rootleaf] = in_nextedge;
}
}
/* set new internal edge's (nextedge) cluster size */
(edge[in_nextedge]).clsize = (edge[dockedge]).clsize;
/* add newleaf new external edge's (nextedge+1) cluster */
(edge[in_nextedge + 1]).cluster[0] = newleaf;
(edge[in_nextedge + 1]).clsize = 1;
curredge = &(edge[in_nextedge+1]);
if (curredge->up != NULL) { /* not root */
if (((curredge->up)->downright) == curredge) {
wherefrom = DOWNRIGHT;
} else {
wherefrom = DOWNLEFT;
}
/* climbing up from insertion point */
updatecluster_mrca(curredge->up, DOWNRIGHT, newleaf,
edge, Maxspc, Maxbrnch, edgeofleaf,
in_nextedge, in_nextleaf, mrcamatrix);
}
} /* updatetreeclusters_mrca */
/******************/
#if 0
void updatesplit_split(ONEEDGE *curredge, /* current edge */
int wherefrom, /* direction to last curr. edge */
int newleaf, /* leaf to add to splits */
ONEEDGE *edge, /* edge array */
int Maxspc, /* No. of species */
int Maxbrnch, /* No. of branches */
int *edgeofleaf, /* ext. edge idx array */
int in_nextedge, /* next free edge idx */
int in_nextleaf) /* next free leaf idx */
{
switch (wherefrom) {
case UP:
curredge->split[(curredge->upsize)++] = newleaf; /* leaf is up */
if (curredge->downright != NULL) { /* not leaf */
updatesplit_split(curredge->downright, UP, newleaf,
edge, Maxspc, Maxbrnch, edgeofleaf, in_nextedge, in_nextleaf);
updatesplit_split(curredge->downleft, UP, newleaf,
edge, Maxspc, Maxbrnch, edgeofleaf, in_nextedge, in_nextleaf);
} else { /* nothing to do */
return;
}
break;
case DOWNLEFT:
curredge->split[Maxspc - ++(curredge->downsize)] = newleaf; /* leaf is down */
if (curredge->downright != NULL) { /* not leaf */
updatesplit_split(curredge->downright, UP, newleaf,
edge, Maxspc, Maxbrnch, edgeofleaf, in_nextedge, in_nextleaf);
}
if (curredge->up != NULL) { /* not root */
if ((curredge->up)->downright == curredge) {
updatesplit_split(curredge->up, DOWNRIGHT, newleaf,
edge, Maxspc, Maxbrnch, edgeofleaf, in_nextedge, in_nextleaf);
} else {
updatesplit_split(curredge->up, DOWNLEFT, newleaf,
edge, Maxspc, Maxbrnch, edgeofleaf, in_nextedge, in_nextleaf);
}
}
break;
case DOWNRIGHT:
curredge->split[Maxspc - ++(curredge->downsize)] = newleaf; /* leaf is down */
if (curredge->downright != NULL) { /* not leaf */
updatesplit_split(curredge->downleft, UP, newleaf,
edge, Maxspc, Maxbrnch, edgeofleaf, in_nextedge, in_nextleaf);
}
if ((curredge->up) != NULL) { /* not root */
if ((curredge->up)->downright == curredge) {
updatesplit_split(curredge->up, DOWNRIGHT, newleaf,
edge, Maxspc, Maxbrnch, edgeofleaf, in_nextedge, in_nextleaf);
} else {
updatesplit_split(curredge->up, DOWNLEFT, newleaf,
edge, Maxspc, Maxbrnch, edgeofleaf, in_nextedge, in_nextleaf);
}
}
break;
}
} /* updatesplit_split */
/******************/
/* add next leaf on the specified edge */
void updatetreesplits_split(int dockedge, /* dockedge */
int newleaf, /* leaf to add to splits */
ONEEDGE *edge, /* edge array */
int Maxspc, /* No. of species */
int *edgeofleaf, /* ext. edge idx array */
int in_nextedge, /* next free edge idx */
int in_nextleaf, /* next free leaf idx */
ivector permut)
{
int n; /* counter */
int wherefrom; /* direction to last edge */
ONEEDGE *curredge; /* edge array */
/* copy clusters from dockedge to in_nextedge */
/* up-cluster: upsize-1 .. 0 */
for (n=(edge[dockedge].upsize) - 1; n >= 0; n--) {
(edge[in_nextedge]).split[n] = (edge[dockedge]).split[n];
}
(edge[in_nextedge]).upsize = (edge[dockedge]).upsize;
/* down-cluster: downsize .. Maxspc-1 */
for (n=Maxspc - (edge[dockedge]).downsize; n < Maxspc; n++) {
(edge[in_nextedge]).split[n] = (edge[dockedge]).split[n];
}
(edge[in_nextedge]).downsize = (edge[dockedge]).downsize;
/* add the new taxon to in_nextedge */
(edge[in_nextedge]).split[Maxspc - ++((edge[in_nextedge]).downsize)]
= permut[in_nextleaf];
/* setup clusters for new external edge (in_nextedge+1) */
/* up-cluster: all taxa in tree */
for (n=0; n < in_nextleaf; n++) {
(edge[in_nextedge+1]).split[n] = permut[n];
}
(edge[in_nextedge+1]).upsize = in_nextleaf;
/* down-cluster: the new taxon itself */
(edge[in_nextedge+1]).split[Maxspc-1] = permut[in_nextleaf];
(edge[in_nextedge+1]).downsize = 1;
curredge = &(edge[in_nextedge]);
if (curredge->up != NULL) { /* not root */
if (((curredge->up)->downright) == curredge) {
wherefrom = DOWNRIGHT;
} else {
wherefrom = DOWNLEFT;
}
/* climbing up from insertion point */
updatesplit_split(curredge->up, wherefrom, newleaf,
edge, Maxspc, Maxbrnch, edgeofleaf,
in_nextedge, in_nextleaf);
}
/* climbing down to dockedge from insertion point */
updatesplit_split(&(edge[dockedge]), UP, newleaf,
edge, Maxspc, Maxbrnch, edgeofleaf,
in_nextedge, in_nextleaf);
} /* updatetreesplits_split */
#endif
/******************/
/* add next leaf on the specified edge */
void addnextleaf_mrca(int dockedge, /* insert here */
ONEEDGE *edge, /* edge array */
int *edgeofleaf, /* ext. edge idx array */
int rootleaf, /* uppest leaf */
int Maxspc, /* No. of species */
int Maxbrnch, /* No. of branches */
int *in_nextedge, /* next free edge idx */
int *in_nextleaf, /* next free leaf idx */
ivector permut,
imatrix mrcamatrix) /* MRCA matrix */
{
int nextedge;
int nextleaf;
nextedge=*in_nextedge;
nextleaf=*in_nextleaf;
if (dockedge >= nextedge) {
/* Trying to add leaf nextleaf to nonexisting edge dockedge */
fprintf(STDOUT, "\n\n\nHALT: PLEASE REPORT ERROR F TO DEVELOPERS\n\n\n");
# if PARALLEL
PP_Finalize();
# endif
exit(1);
}
if (nextleaf >= Maxspc) {
/* Trying to add leaf nextleaf to a tree with Maxspc leaves */
fprintf(STDOUT, "\n\n\nHALT: PLEASE REPORT ERROR G TO DEVELOPERS\n\n\n");
# if PARALLEL
PP_Finalize();
# endif
exit(1);
}
/* necessary change in edgeofleaf if dockedge == root edge */
if (edgeofleaf[rootleaf] == dockedge)
edgeofleaf[rootleaf] = nextedge;
/* adding nextedge to the tree */
edge[nextedge].up = edge[dockedge].up;
edge[nextedge].downleft = &edge[dockedge];
edge[nextedge].downright = &edge[nextedge+1];
edge[dockedge].up = &edge[nextedge];
/* if not root edge: connect ancestor to internal edge (nextedge) */
if (edge[nextedge].up != NULL) {
if ( ((edge[nextedge].up)->downleft) == &edge[dockedge] )
(edge[nextedge].up)->downleft = &edge[nextedge];
else
(edge[nextedge].up)->downright = &edge[nextedge];
}
/* adding new external edge (nextedge+1) to the tree */
edge[nextedge+1].up = &edge[nextedge];
edge[nextedge+1].downleft = NULL;
edge[nextedge+1].downright = NULL;
edge[nextedge+1].taxon = permut[nextleaf];
edgeofleaf[permut[nextleaf]] = nextedge+1;
#if 0
updatetreesplits_split(dockedge, permut[nextleaf], edge, Maxspc,
edgeofleaf, nextedge, nextleaf, permut);
#endif
updatetreeclusters_mrca(dockedge, permut[nextleaf], edge, rootleaf,
Maxspc, edgeofleaf, nextedge, nextleaf,
permut, mrcamatrix);
(*in_nextedge) += 2;
(*in_nextleaf) ++;
} /* addnextleaf_mrca */
/******************/
/* compute edge penalties using splits, if #minedges>1 choose one randomly */
void computepenalties_mrca(ONEEDGE *curredge, /* edge array */
int Maxspc, /* number of taxa */
imatrix mrcamatrix, /* MRCA matrix */
int *minedges, /* minimum edge vector */
int *howmany, /* number minimum edge */
uli *minpenalty) /* minimum penalty */
{
int l, r;
int lmax, rmax;
int taxon;
ONEEDGE *left; /* temp edge ptr */
ONEEDGE *right; /* temp edge ptr */
if (curredge->downleft == NULL) { /* leaf edge */
/* check for new minimal panalty */
if (*minpenalty == curredge->edgeinfo) {
/* another min edge */
minedges[(*howmany)++] = curredge->numedge;
} else { /* -> not same minimum */
if (*minpenalty > curredge->edgeinfo) {
/* new minimum penalty */
*howmany = 1;
minedges[0] = curredge->numedge;
*minpenalty = curredge->edgeinfo;
} /* if new minimum */
} /* same minimum */
} else { /* -> internal edge */
/* propagate to left child */
left = curredge->downleft;
lmax = left->clsize;
left->edgeinfo = 0;
for (l= 0; l<lmax; l++) {
taxon = left->cluster[l];
left->edgeinfo += curredge->penaltyprop[taxon];
left->penaltyprop[taxon] += curredge->penaltyprop[taxon];
}
/* propagate to right child */
right = curredge->downright;
rmax = right->clsize;
right->edgeinfo = 0;
for (r= 0; r<rmax; r++) {
taxon = right->cluster[r];
right->edgeinfo += curredge->penaltyprop[taxon];
right->penaltyprop[taxon] += curredge->penaltyprop[taxon];
}
/* descend downleft */
computepenalties_mrca(left, Maxspc,
mrcamatrix,
minedges, howmany,
minpenalty);
/* descend downright */
computepenalties_mrca(right, Maxspc,
mrcamatrix,
minedges, howmany,
minpenalty);
if (*minpenalty == curredge->edgeinfo) {
/* another min edge */
minedges[(*howmany)++] = curredge->numedge;
} else { /* -> not same minimum */
if (*minpenalty > curredge->edgeinfo) {
/* new minimum penalty */
*howmany = 1;
minedges[0] = curredge->numedge;
*minpenalty = curredge->edgeinfo;
} /* if new minimum */
} /* same minimum */
;
}
} /* computepenalties_mrca */
/******************/
/* compute edge penalties using splits, if #minedges>1 choose one randomly */
void computepenaltiestree_mrca(ONEEDGE *tmpedge, /* edge array */
int *edgeofleaf, /* external edge ptrs */
int rootleaf, /* idx of uppest edge */
int nextleaf, /* next free leaf idx */
int Maxspc, /* number of taxa */
imatrix mrcamatrix, /* MRCA matrix */
int *minedges, /* minimum edge vector */
int *out_howmany, /* number minimum edge */
uli *out_minpenalty, /* minimum penalty */
int *out_minedge) /* minimum edge */
{
int rootedge;
int taxon;
int lmax, rmax;
int l, r;
int howmany = 0;
ONEEDGE *left; /* temp edge ptr */
ONEEDGE *right; /* temp edge ptr */
uli minpenalty;
uli edgeinfo;
ONEEDGE *curredge;
rootedge = edgeofleaf[rootleaf];
curredge = &(tmpedge[rootedge]);
curredge->edgeinfo = curredge->penaltyprop[rootleaf];
if (minedges == NULL)
minedges = (int *) calloc((size_t) Maxspc, sizeof(int));
/* first edge: init */
minedges[0] = rootedge;
howmany = 1;
minpenalty = curredge->edgeinfo;
edgeinfo = 0;
for (l= 0; l<nextleaf-1; l++) {
edgeinfo += (curredge->penaltyprop)[curredge->cluster[l]];
}
#if 0
if ((edgeinfo) != (curredge->edgeinfo))
fprintf(stderr, "%ld != %ld\n", edgeinfo, curredge->edgeinfo);
else
fprintf(stderr, "%ld == %ld\n", edgeinfo, curredge->edgeinfo);
if ((edgeinfo) != (curredge->edgeinfo)) {
/* root edge cluster penalty should be equal to root penalty */
fprintf(STDOUT, "\n\n\nHALT: PLEASE REPORT ERROR HSCL2 TO DEVELOPERS\n\n\n");
# if PARALLEL
PP_Finalize();
# endif
exit(1);
}
#endif
/* propagate to left child */
left = curredge->downleft;
lmax = left->clsize;
left->edgeinfo = 0;
for (l= 0; l<lmax; l++) {
taxon = left->cluster[l];
left->edgeinfo += curredge->penaltyprop[taxon];
left->penaltyprop[taxon] += curredge->penaltyprop[taxon];
}
/* propagate to right child */
right = curredge->downright;
rmax = right->clsize;
right->edgeinfo = 0;
for (r= 0; r<rmax; r++) {
taxon = right->cluster[r];
right->edgeinfo += curredge->penaltyprop[taxon];
right->penaltyprop[taxon] += curredge->penaltyprop[taxon];
}
if ((lmax + rmax) != (nextleaf-1)) {
/* root edge cluster should yield all other leaves */
fprintf(STDOUT, "\n\n\nHALT: PLEASE REPORT ERROR HSCL1 TO DEVELOPERS\n\n\n");
# if PARALLEL
PP_Finalize();
# endif
exit(1);
}
/* descend downleft */
computepenalties_mrca(left, Maxspc,
mrcamatrix,
minedges, &howmany,
&minpenalty);
/* descend downright */
computepenalties_mrca(right, Maxspc,
mrcamatrix,
minedges, &howmany,
&minpenalty);
#if 0 /* optimization (HAS) */
if (howmany > 1)
*out_minedge = minedges[randominteger(howmany)];
else
*out_minedge = minedges[0];
#endif
if (howmany > 1) { /* draw random edge */
int i, k, randomnum;
randomnum = randominteger(howmany) + 1; /* 1 to howmany */
i = -1;
for (k = 0; k < randomnum; k++) {
do {
i++;
} while (tmpedge[i].edgeinfo != minpenalty);
*out_minedge = tmpedge[i].numedge;
}
} else {
*out_minedge = minedges[0];
}
*out_howmany = howmany;
*out_minpenalty = minpenalty;
} /* computepenaltiestree_mrca */
/******************/
#if 0
/* compute edge penalties using splits, if #minedges>1 choose one randomly */
void computepenalties_split(ONEEDGE *tmpedge, /* edge array */
int nextedge, /* next free edge idx */
int Maxspc, /* number of taxa */
ulimatrix neighbormatr, /* neighborhood matrix */
int **out_minedges, /* minimum edge vector */
int *out_howmany, /* number minimum edge */
uli *out_minpenalty, /* minimum penalty */
int *out_minedge) /* minimum edge */
{
int i; /* counter */
int a, b;
int idxA, idxB;
int maxUP; /* index counter for right/left split cluster */
int howmany = 0;
int *minedges = *out_minedges;
uli minpenalty;
for (i = 0; i < nextedge; i++) {
(tmpedge[i]).penalty = 0; /* reset penalty */
maxUP = (tmpedge[i]).upsize;
for (idxB = Maxspc - (tmpedge[i]).downsize; idxB < Maxspc; idxB++) {
b = (tmpedge[i].split)[idxB];
for (idxA = 0; idxA < maxUP; idxA++) {
a = (tmpedge[i].split)[idxA];
(tmpedge[i]).penalty += neighbormatr[a][b];
} /* for all root-sided (up) taxa */
} /* for all non-root-sided (down) taxa */
if (howmany == 0) { /* first edge: init */
if (minedges == NULL)
minedges = (int *) calloc((size_t) Maxspc, sizeof(int));
minedges[howmany++] = i;
minpenalty = tmpedge[i].penalty;
} else { /* -> all other edges */
if (minpenalty == tmpedge[i].penalty) {
/* another min edge */
minedges[howmany++] = i;
} else { /* -> not same minimum */
if (minpenalty > tmpedge[i].penalty) {
/* new minimum penalty */
howmany = 0;
minedges[howmany++] = i;
minpenalty = tmpedge[i].penalty;
} /* if new minimum */
} /* same minimum */
} /* if all other edges */
} /* for all edges */
*out_howmany = howmany,
*out_minpenalty = minpenalty;
#if 0
if (howmany > 1)
*out_minedge = minedges[0];
else
*out_minedge = minedges[0];
#endif
} /* computepenalties_split */
#endif
/******************/
/* trueID-ed (HAS) */
/* perform one single puzzling step to produce one intermediate tree */
void onepstep_mrca( /* PStep (intermediate) tree topol: */
ONEEDGE **edge, /* out: new array of edges */
int **edgeofleaf, /* out: array of extern edge ptrs */
int *rootleaf, /* out: root leaf in topol, starting perm[0] */
unsigned char *quartettopols, /* in: quartetblock with all topols */
int Maxspc, /* in: Number of species (n) */
ivector permut) /* in: species permutation (trueID) */
{
/* local variables: */
int Maxbrnch = (2*Maxspc)-3; /* Number of branches (2n-3) */
int nextedge; /* next free edge index (=3) */
int nextleaf; /* next free leaf index (=3) */
int minedge; /* insertion edge of minimum penalty */
uli mininfo; /* minimum penalty */
int a, b, c, i; /* quartet leaves, i to be inserted */
int idxA, idxB;
int idxC, idxI; /* index counter for quartet leaves */
int chooseX, chooseY; /* end leaves of penalty path */
ONEEDGE *tmpedge; /* new array of edges */
int *tmpedgeofleaf; /* array of extern edge ptrs */
int tmprootleaf; /* root edge */
imatrix mrcamatrix;
int mrca;
int *minedges;
int howmany;
#if 0
ulimatrix neighbormatr;
neighbormatr = new_ulimatrix(Maxspc,Maxspc);
#endif
minedges = (int *) calloc((size_t) Maxspc, sizeof(int));
if (minedges == NULL) maerror("minimal edge set in onepstep_mrca");
mrcamatrix = new_imatrix(Maxspc,Maxspc);
/* allocate and initialize new tree topology */
inittree_mrca(&tmpedge, &tmpedgeofleaf, &tmprootleaf, Maxspc, Maxbrnch, &nextedge, &nextleaf, permut, mrcamatrix);
/* check_mrca(tmpedge, Maxspc, tmpedgeofleaf, nextedge, nextleaf, permut); */
for (idxI = 3; idxI < Maxspc; idxI++) {
i = permut[idxI];
#if 0
fprintf(stderr, "----------------------------------------------\n");
fprintf(stderr, " %d. adding taxon %d \n", idxI, i);
fprintf(stderr, "----------------------------------------------\n");
/* initialize penalty neighborhood matrix + leafpenalty vector */
for (idxA = 0; idxA < nextleaf - 1; idxA++) {
a = permut[idxA];
for (idxB = idxA + 1; idxB < nextleaf; idxB++) {
b = permut[idxB];
neighbormatr[a][b] = 0;
neighbormatr[b][a] = 0;
}
} /* for all entries in the upper neighbormatr */
#endif
/*
* core of quartet puzzling algorithm
*/
for (idxA = 0; idxA < nextleaf - 2; idxA++) {
a = permut[idxA];
for (idxB = idxA + 1; idxB < nextleaf - 1; idxB++) {
b = permut[idxB];
for (idxC = idxB + 1; idxC < nextleaf; idxC++) {
c = permut[idxC];
/* check which two leaves out of
a,b,c are closer related to each
other than to leaf i according to a
least squares fit of the continous
Bayesian weights to the seven
trivial "attractive regions". We
assign a score of 1 to all edges
on the penalty path between these
two leaves chooseX and chooseY */
/* de-trueID-ed (HAS) */
checkquartet(a, b, c, i, &chooseX, &chooseY);
#if 0
(neighbormatr[chooseX][chooseY])++;
(neighbormatr[chooseY][chooseX])++;
#endif
mrca = mrcamatrix[chooseX][chooseY];
(tmpedge[mrca].penaltyprop[chooseX])++;
(tmpedge[mrca].penaltyprop[chooseY])++;
} /* for all quartets q=(a,b,c,i): c */
} /* for all quartets q=(a,b,c,i): b */
} /* for all quartets q=(a,b,c,i): a */
#if 0
computepenalties_split(tmpedge, nextedge, Maxspc, neighbormatr,
&minedges, &howmany, &mininfo, &minedge);
{int m,n;
for (m=0; m<nextedge; m++) {
fprintf(stderr, "EEE e[%2d]%c t(%2d): cl[%d]=",
m, (tmpedge[m].taxon >= 0) ? 'e' : 'i', tmpedge[m].taxon,
tmpedge[m].clsize);
for (n=0; n<tmpedge[m].clsize; n++)
fprintf(stderr, "%d, ", tmpedge[m].cluster[n]);
fprintf(stderr, "\n");
}}
#endif
computepenaltiestree_mrca(tmpedge, tmpedgeofleaf, tmprootleaf,
nextleaf, Maxspc, mrcamatrix,
minedges, &howmany, &mininfo, &minedge);
/* add the next leaf on minedge */
addnextleaf_mrca(minedge, tmpedge, tmpedgeofleaf, tmprootleaf, Maxspc, Maxbrnch, &nextedge, &nextleaf, permut, mrcamatrix);
for (a = 0; a < Maxbrnch; a++) {
for (b = 0; b < Maxspc; b++) {
/* (tmpedge)[a].cluster[b]=-1; */
(tmpedge)[a].penaltyprop[b]=0;
}
}
} /* adding all other leafs */
*edge = tmpedge; /* export tree topology */
*edgeofleaf = tmpedgeofleaf; /* export array of extern edge ptrs */
*rootleaf = tmprootleaf; /* export root edge */
free(minedges);
free_imatrix(mrcamatrix);
} /* onepstep_mrca */
|