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

📄 file_util.c

📁 fastDNAml is an attempt to solve the same problem as DNAML, but to do so faster and using less memo
💻 C
字号:
#include  <stdlib.h>#include  <stdio.h>#include  <string.h>#include  "fastDNAml_types.h"#include  "fastDNAml_funcs.h"#include  "fastDNAml_globals.h"/*=======================================================================*//*                         Read a tree from a file                       *//*=======================================================================*//*  1.0.A  Processing of quotation marks in comment removed */int treeFinishCom (FILE *fp, char **strp)  { /* treeFinishCom */    int  ch;    while ((ch = getc(fp)) != EOF && ch != ']') {      if (strp != NULL) *(*strp)++ = ch;    /* save character  */      if (ch == '[') {                      /* nested comment; find its end */        if ((ch = treeFinishCom(fp, strp)) == EOF)  break;        if (strp != NULL) *(*strp)++ = ch;  /* save closing ]  */        }      }    if (strp != NULL) **strp = '\0';        /* terminate string  */    return  ch;  } /* treeFinishCom */int treeGetCh (FILE *fp)         /* get next nonblank, noncomment character */  { /* treeGetCh */    int  ch;    while ((ch = getc(fp)) != EOF) {      if (whitechar(ch)) ;      else if (ch == '[') {                   /* comment; find its end */        if ((ch = treeFinishCom(fp, (char **) NULL)) == EOF)  break;        }      else  break;      }    return  ch;  } /* treeGetCh */boolean  treeGetLabel (FILE *fp, char *lblPtr, int maxlen)  { /* treeGetLabel */    int      ch;    boolean  done, quoted, lblfound;    if (--maxlen < 0) lblPtr = (char *) NULL;  /* reserves space for '\0' */    else if (lblPtr == NULL) maxlen = 0;    ch = getc(fp);    done = treeLabelEnd(ch);    lblfound = ! done;    quoted = (ch == '\'');    if (quoted && ! done) {ch = getc(fp); done = (ch == EOF);}    while (! done) {      if (quoted) {        if (ch == '\'') {ch = getc(fp); if (ch != '\'') break;}        }      else if (treeLabelEnd(ch)) break;      else if (ch == '_') ch = ' ';  /* unquoted _ goes to space */      if (--maxlen >= 0) *lblPtr++ = ch;      ch = getc(fp);      if (ch == EOF) break;      }    if (ch != EOF)  (void) ungetc(ch, fp);    if (lblPtr != NULL) *lblPtr = '\0';    return lblfound;  } /* treeGetLabel */boolean  treeFlushLabel (FILE *fp)  { /* treeFlushLabel */    return  treeGetLabel(fp, (char *) NULL, (int) 0);  } /* treeFlushLabel */int  treeFindTipName (FILE *fp, tree *tr)  { /* treeFindTipName */    char    *nameptr, str[nmlngth+2];    int      n;    if (tr->prelabeled) {      if (treeGetLabel(fp, str, nmlngth+2))        n = treeFindTipByLabel(str, tr);      else        n = 0;      }    else if (tr->ntips < tr->mxtips) {      n = tr->ntips + 1;      nameptr = tr->nodep[n]->name;      if (! treeGetLabel(fp, nameptr, nmlngth+1)) n = 0;      }    else {      n = 0;      }    return  n;  } /* treeFindTipName */void  treeEchoContext (FILE *fp1, FILE *fp2, int n) { /* treeEchoContext */   int      ch;   boolean  waswhite;   waswhite = TRUE;   while (n > 0 && ((ch = getc(fp1)) != EOF)) {     if (whitechar(ch)) {       ch = waswhite ? '\0' : ' ';       waswhite = TRUE;       }     else {       waswhite = FALSE;       }     if (ch > '\0') {putc(ch, fp2); n--;}     } } /* treeEchoContext */boolean treeProcessLength (FILE *fp, double *dptr)  { /* treeProcessLength */    int  ch;    if ((ch = treeGetCh(fp)) == EOF)  return FALSE;    /*  Skip comments */    (void) ungetc(ch, fp);    if (fscanf(fp, "%lf", dptr) != 1) {      printf("ERROR: treeProcessLength: Problem reading branch length\n");      treeEchoContext(fp, stdout, 40);      printf("\n");      return  FALSE;      }    return  TRUE;  } /* treeProcessLength */boolean  treeFlushLen (FILE  *fp)  { /* treeFlushLen */    double  dummy;    int     ch;    if ((ch = treeGetCh(fp)) == ':') return treeProcessLength(fp, & dummy);    if (ch != EOF) (void) ungetc(ch, fp);    return TRUE;  } /* treeFlushLen */boolean  treeNeedCh (FILE *fp, int c1, char *where)  { /* treeNeedCh */    int  c2;    if ((c2 = treeGetCh(fp)) == c1)  return TRUE;    printf("ERROR: Expecting '%c' %s tree; found:", c1, where);    if (c2 == EOF) {      printf("End-of-File");      }    else {      ungetc(c2, fp);      treeEchoContext(fp, stdout, 40);      }    putchar('\n');    return FALSE;  } /* treeNeedCh */int saveTreeCom (FILE *fp, char  **comstrp)  { /* saveTreeCom */    int  ch;    boolean  inquote;    inquote = FALSE;    while ((ch = getc(fp)) != EOF && (inquote || ch != ']')) {      *(*comstrp)++ = ch;                        /* save character  */      if (ch == '[' && ! inquote) {              /* comment; find its end */        if ((ch = saveTreeCom(fp,comstrp)) == EOF)  break;        *(*comstrp)++ = ch;                      /* add ] */        }      else if (ch == '\'') inquote = ! inquote;  /* start or end of quote */      }    return  ch;  } /* saveTreeCom */boolean processTreeCom (FILE *fp, tree *tr)  { /* processTreeCom */    int   text_started, functor_read, com_open;    /*  Accept prefatory "phylip_tree(" or "pseudoNewick("  */    functor_read = text_started = 0;    (void) fscanf(fp, " p%nhylip_tree(%n", & text_started, & functor_read);    if (text_started && ! functor_read) {      (void) fscanf(fp, "seudoNewick(%n", & functor_read);      if (! functor_read) {        printf("Start of tree 'p...' not understood.\n");        return FALSE;        }      }    com_open = 0;    (void) fscanf(fp, " [%n", & com_open);    if (com_open) {                                  /* comment; read it */      char  com[1024], *com_end;      com_end = com;      if (treeFinishCom(fp, & com_end) == EOF) {     /* omits enclosing []s */        printf("Missing end of tree comment\n");        return FALSE;        }      (void) readKeyValue(com, likelihood_key, "%lg",                               (void *) &(tr->likelihood));      (void) readKeyValue(com, opt_level_key,  "%d",                               (void *) &(tr->opt_level));      (void) readKeyValue(com, smoothed_key,   "%d",                               (void *) &(tr->smoothed));      if (functor_read) (void) fscanf(fp, " ,");   /* remove trailing comma */      }    return (functor_read > 0);  } /* processTreeCom */boolean treeReadLen (FILE *fp, tree *tr)  { /* treeReadLen */    nodeptr  p;    int      i, ch;    boolean  is_fact;    for (i = 1; i <= tr->mxtips; i++) tr->nodep[i]->back = (node *) NULL;    tr->start       = tr->nodep[tr->mxtips];    tr->ntips       = 0;    tr->nextnode    = tr->mxtips + 1;    tr->opt_level   = 0;    tr->log_f_valid = 0;    tr->smoothed    = FALSE;    tr->rooted      = FALSE;    is_fact = processTreeCom(fp, tr);    p = tr->nodep[(tr->nextnode)++];    if (! treeNeedCh(fp, '(', "at start of"))       return FALSE;    if (! addElementLen(fp, tr, p))                 return FALSE;    if (! treeNeedCh(fp, ',', "in"))                return FALSE;    if (! addElementLen(fp, tr, p->next))           return FALSE;    if (! tr->rooted) {      if ((ch = treeGetCh(fp)) == ',') {        /*  An unrooted format */        if (! addElementLen(fp, tr, p->next->next)) return FALSE;        }      else {                                    /*  A rooted format */        tr->rooted = TRUE;        if (ch != EOF)  (void) ungetc(ch, fp);        }      }    else {      p->next->next->back = (nodeptr) NULL;      }    if (! treeNeedCh(fp, ')', "in"))                return FALSE;    (void) treeFlushLabel(fp);    if (! treeFlushLen(fp))                         return FALSE;    if (is_fact) {      if (! treeNeedCh(fp, ')', "at end of"))       return FALSE;      if (! treeNeedCh(fp, '.', "at end of"))       return FALSE;      }    else {      if (! treeNeedCh(fp, ';', "at end of"))       return FALSE;      }    if (tr->rooted) {      p->next->next->back = (nodeptr) NULL;      tr->start = uprootTree(tr, p->next->next);      if (! tr->start)                              return FALSE;      }    else {      tr->start = p->next->next->back;  /* This is start used by treeString */      }    return  (initrav(tr, tr->start) && initrav(tr, tr->start->back));  } /* treeReadLen */

⌨️ 快捷键说明

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