/* This program computes the posterior probability of */ /* trees in the posterior distribution obtained from */ /* MrBayes. The input file is the *.out.t file from */ /* MrBayes, with the top lines removed and a sinlge */ /* line displaying the number of taxa and the number of*/ /* trees in the *.out.t file. */ /* Written by L. Salter, July 3, 2003 */ #include #include #include #include #include #include #include #include "post_prob.h" int *ppTwoRow[2], **ppMatrix, *filled_ind, *parents, nseq; char *aa; double *priorprobs; int ***pppTR, **rootlist, *rootlist_curr; char **matnumlist; void write_tree_root(int node, int previous_node, FILE *fp, int treenum) { if (pppTR[treenum][0][node-(nseq+1)]<=nseq && pppTR[treenum][1][node-(nseq+1)]<=nseq) { fprintf(fp,"(%d",pppTR[treenum][0][node-(nseq+1)]); fprintf(fp,",%d",pppTR[treenum][1][node-(nseq+1)]); fprintf(fp,")"); } if (pppTR[treenum][0][node-(nseq+1)]<=nseq && pppTR[treenum][1][node-(nseq+1)]>nseq) { fprintf(fp,"(%d",pppTR[treenum][0][node-(nseq+1)]); fprintf(fp,","); write_tree_root(pppTR[treenum][1][node-(nseq+1)],node,fp,treenum); fprintf(fp,")"); } if (pppTR[treenum][0][node-(nseq+1)]>nseq && pppTR[treenum][1][node-(nseq+1)]<=nseq) { fprintf(fp,"(%d",pppTR[treenum][1][node-(nseq+1)]); fprintf(fp,","); write_tree_root(pppTR[treenum][0][node-(nseq+1)],node,fp,treenum); fprintf(fp,")"); } if (pppTR[treenum][0][node-(nseq+1)]>nseq && pppTR[treenum][1][node-(nseq+1)]>nseq) { fprintf(fp,"("); write_tree_root(pppTR[treenum][0][node-(nseq+1)],node,fp,treenum); fprintf(fp,","); write_tree_root(pppTR[treenum][1][node-(nseq+1)],node,fp,treenum); fprintf(fp,")"); } } void PrintTrees(int tot_trees) { FILE *fp; int k; int i, j; fp = fopen("treefile","w"); for (k=0; k=nseq+1 && stop==0) { if (filled_ind[node-(nseq+1)]==1) node = node-1; else stop=1; } return node; } int CloseBack(int open, int current, int last_char, int last_tipp, FILE* fp, double read_length) { int sss; int i, j; sss=getc(fp); if (sss!=';') { if (ppTwoRow[0][open-(nseq+1)] == 0) j=0; else { j=1; filled_ind[open-(nseq+1)] = 1; } if (sss == ':') { read_length = ReadLength(fp); sss = fgetc(fp); } ppTwoRow[j][open-(nseq+1)] = current; /*ppLengthMat[open][current] = ppLengthMat[current][open] = read_length;*/ } return sss; } void ReadTreePrior(FILE* fp) { int ss, ss1, ss2, ss3, last_ss, lastlast_ss, last_tip; int next_avail = nseq+1, last_open=nseq+1, curr_node=nseq+1; int parent; double read_length; ss = fgetc(fp); last_ss = ss; ss = fgetc(fp); while (!feof(fp) && ss!=';') { if (ss == '(') { next_avail = NextNode(next_avail); curr_node = next_avail; last_ss = ss; ss = fgetc(fp); } else { if (ss == ',') { last_ss = ss; ss = AddToNode(curr_node,next_avail,last_tip,fp,read_length); } else { if (ss == ')') { last_open = FindLastOpen(curr_node); lastlast_ss = last_ss; last_ss = ss; ss = CloseBack(last_open,curr_node,lastlast_ss,last_tip,fp,read_length); if (ss!=';' && filled_ind[last_open-(nseq+1)]==1) curr_node = FindParentI(curr_node); else curr_node = last_open; } else { ss2 = fgetc(fp); if (ss2 == ':') { ungetc(ss2,fp); last_tip = ss; } else { ss3 = fgetc(fp); if (ss3 == ':') { ungetc(ss3,fp); last_tip = 10*(ss-48) + ss2; } else last_tip = 100*(ss-48) + 10*(ss2-48) + ss3; } last_ss = last_tip; ss = fgetc(fp); if (ss == ':') read_length = ReadLength(fp); parent = FindParentI(last_tip-48); /*if (parent != 0) ppLengthMat[parent][last_tip-48] = ppLengthMat[last_tip-48][parent] = read_length; */ ss = fgetc(fp); } } } } } /*** Function to search through ppTwoRow to find parent ***/ /*** of a specified node. ***/ int find_parent(int target) { int i, j, parent=0; for (j=0; j<2; j++) { for (i=0; inseq+1) ppTwoRow[i][j] = ppTwoRow[i][j]-1; /*printf("%d ",ppTwoRow[i][j]);*/ } /*printf("%d\n",ppTwoRow[i][j]); fflush(0);*/ } for (ii=0;ii0) l=0; else l=1; while (check!=48 && check!=0 && l