📄 best_tree.c
字号:
bt->nkeep = 0; if (bt->ninit <= 0) { if (! (bt->start = setupTopol(numsp, sites))) return 0; bt->ninit = -1; bt->nvalid = 0; bt->numtrees = 0; bt->best = unlikely; bt->improved = FALSE; bt->byScore = (topol **) Malloc((newkeep+1) * sizeof(topol *)); bt->byTopol = (topol **) Malloc((newkeep+1) * sizeof(topol *)); if (! bt->byScore || ! bt->byTopol) { fprintf(stderr, "initBestTree: Malloc failure\n"); return 0; } } else if (ABS(newkeep) > bt->ninit) { if (newkeep < 0) newkeep = -(bt->ninit); else newkeep = bt->ninit; } if (newkeep < 1) { /* Use negative newkeep to clear list */ newkeep = -newkeep; if (newkeep < 1) newkeep = 1; bt->nvalid = 0; bt->best = unlikely; } if (bt->nvalid >= newkeep) { bt->nvalid = newkeep; bt->worst = bt->byScore[newkeep]->likelihood; } else { bt->worst = unlikely; } for (i = bt->ninit + 1; i <= newkeep; i++) { nlogf = (i <= maxlogf) ? sites : 0; if (! (bt->byScore[i] = setupTopol(numsp, nlogf))) break; bt->byTopol[i] = bt->byScore[i]; bt->ninit = i; } return (bt->nkeep = MIN(newkeep, bt->ninit)); } /* initBestTree */int resetBestTree (bestlist *bt) { /* resetBestTree */ bt->best = unlikely; bt->worst = unlikely; bt->nvalid = 0; bt->improved = FALSE; } /* resetBestTree */boolean freeBestTree(bestlist *bt) { /* freeBestTree */ while (bt->ninit >= 0) freeTopol(bt->byScore[(bt->ninit)--]); freeTopol(bt->start); return TRUE; } /* freeBestTree *//* Compare two trees, assuming that each is in standard order. Return * -1 if first preceeds second, 0 if they are identical, or +1 if first * follows second in standard order. Lower number tips preceed higher * number tips. A tip preceeds a corresponding internal node. Internal * nodes are ranked by their lowest number tip. */int cmpSubtopol (connptr p10, connptr p1, connptr p20, connptr p2) { /* cmpSubtopol */ connptr p1d, p2d; int cmp; if (! p1->descend && ! p2->descend) /* Two tips */ return cmpTipVal(p1->valptr, p2->valptr); if (! p1->descend) return -1; /* p1 = tip, p2 = node */ if (! p2->descend) return 1; /* p2 = tip, p1 = node */ p1d = p10 + p1->descend; p2d = p20 + p2->descend; while (1) { /* Two nodes */ if (cmp = cmpSubtopol(p10, p1d, p20, p2d)) return cmp; /* Subtrees */ if (! p1d->sibling && ! p2d->sibling) return 0; /* Lists done */ if (! p1d->sibling) return -1; /* One done, other not */ if (! p2d->sibling) return 1; /* One done, other not */ p1d = p10 + p1d->sibling; /* Neither done */ p2d = p20 + p2d->sibling; } } /* cmpSubtopol */int cmpTopol (void *tpl1, void *tpl2) { /* cmpTopol */ connptr r1, r2; int cmp; r1 = ((topol *) tpl1)->links; r2 = ((topol *) tpl2)->links; cmp = cmpTipVal(tipValPtr(r1->p), tipValPtr(r2->p)); if (cmp) return cmp; return cmpSubtopol(r1, r1, r2, r2); } /* cmpTopol */int cmpTplScore (void *tpl1, void *tpl2) { /* cmpTplScore */ double l1, l2; l1 = ((topol *) tpl1)->likelihood; l2 = ((topol *) tpl2)->likelihood; return (l1 > l2) ? -1 : ((l1 == l2) ? 0 : 1); } /* cmpTplScore *//* Find an item in a sorted list of n items. If the item is in the list, * return its index. If it is not in the list, return the negative of the * position into which it should be inserted. */int findInList (void *item, void *list[], int n, int (* cmpFunc)()) { /* findInList */ int mid, hi, lo, cmp; if (n < 1) return -1; /* No match; first index */ lo = 1; mid = 0; hi = n; while (lo < hi) { mid = (lo + hi) >> 1; cmp = (* cmpFunc)(item, list[mid-1]); if (cmp) { if (cmp < 0) hi = mid; else lo = mid + 1; } else return mid; /* Exact match */ } if (lo != mid) { cmp = (* cmpFunc)(item, list[lo-1]); if (cmp == 0) return lo; } if (cmp > 0) lo++; /* Result of step = 0 test */ return -lo; } /* findInList */int findTreeInList (bestlist *bt, tree *tr) { /* findTreeInList */ topol *tpl; tpl = bt->byScore[0]; saveTree(tr, tpl); return findInList((void *) tpl, (void **) (& (bt->byTopol[1])), bt->nvalid, cmpTopol); } /* findTreeInList */int saveBestTree (bestlist *bt, tree *tr) { /* saveBestTree */ double *tr_log_f, *tpl_log_f; topol *tpl, *reuse; int tplNum, scrNum, reuseScrNum, reuseTplNum, i, oldValid, newValid; tplNum = findTreeInList(bt, tr); tpl = bt->byScore[0]; oldValid = newValid = bt->nvalid; if (tplNum > 0) { /* Topology is in list */ reuse = bt->byTopol[tplNum]; /* Matching topol */ reuseScrNum = reuse->scrNum; reuseTplNum = reuse->tplNum; } /* Good enough to keep? */ else if (tr->likelihood < bt->worst) return 0; else { /* Topology is not in list */ tplNum = -tplNum; /* Add to list (not replace) */ if (newValid < bt->nkeep) bt->nvalid = ++newValid; reuseScrNum = newValid; /* Take worst tree */ reuse = bt->byScore[reuseScrNum]; reuseTplNum = (newValid > oldValid) ? newValid : reuse->tplNum; if (tr->likelihood > bt->start->likelihood) bt->improved = TRUE; } scrNum = findInList((void *) tpl, (void **) (& (bt->byScore[1])), oldValid, cmpTplScore); scrNum = ABS(scrNum); if (scrNum < reuseScrNum) for (i = reuseScrNum; i > scrNum; i--) (bt->byScore[i] = bt->byScore[i-1])->scrNum = i; else if (scrNum > reuseScrNum) { scrNum--; for (i = reuseScrNum; i < scrNum; i++) (bt->byScore[i] = bt->byScore[i+1])->scrNum = i; } if (tplNum < reuseTplNum) for (i = reuseTplNum; i > tplNum; i--) (bt->byTopol[i] = bt->byTopol[i-1])->tplNum = i; else if (tplNum > reuseTplNum) { tplNum--; for (i = reuseTplNum; i < tplNum; i++) (bt->byTopol[i] = bt->byTopol[i+1])->tplNum = i; } if (tpl_log_f = tpl->log_f) { tr_log_f = tr->log_f; i = tpl->log_f_valid = tr->log_f_valid; while (--i >= 0) *tpl_log_f++ = *tr_log_f++; } else { tpl->log_f_valid = 0; } tpl->scrNum = scrNum; tpl->tplNum = tplNum; bt->byTopol[tplNum] = bt->byScore[scrNum] = tpl; bt->byScore[0] = reuse; if (scrNum == 1) bt->best = tr->likelihood; if (newValid == bt->nkeep) bt->worst = bt->byScore[newValid]->likelihood; return scrNum; } /* saveBestTree */int startOpt (bestlist *bt, tree *tr) { /* startOpt */ int scrNum; scrNum = saveBestTree(bt, tr); copyTopol(bt->byScore[scrNum], bt->start); bt->improved = FALSE; return scrNum; } /* startOpt */int setOptLevel (bestlist *bt, int opt_level) { /* setOptLevel */ int tplNum, scrNum; tplNum = findInList((void *) bt->start, (void **) (&(bt->byTopol[1])), bt->nvalid, cmpTopol); if (tplNum > 0) { bt->byTopol[tplNum]->opt_level = opt_level; scrNum = bt->byTopol[tplNum]->scrNum; } else { scrNum = 0; } return scrNum; } /* setOptLevel */int recallBestTree (bestlist *bt, int rank, tree *tr) { /* recallBestTree */ if (rank < 1) rank = 1; if (rank > bt->nvalid) rank = bt->nvalid; if (rank > 0) if (! restoreTree(bt->byScore[rank], tr)) return FALSE; return rank; } /* recallBestTree *//*=======================================================================*//* End of best tree routines *//*=======================================================================*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -