char *printree(NODE *n) { if (n != NULL) { printree(n->ln); //printf("%s\n", n->idx->k); return n->idx->k; printree(n->rn); } else return NULL; }
//--------------|--------------------------------------------- void acism_dump(ACISM const* psp, PS_DUMP_TYPE pdt, FILE *out, MEMREF const*pattv) { int i, empty; char charv[256]; int symdist[257] = {}; for (i = 256; --i >=0;) charv[psp->symv[i]] = i; if (pdt & PS_STATS) { for (i = psp->tran_size, empty = 0; --i >= 0;) { if (psp->tranv[i]) { ++symdist[t_sym(psp, psp->tranv[i])]; } else ++empty; } fprintf(out, "strs:%zu syms:%zu chars:%zu " "trans:%d empty:%d mod:%d hash:%d size:%lu\n", psp->nstrs, psp->nsyms, psp->nchars, psp->tran_size, empty, psp->hash_mod, psp->hash_size, (long)sizeof(ACISM) + p_size(psp)); } if (pdt & PS_TRAN) { fprintf(out, "==== TRAN:\n%8s %8s Ch MS %8s\n", "Cell", "State", "Next"); for (i = 1; i < (int)psp->tran_size; ++i) { fprintf(out, "%8d %8d ", i, i - t_sym(psp, psp->tranv[i])); printrans(psp, i, charv, out, pattv); } } if (pdt & PS_HASH) { fprintf(out, "==== HASH:\n.....: state strno\n"); for (i = 0; i < (int)psp->hash_size; ++i) { STATE state = psp->hashv[i].state; if (state) fprintf(out, "%5d: %7"FX"u %3d %8"FX"u %.*s\n", i, state, i - p_hash(psp, state), PSTR(psp, psp->hashv[i].strno, pattv)); else fprintf(out, "%5d: %7"FX"d --- %8"FX"d\n", i, state, psp->hashv[i].strno); } } if (pdt & PS_TREE) { fprintf(out, "==== TREE:\n"); char str[psp->maxlen + 1]; printree(psp, 0, 0, str, charv, out, pattv); } //TODO: calculate stats: backref chain lengths ... }
void maketree() { /* construct the tree */ long i ; inputdata(replicates, printdata, lower, upper, x, reps); /* if (njoin && (spp < 3)) { mexPrintf("\nERROR: Neighbor-Joining runs must have at least 3 species\n\n"); exxit(-1); } */ if (progress) putchar('\n'); if (ith == 1) setuptree(&curtree, nonodes2 + 1); for (i = 1; i <= spp; i++) enterorder[i - 1] = i; if (jumble) randumize(seed, enterorder); for (i = 0; i < spp; i++) cluster[i] = curtree.nodep[i]; jointree(); if (njoin) curtree.start = curtree.nodep[outgrno - 1]->back; printree(curtree.start, treeprint, njoin, (boolean)(!njoin)); if (treeprint) summarize(); if (trout) { col = 0; if (njoin) treeout(curtree.start, &col, 0.43429448222, njoin, curtree.start); else curtree.root = curtree.start, treeoutr(curtree.start,&col,&curtree); } if (progress) { mexPrintf("\nOutput written on file \"%s\"\n\n", outfilename); if (trout) mexPrintf("Tree written on file \"%s\"\n\n", outtreename); } } /* maketree */
void maketree() { /* construct the tree */ long i ; inputdata(replicates, printdata, lower, upper, x, reps); if (progress) putchar('\n'); if (ith == 1) setuptree(&curtree, nonodes2 + 1); for (i = 1; i <= spp; i++) enterorder[i - 1] = i; if (jumble) randumize(seed, enterorder); for (i = 0; i < spp; i++) cluster[i] = curtree.nodep[i]; jointree(); if (outgropt) curtree.start = curtree.nodep[outgrno - 1]->back; printree(&curtree, curtree.start->next, treeprint, njoin, !njoin); if (treeprint) summarize(); if (trout) { col = 0; if (njoin) treeout(curtree.start, &col, 0.43429448222, njoin, curtree.start); else curtree.root = curtree.start, treeoutr(curtree.start,&col,&curtree); } if (progress) { printf("\nOutput written on output file\n\n"); if (trout) printf("Tree written on tree file\n\n"); } } /* maketree */
static void printree(ACISM const*psp, int state, int depth, char *str, char const *charv, FILE*out, MEMREF const*pattv) { SYMBOL sym; TRAN x; if (depth > (int)psp->maxlen) { fputs("oops\n", out); return; } x = psp->tranv[state]; fprintf(out, "%5d:%.*s", state, depth, str); if (t_valid(psp,x) && t_next(psp,x)) fprintf(out, " b=%"FX"d%s", t_next(psp,x), x & T_FLAGS ? " BAD" : ""); fprintf(out, "\n"); for (sym = 1; sym < psp->nsyms; ++sym) { x = p_tran(psp, state, sym); if (t_valid(psp, x)) { str[depth] = charv[sym]; fprintf(out, "%*s%c %c%c", depth+6, "", charv[sym], x & IS_MATCH ? 'M' : '-', x & IS_SUFFIX ? 'S' : '-'); if (x & IS_MATCH && pattv && t_isleaf(psp, x)) fprintf(out, " %.0d -> %.*s", PSTR(psp, t_strno(psp,x), pattv)); if (x & IS_SUFFIX) fprintf(out, " ->S %"FX"d", t_next(psp, psp->tranv[state])); fprintf(out, "\n"); if (!t_isleaf(psp, x)) printree(psp, t_next(psp, x), depth+1, str, charv, out, pattv); } } }
void maketree() { /* construct the tree */ long i; if (usertree) { /* Open in binary: ftell() is broken for UNIX line-endings under WIN32 */ openfile(&intree,INTREE,"input tree file", "rb",progname,intreename); numtrees = countsemic(&intree); if(numtrees > MAXSHIMOTREES) shimotrees = MAXSHIMOTREES; else shimotrees = numtrees; if (numtrees > 2) initseed(&inseed, &inseed0, seed); if (treeprint) { fprintf(outfile, "User-defined tree"); if (numtrees > 1) putc('s', outfile); putc('\n', outfile); } setuptree(&curtree, nonodes2); for (which = 1; which <= spp; which++) inittip(which, &curtree); which = 1; while (which <= numtrees) { for (i = 0 ; i < nonodes2 ; i++) { if ( i > spp) { /* must do this since not all nodes may be used if an unrooted tree is read in after a rooted one */ curtree.nodep[i]->back = NULL; curtree.nodep[i]->next->back = NULL; curtree.nodep[i]->next->next->back = NULL; } else curtree.nodep[i]->back = NULL; } treeread2 (intree, &curtree.start, curtree.nodep, lengths, &trweight, &goteof, &haslengths, &spp,false,nonodes2); curtree.start = curtree.nodep[outgrno - 1]->back; treevaluate(); printree(); summarize(); which++; } FClose(intree); if (numtrees > 1 && loci > 1 ) { weight = (long *)Malloc(loci*sizeof(long)); for (i = 0; i < loci; i++) weight[i] = 1; standev2(numtrees, maxwhich, 0, loci-1, maxlogl, l0gl, l0gf, seed); free(weight); fprintf(outfile, "\n\n"); } } else { /* if ( !usertree ) */ if (jumb == 1) { setuptree(&curtree, nonodes2); setuptree(&priortree, nonodes2); setuptree(&bestree, nonodes2); if (njumble > 1) setuptree(&bestree2, nonodes2); } for (i = 1; i <= spp; i++) enterorder[i - 1] = i; if (jumble) randumize(seed, enterorder); nextsp = 3; buildsimpletree(&curtree); curtree.start = curtree.nodep[enterorder[0] - 1]->back; if (jumb == 1) numtrees = 1; nextsp = 4; if (progress) { printf("Adding species:\n"); writename(0, 3, enterorder); #ifdef WIN32 phyFillScreenColor(); #endif } while (nextsp <= spp) { buildnewtip(enterorder[nextsp - 1], &curtree, nextsp); copy_(&curtree, &priortree); bestree.likelihood = -DBL_MAX; addtraverse(curtree.nodep[enterorder[nextsp - 1] - 1]->back, curtree.start, true ); copy_(&bestree, &curtree); if (progress) { writename(nextsp - 1, 1, enterorder); #ifdef WIN32 phyFillScreenColor(); #endif } if (global && nextsp == spp) { if (progress) { printf("\nDoing global rearrangements\n"); printf(" !"); for (i = 1; i <= spp - 2; i++) if ( (i - spp) % (( nonodes2 / 72 ) + 1 ) == 0 ) putchar('-'); printf("!\n"); printf(" "); } } succeeded = true; while (succeeded) { succeeded = false; if ( global && nextsp == spp ) globrearrange(); else rearrange(curtree.start); if (global && nextsp == spp) putc('\n', outfile); } if (global && nextsp == spp && progress) putchar('\n'); if (njumble > 1) { if (jumb == 1 && nextsp == spp) copy_(&bestree, &bestree2); else if (nextsp == spp) { if (bestree2.likelihood < bestree.likelihood) copy_(&bestree, &bestree2); } } if (nextsp == spp && jumb == njumble) { if (njumble > 1) copy_(&bestree2, &curtree); curtree.start = curtree.nodep[outgrno - 1]->back; printree(); summarize(); } nextsp++; } } if ( jumb < njumble) return; if (progress) { printf("\nOutput written to file \"%s\"\n", outfilename); if (trout) printf("\nTree also written onto file \"%s\"\n", outtreename); } freeview(&curtree, nonodes2); if (!usertree) { freeview(&bestree, nonodes2); freeview(&priortree, nonodes2); } for (i = 0; i < spp; i++) free(x[i]); if (!contchars) { free(locus); free(pbar); } } /* maketree */
void maketree() { /* constructs a binary tree from the pointers in treenode. adds each node at location which yields highest "likelihood" then rearranges the tree for greatest "likelihood" */ long i, j, nextnode; double gotlike; node *item, *nufork, *dummy, *p; char *treestr; fullset = (1L << (bits + 1)) - (1L << 1); if (!usertree) { for (i = 1; i <= (spp); i++) enterorder[i - 1] = i; if (jumble) randumize(seed, enterorder); root = treenode[enterorder[0] - 1]; add(treenode[enterorder[0] - 1], treenode[enterorder[1] - 1], treenode[spp], &root, treenode); if (progress) { printf("Adding species:\n"); writename(0, 2, enterorder); #ifdef WIN32 phyFillScreenColor(); #endif } lastrearr = false; for (i = 3; i <= (spp); i++) { bestyet = -350.0 * spp * chars; item = treenode[enterorder[i - 1] - 1]; nufork = treenode[spp + i - 2]; addpreorder(root, item, nufork); add(there, item, nufork, &root, treenode); like = bestyet; rearrange(&root); if (progress) { writename(i - 1, 1, enterorder); #ifdef WIN32 phyFillScreenColor(); #endif } lastrearr = (i == spp); if (lastrearr) { if (progress) { printf("\nDoing global rearrangements\n"); printf(" !"); for (j = 1; j <= (nonodes); j++) if ( j % (( nonodes / 72 ) + 1 ) == 0 ) putchar('-'); printf("!\n"); #ifdef WIN32 phyFillScreenColor(); #endif } bestlike = bestyet; if (jumb == 1) { bstlike2 = bestlike; nextree = 1; } do { if (progress) printf(" "); gotlike = bestlike; for (j = 0; j < (nonodes); j++) { bestyet = - 350.0 * spp * chars; item = treenode[j]; if (item != root) { nufork = treenode[j]->back; re_move(&item, &nufork, &root, treenode); there = root; addpreorder(root, item, nufork); add(there, item, nufork, &root, treenode); } if (progress) { if ( j % (( nonodes / 72 ) + 1 ) == 0 ) putchar('.'); fflush(stdout); } } if (progress) { putchar('\n'); #ifdef WIN32 phyFillScreenColor(); #endif } } while (bestlike > gotlike); } } if (progress) putchar('\n'); for (i = spp - 1; i >= 1; i--) re_move(&treenode[i], &dummy, &root, treenode); if (jumb == njumble) { if (treeprint) { putc('\n', outfile); if (nextree == 2) fprintf(outfile, "One most parsimonious tree found:\n"); else fprintf(outfile, "%6ld trees in all found\n", nextree - 1); } if (nextree > maxtrees + 1) { if (treeprint) fprintf(outfile, "here are the first%4ld of them\n", (long)maxtrees); nextree = maxtrees + 1; } if (treeprint) putc('\n', outfile); for (i = 0; i <= (nextree - 2); i++) { root = treenode[0]; add(treenode[0], treenode[1], treenode[spp], &root, treenode); for (j = 3; j <= spp; j++) { add(treenode[bestrees[i].btree[j - 1] - 1], treenode[j - 1], treenode[spp + j - 2], &root, treenode);} evaluate(root); printree(1.0, treeprint, root); describe(); for (j = 1; j < (spp); j++) re_move(&treenode[j], &dummy, &root, treenode); } } } else { if (numtrees > 2) { emboss_initseed(inseed, &inseed0, seed); printf("\n"); } if (treeprint) { fprintf(outfile, "User-defined tree"); if (numtrees > 1) putc('s', outfile); fprintf(outfile, ":\n"); } names = (boolean *)Malloc(spp*sizeof(boolean)); which = 1; firsttree = true; /**/ nodep = NULL; /**/ nextnode = 0; /**/ haslengths = 0; /**/ phirst = 0; /**/ zeros = (long *)Malloc(chars*sizeof(long)); /**/ for (i = 0; i < chars; i++) /**/ zeros[i] = 0; /**/ while (which <= numtrees) { treestr = ajStrGetuniquePtr(&phylotrees[which-1]->Tree); treeread(&treestr, &root, treenode, &goteof, &firsttree, nodep, &nextnode, &haslengths, &grbg, initdollopnode,false,nonodes); for (i = spp; i < (nonodes); i++) { p = treenode[i]; for (j = 1; j <= 3; j++) { p->stateone = (bitptr)Malloc(words*sizeof(long)); p->statezero = (bitptr)Malloc(words*sizeof(long)); p = p->next; } } /* debug: see comment at initdollopnode() */ if (treeprint) fprintf(outfile, "\n\n"); evaluate(root); printree(1.0, treeprint, root); describe(); which++; } FClose(intree); fprintf(outfile, "\n\n"); if (numtrees > 1 && chars > 1) standev(numtrees, minwhich, minsteps, nsteps, fsteps, seed); free(names); } if (jumb == njumble) { if (progress) { printf("Output written to file \"%s\"\n\n", outfilename); if (trout) printf("Trees also written onto file \"%s\"\n\n", outtreename); } if (ancseq) freegarbage(&garbage); } } /* maketree */
void maketree() { /* contruct the tree */ long nextsp,numtrees; boolean succeeded=false; long i, j, which; if (usertree) { inputdata(replicates, printdata, lower, upper, x, reps); setuptree(&curtree, nonodes2); for (which = 1; which <= spp; which++) setuptipf(which, &curtree); if (eoln(infile)) { fscanf(infile, "%*[^\n]"); getc(infile); } openfile(&intree,INTREE,"input tree file","r",progname,intreename); fscanf(intree, "%ld%*[^\n]", &numtrees); getc(intree); if (numtrees > MAXNUMTREES) { printf("\nERROR: number of input trees is read incorrectly from %s\n", intreename); exxit(-1); } if (treeprint) { fprintf(outfile, "User-defined tree"); if (numtrees > 1) putc('s', outfile); fprintf(outfile, ":\n\n"); } first = true; which = 1; while (which <= numtrees) { treeread2 (intree, &curtree.start, curtree.nodep, lengths, &trweight, &goteof, intreename, "Fitch", &haslengths, &spp); nums = spp; curtree.start = curtree.nodep[outgrno - 1]->back; treevaluate(); printree(&curtree, curtree.start, treeprint, false, false); summarize(numtrees); which++; } FClose(intree); } else { if (jumb == 1) { inputdata(replicates, printdata, lower, upper, x, reps); setuptree(&curtree, nonodes2); setuptree(&priortree, nonodes2); setuptree(&bestree, nonodes2); if (njumble > 1) setuptree(&bestree2, nonodes2); } for (i = 1; i <= spp; i++) enterorder[i - 1] = i; if (jumble) randumize(seed, enterorder); nextsp = 3; buildsimpletree(&curtree, nextsp); curtree.start = curtree.nodep[enterorder[0] - 1]->back; if (jumb == 1) numtrees = 1; nextsp = 4; if (progress) { printf("Adding species:\n"); writename(0, 3, enterorder); #ifdef WIN32 phyFillScreenColor(); #endif } while (nextsp <= spp) { nums = nextsp; buildnewtip(enterorder[nextsp - 1], &curtree, nextsp); copy_(&curtree, &priortree); bestree.likelihood = -99999.0; addtraverse(curtree.nodep[enterorder[nextsp - 1] - 1]->back, curtree.start, true, &numtrees,&succeeded); copy_(&bestree, &curtree); if (progress) { writename(nextsp - 1, 1, enterorder); #ifdef WIN32 phyFillScreenColor(); #endif } if (global && nextsp == spp) { if (progress) { printf("Doing global rearrangements\n"); printf(" !"); for (j = 1; j <= (spp - 2); j++) putchar('-'); printf("!\n"); printf(" "); } } succeeded = true; while (succeeded) { succeeded = false; rearrange(curtree.start, &numtrees,&nextsp,&succeeded); if (global && ((nextsp) == spp) && progress) printf("\n "); } if (global && nextsp == spp) { putc('\n', outfile); if (progress) putchar('\n'); } if (njumble > 1) { if (jumb == 1 && nextsp == spp) copy_(&bestree, &bestree2); else if (nextsp == spp) { if (bestree2.likelihood < bestree.likelihood) copy_(&bestree, &bestree2); } } if (nextsp == spp && jumb == njumble) { if (njumble > 1) copy_(&bestree2, &curtree); curtree.start = curtree.nodep[outgrno - 1]->back; printree(&curtree, curtree.start, treeprint, true, false); summarize(numtrees); } nextsp++; } } if (jumb == njumble && progress) { printf("\nOutput written to output file\n\n"); if (trout) { printf("Tree also written onto file\n"); putchar('\n'); } } } /* maketree */
void maketree() { /* tree construction recursively by branch and bound */ long i, j, k; node2 *dummy; fullset = (1L << (bits + 1)) - (1L << 1); if (progress) { printf("\nHow many\n"); printf("trees looked Approximate\n"); printf("at so far Length of How many percentage\n"); printf("(multiples shortest tree trees this long searched\n"); printf("of %4ld): found so far found so far so far\n", howoften); printf("---------- ------------ ------------ ------------\n"); #ifdef WIN32 phyFillScreenColor(); #endif } done = false; mults = 0; examined = 0; nextree = 1; root = treenode[0]; firsttime = true; for (i = 0; i < (spp); i++) added[i] = false; added[0] = true; order[0] = 1; k = 2; fracdone = 0.0; fracinc = 1.0; bestyet = -1.0; addit(k); if (done) { if (progress) { printf("Search broken off! Not guaranteed to\n"); printf(" have found the most parsimonious trees.\n"); } if (treeprint) { fprintf(outfile, "Search broken off! Not guaranteed to\n"); fprintf(outfile, " have found the most parsimonious\n"); fprintf(outfile, " trees, but here is what we found:\n"); } } if (treeprint) { fprintf(outfile, "\nrequires a total of %18.3f\n\n", bestyet); if (nextree == 2) fprintf(outfile, "One most parsimonious tree found:\n"); else fprintf(outfile, "%5ld trees in all found\n", nextree - 1); } if (nextree > maxtrees + 1) { if (treeprint) fprintf(outfile, "here are the first%4ld of them\n", (long)maxtrees); nextree = maxtrees + 1; } if (treeprint) putc('\n', outfile); for (i = 0; i < (spp); i++) added[i] = true; for (i = 0; i <= (nextree - 2); i++) { for (j = k; j <= (spp); j++) add3(treenode[bestrees[i][j - 1] - 1], treenode[bestorders[i][j - 1] - 1], treenode[spp + j - 2], &root, treenode); if (noroot) reroot(treenode[outgrno - 1]); didreroot = (outgropt && noroot); evaluate(root); printree(treeprint, noroot, didreroot, root); describe(); for (j = k - 1; j < (spp); j++) re_move3(&treenode[bestorders[i][j] - 1], &dummy, &root, treenode); } if (progress) { printf("\nOutput written to file \"%s\"\n\n", outfilename); if (trout) printf("Trees also written onto file \"%s\"\n\n", outtreename); } if (ancseq) freegarbage(&garbage); } /* maketree */