Example #1
0
void getalleles()
{ /* set up number of alleles at loci */
  long i, j, m;

  if (!firstset)
    samenumsp(&loci, ith);
  if (contchars ) {
    totalleles = loci;
    for (i = 1; i <= loci; i++) {
      locus[i - 1] = i;
      alleles[i - 1] = 1;
    }
    df = loci;
  } else {
    totalleles = 0;
    scan_eoln(infile);
    if (printdata) {
      fprintf(outfile, "\nNumbers of alleles at the loci:\n");
      fprintf(outfile, "------- -- ------- -- --- -----\n\n");
    }
    for (i = 1; i <= loci; i++) {
      if (eoln(infile)) 
        scan_eoln(infile);
      if (fscanf(infile, "%ld", &alleles[i - 1]) != 1) {
        printf("ERROR: Unable to read number of alleles at locus %ld\n", i);
        exxit(-1);
      }
      if (alleles[i - 1] <= 0) {
    printf("ERROR: Bad number of alleles: %ld at locus %ld\n", alleles[i-1], i);
        exxit(-1);
      }
      totalleles += alleles[i - 1];
      if (printdata)
        fprintf(outfile, "%4ld", alleles[i - 1]);
    }
    locus = (long *)Malloc(totalleles*sizeof(long));
    m = 0;
    for (i = 1; i <= loci; i++) {
      for (j = 0; j < alleles[i - 1]; j++)
        locus[m+j] = i;
      m += alleles[i - 1];
    }
    df = totalleles - loci;
  }
  allocview(&curtree, nonodes2, totalleles);
  if (!usertree) {
    allocview(&bestree, nonodes2, totalleles);
    allocview(&priortree, nonodes2, totalleles);
    if (njumble > 1)
      allocview(&bestree2, nonodes2, totalleles);
  }
  for (i = 0; i < spp; i++)
    x[i] = (phenotype3)Malloc(totalleles*sizeof(double));
  pbar = (double *)Malloc(totalleles*sizeof(double));
  if (usertree)
    for (i = 0; i < MAXSHIMOTREES; i++)
      l0gf[i] = (double *)Malloc(totalleles*sizeof(double));
  if (printdata)
    putc('\n', outfile);
}  /* getalleles */
Example #2
0
void dist_inputdata_modified(boolean replicates, boolean printdata, boolean lower,
                    boolean upper, vector *x, intvector *reps)
{
    /* read in distance matrix */
    /* used in fitch & neighbor */
    long i=0, j=0, k=0, columns=0;
    boolean skipit=false, skipother=false;

    if (replicates)
        columns = 4;
    else
        columns = 6;
    
    for (i = 0; i < spp; i++) {
        x[i][i] = 0.0;
        initname_modified(i);

        for (j = 0; j < spp; j++) {
            skipit = ((lower && j + 1 >= i + 1) || (upper && j + 1 <= i + 1));
            skipother = ((lower && i + 1 >= j + 1) || (upper && i + 1 <= j + 1));
            
            if (!skipit) {
                if (replicates) {
                    assert(0);
//                     if (eoln(infile))
//                         scan_eoln(infile);
//                     if (fscanf(infile, "%ld", &reps[i][j]) != 1) {
//                         printf("The infile is of the wrong type\n");
//                         exxit(-1);
//                     }
                } else
                    reps[i][j] = 1;
            }
            
            if (!skipit && skipother) {
                x[j][i] = x[i][j];
                reps[j][i] = reps[i][j];
            }
            if ((i == j) && (fabs(x[i][j]) > 0.000000001)) {
                printf("\nERROR: diagonal element of row %ld of distance matrix ", i+1);
                printf("is not zero.\n");
                printf("       Is it a distance matrix?\n\n");
                exxit(-1);        
            }
            if ((j < i) && (fabs(x[i][j]-x[j][i]) > 0.000000001)) {
                printf("ERROR: distance matrix is not symmetric:\n");
                printf("       (%ld,%ld) element and (%ld,%ld) element are unequal.\n",
                    i+1, j+1, j+1, i+1);
                printf("       They are %10.6f and %10.6f, respectively.\n",
                    x[i][j], x[j][i]);
                printf("       Is it a distance matrix?\n\n");
                exxit(-1);
            }
        }
    }
   
}  /* inputdata */
Example #3
0
void inputancestors(boolean *anczero0, boolean *ancone0)
{
  /* reads the ancestral states for each character */
  /* used in dollop, dolmove, dolpenny, mix, move, & penny */
  long i;
  Char ch;

  for (i = 0; i < (chars); i++) {
    anczero0[i] = true;
    ancone0[i] = true;
    do {
      if (eoln(ancfile))
        scan_eoln(ancfile);
      ch = gettc(ancfile);
      if (ch == '\n')
        ch = ' ';
    } while (ch == ' ');
    if (ch == 'p')
      ch = 'P';
    if (ch == 'b')
      ch = 'B';
    if (strchr("10PB?",ch) != NULL){
      anczero0[i] = (ch == '1') ? false : anczero0[i];
      ancone0[i] = (ch == '0') ? false : ancone0[i];
    } else {
      printf("BAD ANCESTOR STATE: %cAT CHARACTER %4ld\n", ch, i + 1);
      exxit(-1);
    }
  }
  scan_eoln(ancfile);
}  /* inputancestorsnew */
Example #4
0
void inputoptions()
{
  /* read the options information */
  Char ch;
  long i, extranum, cursp, curst;

  if (!firstset) {
    if (eoln(infile)) {
      fscanf(infile, "%*[^\n]");
      getc(infile);
    }
    fscanf(infile, "%ld%ld", &cursp, &curst);
    if (cursp != spp) {
      printf("\nERROR: INCONSISTENT NUMBER OF SPECIES IN DATA SET %4ld\n",
             ith);
      exxit(-1);
    }
    sites = curst;
  }
  for (i = 1; i <= sites; i++)
    weight[i] = 1;
  weightsum = sites;
  extranum = 0;
  fscanf(infile, "%*[ 0-9]");
  readoptions(&extranum, "W");
  for (i = 1; i <= extranum; i++) {
      matchoptions(&ch, "W");
      inputweights2(1, sites+1, &weightsum, weight, &weights, "RESTDIST");
  }
}  /* inputoptions */
Example #5
0
void bootweights()
{   /* sets up weights by resampling data */
    long i, j, k, blocks;
    double p, q, r;
    long grp = 0, site = 0;

    ws = newgroups;
    for (i = 0; i < (ws); i++)
        weight[i] = 0;
    blocks = fracsample * newgroups / blocksize;
    for (i = 1; i <= (blocks); i++) {
        j = (long)(newgroups * randum(seed)) + 1;
        for (k = 0; k < blocksize; k++) {
            weight[j - 1]++;
            j++;
            if (j > newgroups)
                j = 1;
        }
    }

    /* Count number of replicated groups */
    newergroups = 0;
    newersites  = 0;
    for (i = 0; i < newgroups; i++) {
        newergroups += weight[i];
        newersites  += newhowmany[i] * weight[i];
    }

    if (newergroups < 1) {
        fprintf(stdout, "ERROR: sampling frequency or number of sites is too small\n");
        exxit(-1);
    }

    /* reallocate "newer" arrays, sized by output groups:
     * newerfactor, newerwhere, newerhowmany, and charorder */
    allocnewer(newergroups, newersites);

    /* Replicate each group i weight[i] times */
    grp = 0;
    site = 0;
    for (i = 0; i < newgroups; i++) {
        for (j = 0; j < weight[i]; j++) {
            for (k = 0; k < newhowmany[i]; k++) {
                newerfactor[site] = grp + 1;
                site++;
            }
            newerwhere[grp] = newwhere[i];
            newerhowmany[grp] = newhowmany[i];
            grp++;
        }
    }
}  /* bootweights */
Example #6
0
void sumlikely(node *p, node *q, double *sum)
{ /* sum contribution to likelihood over forks in tree */
  long i, j, m;
  double term, sumsq, vee;
  double temp;

  if (!p->tip)
    sumlikely(p->next->back, p->next->next->back, sum);
  if (!q->tip)
    sumlikely(q->next->back, q->next->next->back, sum);
  if (p->back == q)
    vee = p->v;
  else
    vee = p->v + q->v;
  vee += p->deltav + q->deltav;
  if (vee <= 1.0e-10) {
    printf("ERROR: check for two identical species ");
    printf("and eliminate one from the data\n");
    exxit(-1);
  }
  sumsq = 0.0;
  if (usertree && which <= MAXSHIMOTREES) {
    for (i = 0; i < loci; i++)
      l0gf[which - 1][i] += (1 - alleles[i]) * log(vee) / 2.0;
  }
  if (contchars) {
    m = 0;
    for (i = 0; i < loci; i++) {
      temp = p->view[i] - q->view[i];
      term = temp * temp;
      if (usertree && which <= MAXSHIMOTREES)
        l0gf[which - 1][i] -= term / (2.0 * vee);
      sumsq += term;
    }
  } else {
    m = 0;
    for (i = 0; i < loci; i++) {
      for (j = 1; j < alleles[i]; j++) {
        temp = p->view[m+j-1] - q->view[m+j-1];
        term = temp * temp;
        if (usertree && which <= MAXSHIMOTREES)
          l0gf[which - 1][i] -= term / (2.0 * vee);
        sumsq += term;
      }
      m += alleles[i];
    }
  }
  (*sum) += df * log(vee) / -2.0 - sumsq / (2.0 * vee);
}  /* sumlikely */
Example #7
0
void inputoptions()
{
  /* read options information */
  long i;

  if (!firstset && !justwts) {
    samenumsp(&sites, ith);
    reallocsites();
  }
  for (i = 0; i < sites; i++) {
    category[i] = 1;
    oldweight[i] = 1;
  }
  if (justwts || weights)
    inputweights(sites, oldweight, &weights);
  if (printdata)
    putc('\n', outfile);
  if (jukes && printdata)
    fprintf(outfile, "  Jukes-Cantor Distance\n");
  if (kimura && printdata)
    fprintf(outfile, "  Kimura 2-parameter Distance\n");
  if (f84 && printdata)
    fprintf(outfile, "  F84 Distance\n");
  if (similarity)
    fprintf(outfile, "  \n  Table of similarity between sequences\n");
  if (firstset && printdata && (kimura || f84))
    fprintf(outfile, "\nTransition/transversion ratio = %10.6f\n", ttratio);
  if (ctgry && categs > 1) {
    inputcategs(0, sites, category, categs, "DnaDist");
    if (printdata)
      printcategs(outfile, sites, category, "Site categories");
  } else if (printdata && (categs > 1)) {
    fprintf(outfile, "\nSite category   Rate of change\n\n");
    for (i = 1; i <= categs; i++)
      fprintf(outfile, "%12ld%13.3f\n", i, rate[i - 1]);
    putc('\n', outfile);
    printcategories();
  }
  if ((jukes || kimura || logdet) && freqsfrom) {
    printf(" WARNING: CANNOT USE EMPIRICAL BASE FREQUENCIES");
    printf(" WITH JUKES-CANTOR, KIMURA, JIN/NEI OR LOGDET DISTANCES\n");
    exxit(-1);
  }
  if (jukes)
    ttratio = 0.5000001;
  if (weights && printdata)
    printweights(outfile, 0, sites, oldweight, "Sites");
}  /* inputoptions */
Example #8
0
void maketree()
{
  /* construct the tree */
  long i ;

  inputdata(replicates, printdata, lower, upper, x, reps);
  if (njoin && (spp < 3)) {
    printf("\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) {
    printf("\nOutput written on file \"%s\"\n\n", outfilename);
    if (trout)
      printf("Tree written on file \"%s\"\n\n", outtreename);
  }
}  /* maketree */
Example #9
0
void inputmixture(bitptr wagner0)
{
  /* input mixture of methods */
  /* used in mix, move, & penny */
  long i, j, k;
  Char ch;
  boolean wag;

  for (i = 0; i < (words); i++)
    wagner0[i] = 0;
  j = 0;
  k = 1;
  for (i = 1; i <= (chars); i++) {
    do {
      if (eoln(mixfile))
        scan_eoln(mixfile);
      ch = gettc(mixfile);
      if (ch == '\n')
        ch = ' ';
    } while (ch == ' ');
    uppercase(&ch);
    wag = false;
    if (ch == 'W' || ch == '?')
      wag = true;
    else if (ch == 'S' || ch == 'C')
      wag = false;
    else {
      printf("BAD METHOD: %c\n", ch);
      exxit(-1);
    }
    if (wag)
      wagner0[k - 1] = (long)wagner0[k - 1] | (1L << j);
    j++;
    if (j > bits) {
      j = 1;
      k++;
    }
  }
  scan_eoln(mixfile);
}  /* inputmixture */
Example #10
0
void bootweights()
{ /* sets up weights by resampling data */
  long i, j, k, blocks;
  double p, q, r;
  long grp = 0, site = 0;

  ws = newgroups;
  for (i = 0; i < (ws); i++)
    weight[i] = 0;
  if (jackknife) {
    if (fabs(newgroups*fracsample - (long)(newgroups*fracsample+0.5))
       > 0.00001) {
      if (randum(seed)
          < (newgroups*fracsample - (long)(newgroups*fracsample))
            /((long)(newgroups*fracsample+1.0)-(long)(newgroups*fracsample)))
        q = (long)(newgroups*fracsample)+1;
      else
        q = (long)(newgroups*fracsample);
    } else
      q = (long)(newgroups*fracsample+0.5);
    r = newgroups;
    p = q / r;
    ws = 0;
    for (i = 0; i < (newgroups); i++) {
      if (randum(seed) < p) {
        weight[i]++;
        ws++;
        q--;
      }
      r--;
      if (i + 1 < newgroups)
        p = q / r;
    }
  } else if (permute) {
    for (i = 0; i < (newgroups); i++)
      weight[i] = 1;
  } else if (bootstrap) {
    blocks = fracsample * newgroups / blocksize;
    for (i = 1; i <= (blocks); i++) {
      j = (long)(newgroups * randum(seed)) + 1;
      for (k = 0; k < blocksize; k++) {
        weight[j - 1]++;
        j++;
        if (j > newgroups)
          j = 1;
      }
    }
  } else             /* case of rewriting data */
    for (i = 0; i < (newgroups); i++)
      weight[i] = 1;

  /* Count number of replicated groups */
  newergroups = 0;
  newersites  = 0;
  for (i = 0; i < newgroups; i++) {
    newergroups += weight[i];
    newersites  += newhowmany[i] * weight[i];
  }

  if (newergroups < 1) {
    fprintf(stdout, "ERROR: sampling frequency or number of sites is too small\n");
    exxit(-1);
  }
  
  /* reallocate "newer" arrays, sized by output groups:
   * newerfactor, newerwhere, newerhowmany, and charorder */
  allocnewer(newergroups, newersites);
  
  /* Replicate each group i weight[i] times */
  grp = 0;
  site = 0;
  for (i = 0; i < newgroups; i++) {
    for (j = 0; j < weight[i]; j++) {
      for (k = 0; k < newhowmany[i]; k++) {
        newerfactor[site] = grp + 1;
        site++;
      }
      newerwhere[grp] = newwhere[i];
      newerhowmany[grp] = newhowmany[i];
      grp++;
    }
  }
}  /* bootweights */
Example #11
0
void inputdata(boolean replicates, boolean printdata, boolean lower,
                        boolean upper, vector *x, intvector *reps)
{
  /* read in distance matrix */
  /* used in fitch & neighbor */
  long i=0, j=0, k=0, columns=0;
  boolean skipit=false, skipother=false;

  if (replicates)
    columns = 4;
  else
    columns = 6;
  if (printdata) {
    fprintf(outfile, "\nName                       Distances");
    if (replicates)
      fprintf(outfile, " (replicates)");
    fprintf(outfile, "\n----                       ---------");
    if (replicates)
      fprintf(outfile, "-------------");
    fprintf(outfile, "\n\n");
  }
  for (i = 0; i < spp; i++) {
    x[i][i] = 0.0;
    scan_eoln(infile);
    initname(i);
    for (j = 0; j < spp; j++) {
      skipit = ((lower && j + 1 >= i + 1) || (upper && j + 1 <= i + 1));
      skipother = ((lower && i + 1 >= j + 1) || (upper && i + 1 <= j + 1));
      if (!skipit) {
        if (eoln(infile))
          scan_eoln(infile);
        if (fscanf(infile, "%lf", &x[i][j]) != 1)  {
          printf("The infile is of the wrong type\n");
          exxit(-1);
        }
        if (replicates) {
          if (eoln(infile))
            scan_eoln(infile);
          if (fscanf(infile, "%ld", &reps[i][j]) != 1) {
            printf("The infile is of the wrong type\n");
            exxit(-1);
          }
        } else
          reps[i][j] = 1;
      }
      if (!skipit && skipother) {
          x[j][i] = x[i][j];
          reps[j][i] = reps[i][j];
      }
      if ((i == j) && (fabs(x[i][j]) > 0.000000001)) {
       printf("\nERROR: diagonal element of row %ld of distance matrix ", i+1);
        printf("is not zero.\n");
        printf("       Is it a distance matrix?\n\n");
        exxit(-1);        
      }
      if ((j < i) && (fabs(x[i][j]-x[j][i]) > 0.000000001)) {
        printf("ERROR: distance matrix is not symmetric:\n");
        printf("       (%ld,%ld) element and (%ld,%ld) element are unequal.\n",
               i+1, j+1, j+1, i+1);
        printf("       They are %10.6f and %10.6f, respectively.\n",
               x[i][j], x[j][i]);
        printf("       Is it a distance matrix?\n\n");
        exxit(-1);
      }
    }
  }
  scan_eoln(infile);
  if (!printdata)
    return;
  for (i = 0; i < spp; i++) {
    for (j = 0; j < nmlngth; j++)
      putc(nayme[i][j], outfile);
    putc(' ', outfile);
    for (j = 1; j <= spp; j++) {
      fprintf(outfile, "%10.5f", x[i][j - 1]);
      if (replicates)
        fprintf(outfile, " (%3ld)", reps[i][j - 1]);
      if (j % columns == 0 && j < spp) {
        putc('\n', outfile);
        for (k = 1; k <= nmlngth + 1; k++)
          putc(' ', outfile);
      }
    }
    putc('\n', outfile);
  }
  putc('\n', outfile);
}  /* inputdata */
Example #12
0
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 */
Example #13
0
void inputdata(pointptr treenode,boolean dollo,boolean printdata,FILE *outfile)
{
  /* input the names and character state data for species */
  /* used in Dollop, Dolpenny, Dolmove, & Move */
  long i, j, l;
  char k;
  Char charstate;
  /* possible states are '0', '1', 'P', 'B', and '?' */

  if (printdata)
    headings(chars, "Characters", "----------");
  for (i = 0; i < (chars); i++)
    extras[i] = 0;
  for (i = 1; i <= spp; i++) {
    initname(i-1);
    if (printdata) {
      for (j = 0; j < nmlngth; j++)
        putc(nayme[i - 1][j], outfile);
      fprintf(outfile, "   ");
    }
    for (j = 0; j < (words); j++) {
      treenode[i - 1]->stateone[j] = 0;
      treenode[i - 1]->statezero[j] = 0;
    }
    for (j = 1; j <= (chars); j++) {
      k = (j - 1) % bits + 1;
      l = (j - 1) / bits + 1;
      do {
        if (eoln(infile)) 
          scan_eoln(infile);
        charstate = gettc(infile);
      } while (charstate == ' ' || charstate == '\t');
      if (charstate == 'b')
        charstate = 'B';
      if (charstate == 'p')
        charstate = 'P';
      if (charstate != '0' && charstate != '1' && charstate != '?' &&
          charstate != 'P' && charstate != 'B') {
        printf("\n\nERROR: Bad character state: %c ",charstate);
        printf("at character %ld of species %ld\n\n", j, i);
        exxit(-1);
      }
      if (printdata) {
        newline(outfile, j, 55, nmlngth + 3);
        putc(charstate, outfile);
        if (j % 5 == 0)
          putc(' ', outfile);
      }
      if (charstate == '1')
        treenode[i - 1]->stateone[l - 1] =
          ((long)treenode[i - 1]->stateone[l - 1]) | (1L << k);
      if (charstate == '0')
        treenode[i - 1]->statezero[l - 1] =
          ((long)treenode[i - 1]->statezero[l - 1]) | (1L << k);
      if (charstate == 'P' || charstate == 'B') {
        if (dollo)
          extras[j - 1] += weight[j - 1];
        else {
          treenode[i - 1]->stateone[l - 1] =
            ((long)treenode[i - 1]->stateone[l - 1]) | (1L << k);
          treenode[i - 1]->statezero[l - 1] =
            ((long)treenode[i - 1]->statezero[l - 1]) | (1L << k);
        }
      }
    }
    scan_eoln(infile);
    if (printdata)
      putc('\n', outfile);
  }
  if (printdata)
    fprintf(outfile, "\n\n");
}  /* inputdata */
Example #14
0
void inputdata2(pointptr2 treenode)
{
  /* input the names and character state data for species */
  /* used in Mix & Penny */
  long i, j, l;
  char k;
  Char charstate;
  /* possible states are '0', '1', 'P', 'B', and '?' */

  if (printdata)
    headings(chars, "Characters", "----------");
  for (i = 0; i < (chars); i++)
    extras[i] = 0;
  for (i = 1; i <= spp; i++) {
    initname(i-1);
    if (printdata) {
      for (j = 0; j < nmlngth; j++)
        putc(nayme[i - 1][j], outfile);
    }
    fprintf(outfile, "   ");
    for (j = 0; j < (words); j++) {
      treenode[i - 1]->fulstte1[j] = 0;
      treenode[i - 1]->fulstte0[j] = 0;
      treenode[i - 1]->empstte1[j] = 0;
      treenode[i - 1]->empstte0[j] = 0;
    }
    for (j = 1; j <= (chars); j++) {
      k = (j - 1) % bits + 1;
      l = (j - 1) / bits + 1;
      do {
        if (eoln(infile)) 
          scan_eoln(infile);
        charstate = gettc(infile);
        if (charstate == '\n' || charstate == '\t')
          charstate = ' ';
      } while (charstate == ' ');
      if (charstate == 'b')          charstate = 'B';
      if (charstate == 'p')          charstate = 'P';
      if (charstate != '0' && charstate != '1' && charstate != '?' &&
          charstate != 'P' && charstate != 'B') {
        printf("\n\nERROR: Bad character state: %c ",charstate);
        printf("at character %ld of species %ld\n\n", j, i);
        exxit(-1);
      }
      if (printdata) {
        newline(outfile, j, 55, nmlngth + 3);
        putc(charstate, outfile);
        if (j % 5 == 0)
          putc(' ', outfile);
      }
      if (charstate == '1') {
        treenode[i-1]->fulstte1[l-1] =
          ((long)treenode[i-1]->fulstte1[l-1]) | (1L << k);
        treenode[i-1]->empstte1[l-1] =
          treenode[i-1]->fulstte1[l-1];
      }
      if (charstate == '0') {
        treenode[i-1]->fulstte0[l-1] =
          ((long)treenode[i-1]->fulstte0[l-1]) | (1L << k);
        treenode[i-1]->empstte0[l-1] =
          treenode[i-1]->fulstte0[l-1];
      }
      if (charstate == 'P' || charstate == 'B')
        extras[j-1] += weight[j-1];
    }
    scan_eoln(infile);
    if (printdata)
      putc('\n', outfile);
  }
  fprintf(outfile, "\n\n");
}  /* inputdata2 */
Example #15
0
void seqboot_inputdata()
{
    /* input the names and sequences for each species */
    long i, j, k, l, m, n, basesread, basesnew=0;
    double x;
    Char charstate;
    boolean allread, done;

    nodep = matrix_char_new(spp, sites);
    j = nmlngth + (sites + (sites - 1) / 10) / 2 - 5;
    if (j < nmlngth - 1)
        j = nmlngth - 1;
    if (j > 37)
        j = 37;

    interleaved = (interleaved && ((data == seqs) || (data == restsites)));
    basesread = 0;
    allread = false;
    while (!allread) {
        /* eat white space -- if the separator line has spaces on it*/
        do {
            charstate = gettc(infile);
        } while (charstate == ' ' || charstate == '\t');
        ungetc(charstate, infile);
        if (eoln(infile))
            scan_eoln(infile);
        i = 1;
        while (i <= spp) {
            if ((interleaved && basesread == 0) || !interleaved)
                initname(i-1);
            j = interleaved ? basesread : 0;
            done = false;
            while (!done && !eoff(infile)) {
                if (interleaved)
                    done = true;
                while (j < sites && !(eoln(infile) ||eoff(infile))) {
                    charstate = gettc(infile);
                    if (charstate == '\n' || charstate == '\t')
                        charstate = ' ';
                    if (charstate == ' ' ||
                            (data == seqs && charstate >= '0' && charstate <= '9'))
                        continue;
                    uppercase(&charstate);
                    j++;
                    if (charstate == '.')
                        charstate = nodep[0][j-1];
                    nodep[i-1][j-1] = charstate;
                }
                if (interleaved)
                    continue;
                if (j < sites)
                    scan_eoln(infile);
                else if (j == sites)
                    done = true;
            }
            if (interleaved && i == 1)
                basesnew = j;
            scan_eoln(infile);
            if ((interleaved && j != basesnew) || ((!interleaved) && j != sites)) {
                printf("\n\nERROR: sequences out of alignment at site %ld", j+1);
                printf(" of species %ld\n\n", i);
                exxit(-1);
            }
            i++;
        }
        if (interleaved) {
            basesread = basesnew;
            allread = (basesread == sites);
        } else
            allread = (i > spp);
    }
    if (!printdata)
        return;

}  /* seqboot_inputdata */
Example #16
0
void interpret(char *line)
{
    char *tok, *last_s;
    char *sep = ",";
    for (tok = strtok_r(line, sep, &last_s); tok;
         tok = strtok_r(NULL, sep, &last_s)) {
        char *trim_tok = calloc(strlen(tok), sizeof(char));
        strim(tok, trim_tok);

        if (strcmp(trim_tok, "ps") == 0) {
            ps();
            return;
        }
        else if (strcmp(trim_tok, "nanosynth") == 0 ||
                 strcmp(trim_tok, "ns") == 0) {
            add_nanosynth(mixr);
        }
        else if (strcmp(trim_tok, "midi") == 0) {
            //// run the MIDI event looprrr...
            pthread_t midi_th;
            if (pthread_create(&midi_th, NULL, midiman, NULL)) {
                fprintf(stderr, "Errrr, wit tha midi..\n");
                return;
            }
            pthread_detach(midi_th);
        }
        else if (strcmp(trim_tok, "ls") == 0) {
            list_sample_dir();
        }
        else if (strcmp(trim_tok, "delay") == 0) {
            delay_toggle(mixr);
        }
        else if (strcmp(trim_tok, "exit") == 0) {
            exxit();
        }
        else if (strcmp(trim_tok, "help") == 0) {
            print_help();
        }

        // SIGNAL START
        regmatch_t ns_match[3];
        regex_t ns_cmdtype_rx;
        regcomp(&ns_cmdtype_rx,
                "^(bitwize|sine|sawd|sawu|tri|square) ([[:digit:].]+)$",
                REG_EXTENDED | REG_ICASE);
        if (regexec(&ns_cmdtype_rx, trim_tok, 3, ns_match, 0) == 0) {

            float val = 0;
            char cmd[20];
            SBMSG *msg = new_sbmsg();
            sscanf(trim_tok, "%s %f", cmd, &val);
            msg->freq = val;

            strncpy(msg->cmd, "timed_sig_start", 19);
            printf("PARAMZZZ %s\n", cmd);
            strncpy(msg->params, cmd, 10);
            thrunner(msg);
        }

        // TODO: move regex outside function to compile once
        regmatch_t pmatch[3];
        regex_t cmdtype_rx;
        regcomp(&cmdtype_rx,
                "^(bpm|duck|keys|midi|reset|solo|stop|up|vol) ([[:digit:].]+)$",
                REG_EXTENDED | REG_ICASE);

        if (regexec(&cmdtype_rx, trim_tok, 3, pmatch, 0) == 0) {

            float val = 0;
            char cmd[20];
            SBMSG *msg = new_sbmsg();

            sscanf(trim_tok, "%s %f", cmd, &val);
            msg->freq = val;

            int is_val_a_valid_sig_num =
                (val >= 0 && val < mixr->soundgen_num) ? 1 : 0;

            if (strcmp(cmd, "vol") == 0) {
                if (val >= 0 && val <= 1) {
                    mixr->volume = val;
                }
            }

            if (strcmp(cmd, "bpm") == 0) {
                mixer_update_bpm(mixr, val);
                for (int i = 0; i < mixr->soundgen_num; i++) {
                    for (int j = 0;
                         j < mixr->sound_generators[i]->envelopes_num; j++) {
                        update_envelope_stream_bpm(
                            mixr->sound_generators[i]->envelopes[j]);
                    }
                    if (mixr->sound_generators[i]->type == SAMPLER_TYPE) {
                        sampler_resample_to_loop_size(
                            (SAMPLER *)mixr->sound_generators[i]);
                    }
                }
            }
            else if (is_val_a_valid_sig_num) {

                if (strcmp(cmd, "keys") == 0) {
                    if (mixr->sound_generators[(int)val]->type ==
                        NANOSYNTH_TYPE) {
                        printf("KEYS!\n");
                        keys(val);
                    }
                    else {
                        printf("Can only run keys() on an nanosynth Type, ya "
                               "dafty\n");
                    }
                }
                else if (strcmp(cmd, "midi") == 0) {
                    if (mixr->sound_generators[(int)val]->type ==
                        NANOSYNTH_TYPE) {
                        printf("MIDI fer %d\n", (int)val);
                        mixr->midi_control_destination = NANOSYNTH;
                        mixr->active_midi_soundgen_num = val;
                    }
                    else {
                        printf("Can only run keys() on an nanosynth Type, ya "
                               "dafty\n");
                    }
                }
                else if (strcmp(cmd, "reset") == 0) {
                    if (mixr->sound_generators[(int)val]->type ==
                        NANOSYNTH_TYPE) {
                        printf("RESET fer %d\n", (int)val);
                        nanosynth *ns =
                            (nanosynth *)mixr->sound_generators[(int)val];
                        nanosynth_reset_melody(ns);
                    }
                    else if (mixr->sound_generators[(int)val]->type ==
                             DRUM_TYPE) {
                        DRUM *d = (DRUM *)mixr->sound_generators[(int)val];
                        for (int i = 0; i < d->num_patterns; i++)
                            d->patterns[i] = 0;
                    }
                    else {
                        printf("Can only run keys() on an nanosynth Type, ya "
                               "dafty\n");
                    }
                }
                else if (strcmp(cmd, "stop") == 0) {
                    msg->sound_gen_num = val;
                    strncpy(msg->cmd, "fadedownrrr", 19);
                    thrunner(msg);
                }
                else if (strcmp(cmd, "up") == 0) {
                    msg->sound_gen_num = val;
                    strncpy(msg->cmd, "fadeuprrr", 19);
                    thrunner(msg);
                }
                else if (strcmp(cmd, "duck") == 0) {
                    msg->sound_gen_num = val;
                    strncpy(msg->cmd, "duckrrr", 19);
                    thrunner(msg);
                }
                else if (strcmp(cmd, "solo") == 0) {
                    for (int i = 0; i < mixr->soundgen_num; i++) {
                        if (i != val) {
                            SBMSG *msg = new_sbmsg();
                            msg->sound_gen_num = i;
                            printf("Duckin' %d\n", i);
                            strncpy(msg->cmd, "duckrrr", 19);
                            thrunner(msg);
                        }
                    }
                }
            }
        }

        regmatch_t sxmatch[3];
        regex_t sx_rx;
        regcomp(&sx_rx,
                "^(beatrepeat|distort|decimate|life|rec|env) ([[:digit:].]+)$",
                REG_EXTENDED | REG_ICASE);
        if (regexec(&sx_rx, trim_tok, 2, sxmatch, 0) == 0) {
            double val = 0;
            char cmd_type[10];
            sscanf(trim_tok, "%s %lf", cmd_type, &val);
            int is_val_a_valid_sig_num =
                (val >= 0 && val < mixr->soundgen_num) ? 1 : 0;
            if (is_val_a_valid_sig_num) {
                if (strncmp(cmd_type, "distort", 10) == 0) {
                    printf("DISTORTTTTTTT!\n");
                    add_distortion_soundgen(mixr->sound_generators[(int)val]);
                }
                else if ((strncmp(cmd_type, "beatrepeat", 10) == 0)) {
                    printf("Toggling BEATREPEAT for %d\n", (int)val);
                    beatrepeat *b =
                        (beatrepeat *)mixr->sound_generators[(int)val]
                            ->effects[0];
                    b->m_active = 1 - b->m_active;
                }
                else if ((strncmp(cmd_type, "rec", 4) == 0)) {
                    if (mixr->sound_generators[(int)val]->type !=
                        NANOSYNTH_TYPE) {
                        printf("Beat it, ya chancer\n");
                    }
                    else {
                        printf("Toggling REC for %d\n", (int)val);
                        nanosynth *ns =
                            (nanosynth *)mixr->sound_generators[(int)val];
                        ns->recording = 1 - ns->recording;
                    }
                }
                else if ((strncmp(cmd_type, "life", 5) == 0)) {
                    if (mixr->sound_generators[(int)val]->type != DRUM_TYPE) {
                        printf("Beat it, ya chancer\n");
                    }
                    else {
                        printf("Toggling LIFE for %d\n", (int)val);
                        DRUM *d = (DRUM *)mixr->sound_generators[(int)val];
                        d->game_of_life_on = 1 - d->game_of_life_on;
                        // printf("LIFE now %d\n", d->game_of_life_on);
                        // ps();
                    }
                }
                else if ((strncmp(cmd_type, "env", 4) == 0)) {
                    printf("Toggling ENV for %d\n", (int)val);
                    mixr->sound_generators[(int)val]->envelopes_enabled =
                        1 - mixr->sound_generators[(int)val]->envelopes_enabled;
                }
                else {
                    printf("DECIMATE!\n");
                    add_decimator_soundgen(mixr->sound_generators[(int)val]);
                }
            }
            else {
                printf("Cmonbuddy, playdagem, eh? Only valid signal nums "
                       "allowed\n");
            }
        }

        regmatch_t sc_match[5];
        regex_t sc_rx;
        regcomp(&sc_rx,
                "^(sidechain) ([[:digit:]]) ([[:digit:]]) ([[:digit:]]+)$",
                REG_EXTENDED | REG_ICASE);
        if (regexec(&sc_rx, trim_tok, 4, sc_match, 0) == 0) {
            int val1, val2, val3;
            char cmd_type[10];
            sscanf(trim_tok, "%s %d %d %d", cmd_type, &val1, &val2, &val3);
            printf("SIDECHAIN! %d %d %d\n", val1, val2, val3);
            if (mixr->sound_generators[val2]->type == DRUM_TYPE) {
                DRUM *d = (DRUM *)mixr->sound_generators[val2];
                int pat_array[16];
                int_pattern_to_array(d->patterns[d->cur_pattern_num],
                                     pat_array);
                for (int i = 0; i < 16; i++) {
                    printf("%d ", pat_array[i]);
                }
                printf("\n");
                ENVSTREAM *e = new_sidechain_stream(pat_array, val3);
                add_envelope_soundgen(mixr->sound_generators[(int)val1], e);
            }
        }

        regmatch_t tpmatch[4];
        regex_t tsigtype_rx;
        regcomp(&tsigtype_rx,
                "^(beatrepeat|vol|freq|delay|reverb|res|randd|allpass|midi|"
                "lowpass|highpass|bandpass|nanosynth|sustain|swing) "
                "([[:digit:].]+) ([[:alnum:].]+)$",
                REG_EXTENDED | REG_ICASE);
        if (regexec(&tsigtype_rx, trim_tok, 3, tpmatch, 0) == 0) {
            double val1 = 0;
            double val2 = 0;
            char cmd_type[10];
            sscanf(trim_tok, "%s %lf %lf", cmd_type, &val1, &val2);

            int is_val_a_valid_sig_num =
                (val1 >= 0 && val1 < mixr->soundgen_num) ? 1 : 0;

            if (is_val_a_valid_sig_num) {

                if (strcmp(cmd_type, "vol") == 0) {
                    vol_change(mixr, val1, val2);
                    printf("VOL! %s %f %lf\n", cmd_type, val1, val2);
                }
                if (strcmp(cmd_type, "sustain") == 0) {
                    printf("SUSTAIN! %s %lf %lf\n", cmd_type, val1, val2);
                    if (mixr->sound_generators[(int)val1]->type ==
                        NANOSYNTH_TYPE) {
                        nanosynth *ns =
                            (nanosynth *)mixr->sound_generators[(int)val1];
                        // convert to midi ticks per millisec * val2
                        // one midi pulse in msec = 1 / (60/bpm) * PPQN / 1000
                        int sustain_lev =
                            val2 * (1 / (60.0 / mixr->bpm) * PPQN / 1000);
                        nanosynth_set_sustain(ns, sustain_lev);
                    }
                }
                if (strcmp(cmd_type, "beatrepeat") == 0) {
                    printf("BEAT REPEAT CALLED FOR! %s %.lf %.lf\n", cmd_type,
                           val1, val2);
                    add_beatrepeat_soundgen(mixr->sound_generators[(int)val1],
                                            val2);
                }
                if (strcmp(cmd_type, "delay") == 0) {
                    printf("DELAY CALLED FOR! %s %.lf %.lf\n", cmd_type, val1,
                           val2);
                    add_delay_soundgen(mixr->sound_generators[(int)val1], val2);
                }
                if (strcmp(cmd_type, "midi") == 0) {
                    printf("MIDI CALLED FOR! %s %.lf %.lf\n", cmd_type, val1,
                           val2);
                    if (val2 >=
                        mixr->sound_generators[(int)val1]->effects_num) {
                        printf("Git tae f**k - no enuff effects!\n");
                        return;
                    }
                    if (mixr->sound_generators[(int)val1]
                            ->effects[(int)val2]
                            ->type != DELAY) {
                        printf("Git tae f**k - only delay effects for the "
                               "moment..ITS %d\n",
                               mixr->sound_generators[(int)val1]
                                   ->effects[(int)val2]
                                   ->type);
                        return;
                    }
                    // else
                    printf("SUCCESS! GOLDEN MIDI DELAY!\n");
                    mixr->midi_control_destination = DELAYFX;
                    mixr->active_midi_soundgen_num = val1;
                    mixr->active_midi_soundgen_effect_num = val2;
                }
                if (strcmp(cmd_type, "lowpass") == 0) {
                    printf("LOWPASS CALLED FOR! %s %.lf %.lf\n", cmd_type, val1,
                           val2);
                    add_freq_pass_soundgen(mixr->sound_generators[(int)val1],
                                           val2, LOWPASS);
                }
                if (strcmp(cmd_type, "highpass") == 0) {
                    printf("HIGHPASS CALLED FOR! %s %.lf %.lf\n", cmd_type,
                           val1, val2);
                    add_freq_pass_soundgen(mixr->sound_generators[(int)val1],
                                           val2, HIGHPASS);
                }
                if (strcmp(cmd_type, "bandpass") == 0) {
                    printf("BANDPASS CALLED FOR! %s %.lf %.lf\n", cmd_type,
                           val1, val2);
                    add_freq_pass_soundgen(mixr->sound_generators[(int)val1],
                                           val2, BANDPASS);
                }
                if (strcmp(cmd_type, "randd") == 0) {
                    printf("RANDY!\n");
                    SBMSG *msg = new_sbmsg();
                    strncpy(msg->cmd, "randdrum", 9);
                    msg->sound_gen_num = val1;
                    msg->looplen = val2;
                    thrunner(msg);
                }
            }
            else {
                printf("Oofft mate, you don't have enough sound_gens for "
                       "that..\n");
            }
        }

        // chord info // TODO - simplify - don't need a regex here but whatevs
        regmatch_t chmatch[2];
        regex_t chord_rx;
        regcomp(&chord_rx, "^chord ([[:alpha:]]+)$", REG_EXTENDED | REG_ICASE);
        if (regexec(&chord_rx, trim_tok, 2, chmatch, 0) == 0) {
            int note_len = chmatch[1].rm_eo - chmatch[1].rm_so;
            char note[note_len + 1];
            strncpy(note, trim_tok + chmatch[1].rm_so, note_len);
            note[note_len] = '\0';
            chordie(note);
        }

        // Euclidean drum sample play!
        regmatch_t eumatch[5];
        regex_t eurx;
        regcomp(&eurx,
                // TODO - ugh!! clean this shit up..
                //"^(uplay|uaddd) ([.[:alnum:]]+) ([[:digit:]]+)
                //([\"true\"|\"false\"])$",
                "^(uplay|uaddd|beatrepeat) ([.[:alnum:]]+) ([[:digit:]]+) "
                "([.[:alnum:]]+)$",
                REG_EXTENDED | REG_ICASE);
        if (regexec(&eurx, trim_tok, 5, eumatch, 0) == 0) {

            char cmd_type[10];
            char tmp[32];
            int num_beats;
            char bool_start_at_zero[10];
            sscanf(trim_tok, "%s %s %d %s", cmd_type, tmp, &num_beats,
                   bool_start_at_zero);
            int filename_len = eumatch[2].rm_eo - eumatch[2].rm_so;
            char filename[filename_len + 1];
            strncpy(filename, trim_tok + eumatch[2].rm_so, filename_len);
            filename[filename_len] = '\0';

            // if (strncmp(bool_start_at_zero, "true", 5) &&
            //    strncmp(bool_start_at_zero, "false", 6)) {
            //    printf("Dingie!\n");
            //    return;
            //}
            bool start_at_zero =
                strcmp(bool_start_at_zero, "true") == 0 ? true : false;

            printf("EUCLIDEAN, MO-FO! %s %d %s\n", filename, num_beats,
                   bool_start_at_zero);

            if (strcmp(cmd_type, "uplay") == 0) {
                printf("NEW UPLAY DRUM\n");
                add_drum_euclidean(mixr, filename, num_beats, start_at_zero);
            }
            else if (strcmp(cmd_type, "uaddd") == 0) {
                int val = atoi(filename);
                int is_val_a_valid_sig_num =
                    (val >= 0 && val < mixr->soundgen_num) ? 1 : 0;
                if (is_val_a_valid_sig_num &&
                    mixr->sound_generators[val]->type == DRUM_TYPE) {
                    int euclidean_pattern =
                        create_euclidean_rhythm(num_beats, 16);
                    if (start_at_zero)
                        euclidean_pattern = shift_bits_to_leftmost_position(
                            euclidean_pattern, 16);
                    add_int_pattern(mixr->sound_generators[val],
                                    euclidean_pattern);
                    printf("Adding UPLAY DRUM\n");
                }
            }
            else if (strcmp(cmd_type, "beatrepeat") == 0) {
                printf("Beat repeat paramzzz change\n");
                printf("Sub: %s %d %s\n", tmp, num_beats, bool_start_at_zero);
                // if (beatrepeat *br, int num_beats)
                if (mixr->sound_generators[num_beats]->effects[0]->type ==
                    BEATREPEAT) {
                    printf("BOOYABEATREPEAT!\n");
                    beatrepeat *br =
                        (beatrepeat *)mixr->sound_generators[num_beats]
                            ->effects[0];
                    if (strncmp(tmp, "nbeats", 4) == 0) {
                        printf("NBEATZZZ\n");
                        beatrepeat_change_num_beats_to_repeat(
                            br, atoi(bool_start_at_zero));
                    }
                    else if (strncmp(tmp, "16th", 4) == 0) {
                        printf("16thhhyhth\n");
                        beatrepeat_change_selected_sixteenth(
                            br, atoi(bool_start_at_zero));
                    }
                }
            }
        }

        // drum sample play
        regmatch_t fmatch[4];
        regex_t file_rx;
        regcomp(
            &file_rx,
            "^(play|addd) ([.[:alnum:]]+) ((all|none|[[:digit:][:space:]]+))$",
            REG_EXTENDED | REG_ICASE);
        if (regexec(&file_rx, trim_tok, 4, fmatch, 0) == 0) {

            char cmd_type[10];
            sscanf(trim_tok, "%s", cmd_type);

            int pattern_len = fmatch[3].rm_eo - fmatch[3].rm_so;
            char tmp_pattern[pattern_len + 1];
            strncpy(tmp_pattern, trim_tok + fmatch[3].rm_so, pattern_len);
            tmp_pattern[pattern_len] = '\0';

            char pattern[38];
            if (strcmp(tmp_pattern, "all") == 0) {
                strncpy(pattern, "0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15", 38);
            }
            else if (strcmp(tmp_pattern, "none") == 0) {
                strncpy(pattern, "", 38);
            }
            else {
                strncpy(pattern, tmp_pattern, 38);
            }

            int filename_len = fmatch[2].rm_eo - fmatch[2].rm_so;
            char filename[filename_len + 1];
            strncpy(filename, trim_tok + fmatch[2].rm_so, filename_len);
            filename[filename_len] = '\0';

            if (strcmp(cmd_type, "play") == 0) {
                add_drum_char_pattern(mixr, filename, pattern);
            }
            else {
                int val = atoi(filename);
                int is_val_a_valid_sig_num =
                    (val >= 0 && val < mixr->soundgen_num) ? 1 : 0;
                if (is_val_a_valid_sig_num &&
                    mixr->sound_generators[val]->type == DRUM_TYPE) {
                    add_char_pattern(mixr->sound_generators[val], pattern);
                }
            }
        }

        // Byte beat
        regmatch_t bytematch[3];
        regex_t byte_rx;
        regcomp(&byte_rx, "^(byte) (([t[:punct:][:digit:] ]+))$",
                REG_EXTENDED | REG_ICASE);
        if (regexec(&byte_rx, trim_tok, 3, bytematch, 0) == 0) {
            int pattern_len = bytematch[2].rm_eo - bytematch[2].rm_so;
            char pattern[pattern_len + 1];
            strncpy(pattern, trim_tok + bytematch[2].rm_so, pattern_len);
            pattern[pattern_len] = '\0';
            printf("Byte pattern is %s\n", pattern);
            // TODO validate byte pattern
        }

        // nanosynth melody loop
        regmatch_t fmm_match[4];
        regex_t fmm_rx;
        regcomp(&fmm_rx,
                "^(melody|maddd) ([[:digit:]]+) ([[:alnum:][:space:]:#]+)$",
                REG_EXTENDED | REG_ICASE);
        if (regexec(&fmm_rx, trim_tok, 4, fmm_match, 0) == 0) {
            printf("MELODY MATCH\n");

            char cmd_type[10];
            int sig_num;
            sscanf(trim_tok, "%s %d", cmd_type, &sig_num);

            int pattern_len = fmm_match[3].rm_eo - fmm_match[3].rm_so;
            char pattern[pattern_len + 1];
            strncpy(pattern, trim_tok + fmm_match[3].rm_so, pattern_len);
            pattern[pattern_len] = '\0';
            printf("MELODY PATTERN %s\n", pattern);

            int is_val_a_valid_sig_num =
                (sig_num >= 0 && sig_num < mixr->soundgen_num) ? 1 : 0;
            if (is_val_a_valid_sig_num) {
                if (strncmp(cmd_type, "melody", 7) == 0) {
                    printf("First melody!\n");
                    // keys_start_melody_player(sig_num, pattern);
                }
                else {
                    printf("Maaaaaddd for it!\n");
                    // melody_loop *mloop = mloop_from_pattern(pattern);
                    // nanosynth_add_melody_loop(mixr->sound_generators[sig_num],
                    //                           mloop);
                }
            }
        }

        // loop sample play
        regmatch_t sfmatch[4];
        regex_t sfile_rx;
        regcomp(&sfile_rx, "^(sloop) ([.[:alnum:]]+) ([.[:digit:]]+)$",
                REG_EXTENDED | REG_ICASE);
        if (regexec(&sfile_rx, trim_tok, 4, sfmatch, 0) == 0) {

            printf("SLOOPY CMON!\n");
            int filename_len = sfmatch[2].rm_eo - sfmatch[2].rm_so;
            char filename[filename_len + 1];
            strncpy(filename, trim_tok + sfmatch[2].rm_so, filename_len);
            filename[filename_len] = '\0';

            printf("SLOOPY FILE DONE - DOING LEN!\n");
            int looplen_len = sfmatch[3].rm_eo - sfmatch[3].rm_so;
            char looplen_char[looplen_len + 1];
            strncpy(looplen_char, trim_tok + sfmatch[3].rm_so, looplen_len);
            looplen_char[looplen_len] = '\0';
            double looplen = atof(looplen_char);

            printf("LOOPYZZ %s %f\n", filename, looplen);
            SBMSG *msg = new_sbmsg();
            strncpy(msg->cmd, "timed_sig_start", 19);
            strncpy(msg->params, "sloop", 10);
            strncpy(msg->filename, filename, 99);
            msg->looplen = looplen;
            thrunner(msg);
        }

        // envelope pattern
        regmatch_t ematch[5];
        regex_t env_rx;
        regcomp(&env_rx, "^(env) ([[:digit:]]+) ([[:digit:]]+) ([[:digit:]]+)$",
                REG_EXTENDED | REG_ICASE);
        if (regexec(&env_rx, trim_tok, 5, ematch, 0) == 0) {
            printf("ENVELOPeeee!\n");

            double val1 = 0;
            double val2 = 0;
            double val3 = 0;
            char cmd_type[10];
            sscanf(trim_tok, "%s %lf %lf %lf", cmd_type, &val1, &val2, &val3);

            if (mixr->soundgen_num > val1) {
                // printf("ENV CALLED FOR! %s %.lf %.lf\n", cmd_type, val1,
                // val2);
                if (val3 >= 0 && val3 < 4) {
                    printf("Calling env with %lf %lf %lf\n", val1, val2, val3);
                    ENVSTREAM *e = new_envelope_stream(val2, val3);
                    add_envelope_soundgen(mixr->sound_generators[(int)val1], e);
                }
                else {
                    printf("Sorry, envelope type has to be between 0 and 3");
                }
            }
            else {
                printf("Oofft mate, you don't have enough sound_gens for "
                       "that..\n");
            }
        }

        regmatch_t sloop_add[5];
        regex_t sladd_rx;
        regcomp(&sladd_rx,
                "^(sladdd) ([[:digit:]]+) ([.[:alnum:]]+) ([[:digit:]]+)$",
                REG_EXTENDED | REG_ICASE);
        if (regexec(&sladd_rx, trim_tok, 5, sloop_add, 0) == 0) {
            printf("SAMPLER ADD SAMPLE!!zzzz\n");

            char cmd_type[10];
            int signum = 0;
            char filename[30];
            int loop_len = 0;
            sscanf(trim_tok, "%s %d %s %d", cmd_type, &signum, filename,
                   &loop_len);

            printf("%s %d %s %d\n", cmd_type, signum, filename, loop_len);
            int is_val_a_valid_sig_num =
                (signum >= 0 && signum < mixr->soundgen_num) ? 1 : 0;
            if (is_val_a_valid_sig_num &&
                mixr->sound_generators[signum]->type == SAMPLER_TYPE) {
                printf("BBBBBBOOO YEH!\n");

                sampler_add_sample((SAMPLER *)mixr->sound_generators[signum],
                                   filename, loop_len);
            }
        }
        regmatch_t bytebeat_cmd[3];
        regex_t bytebeat_rx;
        regcomp(&bytebeat_rx, "^(byte) (.*)$", REG_EXTENDED | REG_ICASE);
        if (regexec(&bytebeat_rx, trim_tok, 3, bytebeat_cmd, 0) == 0) {
            printf("BYTESZZZBEAT!\n");
            int bytebeat_char_len =
                bytebeat_cmd[2].rm_eo - bytebeat_cmd[2].rm_so;
            printf("bytebeat_char_len is %d\n", bytebeat_char_len);
            char bytebeat[bytebeat_char_len + 1];
            strncpy(bytebeat, trim_tok + bytebeat_cmd[2].rm_so,
                    bytebeat_char_len);
            bytebeat[bytebeat_char_len] = '\0';

            printf("Byte beat izzz %s\n", bytebeat);
            add_bytebeat(mixr, bytebeat);
        }
    }
}
Example #17
0
void getoptions()
{
  /* interactively set options */
  long inseed0=0, loopcount;
  Char ch;
  boolean done=false;

  putchar('\n');
  minev = false;
  global = false;
  jumble = false;
  njumble = 1;
  lengths = false;
  lower = false;
  negallowed = false;
  outgrno = 1;
  outgropt = false;
  power = 2.0;
  replicates = false;
  trout = true;
  upper = false;
  usertree = false;
  printdata = false;
  progress = true;
  treeprint = true;
  loopcount = 0;
  do {
    cleerhome();
    printf("\nFitch-Margoliash method version %s\n\n",VERSION);
    printf("Settings for this run:\n");
    printf("  D      Method (F-M, Minimum Evolution)?  %s\n",
             (minev ? "Minimum Evolution" : "Fitch-Margoliash"));
    printf("  U                 Search for best tree?  %s\n",
           (usertree ? "No, use user trees in input file" : "Yes"));
    if (usertree) {
      printf("  N          Use lengths from user trees?  %s\n",
             (lengths ? "Yes" : "No"));
    }
    printf("  P                                Power?%9.5f\n",power);
    printf("  -      Negative branch lengths allowed?  %s\n",
           negallowed ? "Yes" : "No");
    printf("  O                        Outgroup root?");
    if (outgropt)
      printf("  Yes, at species number%3ld\n", outgrno);
    else
      printf("  No, use as outgroup species%3ld\n", outgrno);
    printf("  L         Lower-triangular data matrix?");
    if (lower)
      printf("  Yes\n");
    else
      printf("  No\n");
    printf("  R         Upper-triangular data matrix?");
    if (upper)
      printf("  Yes\n");
    else
      printf("  No\n");
    printf("  S                        Subreplicates?");
    if (replicates)
      printf("  Yes\n");
    else
      printf("  No\n");
    if (!usertree) {
      printf("  G                Global rearrangements?");
      if (global)
        printf("  Yes\n");
      else
        printf("  No\n");
      printf("  J     Randomize input order of species?");
      if (jumble)
        printf("  Yes (seed =%8ld,%3ld times)\n", inseed0, njumble);
      else
        printf("  No. Use input order\n");
    }
    printf("  M           Analyze multiple data sets?");
    if (mulsets)
      printf("  Yes, %2ld sets\n", datasets);
    else
      printf("  No\n");
    printf("  0   Terminal type (IBM PC, ANSI, none)?");
    if (ibmpc)
      printf("  IBM PC\n");
    if (ansi)
      printf("  ANSI\n");
    if (!(ibmpc || ansi))
      printf("  (none)\n");
    printf("  1    Print out the data at start of run");
    if (printdata)
      printf("  Yes\n");
    else
      printf("  No\n");
    printf("  2  Print indications of progress of run");
    if (progress)
      printf("  Yes\n");
    else
      printf("  No\n");
    printf("  3                        Print out tree");
    if (treeprint)
      printf("  Yes\n");
    else
      printf("  No\n");
    printf("  4       Write out trees onto tree file?");
    if (trout)
      printf("  Yes\n");
    else
      printf("  No\n");
    printf(
   "\n  Y to accept these or type the letter for one to change\n");
#ifdef WIN32
    phyFillScreenColor();
#endif
    scanf("%c%*[^\n]", &ch);
    getchar();
    uppercase(&ch);
    done = (ch == 'Y');
   if (!done) {
      if (strchr("DJOUNPG-LRSM01234",ch) != NULL) {
        switch (ch) {

        case 'D':
          minev = !minev;
          if (minev && (!negallowed))
            negallowed = true;
          break;

        case '-':
          negallowed = !negallowed;
          break;

        case 'G':
          global = !global;
          break;

        case 'J':
          jumble = !jumble;
          if (jumble)
            initjumble(&inseed, &inseed0, seed, &njumble);
          else njumble = 1;
          break;

        case 'L':
          lower = !lower;
          break;

         case 'N':
           lengths = !lengths;
           break;

        case 'O':
          outgropt = !outgropt;
          if (outgropt)
            initoutgroup(&outgrno, spp);
          break;

        case 'P':
          initpower(&power);
          break;

        case 'R':
          upper = !upper;
          break;

        case 'S':
          replicates = !replicates;
          break;

        case 'U':
          usertree = !usertree;
          break;

        case 'M':
          mulsets = !mulsets;
          if (mulsets)
            initdatasets(&datasets);
          jumble = true;
          if (jumble)
            initseed(&inseed, &inseed0, seed);
          break;

        case '0':
          initterminal(&ibmpc, &ansi);
          break;

        case '1':
          printdata = !printdata;
          break;

        case '2':
          progress = !progress;
          break;

        case '3':
          treeprint = !treeprint;
          break;

        case '4':
          trout = !trout;
          break;
        }
      } else
        printf("Not a possible option!\n");
    }
    countup(&loopcount, 100);
  } while (!done);
  if (lower && upper) {
    printf("ERROR: Data matrix cannot be both uppeR and Lower triangular\n");
    exxit(-1);
  }
}  /* getoptions */
Example #18
0
void makev(long m, long n, double *v)
{
  /* compute one distance */
  long i, ii, it, numerator, denominator;
  double f, g=0, h, p1, p2, p3, q1, pp, tt, delta, vv;

  numerator = 0;
  denominator = 0;
  for (i = 0; i < endsite; i++) {
    ii = alias[i + 1];
    if ((y[m-1][ii-1] == '+') || 
        (y[n-1][ii-1] == '+')) {
      denominator += weight[i + 1];
      if ((y[m-1][ii-1] == '+') && (y[n-1][ii-1] == '+')) {
        numerator += weight[i + 1];
      }
    }
  }
  f = 2*numerator/(double)(denominator+numerator);
  if (restsites) {
    if (exp(-sitelength*1.38629436) > f) {
      printf("\nERROR: Infinite distance between ");
      printf(" species %3ld and %3ld\n", m, n);
      exxit(-1);
    }
  }
  if (!restsites) {
    if (!neili) {
      f = (sqrt(f*(f+8.0))-f)/2.0;
    }
    else {
      g = initialv;
      delta = g;
      it = 0;
      while (fabs(delta) > 0.00002 && it < iterationsr) {
        it++;
        h = g;
        g = exp(0.25*log(f * (3-2*g)));
        delta = g - h;
      }
    }
  }
  if ((!restsites) && neili)
    vv = - (2.0/sitelength) * log(g);
  else {
    if (neili && restsites) {
      pp = exp(log(f)/(2*sitelength));
      vv = -(3.0/2.0)*log((4.0/3.0)*pp - (1.0/3.0));      
    } else {
      pp = exp(log(f)/sitelength); 
      delta = initialv;
      tt = delta;
      it = 0;
      while (fabs(delta) > 0.000001 && it < iterationsr) {
        it++;
        if (gama) {
          p1 = exp(-cvi * log(1 + tt / cvi));
          p2 = exp(-cvi * log(1 + xv * tt / cvi))
            - exp(-cvi * log(1 + tt / cvi));
          p3 = 1.0 - exp(-cvi * log(1 + xv * tt / cvi));
        } else {
          p1 = exp(-tt);
          p2 = exp(-xv * tt) - exp(-tt);
          p3 = 1.0 - exp(-xv * tt);
        }
        q1 = p1 + p2 / 2.0 + p3 / 4.0;
        g = q1 - pp;
        if (g < 0.0)
          delta = fabs(delta) / -2.0;
        else
          delta = fabs(delta);
        tt += delta;
      }
      vv = fracchange * tt;
    }
  }
  *v = fabs(vv);
}  /* makev */
Example #19
0
void inputdata()
{ /* read species data */
  long i, j, k, l, m, m0, n, p;
  double sum;

  if (printdata) {
    fprintf(outfile, "\nName");
    if (contchars)
      fprintf(outfile, "                       Phenotypes\n");
    else
      fprintf(outfile, "                 Gene Frequencies\n");
    fprintf(outfile, "----");
    if (contchars)
      fprintf(outfile, "                       ----------\n");
    else
      fprintf(outfile, "                 ---- -----------\n");
    putc('\n', outfile);
    if (!contchars) {
      for (j = 1; j <= nmlngth - 8; j++)
        putc(' ', outfile);
      fprintf(outfile, "locus:");
      p = 1;
      for (j = 1; j <= loci; j++) {
        if (all)
          n = alleles[j - 1];
        else
          n = alleles[j - 1] - 1;
        for (k = 1; k <= n; k++) {
          fprintf(outfile, "%10ld", j);
          if (p % 6 == 0 && (all || p < df)) {
            putc('\n', outfile);
            for (l = 1; l <= nmlngth - 2; l++)
              putc(' ', outfile);
          }
          p++;
        }
      }
      fprintf(outfile, "\n\n");
    }
  }
  for (i = 0; i < spp; i++) {
    scan_eoln(infile);
    initname(i);
    if (printdata)
      for (j = 0; j < nmlngth; j++)
        putc(nayme[i][j], outfile);
    m = 1;
    p = 1;
    for (j = 1; j <= loci; j++) {
      m0 = m;
      sum = 0.0;
      if (contchars)
        n = 1;
      else if (all)
        n = alleles[j - 1];
      else
        n = alleles[j - 1] - 1;
      for (k = 1; k <= n; k++) {
        if (eoln(infile)) 
          scan_eoln(infile);
        if (fscanf(infile, "%lf", &x[i][m - 1]) != 1) {
          printf("ERROR: unable to read allele frequency"
              "for species %ld, locus %ld\n", i+1, j);
          exxit(-1);
        }
        sum += x[i][m - 1];
        if (!contchars && x[i][m - 1] < 0.0) {
          printf("\n\nERROR: locus %ld in species %ld: an allele", j, i+1);
          printf(" frequency is negative\n");
          exxit(-1);
        }
        if (printdata) {
          fprintf(outfile, "%10.5f", x[i][m - 1]);
          if (p % 6 == 0 && (all || p < df)) {
            putc('\n', outfile);
            for (l = 1; l <= nmlngth; l++)
              putc(' ', outfile);
          }
        }
        p++;
        m++;
      }
      if (all && !contchars) {
        if (fabs(sum - 1.0) > epsilon2) {
          printf(
      "\n\nERROR: Locus %ld in species %ld: frequencies do not add up to 1\n",
                  j, i + 1);
          printf("\nFrequencies are:\n");
          for (l = m0; l <= m-3; l++)
            printf("%f+", x[i][l]);
          printf("%f = %f\n\n", x[i][m-2], sum);
          exxit(-1);
        } else {
          for (l = 0; l <= m-2; l++)
            x[i][l] /= sum;
        }
      }
      if (!all && !contchars) {
        x[i][m-1] = 1.0 - sum;
        if (x[i][m-1] < 0.0) {
          if (x[i][m-1] > -epsilon2) {
            for (l = 0; l <= m-2; l++)
              x[i][l] /= sum;
            x[i][m-1] = 0.0;
          } else {
            printf("\n\nERROR: Locus %ld in species %ld: ", j, i + 1);
            printf("frequencies add up to more than 1\n");
            printf("\nFrequencies are:\n");
            for (l = m0-1; l <= m-3; l++)
              printf("%f+", x[i][l]);
            printf("%f = %f\n\n", x[i][m-2], sum);
            exxit(-1);
          }
        }
        m++;
      }
    }
    if (printdata)
      putc('\n', outfile);
  }
  scan_eoln(infile);
  if (printdata)
    putc('\n', outfile);
}  /* inputdata */
Example #20
0
void makev(long m, long n, double *v)
{
  /* compute one distance */
  long i, ii, it, numerator, denominator;
  double f, g=0, h, b, c, p1, p2, p3, q1, pp, tt, delta, vv, slope;

  numerator = 0;
  denominator = 0;
  for (i = 0; i < endsite; i++) {
    ii = alias[i];
    if ((y[m-1][ii-1] == '+') || (y[n-1][ii-1] == '+')) {
      denominator += weight[i];
      if ((y[m-1][ii-1] == '+') && (y[n-1][ii-1] == '+')) {
        numerator += weight[i];
      }
    }
  }
  f = 2*numerator/(double)(denominator+numerator);
  if (restsites) {
    if (exp(-sitelength*1.38629436) > f) {
      printf("\nWARNING: INFINITE DISTANCE BETWEEN ");
      printf(" SPECIES %3ld AND %3ld\n", m, n);
      exxit(-1);
    }
  }
  if (!restsites) {
    if (!neili) {
      f = (sqrt(f*(f+8.0))-f)/2.0;
    }
    else {
      g = initialv;
      h = g;
      delta = g;
      it = 0;
      while (fabs(delta) > 0.00002 && it < iterationsr) {
        it++;
        h = g;
        g = exp(0.25*log(f * (3-2*g)));
        delta = g - h;
      }
    }
  }
  if ((!restsites) && neili)
    vv = - (2.0/sitelength) * log(g);
  else {
    pp = exp(log(f)/sitelength);
    delta = initialv;
    tt = delta;
    it = 0;
    while (fabs(delta) > 0.00002 && it < iterationsr) {
      it++;
      if (gama) {
        p1 = exp(-cvi * log(1 + tt / cvi));
        p2 = exp(-cvi * log(1 + xv * tt / cvi))
              - exp(-cvi * log(1 + tt / cvi));
        p3 = 1.0 - exp(-cvi * log(1 + xv * tt / cvi));
      } else {
        p1 = exp(-tt);
        p2 = exp(-xv * tt) - exp(-tt);
        p3 = 1.0 - exp(-xv * tt);
      }
      q1 = p1 + p2 / 2.0 + p3 / 4.0;
      g = q1 - pp;
      if (gama)
        slope = - 0.5 * (1 / (1 + tt / cvi)) * exp(-cvi * log(1 + tt / cvi))
                  - 0.25 * (xv / (1 + xv * tt / cvi)) *
                  exp(-cvi * log(1 + xv * tt / cvi));
      else
        slope = - 0.5*exp(-tt) - 0.25*exp(-xv*tt);
      if (g < 0.0)
        delta = fabs(delta) / -2.0;
      else
        delta = fabs(delta);
      tt += delta;
    }
    vv = fracchange * tt;
  }
  *v = vv;
}  /* makev */
Example #21
0
void restdist_inputdata()
{
  /* read the species and sites data */
  long i, j, k, l, sitesread = 0, sitesnew = 0;
  Char ch;
  boolean allread, done;

  if (printdata)
    putc('\n', outfile);
  j = nmlngth + (sites + (sites - 1) / 10) / 2 - 5;
  if (j < nmlngth - 1)
    j = nmlngth - 1;
  if (j > 39)
    j = 39;
  if (printdata) {
    fprintf(outfile, "Name");
    for (i = 1; i <= j; i++)
      putc(' ', outfile);
    fprintf(outfile, "Sites\n");
    fprintf(outfile, "----");
    for (i = 1; i <= j; i++)
      putc(' ', outfile);
    fprintf(outfile, "-----\n\n");
  }
  sitesread = 0;
  allread = false;
  while (!(allread)) {
    allread = true;
    if (eoln(infile)) {
      fscanf(infile, "%*[^\n]");
      getc(infile);
    }
    i = 1;
    while (i <= spp ) {
      if ((interleaved && sitesread == 0) || !interleaved)
        initname(i - 1);
      if (interleaved)
        j = sitesread;
      else
        j = 0;
      done = false;
      while (!done && !eoff(infile)) {
        if (interleaved)
          done = true;
        while (j < sites && !(eoln(infile) || eoff(infile))) {
          ch = getc(infile);
          if (ch == '\n')
            ch = ' ';
          if (ch == ' ')
            continue;
          uppercase(&ch);
          if (ch != '1' && ch != '0' && ch != '+' && ch != '-' && ch != '?') {
            printf(" WARNING -- BAD SYMBOL %c",ch);
            printf(" AT POSITION %5ld OF SPECIES %3ld\n",j,i);
            exxit(-1);
          }
          if (ch == '1')
            ch = '+';
          if (ch == '0')
            ch = '-';
          j++;
          y[i - 1][j - 1] = ch;
        }
        if (interleaved)
          continue;
        if (j < sites) {
          fscanf(infile, "%*[^\n]");
          getc(infile);
        } else if (j == sites)
          done = true;
      }
      if (interleaved && i == 1)
        sitesnew = j;
      fscanf(infile, "%*[^\n]");
      getc(infile);
      if ((interleaved && j != sitesnew ) || ((!interleaved) && j != sites)){
        printf("ERROR: SEQUENCES OUT OF ALIGNMENT\n");
        exxit(-1);}
      i++;
    }
    if (interleaved) {
      sitesread = sitesnew;
      allread = (sitesread == sites);
    } else
      allread = (i > spp);
  }
  if (printdata) {
    for (i = 1; i <= ((sites - 1) / 60 + 1); i++) {
      for (j = 0; j < spp; j++) {
        for (k = 0; k < nmlngth; k++)
          putc(nayme[j][k], outfile);
        fprintf(outfile, "   ");
        l = i * 60;
        if (l > sites)
          l = sites;
        for (k = (i - 1) * 60 + 1; k <= l; k++) {
          putc(y[j][k - 1], outfile);
          if (k % 10 == 0 && k % 60 != 0)
            putc(' ', outfile);
        }
        putc('\n', outfile);
      }
      putc('\n', outfile);
    }
    putc('\n', outfile);
  }
}  /* restdist_inputdata */
Example #22
0
void seqboot_inputdata()
{
  /* input the names and sequences for each species */
  long i, j, k, l, m, n, basesread, basesnew=0;
  double x;
  Char charstate;
  boolean allread, done;

  if (data == genefreqs) {
    nodef = matrix_double_new(spp, sites);
  } else {
    nodep = matrix_char_new(spp, sites);
  }
  j = nmlngth + (sites + (sites - 1) / 10) / 2 - 5;
  if (j < nmlngth - 1)
    j = nmlngth - 1;
  if (j > 37)
    j = 37;
  if (printdata) {
    fprintf(outfile, "\nBootstrapping algorithm, version %s\n\n\n",VERSION);
    if (bootstrap)  {
      if (blocksize > 1) {
        if (regular)      
      fprintf(outfile, "Block-bootstrap with block size %ld\n\n", blocksize);
        else
          fprintf(outfile, "Partial (%2.0f%%) block-bootstrap with block size %ld\n\n",
                  100*fracsample, blocksize);
      } else {
        if (regular)
          fprintf(outfile, "Bootstrap\n\n");
        else 
          fprintf(outfile, "Partial (%2.0f%%) bootstrap\n\n", 100*fracsample);
      }
    } else {
      if (jackknife) {
        if (regular)
          fprintf(outfile, "Delete-half Jackknife\n\n");
        else
    fprintf(outfile, "Delete-%2.0f%% Jackknife\n\n", 100*(1.0-fracsample));
      } else {
        if (permute) {
          fprintf(outfile, "Species order permuted separately for each");
          if (data == genefreqs)
            fprintf(outfile, " locus\n\n");
          if (data == seqs)
            fprintf(outfile, " site\n\n");
          if (data == morphology)
            fprintf(outfile, " character\n\n");
          if (data == restsites)
            fprintf(outfile, " site\n\n");
        }
        else {
          if (ild) {
            if (data == genefreqs)
              fprintf(outfile, "Locus");
            if (data == seqs)
              fprintf(outfile, "Site");
            if (data == morphology)
              fprintf(outfile, "Character");
            if (data == restsites)
              fprintf(outfile, "Site");
            fprintf(outfile, " order permuted\n\n");
          } else {
            if (lockhart)
              if (data == genefreqs)
                fprintf(outfile, "Locus");
              if (data == seqs)
                fprintf(outfile, "Site");
              if (data == morphology)
                fprintf(outfile, "Character");
              if (data == restsites)
                fprintf(outfile, "Site");
         fprintf(outfile, " order permuted separately for each species\n\n");
          }
        }
      }
    }
    if (data == genefreqs)
      fprintf(outfile, "%3ld species, %3ld  loci\n\n", spp, loci);
    else {
      fprintf(outfile, "%3ld species, ", spp);
      if (data == seqs)
        fprintf(outfile, "%3ld  sites\n\n", sites);
        else if (data == morphology)
          fprintf(outfile, "%3ld  characters\n\n", sites);
          else if (data == restsites)
            fprintf(outfile, "%3ld  sites\n\n", sites);
    }
    fprintf(outfile, "Name");
    for (i = 1; i <= j; i++)
      putc(' ', outfile);
    fprintf(outfile, "Data\n");
    fprintf(outfile, "----");
    for (i = 1; i <= j; i++)
      putc(' ', outfile);
    fprintf(outfile, "----\n\n");
  }
  interleaved = (interleaved && ((data == seqs) || (data == restsites)));
  if (data == genefreqs) {
    for (i = 1; i <= (spp); i++) {
      initname(i - 1);
      j = 1;
      while (j <= sites && !eoff(infile)) {
        if (eoln(infile)) 
          scan_eoln(infile);
        if ( fscanf(infile, "%lf", &x) != 1) {
          printf("ERROR: Invalid value for locus %ld of species %ld\n", j, i);
          exxit(-1);
        } else if ((unsigned)x > 1.0) {
          printf("GENE FREQ OUTSIDE [0,1] in species %ld\n", i);
          exxit(-1);
        } else {
          nodef[i - 1][j - 1] = x;
          j++;
        }
      }
      scan_eoln(infile);
    }
    return;
  }
  basesread = 0;
  allread = false;
  while (!allread) {
    /* eat white space -- if the separator line has spaces on it*/
    do {
      charstate = gettc(infile);
    } while (charstate == ' ' || charstate == '\t');
    ungetc(charstate, infile);
    if (eoln(infile)) 
      scan_eoln(infile);
    i = 1;
    while (i <= spp) {
      if ((interleaved && basesread == 0) || !interleaved)
        initname(i-1);
      j = interleaved ? basesread : 0;
      done = false;
      while (!done && !eoff(infile)) {
        if (interleaved)
          done = true;
        while (j < sites && !(eoln(infile) ||eoff(infile))) {
          charstate = gettc(infile);
          if (charstate == '\n' || charstate == '\t')
            charstate = ' ';
          if (charstate == ' ' ||
              (data == seqs && charstate >= '0' && charstate <= '9'))
            continue;
          uppercase(&charstate);
          j++;
          if (charstate == '.')
            charstate = nodep[0][j-1];
          nodep[i-1][j-1] = charstate;
        }
        if (interleaved)
          continue;
        if (j < sites) 
          scan_eoln(infile);
        else if (j == sites)
          done = true;
      }
      if (interleaved && i == 1)
        basesnew = j;
      scan_eoln(infile);
      if ((interleaved && j != basesnew) || ((!interleaved) && j != sites)){
        printf("\n\nERROR: sequences out of alignment at site %ld", j+1);
        printf(" of species %ld\n\n", i);
        exxit(-1);}
      i++;
    }
    if (interleaved) {
      basesread = basesnew;
      allread = (basesread == sites);
    } else
      allread = (i > spp);
  }
  if (!printdata)
    return;
  if (data == genefreqs)
    m = (sites - 1) / 8 + 1;
  else
    m = (sites - 1) / 60 + 1;
  for (i = 1; i <= m; i++) {
    for (j = 0; j < spp; j++) {
      for (k = 0; k < nmlngth; k++)
        putc(nayme[j][k], outfile);
      fprintf(outfile, "   ");
      if (data == genefreqs)
        l = i * 8;
      else
        l = i * 60;
      if (l > sites)
        l = sites;
      if (data == genefreqs)
        n = (i - 1) * 8;
      else
        n = (i - 1) * 60;
      for (k = n; k < l; k++) {
        if (data == genefreqs)
          fprintf(outfile, "%8.5f", nodef[j][k]);
        else {
          if (j + 1 > 1 && nodep[j][k] == nodep[0][k])
            charstate = '.';
          else
            charstate = nodep[j][k];
          putc(charstate, outfile);
          if ((k + 1) % 10 == 0 && (k + 1) % 60 != 0)
            putc(' ', outfile);
        
        }
      }
      putc('\n', outfile);
    }
    putc('\n', outfile);
  }
  putc('\n', outfile);
}  /* seqboot_inputdata */
Example #23
0
void restdist_inputdata()
{
  /* read the species and sites data */
  long i, j, k, l, sitesread = 0, sitesnew = 0;
  Char ch;
  boolean allread, done;

  if (printdata)
    putc('\n', outfile);
  j = nmlngth + (sites + (sites - 1) / 10) / 2 - 5;
  if (j < nmlngth - 1)
    j = nmlngth - 1;
  if (j > 39)
    j = 39;
  if (printdata) {
    fprintf(outfile, "Name");
    for (i = 1; i <= j; i++)
      putc(' ', outfile);
    fprintf(outfile, "Sites\n");
    fprintf(outfile, "----");
    for (i = 1; i <= j; i++)
      putc(' ', outfile);
    fprintf(outfile, "-----\n\n");
  }
  sitesread = 0;
  allread = false;
  while (!(allread)) {
    /* eat white space -- if the separator line has spaces on it*/
    do {
      ch = gettc(infile);
    } while (ch == ' ' || ch == '\t');
    ungetc(ch, infile);
    if (eoln(infile))
      scan_eoln(infile);
    i = 1;
    while (i <= spp ) {
      if ((interleaved && sitesread == 0) || !interleaved)
        initname(i - 1);
      if (interleaved)
        j = sitesread;
      else
        j = 0;
      done = false;
      while (!done && !eoff(infile)) {
        if (interleaved)
          done = true;
        while (j < sites && !(eoln(infile) || eoff(infile))) {
          ch = gettc(infile);
          if (ch == '\n' || ch == '\t')
            ch = ' ';
          if (ch == ' ')
            continue;
          uppercase(&ch);
          if (ch != '1' && ch != '0' && ch != '+' && ch != '-' && ch != '?') {
            printf(" ERROR -- Bad symbol %c",ch);
            printf(" at position %ld of species %ld\n", j+1, i);
            exxit(-1);
          }
          if (ch == '1')
            ch = '+';
          if (ch == '0')
            ch = '-';
          j++;
          y[i - 1][j - 1] = ch;
        }
        if (interleaved)
          continue;
        if (j < sites) 
          scan_eoln(infile);
        else if (j == sites)
          done = true;
      }
      if (interleaved && i == 1)
        sitesnew = j;
      scan_eoln(infile);
      if ((interleaved && j != sitesnew ) || ((!interleaved) && j != sites)){
        printf("ERROR: SEQUENCES OUT OF ALIGNMENT\n");
        exxit(-1);}
      i++;
    }
    if (interleaved) {
      sitesread = sitesnew;
      allread = (sitesread == sites);
    } else
      allread = (i > spp);
  }
  if (printdata) {
    for (i = 1; i <= ((sites - 1) / 60 + 1); i++) {
      for (j = 0; j < spp; j++) {
        for (k = 0; k < nmlngth; k++)
          putc(nayme[j][k], outfile);
        fprintf(outfile, "   ");
        l = i * 60;
        if (l > sites)
          l = sites;
        for (k = (i - 1) * 60 + 1; k <= l; k++) {
          putc(y[j][k - 1], outfile);
          if (k % 10 == 0 && k % 60 != 0)
            putc(' ', outfile);
        }
        putc('\n', outfile);
      }
      putc('\n', outfile);
    }
    putc('\n', outfile);
  }
}  /* restdist_inputdata */