char *Tree2String(char *treestr, tree *tr, nodeptr p, boolean printBranchLengths, boolean printNames, boolean printLikelihood, boolean rellTree, boolean finalPrint, int perGene, boolean branchLabelSupport, boolean printSHSupport) { if(rellTree) assert(!branchLabelSupport && !printSHSupport); if(branchLabelSupport) assert(!rellTree && !printSHSupport); if(printSHSupport) assert(!branchLabelSupport && !rellTree); Tree2StringREC(treestr, tr, p, printBranchLengths, printNames, printLikelihood, rellTree, finalPrint, perGene, branchLabelSupport, printSHSupport); while (*treestr) treestr++; return treestr; }
char *Tree2String(char *treestr, tree *tr, nodeptr p, boolean printBranchLengths, boolean printNames, boolean printLikelihood, boolean rellTree, boolean finalPrint, analdef *adef, int perGene, boolean branchLabelSupport, boolean printSHSupport, boolean printIC, boolean printSHSupports) { if(rellTree) assert(!branchLabelSupport && !printSHSupport); if(branchLabelSupport) assert(!rellTree && !printSHSupport); if(printSHSupport) assert(!branchLabelSupport && !rellTree); if(finalPrint && adef->outgroup) { nodeptr startNode = tr->start; if(tr->numberOfOutgroups > 1) { nodeptr root; nodeptr *subtrees = (nodeptr *)rax_malloc(sizeof(nodeptr) * tr->mxtips); int i, k, count = 0; int *nodeNumbers = (int*)rax_malloc(sizeof(int) * tr->numberOfOutgroups); int *foundVector = (int*)rax_malloc(sizeof(int) * tr->numberOfOutgroups); boolean monophyletic = FALSE; collectSubtrees(tr, subtrees, &count, tr->numberOfOutgroups); /*printf("Found %d subtrees of size %d\n", count, tr->numberOfOutgroups);*/ for(i = 0; (i < count) && (!monophyletic); i++) { int l, sum, nc = 0; for(k = 0; k < tr->numberOfOutgroups; k++) { nodeNumbers[k] = -1; foundVector[k] = 0; } checkOM(subtrees[i], nodeNumbers, &nc, tr); for(l = 0; l < tr->numberOfOutgroups; l++) for(k = 0; k < tr->numberOfOutgroups; k++) { if(nodeNumbers[l] == tr->outgroupNums[k]) foundVector[l] = 1; } sum = 0; for(l = 0; l < tr->numberOfOutgroups; l++) sum += foundVector[l]; if(sum == tr->numberOfOutgroups) { root = subtrees[i]; tr->start = root; /*printf("outgroups are monphyletic!\n");*/ monophyletic = TRUE; } else { if(sum > 0) { /*printf("outgroups are NOT monophyletic!\n");*/ monophyletic = FALSE; } } } if(!monophyletic) { printf("WARNING, outgroups are not monophyletic, using first outgroup \"%s\"\n", tr->nameList[tr->outgroupNums[0]]); printf("from the list to root the tree!\n"); { FILE *infoFile = myfopen(infoFileName, "ab"); fprintf(infoFile, "\nWARNING, outgroups are not monophyletic, using first outgroup \"%s\"\n", tr->nameList[tr->outgroupNums[0]]); fprintf(infoFile, "from the list to root the tree!\n"); fclose(infoFile); } tr->start = tr->nodep[tr->outgroupNums[0]]; rootedTree(treestr, tr, tr->start->back, printBranchLengths, printNames, printLikelihood, rellTree, finalPrint, adef, perGene, branchLabelSupport, printSHSupport); } else { if(isTip(tr->start->number, tr->rdta->numsp)) { printf("Outgroup-Monophyly ERROR; tr->start is a tip \n"); errorExit(-1); } if(isTip(tr->start->back->number, tr->rdta->numsp)) { printf("Outgroup-Monophyly ERROR; tr->start is a tip \n"); errorExit(-1); } rootedTree(treestr, tr, tr->start->back, printBranchLengths, printNames, printLikelihood, rellTree, finalPrint, adef, perGene, branchLabelSupport, printSHSupport); } rax_free(foundVector); rax_free(nodeNumbers); rax_free(subtrees); } else { tr->start = tr->nodep[tr->outgroupNums[0]]; rootedTree(treestr, tr, tr->start->back, printBranchLengths, printNames, printLikelihood, rellTree, finalPrint, adef, perGene, branchLabelSupport, printSHSupports); } tr->start = startNode; } else Tree2StringREC(treestr, tr, p, printBranchLengths, printNames, printLikelihood, rellTree, finalPrint, perGene, branchLabelSupport, printSHSupport, printIC, printSHSupports); while (*treestr) treestr++; return treestr; }
static char *Tree2StringREC(char *treestr, tree *tr, nodeptr p, boolean printBranchLengths, boolean printNames, boolean printLikelihood, boolean rellTree, boolean finalPrint, int perGene, boolean branchLabelSupport, boolean printSHSupport) { char *nameptr; if(isTip(p->number, tr->mxtips)) { if(printNames) { nameptr = tr->nameList[p->number]; sprintf(treestr, "%s", nameptr); } else sprintf(treestr, "%d", p->number); while (*treestr) treestr++; } else { *treestr++ = '('; treestr = Tree2StringREC(treestr, tr, p->next->back, printBranchLengths, printNames, printLikelihood, rellTree, finalPrint, perGene, branchLabelSupport, printSHSupport); *treestr++ = ','; treestr = Tree2StringREC(treestr, tr, p->next->next->back, printBranchLengths, printNames, printLikelihood, rellTree, finalPrint, perGene, branchLabelSupport, printSHSupport); if(p == tr->start->back) { *treestr++ = ','; treestr = Tree2StringREC(treestr, tr, p->back, printBranchLengths, printNames, printLikelihood, rellTree, finalPrint, perGene, branchLabelSupport, printSHSupport); } *treestr++ = ')'; } if(p == tr->start->back) { if(printBranchLengths && !rellTree) sprintf(treestr, ":0.0;\n"); else sprintf(treestr, ";\n"); } else { if(rellTree || branchLabelSupport || printSHSupport) { if(( !isTip(p->number, tr->mxtips)) && ( !isTip(p->back->number, tr->mxtips))) { assert(p->bInf != (branchInfo *)NULL); if(rellTree) sprintf(treestr, "%d:%8.20f", p->bInf->support, p->z[0]); if(branchLabelSupport) sprintf(treestr, ":%8.20f[%d]", p->z[0], p->bInf->support); if(printSHSupport) sprintf(treestr, ":%8.20f[%d]", getBranchLength(tr, perGene, p), p->bInf->support); } else { if(rellTree || branchLabelSupport) sprintf(treestr, ":%8.20f", p->z[0]); if(printSHSupport) sprintf(treestr, ":%8.20f", getBranchLength(tr, perGene, p)); } } else { if(printBranchLengths) sprintf(treestr, ":%8.20f", getBranchLength(tr, perGene, p)); else sprintf(treestr, "%s", "\0"); } } while (*treestr) treestr++; return treestr; }
static char *Tree2StringREC(char *treestr, tree *tr, nodeptr p, boolean printBranchLengths, boolean printNames, boolean printLikelihood, boolean rellTree, boolean finalPrint, int perGene, boolean branchLabelSupport, boolean printSHSupport, boolean printIC, boolean printSHSupports) { char *nameptr; if(isTip(p->number, tr->rdta->numsp)) { if(printNames) { nameptr = tr->nameList[p->number]; sprintf(treestr, "%s", nameptr); } else sprintf(treestr, "%d", p->number); while (*treestr) treestr++; } else { *treestr++ = '('; treestr = Tree2StringREC(treestr, tr, p->next->back, printBranchLengths, printNames, printLikelihood, rellTree, finalPrint, perGene, branchLabelSupport, printSHSupport, printIC, printSHSupports); *treestr++ = ','; treestr = Tree2StringREC(treestr, tr, p->next->next->back, printBranchLengths, printNames, printLikelihood, rellTree, finalPrint, perGene, branchLabelSupport, printSHSupport, printIC, printSHSupports); if(p == tr->start->back) { *treestr++ = ','; treestr = Tree2StringREC(treestr, tr, p->back, printBranchLengths, printNames, printLikelihood, rellTree, finalPrint, perGene, branchLabelSupport, printSHSupport, printIC, printSHSupports); } *treestr++ = ')'; } if(p == tr->start->back) { if(printBranchLengths && !rellTree) sprintf(treestr, ":0.0;\n"); else sprintf(treestr, ";\n"); } else { if(rellTree || branchLabelSupport || printSHSupport || printIC || printSHSupports) { if(( !isTip(p->number, tr->rdta->numsp)) && ( !isTip(p->back->number, tr->rdta->numsp))) { assert(p->bInf != (branchInfo *)NULL); assert(rellTree + branchLabelSupport + printSHSupport + printSHSupports == 1); if(rellTree) { if(printIC) sprintf(treestr, "%1.2f:%8.20f", p->bInf->ic, p->z[0]); else sprintf(treestr, "%d:%8.20f", p->bInf->support, p->z[0]); } if(branchLabelSupport) { if(printIC) sprintf(treestr, ":%8.20f[%1.2f,%1.2f]", p->z[0], p->bInf->ic, p->bInf->icAll); else sprintf(treestr, ":%8.20f[%d]", p->z[0], p->bInf->support); } if(printSHSupport) sprintf(treestr, ":%8.20f[%d]", getBranchLength(tr, perGene, p), p->bInf->support); if(printSHSupports) { int model; sprintf(treestr, ":%8.20f[", getBranchLength(tr, perGene, p)); while(*treestr) treestr++; for(model = 0; model < tr->NumberOfModels - 1; model++) { sprintf(treestr, "%d,", p->bInf->supports[model]); while(*treestr) treestr++; } sprintf(treestr, "%d]", p->bInf->supports[model]); } } else { if(rellTree || branchLabelSupport) sprintf(treestr, ":%8.20f", p->z[0]); if(printSHSupport || printSHSupports) sprintf(treestr, ":%8.20f", getBranchLength(tr, perGene, p)); } } else { if(printBranchLengths) sprintf(treestr, ":%8.20f", getBranchLength(tr, perGene, p)); else sprintf(treestr, "%s", "\0"); } } while (*treestr) treestr++; return treestr; }