⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dnaml.c

📁 一个神经网络工具箱
💻 C
📖 第 1 页 / 共 5 页
字号:
      sum = 0.0;      for (j = 0; j < rcategs; j++) {        nulike[j] = (1.0 - lambda + lambda * probcat[j]) * like[j];        for (k = 1; k <= rcategs; k++) {          if (k != j + 1)              nulike[j] += lambda * probcat[k - 1] * like[k - 1];        }        if ((ally[i] > 0) && (location[ally[i]-1] > 0))          nulike[j] *= contribution[location[ally[i] - 1] - 1][j];        sum += nulike[j];      }      for (j = 0; j < rcategs; j++) {        nulike[j] /= sum;        marginal[i][j] = nulike[j];      }      memcpy(like, nulike, rcategs * sizeof(double));    }    for (i = 0; i < rcategs; i++)      like[i] = 1.0;    for (i = 0; i < sites; i++) {      sum = 0.0;      for (j = 0; j < rcategs; j++) {        nulike[j] = (1.0 - lambda + lambda * probcat[j]) * like[j];        for (k = 1; k <= rcategs; k++) {          if (k != j + 1)              nulike[j] += lambda * probcat[k - 1] * like[k - 1];        }        marginal[i][j] *= like[j] * probcat[j];        sum += nulike[j];      }      for (j = 0; j < rcategs; j++)        nulike[j] /= sum;      memcpy(like, nulike, rcategs * sizeof(double));      sum = 0.0;      for (j = 0; j < rcategs; j++)        sum += marginal[i][j];      for (j = 0; j < rcategs; j++)        marginal[i][j] /= sum;    }    fprintf(outfile, "Most probable category at each site if > 0.95 probability (\".\" otherwise)\n\n");    for (i = 1; i <= nmlngth + 3; i++)      putc(' ', outfile);    for (i = 0; i < sites; i++) {      sum = 0.0;      for (j = 0; j < rcategs; j++)        if (marginal[i][j] > sum) {          sum = marginal[i][j];          mm = j;        }      if (sum >= 0.95)        fprintf(outfile, "%ld", mm+1);      else        putc('.', outfile);      if ((i+1) % 60 == 0) {        if (i != 0) {          putc('\n', outfile);          for (j = 1; j <= nmlngth + 3; j++)            putc(' ', outfile);        }      }      else if ((i+1) % 10 == 0)        putc(' ', outfile);    }    putc('\n', outfile);    for (i = 0; i < sites; i++)      free(marginal[i]);    free(marginal);  }  putc('\n', outfile);  if (hypstate) {    fprintf(outfile, "Probable sequences at interior nodes:\n\n");    fprintf(outfile, "  node       ");    for (i = 0; (i < 13) && (i < ((sites + (sites-1)/10 - 39) / 2)); i++)      putc(' ', outfile);    fprintf(outfile, "Reconstructed sequence (caps if > 0.95)\n\n");    if (!rctgry || (rcategs == 1))      mx0 = 1;    for (i = 0; i < sites; i += 60) {      k = i + 59;      if (k >= sites)        k = sites - 1;      rectrav(curtree.start, i, k);      rectrav(curtree.start->back, i, k);      putc('\n', outfile);      mx0 = mx1;    }  }}  /* summarize */void dnaml_treeout(node *p){  /* write out file with representation of final tree2 */  /* Only works for bifurcations! */  long i, n, w;  Char c;  double x;  if (p->tip) {    n = 0;    for (i = 1; i <= nmlngth; i++) {      if (nayme[p->index-1][i - 1] != ' ')        n = i;    }    for (i = 0; i < n; i++) {      c = nayme[p->index-1][i];      if (c == ' ')        c = '_';      putc(c, outtree);    }    col += n;  } else {    putc('(', outtree);    col++;    dnaml_treeout(p->next->back);    putc(',', outtree);    col++;    if (col > 45) {      putc('\n', outtree);      col = 0;    }    dnaml_treeout(p->next->next->back);    if (p == curtree.start) {      putc(',', outtree);      col++;      if (col > 45) {        putc('\n', outtree);        col = 0;      }      dnaml_treeout(p->back);    }    putc(')', outtree);    col++;  }  x = p->v * fracchange;  if (x > 0.0)    w = (long)(0.43429448222 * log(x));  else if (x == 0.0)    w = 0;  else    w = (long)(0.43429448222 * log(-x)) + 1;  if (w < 0)    w = 0;  if (p == curtree.start)    fprintf(outtree, ";\n");  else {    fprintf(outtree, ":%*.5f", (int)(w + 7), x);    col += w + 8;  }}  /* dnaml_treeout */void inittravtree(node *p){  /* traverse tree to set initialized and v to initial values */  p->initialized = false;  p->back->initialized = false;  if (!p->tip) {    inittravtree(p->next->back);    inittravtree(p->next->next->back);  }} /* inittravtree */void treevaluate(){  /* evaluate a user tree */  long i;  inittravtree(curtree.start);  polishing = true;  smoothit = true;  for (i = 1; i <= smoothings * 4; i++)    smooth (curtree.start);  dummy = evaluate(curtree.start, true);}  /* treevaluate */void maketree(){  long i, j;  boolean dummy_first, goteof;  pointarray dummy_treenode=NULL;  long nextnode;  node *root, *q, *r;  inittable();  if (usertree) {    openfile(&intree,INTREE,"input tree file", "r",progname,intreename);    inittable_for_usertree (intree);    numtrees = countsemic(&intree);    if (numtrees > 2)      initseed(&inseed, &inseed0, seed);    l0gl = (double *) Malloc(numtrees * sizeof(double));    l0gf = (double **) Malloc(numtrees * sizeof(double *));    for (i=0; i < numtrees; ++i)      l0gf[i] = (double *) Malloc(endsite * sizeof(double));    if (treeprint) {      fprintf(outfile, "User-defined tree");      if (numtrees > 1)        putc('s', outfile);      fprintf(outfile, ":\n\n");    }    which = 1;    /* This taken out of tree read, used to be [spp-1], but referring       to [0] produces output identical to what the pre-modified dnaml       produced. */    while (which <= numtrees) {            /* These initializations required each time through the loop         since multiple trees require re-initialization */      haslengths = true;      nextnode         = 0;      dummy_first      = true;      goteof           = false;      treeread(intree, &root, dummy_treenode, &goteof, &dummy_first,               curtree.nodep, &nextnode,               &haslengths, &grbg, initdnamlnode);      q = root;      r = root;      while (!(q->next == root))        q = q->next;      q->next = root->next;      root = q;      chuck(&grbg, r);      curtree.nodep[spp] = q;      if (goteof && (which <= numtrees)) {        /* if we hit the end of the file prematurely */        printf ("\n");        printf ("ERROR: trees missing at end of file.\n");        printf ("\tExpected number of trees:\t\t%ld\n", numtrees);        printf ("\tNumber of trees actually in file:\t%ld.\n\n", which - 1);        exxit(-1);      }      curtree.start = curtree.nodep[0]->back;            treevaluate();      if (reconsider) {        bestyet = UNDEFINED;        succeeded = true;        while (succeeded) {          succeeded = false;          rearrange(curtree.start, curtree.start->back);        }        treevaluate();      }      dnaml_printree();      summarize();      if (trout) {        col = 0;        dnaml_treeout(curtree.start);      }      which++;    }    FClose(intree);    putc('\n', outfile);    if (!auto_ && numtrees > 1 && weightsum > 1 )      standev2(numtrees, maxwhich, 0, endsite-1, maxlogl,               l0gl, l0gf, aliasweight, seed);  } else {    /* If there's no input user tree, */    for (i = 1; i <= spp; i++)      enterorder[i - 1] = i;    if (jumble)      randumize(seed, enterorder);    if (progress) {      printf("\nAdding species:\n");      writename(0, 3, enterorder);#ifdef WIN32      phyFillScreenColor();#endif    }    nextsp = 3;    polishing = false;    buildsimpletree(&curtree);    curtree.start = curtree.nodep[enterorder[0] - 1]->back;    smoothit = improve;    nextsp = 4;    while (nextsp <= spp) {      buildnewtip(enterorder[nextsp - 1], &curtree);      bestyet = UNDEFINED;      if (smoothit)        dnamlcopy(&curtree, &priortree, nonodes2, rcategs);      addtraverse(curtree.nodep[enterorder[nextsp - 1] - 1]->back,                  curtree.start, true);      if (smoothit)        dnamlcopy(&bestree, &curtree, nonodes2, rcategs);      else {        insert_(curtree.nodep[enterorder[nextsp - 1] - 1]->back, qwhere, true);        smoothit = true;        for (i = 1; i<=smoothings; i++) {          smooth (curtree.start);          smooth (curtree.start->back);        }        smoothit = false;        dnamlcopy(&curtree, &bestree, nonodes2, rcategs);        bestyet = curtree.likelihood;      }      if (progress) {        writename(nextsp - 1, 1, enterorder);#ifdef WIN32        phyFillScreenColor();#endif      }      if (global && nextsp == spp && progress) {        printf("Doing global rearrangements\n");        printf("  !");        for (j = 1; j <= (spp - 3); j++)          putchar('-');        printf("!\n");      }      succeeded = true;      while (succeeded) {        succeeded = false;        if (global && nextsp == spp && progress) {          printf("   ");          fflush(stdout);        }        rearrange(curtree.start, curtree.start->back);        if (global && nextsp == spp && progress)          putchar('\n');      }      for (i = spp; i < nextsp + spp - 2; i++) {        curtree.nodep[i]->initialized = false;        curtree.nodep[i]->next->initialized = false;        curtree.nodep[i]->next->next->initialized = false;      }      if (!smoothit) {        smoothit = true;        for (i = 1; i<=smoothings; i++) {          smooth (curtree.start);          smooth (curtree.start->back);        }        smoothit = false;        dnamlcopy(&curtree, &bestree, nonodes2, rcategs);        bestyet = curtree.likelihood;      }      nextsp++;    }    if (global && progress) {      putchar('\n');      fflush(stdout);    }    if (njumble > 1) {      if (jumb == 1)        dnamlcopy(&bestree, &bestree2, nonodes2, rcategs);      else        if (bestree2.likelihood < bestree.likelihood)          dnamlcopy(&bestree, &bestree2, nonodes2, rcategs);    }    if (jumb == njumble) {      if (njumble > 1)        dnamlcopy(&bestree2, &curtree, nonodes2, rcategs);      curtree.start = curtree.nodep[outgrno - 1]->back;      for (i = 0; i < nonodes2; i++) {        if (i < spp)          curtree.nodep[i]->initialized = false;        else {          curtree.nodep[i]->initialized = false;          curtree.nodep[i]->next->initialized = false;          curtree.nodep[i]->next->next->initialized = false;        }       }      treevaluate();      dnaml_printree();      summarize();      if (trout) {        col = 0;        dnaml_treeout(curtree.start);      }    }  }  if (usertree) {    free(l0gl);    for (i=0; i < numtrees; i++)      free(l0gf[i]);    free(l0gf);  }  for (i = 0; i < rcategs; i++)    for (j = 0; j < categs; j++)      free(tbl[i][j]);  for (i = 0; i < rcategs; i++)    free(tbl[i]);  free(tbl);  if (jumb < njumble)    return;  free(contribution);  free(mp);  for (i=0; i < endsite; i++)     free(term[i]);  free(term);  for (i=0; i < endsite; i++)     free(slopeterm[i]);  free(slopeterm);  for (i=0; i < endsite; i++)     free(curveterm[i]);  free(curveterm);  free_all_x_in_array (nonodes2, curtree.nodep);  if (!usertree || reconsider) {    free_all_x_in_array (nonodes2, bestree.nodep);    free_all_x_in_array (nonodes2, priortree.nodep);    if (njumble > 1)      free_all_x_in_array (nonodes2, bestree2.nodep);  }  if (progress) {    printf("\n\nOutput written to file \"%s\"\n\n", outfilename);    if (trout)      printf("Tree also written onto file \"%s\"\n", outtreename);    putchar('\n');  }}  /* maketree */void clean_up(){  /* Free and/or close stuff */  long i;    free (rrate);    free (probcat);    free (rate);    /* Seems to require freeing every time... */    for (i = 0; i < spp; i++) {      free (y[i]);    }    free (y);    free (nayme);    free (enterorder);    free (category);    free (weight);    free (alias);    free (ally);    free (location);    free (aliasweight);#if 0          /* ???? debug ???? */  freetree2(curtree.nodep, nonodes2);  if (! (usertree && !reconsider)) {    freetree2(bestree.nodep, nonodes2);    freetree2(priortree.nodep, nonodes2);  }    if (! (njumble <= 1))    freetree2(bestree2.nodep, nonodes2);#endif  FClose(infile);  FClose(outfile);  FClose(outtree);#ifdef MAC  fixmacfile(outfilename);  fixmacfile(outtreename);#endif}   /* clean_up */void usage(void){    fprintf(stderr,"Usage is %s [options]\n", progname);    fprintf(stderr,"Options\n");    fprintf(stderr,"  -t<double>  Transition/transversion ratio (defalut=2.0)\n");    fprintf(stderr,"  -f<0|1>     Use empiric

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -