boolean testInsertRestoreBIG (tree *tr, nodeptr p, nodeptr q) { if(Thorough) { if (! insertBIG(tr, p, q, tr->numBranches)) return FALSE; evaluateGeneric(tr, p->next->next); } else { if (! insertRestoreBIG(tr, p, q)) return FALSE; { nodeptr x, y; x = p->next->next; y = p->back; if(! isTip(x->number, tr->rdta->numsp) && isTip(y->number, tr->rdta->numsp)) { while ((! x->x)) { if (! (x->x)) newviewGeneric(tr, x); } } if(isTip(x->number, tr->rdta->numsp) && !isTip(y->number, tr->rdta->numsp)) { while ((! y->x)) { if (! (y->x)) newviewGeneric(tr, y); } } if(!isTip(x->number, tr->rdta->numsp) && !isTip(y->number, tr->rdta->numsp)) { while ((! x->x) || (! y->x)) { if (! (x->x)) newviewGeneric(tr, x); if (! (y->x)) newviewGeneric(tr, y); } } } tr->likelihood = tr->endLH; } return TRUE; }
reset_stNNI(state *s) { nodeptr p, q; p = s->p; q = p->back; nodeptr pb1, pb2; pb1 = p->next->back; pb2 = p->next->next->back; nodeptr qb1, qb2; qb1 = q->next->back; qb2 = q->next->next->back; /* whichNNI 1*/ if(s->whichNNI == 1) { hookup(p, q, q->z_tmp, s->tr->numBranches); hookup(p->next, qb1, p->next->z_tmp, s->tr->numBranches); hookup(p->next->next, pb2, p->next->next->z_tmp, s->tr->numBranches); hookup(q->next, pb1, q->next->z_tmp, s->tr->numBranches); hookup(q->next->next, qb2, q->next->next->z_tmp, s->tr->numBranches); } else if(s->whichNNI == 2) { hookup(p, q, p->z_tmp, s->tr->numBranches); hookup(p->next, pb1, p->next->z_tmp, s->tr->numBranches); hookup(p->next->next, qb1, p->next->next->z_tmp, s->tr->numBranches); hookup(q->next, pb2, q->next->z_tmp, s->tr->numBranches); hookup(q->next->next, qb2, q->next->next->z_tmp, s->tr->numBranches); } else { assert(s->whichNNI == 0); hookup(p, q, p->z_tmp, s->tr->numBranches); hookup(p->next, pb1, p->next->z_tmp, s->tr->numBranches); hookup(p->next->next, pb2, p->next->next->z_tmp, s->tr->numBranches); hookup(q->next, qb1, q->next->z_tmp, s->tr->numBranches); hookup(q->next->next, qb2, q->next->next->z_tmp, s->tr->numBranches); } /* reset_branch_length(p, s->tr->numBranches); reset_branch_length(p->next, s->tr->numBranches); reset_branch_length(p->next->next, s->tr->numBranches); reset_branch_length(q->next, s->tr->numBranches); reset_branch_length(q->next->next, s->tr->numBranches); */ newviewGeneric(s->tr, p, FALSE); newviewGeneric(s->tr, q, FALSE); }
void treeEvaluateProgressive(tree *tr) { int i, k; tr->branchCounter = 0; tr->numberOfBranches = 2 * tr->mxtips - 3; tr->bInf = (branchInfo*)rax_malloc(tr->numberOfBranches * sizeof(branchInfo)); setupBranches(tr, tr->start->back, tr->bInf); assert(tr->branchCounter == tr->numberOfBranches); for(i = 0; i < tr->numBranches; i++) tr->partitionConverged[i] = FALSE; for(i = 0; i < 10; i++) { for(k = 0; k < tr->numberOfBranches; k++) { update(tr, tr->bInf[k].oP); newviewGeneric(tr, tr->bInf[k].oP); } evaluateGenericInitrav(tr, tr->start); printf("It %d %f \n", i, tr->likelihood); } }
nodeptr removeNodeRestoreBIG (tree *tr, nodeptr p) { nodeptr q, r; q = p->next->back; r = p->next->next->back; newviewGeneric(tr, q); newviewGeneric(tr, r); hookup(q, r, tr->currentZQR, tr->numBranches); p->next->next->back = p->next->back = (node *) NULL; return q; }
static void resetSimpleNodeProposal(state * instate) { /* prune the insertion */ hookup(instate->q, instate->r, instate->qz, instate->tr->numBranches); instate->p->next->next->back = instate->p->next->back = (nodeptr) NULL; /* insert the pruned tree in its original node */ hookup(instate->p->next, instate->nb, instate->nbz, instate->tr->numBranches); hookup(instate->p->next->next, instate->nnb, instate->nnbz, instate->tr->numBranches); newviewGeneric(instate->tr, instate->p, FALSE); }
static double testInsertThorough(tree *tr, nodeptr r, nodeptr q, boolean useVector) { double result, qz[NUM_BRANCHES], z[NUM_BRANCHES]; nodeptr x = q->back, s = r->back; int j; for(j = 0; j < tr->numBranches; j++) { qz[j] = q->z[j]; z[j] = sqrt(qz[j]); if(z[j] < zmin) z[j] = zmin; if(z[j] > zmax) z[j] = zmax; } hookup(r->next, q, z, tr->numBranches); hookup(r->next->next, x, z, tr->numBranches); hookupDefault(r, s, tr->numBranches); newviewGeneric(tr, r); localSmooth(tr, r, smoothings); if(useVector) result = evaluateGenericVector(tr, r); else result = evaluateGeneric(tr, r); hookup(q, x, qz, tr->numBranches); r->next->next->back = r->next->back = (nodeptr) NULL; return result; }
boolean initrav (tree *tr, nodeptr p) { nodeptr q; if (!isTip(p->number, tr->rdta->numsp)) { q = p->next; do { if (! initrav(tr, q->back)) return FALSE; q = q->next; } while (q != p); newviewGeneric(tr, p); } return TRUE; }
boolean smooth (tree *tr, nodeptr p) { nodeptr q; if (! update(tr, p)) return FALSE; /* Adjust branch */ if (! isTip(p->number, tr->rdta->numsp)) { /* Adjust descendants */ q = p->next; while (q != p) { if (! smooth(tr, q->back)) return FALSE; q = q->next; } if(tr->multiBranch) newviewGenericMasked(tr, p); else newviewGeneric(tr, p); } return TRUE; }
static void traverse_branches(nodeptr p, int *count, state * s, boolean resetBL) { nodeptr q; //printf("current BL at %db%d: %f\n", p->number, p->back->number, p->z[0]); if(resetBL) reset_branch_length(p, s->tr->numBranches); else//can allow for other methods later set_branch_length_sliding_window(p, s->tr->numBranches, s, TRUE); *count += 1; if (! isTip(p->number, s->tr->mxtips)) { /* Adjust descendants */ q = p->next; while (q != p) { traverse_branches(q->back, count, s, resetBL); q = q->next; } newviewGeneric(s->tr, p, FALSE); // not sure if we need this } }
boolean smoothRegion (tree *tr, nodeptr p, int region) { nodeptr q; if (! update(tr, p)) return FALSE; /* Adjust branch */ if(region > 0) { if (!isTip(p->number, tr->rdta->numsp)) { q = p->next; while (q != p) { if (! smoothRegion(tr, q->back, --region)) return FALSE; q = q->next; } newviewGeneric(tr, p); } } return TRUE; }
boolean insertRestoreBIG (tree *tr, nodeptr p, nodeptr q) { nodeptr r, s; r = q->back; s = p->back; if(Thorough) { hookup(p->next, q, tr->currentLZQ, tr->numBranches); hookup(p->next->next, r, tr->currentLZR, tr->numBranches); hookup(p, s, tr->currentLZS, tr->numBranches); } else { double z[NUM_BRANCHES]; int i; for(i = 0; i < tr->numBranches; i++) { double zz; zz = sqrt(q->z[i]); if(zz < zmin) zz = zmin; if(zz > zmax) zz = zmax; z[i] = zz; } hookup(p->next, q, z, tr->numBranches); hookup(p->next->next, r, z, tr->numBranches); } newviewGeneric(tr, p); return TRUE; }
static boolean qsmoothLocal(tree *tr, nodeptr p, int n) { nodeptr q; if(n == 0) return TRUE; else { if (! qupdate(tr, p)) return FALSE; /* Adjust branch */ if (!isTip(p->number, tr->rdta->numsp)) { /* Adjust descendants */ q = p->next; while (q != p) { if (! qsmoothLocal(tr, q->back, n - 1)) return FALSE; q = q->next; } newviewGeneric(tr, p); } return TRUE; } }
static void insertFast (tree *tr, nodeptr p, nodeptr q, int numBranches) { nodeptr r, s; int i; r = q->back; s = p->back; for(i = 0; i < numBranches; i++) tr->lzi[i] = q->z[i]; if(Thorough) { double zqr[NUM_BRANCHES], zqs[NUM_BRANCHES], zrs[NUM_BRANCHES], lzqr, lzqs, lzrs, lzsum, lzq, lzr, lzs, lzmax; double defaultArray[NUM_BRANCHES]; double e1[NUM_BRANCHES], e2[NUM_BRANCHES], e3[NUM_BRANCHES]; double *qz; qz = q->z; for(i = 0; i < numBranches; i++) defaultArray[i] = defaultz; makenewzGeneric(tr, q, r, qz, iterations, zqr, FALSE); makenewzGeneric(tr, q, s, defaultArray, iterations, zqs, FALSE); makenewzGeneric(tr, r, s, defaultArray, iterations, zrs, FALSE); for(i = 0; i < numBranches; i++) { lzqr = (zqr[i] > zmin) ? log(zqr[i]) : log(zmin); lzqs = (zqs[i] > zmin) ? log(zqs[i]) : log(zmin); lzrs = (zrs[i] > zmin) ? log(zrs[i]) : log(zmin); lzsum = 0.5 * (lzqr + lzqs + lzrs); lzq = lzsum - lzrs; lzr = lzsum - lzqs; lzs = lzsum - lzqr; lzmax = log(zmax); if (lzq > lzmax) {lzq = lzmax; lzr = lzqr; lzs = lzqs;} else if (lzr > lzmax) {lzr = lzmax; lzq = lzqr; lzs = lzrs;} else if (lzs > lzmax) {lzs = lzmax; lzq = lzqs; lzr = lzrs;} e1[i] = exp(lzq); e2[i] = exp(lzr); e3[i] = exp(lzs); } hookup(p->next, q, e1, numBranches); hookup(p->next->next, r, e2, numBranches); hookup(p, s, e3, numBranches); } else { double z[NUM_BRANCHES]; for(i = 0; i < numBranches; i++) { z[i] = sqrt(q->z[i]); if(z[i] < zmin) z[i] = zmin; if(z[i] > zmax) z[i] = zmax; } hookup(p->next, q, z, tr->numBranches); hookup(p->next->next, r, z, tr->numBranches); } newviewGeneric(tr, p); if(Thorough) { localSmooth(tr, p, smoothings); for(i = 0; i < numBranches; i++) { tr->lzq[i] = p->next->z[i]; tr->lzr[i] = p->next->next->z[i]; tr->lzs[i] = p->z[i]; } } }
int main(int argc, char * argv[]) { tree * tr; if (argc != 2) { fprintf (stderr, "syntax: %s [binary-alignment-file]\n", argv[0]); return (1); } tr = (tree *)malloc(sizeof(tree)); /* read the binary input, setup tree, initialize model with alignment */ read_msa(tr,argv[1]); tr->randomNumberSeed = 665; makeRandomTree(tr); printf("Number of taxa: %d\n", tr->mxtips); printf("Number of partitions: %d\n", tr->NumberOfModels); /* compute the LH of the full tree */ printf ("Virtual root: %d\n", tr->start->number); evaluateGeneric(tr, tr->start, TRUE); printf("Likelihood: %f\n", tr->likelihood); /* 8 rounds of branch length optimization */ smoothTree(tr, 1); evaluateGeneric(tr, tr->start, TRUE); printf("Likelihood after branch length optimization: %.20f\n", tr->likelihood); /* Now we show how to find a particular LH vector for a node */ int i; int node_number = tr->mxtips + 1; nodeptr p = tr->nodep[node_number]; printf("Pointing to node %d\n", p->number); /* Fix as VR */ newviewGeneric(tr, p, FALSE); newviewGeneric(tr, p->back, FALSE); evaluateGeneric(tr, p, FALSE); printf("Likelihood : %.f\n", tr->likelihood); printf("Make a copy of LH vector for node %d\n", p->number); likelihood_vector *vector = copy_likelihood_vectors(tr, p); for(i=0; i<vector->num_partitions; i++) printf("Partition %d requires %d bytes\n", i, (int)vector->partition_sizes[i]); /* Check we have the same vector in both tree and copied one */ assert(same_vector(tr, p, vector)); /* Now force the p to get a new value (generally branch lengths are NOT updated like this) */ /* This is just an example to show usage (for fast NNI eval), manually updating vectors is not recommended! */ printf("bl : %.40f\n", p->next->z[0]); p->next->z[0] = p->next->back->z[0] = zmin; printf("bl : %.40f\n", p->next->z[0]); newviewGeneric(tr, p, FALSE); assert(!same_vector(tr, p, vector)); evaluateGeneric(tr, p, FALSE); printf("Likelihood : %f\n", tr->likelihood); restore_vector(tr, p, vector); assert(same_vector(tr, p, vector)); evaluateGeneric(tr, p, FALSE); printf("Likelihood after manually restoring the vector : %f\n", tr->likelihood); free_likelihood_vector(vector); /* Pick an inner branch */ printf("numBranches %d \n", tr->numBranches); //tr->numBranches = 1; p = tr->nodep[tr->mxtips + 1]; int partition_id = 0; /* single partition */ double bl = get_branch_length(tr, p, partition_id); printf("z value: %f , bl value %f\n", p->z[partition_id], bl); /* set the bl to 2.5 */ double new_bl = 2.5; set_branch_length(tr, p, partition_id, new_bl); printf("Changed BL to %f\n", new_bl); printf("new z value: %f , new bl value %f\n", p->z[partition_id], get_branch_length(tr, p, partition_id)); /* set back to original */ printf("Changed to previous BL\n"); set_branch_length(tr, p, partition_id, bl); printf("new z value: %f , new bl value %f\n", p->z[partition_id], get_branch_length(tr, p, partition_id)); return (0); }
static double linearSPRs(tree *tr, int radius, boolean veryFast) { int numberOfSubtrees = (tr->mxtips - 2) * 3, count = 0, k, i; double fourScores[4]; nodeptr *ptr = (nodeptr *)rax_malloc(sizeof(nodeptr) * numberOfSubtrees); fourLikelihoods *fourLi = (fourLikelihoods *)rax_malloc(sizeof(fourLikelihoods) * 4); insertions *ins = (insertions*)rax_malloc(sizeof(insertions)); ins->count = 0; ins->maxCount = 2048; ins->s = (scores *)rax_malloc(sizeof(scores) * ins->maxCount); /* recursively compute the roots of all subtrees in the current tree and store them in ptr */ getSubtreeRoots(tr->start->back, ptr, &count, tr->mxtips); assert(count == numberOfSubtrees); tr->startLH = tr->endLH = tr->likelihood; /* loop over subtrees, i.e., execute a full SPR cycle */ for(i = 0; i < numberOfSubtrees; i++) { nodeptr p = ptr[i], p1 = p->next->back, p2 = p->next->next->back; double p1z[NUM_BRANCHES], p2z[NUM_BRANCHES]; ins->count = 0; /*printf("Node %d %d\n", p->number, i);*/ tr->bestOfNode = unlikely; for(k = 0; k < 4; k++) fourScores[k] = unlikely; assert(!isTip(p->number, tr->rdta->numsp)); if(!isTip(p1->number, tr->rdta->numsp) || !isTip(p2->number, tr->rdta->numsp)) { double max = unlikely; int maxInt = -1; for(k = 0; k < tr->numBranches; k++) { p1z[k] = p1->z[k]; p2z[k] = p2->z[k]; } /* remove the current subtree */ removeNodeBIG(tr, p, tr->numBranches); /* pre score with fast insertions */ if(veryFast) Thorough = 1; else Thorough = 0; if (!isTip(p1->number, tr->rdta->numsp)) { fourScores[0] = testInsertFast(tr, p, p1->next->back, ins, veryFast); fourScores[1] = testInsertFast(tr, p, p1->next->next->back, ins, veryFast); } if (!isTip(p2->number, tr->rdta->numsp)) { fourScores[2] = testInsertFast(tr, p, p2->next->back, ins, veryFast); fourScores[3] = testInsertFast(tr, p, p2->next->next->back, ins, veryFast); } if(veryFast) Thorough = 1; else Thorough = 0; /* find the most promising direction */ if(!veryFast) { int j = 0, validEntries = 0; double lmax = unlikely, posterior = 0.0; for(k = 0; k < 4; k++) { fourLi[k].direction = k; fourLi[k].likelihood = fourScores[k]; } qsort(fourLi, 4, sizeof(fourLikelihoods), fourCompare); for(k = 0; k < 4; k++) if(fourLi[k].likelihood > unlikely) validEntries++; lmax = fourLi[0].likelihood; while(posterior <= POSTERIOR_THRESHOLD && j < validEntries) { double all = 0.0, prob = 0.0; for(k = 0; k < validEntries; k++) all += exp(fourLi[k].likelihood - lmax); posterior += (prob = (exp(fourLi[j].likelihood - lmax) / all)); switch(fourLi[j].direction) { case 0: insertBeyond(tr, p, p1->next->back, radius, ins, veryFast); break; case 1: insertBeyond(tr, p, p1->next->next->back, radius, ins, veryFast); break; case 2: insertBeyond(tr, p, p2->next->back, radius, ins, veryFast); break; case 3: insertBeyond(tr, p, p2->next->next->back, radius, ins, veryFast); break; default: assert(0); } j++; } qsort(ins->s, ins->count, sizeof(scores), scoreCompare); Thorough = 1; for(k = 0; k < MIN(ins->count, 20); k++) testInsertCandidates(tr, p, ins->s[k].p); } else { Thorough = 1; for(k = 0; k < 4; k++) { if(max < fourScores[k]) { max = fourScores[k]; maxInt = k; } } /* descend into this direction and re-insert subtree there */ if(maxInt >= 0) { switch(maxInt) { case 0: insertBeyond(tr, p, p1->next->back, radius, ins, veryFast); break; case 1: insertBeyond(tr, p, p1->next->next->back, radius, ins, veryFast); break; case 2: insertBeyond(tr, p, p2->next->back, radius, ins, veryFast); break; case 3: insertBeyond(tr, p, p2->next->next->back, radius, ins, veryFast); break; default: assert(0); } } } /* repair branch and reconnect subtree to its original position from which it was pruned */ hookup(p->next, p1, p1z, tr->numBranches); hookup(p->next->next, p2, p2z, tr->numBranches); /* repair likelihood vectors */ newviewGeneric(tr, p); /* if the rearrangement of subtree rooted at p yielded a better likelihood score restore the altered topology and use it from now on */ if(tr->endLH > tr->startLH) { restoreTreeFast(tr); tr->startLH = tr->endLH = tr->likelihood; } } } return tr->startLH; }
static void doNNIs(tree *tr, nodeptr p, double *lhVectors[3], boolean shSupport, int *interchanges, int *innerBranches, double *pqz_0, double *pz1_0, double *pz2_0, double *qz1_0, double *qz2_0, double *pqz_1, double *pz1_1, double *pz2_1, double *qz1_1, double *qz2_1, double *pqz_2, double *pz1_2, double *pz2_2, double *qz1_2, double *qz2_2) { nodeptr q = p->back, pb1 = p->next->back, pb2 = p->next->next->back; assert(!isTip(p->number, tr->mxtips)); if(!isTip(q->number, tr->mxtips)) { int whichNNI = 0; nodeptr qb1 = q->next->back, qb2 = q->next->next->back; double lh[3]; *innerBranches = *innerBranches + 1; nniSmooth(tr, p, 16); if(shSupport) { evaluateGenericVector(tr, p); memcpy(lhVectors[0], tr->perSiteLL, sizeof(double) * tr->cdta->endsite); } else evaluateGeneric(tr, p); lh[0] = tr->likelihood; storeBranches(tr, p, pqz_0, pz1_0, pz2_0, qz1_0, qz2_0); /*******************************************/ hookup(p, q, pqz_0, tr->numBranches); hookup(p->next, qb1, qz1_0, tr->numBranches); hookup(p->next->next, pb2, pz2_0, tr->numBranches); hookup(q->next, pb1, pz1_0, tr->numBranches); hookup(q->next->next, qb2, qz2_0, tr->numBranches); newviewGeneric(tr, p); newviewGeneric(tr, p->back); nniSmooth(tr, p, 16); if(shSupport) { evaluateGenericVector(tr, p); memcpy(lhVectors[1], tr->perSiteLL, sizeof(double) * tr->cdta->endsite); } else evaluateGeneric(tr, p); lh[1] = tr->likelihood; storeBranches(tr, p, pqz_1, pz1_1, pz2_1, qz1_1, qz2_1); if(lh[1] > lh[0]) whichNNI = 1; /*******************************************/ hookup(p, q, pqz_0, tr->numBranches); hookup(p->next, qb1, qz1_0, tr->numBranches); hookup(p->next->next, pb1, pz1_0, tr->numBranches); hookup(q->next, pb2, pz2_0, tr->numBranches); hookup(q->next->next, qb2, qz2_0, tr->numBranches); newviewGeneric(tr, p); newviewGeneric(tr, p->back); nniSmooth(tr, p, 16); if(shSupport) { evaluateGenericVector(tr, p); memcpy(lhVectors[2], tr->perSiteLL, sizeof(double) * tr->cdta->endsite); } else evaluateGeneric(tr, p); lh[2] = tr->likelihood; storeBranches(tr, p, pqz_2, pz1_2, pz2_2, qz1_2, qz2_2); if(lh[2] > lh[0] && lh[2] > lh[1]) whichNNI = 2; /*******************************************/ if(shSupport) whichNNI = 0; switch(whichNNI) { case 0: hookup(p, q, pqz_0, tr->numBranches); hookup(p->next, pb1, pz1_0, tr->numBranches); hookup(p->next->next, pb2, pz2_0, tr->numBranches); hookup(q->next, qb1, qz1_0, tr->numBranches); hookup(q->next->next, qb2, qz2_0, tr->numBranches); break; case 1: hookup(p, q, pqz_1, tr->numBranches); hookup(p->next, qb1, pz1_1, tr->numBranches); hookup(p->next->next, pb2, pz2_1, tr->numBranches); hookup(q->next, pb1, qz1_1, tr->numBranches); hookup(q->next->next, qb2, qz2_1, tr->numBranches); break; case 2: hookup(p, q, pqz_2, tr->numBranches); hookup(p->next, qb1, pz1_2, tr->numBranches); hookup(p->next->next, pb1, pz2_2, tr->numBranches); hookup(q->next, pb2, qz1_2, tr->numBranches); hookup(q->next->next, qb2, qz2_2, tr->numBranches); break; default: assert(0); } newviewGeneric(tr, p); newviewGeneric(tr, q); if(whichNNI > 0) *interchanges = *interchanges + 1; if(shSupport) p->bInf->support = SHSupport(tr->cdta->endsite, 1000, tr->resample, lh, lhVectors); } if(!isTip(pb1->number, tr->mxtips)) doNNIs(tr, pb1, lhVectors, shSupport, interchanges, innerBranches, pqz_0, pz1_0, pz2_0, qz1_0, qz2_0, pqz_1, pz1_1, pz2_1, qz1_1, qz2_1, pqz_2, pz1_2, pz2_2, qz1_2, qz2_2); if(!isTip(pb2->number, tr->mxtips)) doNNIs(tr, pb2, lhVectors, shSupport, interchanges, innerBranches, pqz_0, pz1_0, pz2_0, qz1_0, qz2_0, pqz_1, pz1_1, pz2_1, qz1_1, qz2_1, pqz_2, pz1_2, pz2_2, qz1_2, qz2_2); return; }
static boolean stNNIproposal(state *s) { //s->newprior = 1; s->bl_prior = 0; int attempts = 0; do{ s->p = selectRandomInnerSubtree(s->tr); /* TODOFER do this ad hoc for NNI requirements*/ if (++attempts > 500) return FALSE; }while(isTip(s->p->number, s->tr->mxtips) || isTip(s->p->back->number, s->tr->mxtips)); assert(!isTip(s->p->number, s->tr->mxtips)); nodeptr p = s->p, q = s->p->back, pb1 = s->p->next->back, pb2 = s->p->next->next->back; assert(!isTip(q->number, s->tr->mxtips)); nodeptr qb1 = q->next->back, qb2 = q->next->next->back; recordNNIBranchInfo(p, s->tr->numBranches); /* do only one type of NNI, nni1 */ double randprop = (double)rand()/(double)RAND_MAX; boolean changeBL = TRUE; if (randprop < 1.0 / 3.0) { s->whichNNI = 1; if(!changeBL) { hookup(p, q, p->z, s->tr->numBranches); hookup(p->next, qb1, q->next->z, s->tr->numBranches); hookup(p->next->next, pb2, p->next->next->z, s->tr->numBranches); hookup(q->next, pb1, p->next->z, s->tr->numBranches); hookup(q->next->next, qb2, q->next->next->z, s->tr->numBranches); } else { hookupBL(p, q, p, s); hookupBL(p->next, qb1, q->next, s); hookupBL(p->next->next, pb2, p->next->next, s); hookupBL(q->next, pb1, p->next, s); hookupBL(q->next->next, qb2, q->next->next, s); } } else if (randprop < 2.0 / 3.0) { s->whichNNI = 2; if(!changeBL) { hookup(p, q, p->z, s->tr->numBranches); hookup(p->next, pb1, p->next->z, s->tr->numBranches); hookup(p->next->next, qb1, q->next->z, s->tr->numBranches); hookup(q->next, pb2, p->next->next->z, s->tr->numBranches); hookup(q->next->next, qb2, q->next->next->z, s->tr->numBranches); } else { hookupBL(p, q, p, s); hookupBL(p->next, pb1, p->next, s); hookupBL(p->next->next, qb1, q->next, s); hookupBL(q->next, pb2, p->next->next, s); hookupBL(q->next->next, qb2, q->next->next, s); } } else { /* change only the branch lengths */ s->whichNNI = 0; if(changeBL) { /* do it like this for symmetry */ hookupBL(p, q, p, s); hookupBL(p->next, pb1, p->next, s); hookupBL(p->next->next, pb2, p->next->next, s); hookupBL(q->next, qb1, q->next, s); hookupBL(q->next->next, qb2, q->next->next, s); } } newviewGeneric(s->tr, p, FALSE); newviewGeneric(s->tr, p->back, FALSE); evaluateGeneric(s->tr, p, FALSE); return TRUE; }
void computePlacementBias(tree *tr, analdef *adef) { int windowSize = adef->slidingWindowSize, k, i, tips, numTraversalBranches = (2 * (tr->mxtips - 1)) - 3; /* compute number of branches into which we need to insert once we have removed a taxon */ char fileName[1024]; FILE *outFile; /* data for each sliding window starting position */ positionData *pd = (positionData *)malloc(sizeof(positionData) * (tr->cdta->endsite - windowSize)); double *nodeDistances = (double*)calloc(tr->cdta->endsite, sizeof(double)), /* array to store node distnces ND for every sliding window position */ *distances = (double*)calloc(tr->cdta->endsite, sizeof(double)); /* array to store avg distances for every site */ strcpy(fileName, workdir); strcat(fileName, "RAxML_SiteSpecificPlacementBias."); strcat(fileName, run_id); outFile = myfopen(fileName, "w"); printBothOpen("Likelihood of comprehensive tree %f\n\n", tr->likelihood); if(windowSize > tr->cdta->endsite) { printBothOpen("The size of your sliding window is %d while the number of sites in the alignment is %d\n\n", windowSize, tr->cdta->endsite); exit(-1); } if(windowSize >= (int)(0.9 * tr->cdta->endsite)) printBothOpen("WARNING: your sliding window of size %d is only slightly smaller than you alignment that has %d sites\n\n", windowSize, tr->cdta->endsite); printBothOpen("Sliding window size: %d\n\n", windowSize); /* prune and re-insert on tip at a time into all branches of the remaining tree */ for(tips = 1; tips <= tr->mxtips; tips++) { nodeptr myStart, p = tr->nodep[tips]->back, /* this is the node at which we are prunung */ p1 = p->next->back, p2 = p->next->next->back; double pz[NUM_BRANCHES], p1z[NUM_BRANCHES], p2z[NUM_BRANCHES]; int branchCounter = 0; /* reset array values for this tip */ for(i = 0; i < tr->cdta->endsite; i++) { pd[i].lh = unlikely; pd[i].p = (nodeptr)NULL; } /* store the three branch lengths adjacent to the position at which we prune */ for(i = 0; i < tr->numBranches; i++) { p1z[i] = p1->z[i]; p2z[i] = p2->z[i]; pz[i] = p->z[i]; } /* prune the taxon, optimizing the branch between p1 and p2 */ removeNodeBIG(tr, p, tr->numBranches); printBothOpen("Pruning taxon Number %d [%s]\n", tips, tr->nameList[tips]); /* find any tip to start traversing the tree */ myStart = findAnyTip(p1, tr->mxtips); /* insert taxon, compute likelihood and remove taxon again from all branches */ traverseBias(p, myStart->back, tr, &branchCounter, pd, windowSize); assert(branchCounter == numTraversalBranches); /* for every sliding window position calc ND to the true/correct position at p */ for(i = 0; i < tr->cdta->endsite - windowSize; i++) nodeDistances[i] = getNodeDistance(p1, pd[i].p, tr->mxtips); /* now analyze */ for(i = 0; i < tr->cdta->endsite; i++) { double d = 0.0; int s = 0; /* check site position, i.e., doe we have windowSize data points available or fewer because we are at the start or the end of the alignment */ /* for each site just accumulate the node distances we have for all sliding windows that passed over this site */ if(i < windowSize) { for(k = 0; k < i + 1; k++, s++) d += nodeDistances[k]; } else { if(i < tr->cdta->endsite - windowSize) { for(k = i - windowSize + 1; k <= i; k++, s++) d += nodeDistances[k]; } else { for(k = i - windowSize; k < (tr->cdta->endsite - windowSize); k++, s++) d += nodeDistances[k + 1]; } } /* now just divide the accumultaed ND distance by the number of distances we have for this position and then add it to the acc distances over all taxa. I just realized that the version on which I did the tests I sent to Simon I used distances[i] = d / ((double)s); instead of distances[i] += d / ((double)s); gamo tin poutana mou */ distances[i] += (d / ((double)s)); } /* re-connect taxon to its original position */ hookup(p->next, p1, p1z, tr->numBranches); hookup(p->next->next, p2, p2z, tr->numBranches); hookup(p, p->back, pz, tr->numBranches); /* fix likelihood vectors */ newviewGeneric(tr, p); } /* now just compute the average ND over all taxa */ for(i = 0; i < tr->cdta->endsite; i++) { double avg = distances[i] / ((double)tr->mxtips); fprintf(outFile, "%d %f\n", i, avg); } printBothOpen("\nTime for EPA-based site-specific placement bias calculation: %f\n", gettime() - masterTime); printBothOpen("Site-specific placement bias statistics written to file %s\n", fileName); fclose(outFile); exit(0); }
int rearrangeBIG(tree *tr, nodeptr p, int mintrav, int maxtrav) { double p1z[NUM_BRANCHES], p2z[NUM_BRANCHES], q1z[NUM_BRANCHES], q2z[NUM_BRANCHES]; nodeptr p1, p2, q, q1, q2; int mintrav2, i; boolean doP = TRUE, doQ = TRUE; if (maxtrav < 1 || mintrav > maxtrav) return 0; q = p->back; if(tr->constrained) { if(! tipHomogeneityChecker(tr, p->back, 0)) doP = FALSE; if(! tipHomogeneityChecker(tr, q->back, 0)) doQ = FALSE; if(doQ == FALSE && doP == FALSE) return 0; } if (!isTip(p->number, tr->rdta->numsp) && doP) { p1 = p->next->back; p2 = p->next->next->back; if(!isTip(p1->number, tr->rdta->numsp) || !isTip(p2->number, tr->rdta->numsp)) { for(i = 0; i < tr->numBranches; i++) { p1z[i] = p1->z[i]; p2z[i] = p2->z[i]; } if (! removeNodeBIG(tr, p, tr->numBranches)) return badRear; if (!isTip(p1->number, tr->rdta->numsp)) { addTraverseBIG(tr, p, p1->next->back, mintrav, maxtrav); addTraverseBIG(tr, p, p1->next->next->back, mintrav, maxtrav); } if (!isTip(p2->number, tr->rdta->numsp)) { addTraverseBIG(tr, p, p2->next->back, mintrav, maxtrav); addTraverseBIG(tr, p, p2->next->next->back, mintrav, maxtrav); } hookup(p->next, p1, p1z, tr->numBranches); hookup(p->next->next, p2, p2z, tr->numBranches); newviewGeneric(tr, p); } } if (!isTip(q->number, tr->rdta->numsp) && maxtrav > 0 && doQ) { q1 = q->next->back; q2 = q->next->next->back; /*if (((!q1->tip) && (!q1->next->back->tip || !q1->next->next->back->tip)) || ((!q2->tip) && (!q2->next->back->tip || !q2->next->next->back->tip))) */ if ( ( ! isTip(q1->number, tr->rdta->numsp) && (! isTip(q1->next->back->number, tr->rdta->numsp) || ! isTip(q1->next->next->back->number, tr->rdta->numsp)) ) || ( ! isTip(q2->number, tr->rdta->numsp) && (! isTip(q2->next->back->number, tr->rdta->numsp) || ! isTip(q2->next->next->back->number, tr->rdta->numsp)) ) ) { for(i = 0; i < tr->numBranches; i++) { q1z[i] = q1->z[i]; q2z[i] = q2->z[i]; } if (! removeNodeBIG(tr, q, tr->numBranches)) return badRear; mintrav2 = mintrav > 2 ? mintrav : 2; if (/*! q1->tip*/ !isTip(q1->number, tr->rdta->numsp)) { addTraverseBIG(tr, q, q1->next->back, mintrav2 , maxtrav); addTraverseBIG(tr, q, q1->next->next->back, mintrav2 , maxtrav); } if (/*! q2->tip*/ ! isTip(q2->number, tr->rdta->numsp)) { addTraverseBIG(tr, q, q2->next->back, mintrav2 , maxtrav); addTraverseBIG(tr, q, q2->next->next->back, mintrav2 , maxtrav); } hookup(q->next, q1, q1z, tr->numBranches); hookup(q->next->next, q2, q2z, tr->numBranches); newviewGeneric(tr, q); } } return 1; }
static void smoothTreeRandom (tree *tr, int maxtimes) { int i, k, *perm; tr->branchCounter = 0; tr->numberOfBranches = 2 * tr->mxtips - 3; tr->bInf = (branchInfo*)rax_malloc(tr->numberOfBranches * sizeof(branchInfo)); perm = (int*)rax_malloc(sizeof(int) * tr->numberOfBranches); setupBranches(tr, tr->start->back, tr->bInf); for(i = 0; i < tr->numBranches; i++) tr->partitionConverged[i] = FALSE; /*printf("%d \n", maxtimes);*/ while (--maxtimes >= 0) { for(i = 0; i < tr->numBranches; i++) tr->partitionSmoothed[i] = TRUE; /*printf("%d %d\n", maxtimes, tr->numberOfBranches);*/ for(k = 0; k < tr->numberOfBranches; k++) perm[k] = k; makePerm(perm, tr->numberOfBranches); for(k = 0; k < tr->numberOfBranches; k++) { /*printf("%d Node %d\n", k, tr->bInf[k].oP->number);*/ update(tr, tr->bInf[perm[k]].oP); newviewGeneric(tr, tr->bInf[perm[k]].oP); } /*if (! smooth(tr, p->back)) return FALSE; if (!isTip(p->number, tr->rdta->numsp)) { q = p->next; while (q != p) { if (! smooth(tr, q->back)) return FALSE; q = q->next; } } */ if (allSmoothed(tr)) break; } for(i = 0; i < tr->numBranches; i++) tr->partitionConverged[i] = FALSE; rax_free(tr->bInf); rax_free(perm); }
static void computeAncestralRec(tree *tr, nodeptr p, int *counter, FILE *probsFile, FILE *statesFile, boolean atRoot) { #ifdef _USE_PTHREADS size_t accumulatedOffset = 0; #endif int model, globalIndex = 0; ancestralState *a = (ancestralState *)malloc(sizeof(ancestralState) * tr->cdta->endsite), *unsortedA = (ancestralState *)malloc(sizeof(ancestralState) * tr->rdta->sites); if(!atRoot) { if(isTip(p->number, tr->mxtips)) return; computeAncestralRec(tr, p->next->back, counter, probsFile, statesFile, atRoot); computeAncestralRec(tr, p->next->next->back, counter, probsFile, statesFile, atRoot); newviewGeneric(tr, p); } newviewGenericAncestral(tr, p, atRoot); #ifdef _USE_PTHREADS masterBarrier(THREAD_GATHER_ANCESTRAL, tr); #endif if(atRoot) { fprintf(probsFile, "ROOT\n"); fprintf(statesFile, "ROOT "); } else { fprintf(probsFile, "%d\n", p->number); fprintf(statesFile, "%d ", p->number); } for(model = 0; model < tr->NumberOfModels; model++) { int offset, i, width = tr->partitionData[model].upper - tr->partitionData[model].lower, states = tr->partitionData[model].states; #ifdef _USE_PTHREADS double *ancestral = tr->ancestralStates; #else double *ancestral = tr->partitionData[model].sumBuffer; #endif if(tr->rateHetModel == CAT) offset = 1; else offset = 4; for(i = 0; i < width; i++, globalIndex++) { double equal = 1.0 / (double)states, max = -1.0; boolean approximatelyEqual = TRUE; int max_l = -1, l; char c; a[globalIndex].states = states; a[globalIndex].probs = (double *)malloc(sizeof(double) * states); for(l = 0; l < states; l++) { double value = ancestral[offset * states * i + l]; if(value > max) { max = value; max_l = l; } approximatelyEqual = approximatelyEqual && (ABS(equal - value) < 0.000001); a[globalIndex].probs[l] = value; } if(approximatelyEqual) c = '?'; else c = getStateCharacter(tr->partitionData[model].dataType, max_l); a[globalIndex].c = c; } #ifdef _USE_PTHREADS accumulatedOffset += width * offset * states; #endif } { int j, k; for(j = 0; j < tr->cdta->endsite; j++) { for(k = 0; k < tr->rdta->sites; k++) if(j == tr->patternPosition[k]) { int sorted = j, unsorted = tr->columnPosition[k] - 1; unsortedA[unsorted].states = a[sorted].states; unsortedA[unsorted].c = a[sorted].c; unsortedA[unsorted].probs = (double*)malloc(sizeof(double) * unsortedA[unsorted].states); memcpy(unsortedA[unsorted].probs, a[sorted].probs, sizeof(double) * a[sorted].states); } } for(k = 0; k < tr->rdta->sites; k++) { for(j = 0; j < unsortedA[k].states; j++) fprintf(probsFile, "%f ", unsortedA[k].probs[j]); fprintf(probsFile, "\n"); fprintf(statesFile, "%c", unsortedA[k].c); } fprintf(probsFile, "\n"); fprintf(statesFile, "\n"); } *counter = *counter + 1; { int j; for(j = 0; j < tr->rdta->sites; j++) free(unsortedA[j].probs); for(j = 0; j < tr->cdta->endsite; j++) free(a[j].probs); } free(a); free(unsortedA); }