source: tags/initial/GDE/PHYLIP/dollop.c

Last change on this file was 2, checked in by oldcode, 24 years ago

Initial revision

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 49.7 KB
Line 
1#include "phylip.h"
2
3/* version 3.52c. (c) Copyright 1993 by Joseph Felsenstein.
4   Written by Joseph Felsenstein, Akiko Fuseki, Sean Lamont, and Andrew Keeffe.
5   Permission is granted to copy and use this program provided no fee is
6   charged for it and provided that this copyright notice is not removed. */
7
8#define nmlngth         10   /* number of characters in species name    */
9#define maxtrees        100  /* maximum number of tied trees stored     */
10#define maxuser         10   /* maximum number of user-defined trees    */
11
12#define ibmpc0          false
13#define ansi0           true
14#define vt520           false
15#define down            2
16
17typedef long *bitptr;
18
19/* nodes will form a binary tree */
20
21typedef struct node {
22                              /* describes a tip species or an ancestor */
23  struct node *next, *back;   /* pointers to nodes                      */
24  long index;                /* number of the node                     */
25  boolean tip;                /* present species are tips of tree       */
26  bitptr stateone, statezero; /* see in PROCEDURE fillin                */
27  long xcoord, ycoord, ymin; /* used by printree                       */
28  long ymax;
29} node;
30
31typedef long *steptr;
32typedef node **pointptr;
33typedef long longer[6];
34
35typedef struct gbit {
36  bitptr bits_;
37  struct gbit *next;
38} gbit;
39
40Static node *root;
41Static FILE *infile, *outfile, *treefile;
42Static long spp, nonodes, chars, wrds, words, inseed, col, datasets, ith, j,
43  l, jumb, njumble;
44/* spp = number of species
45   nonodes = number of nodes in tree
46   chars = number of binary characters
47   words = number of words needed for characters of one organism */
48Static boolean jumble, usertree, weights, thresh, ancvar, questions, dollo,
49               trout,  printdata, progress, treeprint, stepbox,
50               ancseq, mulsets, firstset, ibmpc, vt52, ansi;
51Static steptr extras, weight;
52Static boolean *ancone, *anczero, *ancone0, *anczero0;
53Static pointptr treenode;   /* pointers to all nodes in tree */
54Static Char **nayme;   /* names of species */
55Static double threshold;
56Static double *threshwt;
57Static longer seed;
58Static long *enterorder;
59Static double **fsteps;
60Static steptr numsteps;
61Static long **bestrees;
62Static Char *guess;
63Static gbit *garbage;
64Static long bits = 8*sizeof(long) - 1;
65
66/* Local variables for maketree, propagated globally for C version: */
67long nextree, which, minwhich;
68double like, bestyet, bestlike, bstlike2, minsteps;
69boolean lastrearr;
70double nsteps[maxuser];
71node *there;
72long fullset;
73bitptr zeroanc, oneanc;
74long *place;
75Char ch;
76boolean *naymes;
77steptr numsone, numszero;
78bitptr steps;
79
80
81openfile(fp,filename,mode,application,perm)
82FILE **fp;
83char *filename;
84char *mode;
85char *application;
86char *perm;
87{
88  FILE *of;
89  char file[100];
90  strcpy(file,filename);
91  while (1){
92    of = fopen(file,mode);
93    if (of)
94      break;
95    else {
96      switch (*mode){
97      case 'r':
98        printf("%s:  can't read %s\n",application,file);
99        file[0] = '\0';
100        while (file[0] =='\0'){
101          printf("Please enter a new filename>");
102          gets(file);}
103        break;
104      case 'w':
105      case 'a':
106        printf("%s: can't write %s\n",application,file);
107        file[0] = '\0';
108        while (file[0] =='\0'){
109          printf("Please enter a new filename>");
110          gets(file);}
111        break;
112      }
113    }
114  }
115  *fp=of;
116
117  if (perm != NULL)
118    strcpy(perm,file);
119}
120
121
122void gnu(p)
123gbit **p;
124{
125  /* this and the following are do-it-yourself garbage collectors.
126     Make a new node or pull one off the garbage list */
127  if (garbage != NULL) {
128    *p = garbage;
129    garbage = garbage->next;
130  } else {
131    *p = (gbit *)Malloc(sizeof(gbit));
132    (*p)->bits_ = (bitptr)Malloc(words*sizeof(long));
133  }
134  (*p)->next = NULL;
135}  /* gnu */
136
137
138void chuck(p)
139gbit *p;
140{
141  /* collect garbage on p -- put it on front of garbage list */
142  p->next = garbage;
143  garbage = p;
144}  /* chuck */
145
146double randum(seed)
147long *seed;
148{
149  /* random number generator -- slow but machine independent */
150  long i, j, k, sum;
151  longer mult, newseed;
152  double x;
153
154  mult[0] = 13;
155  mult[1] = 24;
156  mult[2] = 22;
157  mult[3] = 6;
158  for (i = 0; i <= 5; i++)
159    newseed[i] = 0;
160  for (i = 0; i <= 5; i++) {
161    sum = newseed[i];
162    k = i;
163    if (i > 3)
164      k = 3;
165    for (j = 0; j <= k; j++)
166      sum += mult[j] * seed[i - j];
167    newseed[i] = sum;
168    for (j = i; j <= 4; j++) {
169      newseed[j + 1] += newseed[j] / 64;
170      newseed[j] &= 63;
171    }
172  }
173  memcpy(seed, newseed, sizeof(longer));
174  seed[5] &= 3;
175  x = 0.0;
176  for (i = 0; i <= 5; i++)
177    x = x / 64.0 + seed[i];
178  x /= 4.0;
179  return x;
180}  /* randum */
181
182
183
184void uppercase(ch)
185Char *ch;
186{  /* convert a character to upper case -- either ASCII or EBCDIC */
187  *ch = (islower (*ch) ? toupper(*ch) : (*ch));
188}  /* uppercase */
189
190void newline(i, j, k)
191long i, j, k;
192{
193  /* go to new line if i is a multiple of j, indent k spaces */
194  long m;
195
196  if ((i - 1) % j != 0 || i <= 1)
197    return;
198  putchar('\n');
199  for (m = 1; m <= k; m++)
200    putchar(' ');
201}  /* newline */
202
203
204void getoptions()
205{
206  /* interactively set options */
207  long i, inseed0;
208  Char ch;
209  boolean  done1;
210
211  fprintf(outfile,"\nDollo and polymorphism parsimony algorithm,");
212  fprintf(outfile," version %s\n\n",VERSION);
213  putchar('\n');
214  ancvar = false;
215  dollo = true;
216  jumble = false;
217  njumble = 1;
218  thresh = false;
219  threshold = spp;
220  trout = true;
221  usertree = false;
222  weights = false;
223  printdata = false;
224  progress = true;
225  treeprint = true;
226  stepbox = false;
227  ancseq = false;
228  for (;;) {
229    printf(ansi ? "\033[2J\033[H" :
230           vt52 ? "\033E\033H"    : "\n");
231    printf("\nDollo and polymorphism parsimony algorithm, version %s\n\n",
232           VERSION);
233    printf("Settings for this run:\n");
234    printf("  U                 Search for best tree?  %s\n",
235           (usertree ? "No, use user trees in input file" : "Yes"));
236    printf("  P                     Parsimony method?  %s\n",
237           dollo ? "Dollo" : "Polymorphism");
238    if (!usertree) {
239      printf("  J     Randomize input order of species?");
240      if (jumble)
241        printf("  Yes (seed =%8ld,%3ld times)\n", inseed0, njumble);
242      else
243        printf("  No. Use input order\n");
244    }
245    printf("  T              Use Threshold parsimony?");
246    if (thresh)
247      printf("  Yes, count steps up to%4.1f per char.\n", threshold);
248    else
249      printf("  No, use ordinary parsimony\n");
250    printf("  A   Use ancestral states in input file?  %s\n",
251           ancvar ? "Yes" : "No");
252    printf("  M           Analyze multiple data sets?");
253    if (mulsets)
254      printf("  Yes, %2ld sets\n", datasets);
255    else
256      printf("  No\n");
257    printf("  0   Terminal type (IBM PC, VT52, ANSI)?  %s\n",
258           ibmpc ? "IBM PC" :
259           ansi  ? "ANSI"   :
260           vt52  ? "VT52"   : "(none)");
261
262    printf("  1    Print out the data at start of run  %s\n",
263           printdata ? "Yes" : "No");
264    printf("  2  Print indications of progress of run  %s\n",
265           progress ? "Yes" : "No");
266    printf("  3                        Print out tree  %s\n",
267           treeprint ? "Yes" : "No");
268    printf("  4     Print out steps in each character  %s\n",
269           stepbox ? "Yes" : "No");
270    printf("  5     Print states at all nodes of tree  %s\n",
271           ancseq ? "Yes" : "No");
272    printf("  6       Write out trees onto tree file?  %s\n",
273           trout ? "Yes" : "No");
274    printf("\nAre these settings correct? ");
275    printf("(type Y or the letter for one to change)\n");
276    scanf("%c%*[^\n]", &ch);
277    getchar();
278    uppercase(&ch);
279    if (ch == 'Y')
280      break;
281    if (strchr("APJTUM1234560",ch) != NULL){
282      switch (ch) {
283       
284      case 'A':
285        ancvar = !ancvar;
286        break;
287       
288      case 'P':
289        dollo = !dollo;
290        break;
291       
292      case 'J':
293        jumble = !jumble;
294        if (jumble) {
295          do {
296            printf("Random number seed (must be odd)?\n");
297            scanf("%ld%*[^\n]", &inseed);
298            getchar();
299          } while (!(inseed & 1));
300          inseed0 = inseed;
301          for (i = 0; i <= 5; i++)
302            seed[i] = 0;
303          i = 0;
304          do {
305            seed[i] = inseed & 63;
306            inseed /= 64;
307            i++;
308          } while (inseed != 0);
309          printf("Number of times to jumble?\n");
310          scanf("%ld%*[^\n]", &njumble);
311          getchar();
312        }
313        else njumble = 1;
314        break;
315       
316      case 'T':
317        thresh = !thresh;
318        if (thresh) {
319          done1 = false;
320          do {
321            printf("What will be the threshold value?\n");
322            scanf("%lf%*[^\n]", &threshold);
323            getchar();
324            done1 = (threshold >= 1.0);
325            if (!done1)
326              printf("BAD THRESHOLD VALUE:  it must be greater than 1\n");
327            else
328              threshold = (long)(threshold * 10.0 + 0.5) / 10.0;
329          } while (done1 != true);
330        }
331        break;
332       
333      case 'U':
334        usertree = !usertree;
335        break;
336       
337      case 'M':
338        mulsets = !mulsets;
339        if (mulsets) {
340          done1 = false;
341          do {
342            printf("How many data sets?\n");
343            scanf("%ld%*[^\n]", &datasets);
344            getchar();
345            done1 = (datasets >= 1);
346            if (!done1)
347              printf("BAD DATA SETS NUMBER:  it must be greater than 1\n");
348          } while (done1 != true);
349        }
350        break;
351       
352      case '0':
353        if (ibmpc) {
354          ibmpc = false;
355          vt52 = true;
356        } else {
357          if (vt52) {
358            vt52 = false;
359            ansi = true;
360          } else if (ansi)
361            ansi = false;
362          else
363            ibmpc = true;
364        }
365        break;
366       
367      case '1':
368        printdata = !printdata;
369        break;
370       
371      case '2':
372        progress = !progress;
373        break;
374       
375      case '3':
376        treeprint = !treeprint;
377        break;
378       
379      case '4':
380        stepbox = !stepbox;
381        break;
382       
383      case '5':
384        ancseq = !ancseq;
385        break;
386       
387      case '6':
388        trout = !trout;
389        break;
390      }
391    } else
392      printf("Not a possible option!\n");
393  }
394}  /* getoptions */
395
396void inputnumbers()
397{
398  /* input the numbers of species and of characters */
399  fscanf(infile, "%ld%ld", &spp, &chars);
400  if (printdata)
401    fprintf(outfile, "%2ld species, %3ld  characters\n", spp, chars);
402  if (printdata)
403    putc('\n', outfile);
404  words = chars / bits + 1;
405  nonodes = spp * 2 - 1;
406}  /* inputnumbers */
407
408
409void doinit()
410{
411  /* initializes variables */
412  long i;
413  node *p, *q;
414
415  inputnumbers();
416  getoptions();
417  treenode = (pointptr)Malloc(nonodes*sizeof(node *));
418  for (i = 0; i < (spp); i++) {
419    treenode[i] = (node *)Malloc(sizeof(node));
420    treenode[i]->stateone = (bitptr)Malloc(words*sizeof(long));
421    treenode[i]->statezero = (bitptr)Malloc(words*sizeof(long));
422  }
423  for (i = spp; i < (nonodes); i++) {
424    q = NULL;
425    for (j = 1; j <= 3; j++) {
426      p = (node *)Malloc(sizeof(node));
427      p->stateone = (bitptr)Malloc(words*sizeof(long));
428      p->statezero = (bitptr)Malloc(words*sizeof(long));
429      p->next = q;
430      q = p;
431    }
432    p->next->next->next = p;
433    treenode[i] = p;
434  }
435}  /* doinit */
436
437void inputweights()
438{
439  /* input the character weights, 0-9 and A-Z for weights 0 - 35 */
440  Char ch;
441  long i;
442
443  for (i = 1; i < nmlngth; i++)
444    ch = getc(infile);
445  for (i = 0; i < (chars); i++) {
446    do {
447      if (eoln(infile)) {
448        fscanf(infile, "%*[^\n]");
449        getc(infile);
450      }
451      ch = getc(infile);
452    } while (ch == ' ');
453    weight[i] = 1;
454    if (isdigit(ch))
455      weight[i] = ch - '0';
456    else if (isalpha(ch)) {
457      uppercase(&ch);
458      if (ch >= 'A' && ch <= 'I')
459        weight[i] = ch - 'A' + 10;
460      else if (ch >= 'J' && ch <= 'R')
461        weight[i] = ch - 'J' + 19;
462      else
463        weight[i] = ch - 'S' + 28;
464    } else {
465      printf("BAD WEIGHT CHARACTER: %c\n", ch);
466      exit(-1);
467    }
468  }
469  fscanf(infile, "%*[^\n]");
470  getc(infile);
471  weights = true;
472}  /* inputweights */
473
474void printweights()
475{
476  /* print out the weights of characters */
477  long i, j, k;
478
479  fprintf(outfile, "Characters are weighted as follows:\n");
480  fprintf(outfile, "       ");
481  for (i = 0; i <= 9; i++)
482    fprintf(outfile, "%3ld", i);
483  fprintf(outfile, "\n*---------------------------------\n");
484  for (j = 0; j <= (chars / 10); j++) {
485    fprintf(outfile, "%5ld!  ", j * 10);
486    for (i = 0; i <= 9; i++) {
487      k = j * 10 + i;
488      if (k > 0 && k <= chars)
489        fprintf(outfile, "%3ld", weight[k - 1]);
490      else
491        fprintf(outfile, "   ");
492    }
493    putc('\n', outfile);
494  }
495  putc('\n', outfile);
496}  /* printweights */
497
498void inputancestors()
499{
500  /* reads the ancestral states for each character */
501  long i;
502  Char ch;
503
504  for (i = 1; i < nmlngth; i++)
505    ch = getc(infile);
506  for (i = 0; i < (chars); i++) {
507    anczero0[i] = true;
508    ancone0[i] = true;
509    do {
510      if (eoln(infile)) {
511        fscanf(infile, "%*[^\n]");
512        getc(infile);
513      }
514      ch = getc(infile);
515    } while (ch == ' ');
516    if (ch == 'p')
517      ch = 'P';
518    if (ch == 'b')
519      ch = 'B';
520    if (ch == '0' || ch == '1' || ch == 'P' || ch == 'B' || ch == '?') {
521      switch (ch) {
522
523      case '1':
524        anczero0[i] = false;
525        break;
526
527      case '0':
528        ancone0[i] = false;
529        break;
530
531      case 'P':
532        /* blank case */
533        break;
534
535      case 'B':
536        /* blank case */
537        break;
538
539      case '?':
540        /* blank case */
541        break;
542      }
543    } else {
544      printf("BAD ANCESTOR STATE: %c AT CHARACTER %4ld\n", ch, i + 1);
545      exit(-1);
546    }
547  }
548  fscanf(infile, "%*[^\n]");
549  getc(infile);
550}  /* inputancestors */
551
552void printancestors()
553{
554  /* print out list of ancestral states */
555  long i;
556
557  fprintf(outfile, "Ancestral states:\n");
558  for (i = 1; i <= nmlngth + 3; i++)
559    putc(' ', outfile);
560  for (i = 1; i <= (chars); i++) {
561    newline(i, 55, (int)(nmlngth + 3));
562    if (ancone[i - 1] && anczero[i - 1])
563      putc('?', outfile);
564    else if (ancone[i - 1])
565      putc('1', outfile);
566    else
567      putc('0', outfile);
568    if (i % 5 == 0)
569      putc(' ', outfile);
570  }
571  fprintf(outfile, "\n\n");
572}  /* printancestor */
573
574void inputoptions()
575{
576  /* input the information on the options */
577  Char ch;
578  long extranum, i, cursp, curchs;
579  boolean avar;
580
581  if (!firstset) {
582    if (eoln(infile)) {
583      fscanf(infile, "%*[^\n]");
584      getc(infile);
585    }
586    fscanf(infile, "%ld%ld", &cursp, &curchs);
587    if (cursp != spp) {
588      printf("\nERROR: INCONSISTENT NUMBER OF SPECIES IN DATA SET %4ld\n", ith);
589      exit(-1);
590    }
591    chars = curchs;
592  }
593  extranum = 0;
594  avar = false;
595  while (!(eoln(infile))) {
596    ch = getc(infile);
597    uppercase(&ch);
598    if (ch == 'A' || ch == 'W')
599      extranum++;
600    else if (ch != ' ') {
601      printf("BAD OPTION CHARACTER: %c\n", ch);
602      exit(-1);
603    }
604  }
605  fscanf(infile, "%*[^\n]");
606  getc(infile);
607  for (i = 0; i < (chars); i++)
608    weight[i] = 1;
609  for (i = 1; i <= extranum; i++) {
610    ch = getc(infile);
611    uppercase(&ch);
612    if (ch != 'A' && ch != 'W') {
613      printf("ERROR: INCORRECT AUXILIARY OPTIONS LINE");
614      printf(" WHICH STARTS WITH %c\n", ch);
615      exit(-1);
616    }
617    if (ch == 'A') {
618      avar = true;
619      if (!ancvar) {
620        printf("ERROR: ANCESTOR OPTION NOT CHOSEN IN MENU");
621        printf(" WITH OPTION %c IN INPUT\n", ch);
622        exit(-1);
623      } else
624        inputancestors();
625    }
626    if (ch == 'W')
627      inputweights();
628  }
629  if (ancvar && !avar) {
630    puts("ERROR: ANCESTOR OPTION CHOSEN IN MENU WITH NO OPTION A IN INPUT");
631    exit(-1);
632  }
633  if (dollo)
634    fprintf(outfile, "Dollo");
635  else
636    fprintf(outfile, "Polymorphism");
637  fprintf(outfile, " parsimony method\n\n");
638  if (weights && printdata)
639    printweights();
640  for (i = 0; i < (chars); i++) {
641    if (!ancvar) {
642      anczero[i] = true;
643      ancone[i] = false;
644    } else {
645      anczero[i] = anczero0[i];
646      ancone[i] = ancone0[i];
647    }
648  }
649  if (ancvar && avar && printdata)
650    printancestors();
651  questions = false;
652  for (i = 0; i < (chars); i++) {
653    questions = (questions || (ancone[i] && anczero[i]));
654    threshwt[i] = threshold * weight[i];
655  }
656}  /* inputoptions */
657
658void inputdata()
659{
660  /* input the names and character state data for species */
661  long i, j, l;
662  char k;
663  Char charstate;
664  /* possible states are
665     '0', '1', 'P', 'B', and '?' */
666  node *p;
667
668  putc('\n', outfile);
669  j = nmlngth + (chars + (chars - 1) / 10) / 2 - 4;
670  if (j < nmlngth - 1)
671    j = nmlngth - 1;
672  if (j > 36)
673    j = 36;
674  if (printdata) {
675    fprintf(outfile, "Name");
676    for (i = 1; i <= j; i++)
677      putc(' ', outfile);
678    fprintf(outfile, "Characters\n");
679    fprintf(outfile, "----");
680    for (i = 1; i <= j; i++)
681      putc(' ', outfile);
682    fprintf(outfile, "----------\n\n");
683  }
684  for (i = 0; i < (chars); i++)
685    extras[i] = 0;
686  for (i = 1; i <= (nonodes); i++) {
687    treenode[i - 1]->back = NULL;
688    treenode[i - 1]->tip = (i <= spp);
689    treenode[i - 1]->index = i;
690    if (i > spp) {
691      p = treenode[i - 1]->next;
692      while (p != treenode[i - 1]) {
693        p->back = NULL;
694        p->tip = false;
695        p->index = i;
696        p = p->next;
697      }
698    } else {
699      for (j = 0; j < nmlngth; j++) {
700        nayme[i - 1][j] = getc(infile);
701        if (eof(infile) || eoln(infile)){
702          printf("ERROR: END-OF-LINE OR END-OF-FILE");
703          printf(" IN THE MIDDLE OF A SPECIES NAME\n");
704          exit(-1);}
705      }
706      if (printdata) {
707        for (j = 0; j < nmlngth; j++)
708          putc(nayme[i - 1][j], outfile);
709      }
710      fprintf(outfile, "   ");
711      for (j = 0; j < (words); j++) {
712        treenode[i - 1]->stateone[j] = 0;
713        treenode[i - 1]->statezero[j] = 0;
714      }
715      for (j = 1; j <= (chars); j++) {
716        k = (j - 1) % bits + 1;
717        l = (j - 1) / bits + 1;
718        do {
719          if (eoln(infile)) {
720            fscanf(infile, "%*[^\n]");
721            getc(infile);
722          }
723          charstate = getc(infile);
724        } while (charstate == ' ');
725        if (charstate == 'b')
726          charstate = 'B';
727        if (charstate == 'p')
728          charstate = 'P';
729        if (charstate != '0' && charstate != '1' && charstate != '?' &&
730            charstate != 'P' && charstate != 'B') {
731          printf("ERROR: BAD CHARACTER STATE: %c ",charstate);
732          printf("AT CHARACTER %5ld OF SPECIES %3ld\n",j,i);
733          exit(-1);
734        }
735        if (printdata) {
736          newline(j, 55, (int)(nmlngth + 3));
737          putc(charstate, outfile);
738          if (j % 5 == 0)
739            putc(' ', outfile);
740        }
741        if (charstate == '1')
742          treenode[i - 1]->stateone[l - 1] =
743            ((long)treenode[i - 1]->stateone[l - 1]) | (1L << k);
744        if (charstate == '0')
745          treenode[i - 1]->statezero[l - 1] =
746            ((long)treenode[i - 1]->statezero[l - 1]) | (1L << k);
747        if (charstate == 'P' || charstate == 'B') {
748          if (dollo)
749            extras[j - 1] += weight[j - 1];
750          else {
751            treenode[i - 1]->stateone[l - 1] =
752              ((long)treenode[i - 1]->stateone[l - 1]) | (1L << k);
753            treenode[i - 1]->statezero[l - 1] =
754              ((long)treenode[i - 1]->statezero[l - 1]) | (1L << k);
755          }
756        }
757      }
758      fscanf(infile, "%*[^\n]");
759      getc(infile);
760      if (printdata)
761        putc('\n', outfile);
762    }
763  }
764  fprintf(outfile, "\n\n");
765}  /* inputdata */
766
767
768void doinput()
769{
770  /* reads the input data */
771  inputoptions();
772  inputdata();
773}  /* doinput */
774
775
776void add(below, newtip, newfork)
777node *below, *newtip, *newfork;
778{
779  /* inserts the nodes newfork and its left descendant, newtip,
780     to the tree.  below becomes newfork's right descendant */
781  if (below != treenode[below->index - 1])
782    below = treenode[below->index - 1];
783  if (below->back != NULL)
784    below->back->back = newfork;
785  newfork->back = below->back;
786  below->back = newfork->next->next;
787  newfork->next->next->back = below;
788  newfork->next->back = newtip;
789  newtip->back = newfork->next;
790  if (root == below)
791    root = newfork;
792  root->back = NULL;
793}  /* add */
794
795void re_move(item, fork)
796node **item, **fork;
797{
798  /* removes nodes item and its ancestor, fork, from the tree.
799     the new descendant of fork's ancestor is made to be
800     fork's second descendant (other than item).  Also
801     returns pointers to the deleted nodes, item and fork */
802  node *p, *q;
803
804  if ((*item)->back == NULL) {
805    *fork = NULL;
806    return;
807  }
808  *fork = treenode[(*item)->back->index - 1];
809  if (root == *fork) {
810    if (*item == (*fork)->next->back)
811      root = (*fork)->next->next->back;
812    else
813      root = (*fork)->next->back;
814  }
815  p = (*item)->back->next->back;
816  q = (*item)->back->next->next->back;
817  if (p != NULL)
818    p->back = q;
819  if (q != NULL)
820    q->back = p;
821  (*fork)->back = NULL;
822  p = (*fork)->next;
823  while (p != *fork) {
824    p->back = NULL;
825    p = p->next;
826  }
827  (*item)->back = NULL;
828}  /* re_move */
829
830void fillin(p)
831node *p;
832{
833  /* Sets up for each node in the tree two statesets.
834     stateone and statezero are the sets of character
835     states that must be 1 or must be 0, respectively,
836     in a most parsimonious reconstruction, based on the
837     information at or above this node.  Note that this
838     state assignment may change based on information further
839     down the tree.  If a character is in both sets it is in
840     state "P".  If in neither, it is "?". */
841  long i;
842
843  for (i = 0; i < words; i++) {
844    p->stateone[i] = p->next->back->stateone[i] |
845                     p->next->next->back->stateone[i];
846    p->statezero[i] = p->next->back->statezero[i] |
847                      p->next->next->back->statezero[i];
848  }
849}  /* fillin */
850
851void correct(p)
852node *p;
853{  /* get final states for intermediate nodes */
854  long i;
855  long z0, z1, s0, s1, temp;
856
857  if (p->tip)
858    return;
859  for (i = 0; i < (words); i++) {
860    if (p->back == NULL) {
861      s0 = zeroanc[i];
862      s1 = fullset & (~zeroanc[i]);
863    } else {
864      s0 = treenode[p->back->index - 1]->statezero[i];
865      s1 = treenode[p->back->index - 1]->stateone[i];
866    }
867    z0 = (s0 & p->statezero[i]) |
868         (p->next->back->statezero[i] & p->next->next->back->statezero[i]);
869    z1 = (s1 & p->stateone[i]) |
870         (p->next->back->stateone[i] & p->next->next->back->stateone[i]);
871    if (dollo) {
872      temp = z0 & (~(zeroanc[i] & z1));
873      z1 &= ~(fullset & (~zeroanc[i]) & z0);
874      z0 = temp;
875    }
876    temp = fullset & (~z0) & (~z1);
877    p->statezero[i] = z0 | (temp & s0 & (~s1));
878    p->stateone[i] = z1 | (temp & s1 & (~s0));
879  }
880}  /* correct */
881
882
883void postorder(p,numsone,numszero)
884node *p;
885steptr numsone,numszero;
886{
887  /* traverses a binary tree, calling PROCEDURE fillin at a
888     node's descendants before calling fillin at the node */
889  if (p->tip)
890    return;
891  postorder(p->next->back, numsone,numszero);
892  postorder(p->next->next->back, numsone,numszero);
893  fillin(p);
894}  /* postorder */
895
896
897void count(p, numsone,numszero)
898node *p;
899steptr numsone,numszero;
900{
901  /* counts the number of steps in a fork of the tree.
902     The program spends much of its time in this PROCEDURE */
903  long i, j, l;
904
905  if (dollo) {
906    for (i = 0; i < (words); i++)
907      steps[i] = (treenode[p->back->index - 1]->stateone[i] &
908                  p->statezero[i] & zeroanc[i]) |
909          (treenode[p->back->index - 1]->statezero[i] & p->stateone[i] &
910           fullset & (~zeroanc[i]));
911  } else {
912    for (i = 0; i < (words); i++)
913      steps[i] = treenode[p->back->index - 1]->stateone[i] &
914                 treenode[p->back->index - 1]->statezero[i] & p->stateone[i] &
915                 p->statezero[i];
916  }
917  j = 1;
918  l = 0;
919  for (i = 0; i < (chars); i++) {
920    l++;
921    if (l > bits) {
922      l = 1;
923      j++;
924    }
925    if ((unsigned)l < 32 && ((1L << l) & steps[j - 1]) != 0) {
926      if ((unsigned)l < 32 && ((1L << l) & zeroanc[j - 1]) != 0)
927        numszero[i] += weight[i];
928      else
929        numsone[i] += weight[i];
930    }
931  }
932}  /* count */
933
934void preorder(p, numsone,numszero)
935node *p;
936steptr numsone,numszero;
937{
938  /* go back up tree setting up and counting interior node
939     states */
940
941  if (!p->tip) {
942    correct(p);
943    preorder(p->next->back, numsone,numszero);
944    preorder(p->next->next->back, numsone,numszero);
945  }
946  if (p->back != NULL)
947    count(p, numsone,numszero);
948}  /* preorder */
949
950void evaluate(r)
951node *r;
952{
953  /* Determines the number of losses or polymorphisms needed
954     for a tree. This is the minimum number needed to evolve
955     chars on this tree */
956  long i, stepnum, smaller;
957  double sum, term;
958
959  sum = 0.0;
960  for (i = 0; i < (chars); i++) {
961    numszero[i] = 0;
962    numsone[i] = 0;
963  }
964  for (i = 0; i < (words); i++)
965    zeroanc[i] = fullset;
966  postorder(r, numsone,numszero);
967  preorder(r, numsone,numszero);
968  for (i = 0; i < (words); i++)
969    zeroanc[i] = 0;
970  postorder(r, numsone,numszero);
971  preorder(r, numsone,numszero);
972  for (i = 0; i < (chars); i++) {
973    smaller = spp * weight[i];
974    numsteps[i] = smaller;
975    if (anczero[i]) {
976      numsteps[i] = numszero[i];
977      smaller = numszero[i];
978    }
979    if (ancone[i] && numsone[i] < smaller)
980      numsteps[i] = numsone[i];
981    stepnum = numsteps[i] + extras[i];
982    if (stepnum <= threshwt[i])
983      term = stepnum;
984    else
985      term = threshwt[i];
986    sum += term;
987    if (usertree && which <= maxuser)
988      fsteps[which - 1][i] = term;
989    guess[i] = '?';
990    if (!ancone[i] || (anczero[i] && numszero[i] < numsone[i]))
991      guess[i] = '0';
992    else if (!anczero[i] || (ancone[i] && numsone[i] < numszero[i]))
993      guess[i] = '1';
994  }
995  if (usertree && which <= maxuser) {
996    nsteps[which - 1] = sum;
997    if (which == 1) {
998      minwhich = 1;
999      minsteps = sum;
1000    } else if (sum < minsteps) {
1001      minwhich = which;
1002      minsteps = sum;
1003    }
1004  }
1005  like = -sum;
1006}  /* evaluate */
1007
1008
1009void savetree()
1010{
1011  /* record in place where each species has to be
1012     added to reconstruct this tree */
1013  long i, j;
1014  node *p;
1015  boolean done;
1016
1017  for (i = 0; i < (nonodes); i++)
1018    place[i] = 0;
1019  place[root->index - 1] = 1;
1020  for (i = 1; i <= (spp); i++) {
1021    p = treenode[i - 1];
1022    while (place[p->index - 1] == 0) {
1023      place[p->index - 1] = i;
1024      p = p->back;
1025      if (p != NULL)
1026        p = treenode[p->index - 1];
1027    }
1028    if (i > 1) {
1029      place[i - 1] = place[p->index - 1];
1030      j = place[p->index - 1];
1031      done = false;
1032      while (!done) {
1033        place[p->index - 1] = spp + i - 1;
1034        p = treenode[p->index - 1];
1035        p = p->back;
1036        done = (p == NULL);
1037        if (!done)
1038          done = (place[p->index - 1] != j);
1039      }
1040    }
1041  }
1042}  /* savetree */
1043
1044void findtree(pos,found)
1045long *pos;
1046boolean *found;
1047{
1048  /* finds tree given by ARRAY place in ARRAY
1049     bestrees by binary search */
1050  long i, lower, upper;
1051  boolean below, done;
1052
1053  below = false;
1054  lower = 1;
1055  upper = nextree - 1;
1056  (*found) = false;
1057  while (!(*found) && lower <= upper) {
1058    (*pos) = (lower + upper) / 2;
1059    i = 3;
1060    done = false;
1061    while (!done) {
1062      done = (i > spp);
1063      if (!done)
1064        done = (place[i - 1] != bestrees[(*pos) - 1][i - 1]);
1065      if (!done)
1066        i++;
1067    }
1068    (*found) = (i > spp);
1069    below = (place[i - 1] < bestrees[(*pos) - 1][i - 1]);
1070    if ((*found))
1071      break;
1072    if (below)
1073      upper = (*pos) - 1;
1074    else
1075      lower = (*pos) + 1;
1076  }
1077  if (!(*found) && !below)
1078    (*pos)++;
1079}  /* findtree */
1080
1081void addtree(pos)
1082long *pos;
1083{
1084  /* puts tree from ARRAY place in its proper position
1085     in ARRAY bestrees */
1086  long i;
1087
1088  for (i = nextree - 1; i >=(*pos); i--)
1089    memcpy(bestrees[i], bestrees[i - 1], spp*sizeof(long));
1090  for (i = 0; i < (spp); i++)
1091    bestrees[(*pos) - 1][i] = place[i];
1092  nextree++;
1093}  /* addtree */
1094
1095void tryadd(p, item,nufork)
1096node *p;
1097node **item,**nufork;
1098{
1099  /* temporarily adds one fork and one tip to the tree.
1100     if the location where they are added yields greater
1101     "likelihood" than other locations tested up to that
1102     time, then keeps that location as there */
1103/* Local variables for tryadd: */
1104  long pos;
1105  boolean found;
1106  add(p, *item, *nufork);
1107  evaluate(root);
1108  if (lastrearr) {
1109    if (like >= bstlike2) {
1110      savetree();
1111      if (like > bstlike2) {
1112        bestlike = bstlike2 = like;
1113        pos = 1;
1114        nextree = 1;
1115        addtree(&pos);
1116      } else {
1117        pos = 0;
1118        findtree(&pos,&found);
1119        if (!found) {
1120          if (nextree <= maxtrees)
1121            addtree(&pos);
1122        }
1123      }
1124    }
1125  }
1126  if (like > bestyet) {
1127    bestyet = like;
1128    there = p;
1129  }
1130  re_move(item, nufork);
1131}  /* tryadd */
1132
1133void addpreorder(p, item_, nufork_)
1134node *p, *item_, *nufork_;
1135{
1136  /* traverses a binary tree, calling PROCEDURE tryadd
1137     at a node before calling tryadd at its descendants */
1138  node *item= item_;
1139  node *nufork = nufork_;
1140
1141  if (p == NULL)
1142    return;
1143  tryadd(p, &item,&nufork);
1144  if (!p->tip) {
1145    addpreorder(p->next->back, item, nufork);
1146    addpreorder(p->next->next->back, item, nufork);
1147  }
1148}  /* addpreorder */
1149
1150
1151void tryrearr(p, r,success)
1152node *p;
1153node **r;
1154boolean *success;
1155{
1156  /* evaluates one rearrangement of the tree.
1157     if the new tree has greater "likelihood" than the old
1158     one sets success := TRUE and keeps the new tree.
1159     otherwise, restores the old tree */
1160  node *frombelow, *whereto, *forknode;
1161  double oldlike;
1162
1163  if (p->back == NULL)
1164    return;
1165  forknode = treenode[p->back->index - 1];
1166  if (forknode->back == NULL)
1167    return;
1168  oldlike = bestyet;
1169  if (p->back->next->next == forknode)
1170    frombelow = forknode->next->next->back;
1171  else
1172    frombelow = forknode->next->back;
1173  whereto = forknode->back;
1174  re_move(&p, &forknode);
1175  add(whereto, p, forknode);
1176  evaluate(*r);
1177  if (like <= oldlike) {
1178    re_move(&p, &forknode);
1179    add(frombelow, p, forknode);
1180  } else {
1181    (*success) = true;
1182    bestyet = like;
1183  }
1184}  /* tryrearr */
1185
1186void repreorder(p,r,success)
1187node *p;
1188node **r;
1189boolean *success;
1190{
1191  /* traverses a binary tree, calling PROCEDURE tryrearr
1192     at a node before calling tryrearr at its descendants */
1193  if (p == NULL)
1194    return;
1195  tryrearr(p, r,success);
1196  if (!p->tip) {
1197    repreorder(p->next->back, r,success);
1198    repreorder(p->next->next->back, r,success);
1199  }
1200}  /* repreorder */
1201
1202void rearrange(r_)
1203node **r_;
1204{
1205  /* traverses the tree (preorder), finding any local
1206     rearrangement which decreases the number of steps.
1207     if traversal succeeds in increasing the tree's
1208     "likelihood", PROCEDURE rearrange runs traversal again */
1209  node **r         = r_;
1210  boolean success  = true;
1211
1212  while (success) {
1213    success = false;
1214    repreorder(*r, r,&success);
1215  }
1216}  /* rearrange */
1217
1218
1219void findch(c)
1220Char c;
1221{
1222  /* scan forward until find character c */
1223  boolean done;
1224
1225  done = false;
1226  while (!(done)) {
1227    if (c == ',') {
1228      if (ch == '(' || ch == ')' || ch == ';') {
1229        printf("\nERROR IN USER TREE:");
1230        printf(" UNMATCHED PARENTHESIS OR MISSING COMMA\n");
1231        exit(-1);
1232      } else if (ch == ',')
1233        done = true;
1234    } else if (c == ')') {
1235      if (ch == '(' || ch == ',' || ch == ';') {
1236        printf("\nERROR IN USER TREE:");
1237        printf(" UNMATCHED PARENTHESIS OR NOT BIFURCATED NODE\n");
1238        exit(-1);
1239      } else {
1240        if (ch == ')')
1241          done = true;
1242      }
1243    } else if (c == ';') {
1244      if (ch != ';') {
1245        printf("\nERROR IN USER TREE:");
1246        printf(" UNMATCHED PARENTHESIS OR MISSING SEMICOLON\n");
1247        exit(-1);
1248      } else
1249        done = true;
1250    }
1251    if ((done && ch == ')') || !(done)) {
1252      if (eoln(infile)) {
1253        fscanf(infile, "%*[^\n]");
1254        getc(infile);
1255      }
1256      ch = getc(infile);
1257    }
1258  }
1259}  /* findch */
1260
1261void addelement(p, nextnode,lparens,naymes)
1262node **p;
1263long *nextnode,*lparens;
1264boolean *naymes;
1265{
1266  /* recursive procedure adds nodes to user-defined tree */
1267  node *q;
1268  long i, n;
1269  boolean found;
1270  Char str[nmlngth];
1271
1272  do {
1273    if (eoln(infile)) {
1274      fscanf(infile, "%*[^\n]");
1275      getc(infile);
1276    }
1277   ch = getc(infile);
1278  } while (ch == ' ');
1279  if (ch == '(' ) {
1280    if ((*lparens) >= spp - 1) {
1281      printf("\nERROR IN USER TREE: TOO MANY LEFT PARENTHESES\n");
1282      exit(-1);
1283    }
1284    (*nextnode)++;
1285    (*lparens)++;
1286    q = treenode[(*nextnode) - 1];
1287    addelement(&q->next->back, nextnode,lparens,naymes);
1288    q->next->back->back = q->next;
1289    findch(',' );
1290    addelement(&q->next->next->back, nextnode,lparens,naymes);
1291    q->next->next->back->back = q->next->next;
1292    findch(')');
1293    *p = q;
1294    return;
1295  }
1296  for (i = 0; i < nmlngth; i++)
1297    str[i] = ' ';
1298  n = 1;
1299  do {
1300    if (ch == '_')
1301      ch = ' ';
1302    str[n - 1] = ch;
1303    if (eoln(infile)) {
1304      fscanf(infile, "%*[^\n]");
1305      getc(infile);
1306    }
1307    ch = getc(infile);
1308    n++;
1309  } while (ch != ',' && ch != ')' && ch != ':' && n <= nmlngth);
1310  n = 1;
1311  do {
1312    found = true;
1313    for (i = 0; i < nmlngth; i++)
1314      found = (found && str[i] == nayme[n - 1][i]);
1315    if (found) {
1316      if (naymes[n - 1] == false) {
1317        *p = treenode[n - 1];
1318        naymes[n - 1] = true;
1319      } else {
1320        printf("\nERROR IN USER TREE: DUPLICATE NAME FOUND -- ");
1321        for (i = 0; i < nmlngth; i++)
1322          putchar(nayme[n - 1][i]);
1323        putchar('\n');
1324        exit(-1);
1325      }
1326    } else
1327      n++;
1328  } while (!(n > spp || found));
1329  if (n <= spp)
1330    return;
1331  printf("CANNOT FIND SPECIES: ");
1332  for (i = 0; i < nmlngth; i++)
1333    putchar(str[i]);
1334  putchar('\n');
1335}  /* addelement */
1336
1337void treeread()
1338{
1339  /* read in user-defined tree and set it up */
1340  Char ch;
1341  long nextnode    = spp;
1342  long lparens     =0;
1343  long i;
1344
1345  root = treenode[spp];
1346  root->back = NULL;
1347  for (i = 0; i < (spp); i++)
1348    naymes[i] = false;
1349  addelement(&root, &nextnode,&lparens,naymes);
1350  findch(';');
1351  if (progress)
1352    printf("\n\n");
1353  fscanf(infile, "%*[^\n]");
1354  getc(infile);
1355}  /* treeread */
1356
1357
1358void coordinates(p,tipy)
1359node *p;
1360long *tipy;
1361{
1362  /* establishes coordinates of nodes */
1363  node *q, *first, *last;
1364
1365  if (p->tip) {
1366    p->xcoord = 0;
1367    p->ycoord = (*tipy);
1368    p->ymin = (*tipy);
1369    p->ymax = (*tipy);
1370    (*tipy) += down;
1371    return;
1372  }
1373  q = p->next;
1374  do {
1375    coordinates(q->back, tipy);
1376    q = q->next;
1377  } while (p != q);
1378  first = p->next->back;
1379  q = p->next;
1380  while (q->next != p)
1381    q = q->next;
1382  last = q->back;
1383  p->xcoord = last->ymax - first->ymin;
1384  p->ycoord = (first->ycoord + last->ycoord) / 2;
1385  p->ymin = first->ymin;
1386  p->ymax = last->ymax;
1387}  /* coordinates */
1388
1389void drawline(i, scale)
1390long i;
1391double scale;
1392{
1393  /* draws one row of the tree diagram by moving up tree */
1394  node *p, *q, *r, *first, *last;
1395  long n, j;
1396  boolean extra, done;
1397
1398  p = root;
1399  q = root;
1400  extra = false;
1401  if (i == p->ycoord && p == root) {
1402    if (p->index - spp >= 10)
1403      fprintf(outfile, "-%2ld", p->index - spp);
1404    else
1405      fprintf(outfile, "--%ld", p->index - spp);
1406    extra = true;
1407  } else
1408    fprintf(outfile, "  ");
1409  do {
1410    if (!p->tip) {
1411      r = p->next;
1412      done = false;
1413      do {
1414        if (i >= r->back->ymin && i <= r->back->ymax) {
1415          q = r->back;
1416          done = true;
1417        }
1418        r = r->next;
1419      } while (!(done || r == p));
1420      first = p->next->back;
1421      r = p->next;
1422      while (r->next != p)
1423        r = r->next;
1424      last = r->back;
1425    }
1426    done = (p == q);
1427    n = (long)(scale * (p->xcoord - q->xcoord) + 0.5);
1428    if (n < 3 && !q->tip)
1429      n = 3;
1430    if (extra) {
1431      n--;
1432      extra = false;
1433    }
1434    if (q->ycoord == i && !done) {
1435      putc('+', outfile);
1436      if (!q->tip) {
1437        for (j = 1; j <= n - 2; j++)
1438          putc('-', outfile);
1439        if (q->index - spp >= 10)
1440          fprintf(outfile, "%2ld", q->index - spp);
1441        else
1442          fprintf(outfile, "-%ld", q->index - spp);
1443        extra = true;
1444      } else {
1445        for (j = 1; j < n; j++)
1446          putc('-', outfile);
1447      }
1448    } else if (!p->tip) {
1449      if (last->ycoord > i && first->ycoord < i && i != p->ycoord) {
1450        putc('!', outfile);
1451        for (j = 1; j < n; j++)
1452          putc(' ', outfile);
1453      } else {
1454        for (j = 1; j <= n; j++)
1455          putc(' ', outfile);
1456      }
1457    } else {
1458      for (j = 1; j <= n; j++)
1459        putc(' ', outfile);
1460    }
1461    if (p != q)
1462      p = q;
1463  } while (!done);
1464  if (p->ycoord == i && p->tip) {
1465    for (j = 0; j < nmlngth; j++)
1466      putc(nayme[p->index - 1][j], outfile);
1467  }
1468  putc('\n', outfile);
1469}  /* drawline */
1470
1471void printree()
1472{
1473  /* prints out diagram of the tree */
1474  long tipy        = 1;
1475  double scale     = 1.5;
1476  long i;
1477
1478  putc('\n', outfile);
1479  if (!treeprint)
1480    return;
1481  putc('\n', outfile);
1482  coordinates(root,&tipy);
1483  scale = 1.5;
1484  putc('\n', outfile);
1485  for (i = 1; i <= (tipy - down); i++)
1486    drawline(i, scale);
1487  putc('\n', outfile);
1488}  /* printree*/
1489
1490
1491
1492void filltrav(r)
1493node *r;
1494{
1495  /* traverse to fill in interior node states */
1496  if (r->tip)
1497    return;
1498  filltrav(r->next->back);
1499  filltrav(r->next->next->back);
1500  fillin(r);
1501}  /* filltrav */
1502
1503/* Local variables for hyptrav: */
1504struct LOC_hyptrav {
1505  struct LOC_hypstates *LINK;
1506  node *r;
1507  boolean bottom, nonzero;
1508  gbit *zerobelow, *onebelow;
1509} ;
1510
1511void hyprint(Hyptrav,unknown,dohyp)
1512struct LOC_hyptrav *Hyptrav;
1513boolean *unknown;
1514bitptr dohyp;
1515{
1516  /* print out states at node */
1517  long i, j, k;
1518  char l;
1519  boolean dot, a0, a1, s0, s1;
1520
1521  if (Hyptrav->bottom)
1522    fprintf(outfile, "root   ");
1523  else
1524    fprintf(outfile, "%3ld    ", Hyptrav->r->back->index - spp);
1525  if (Hyptrav->r->tip) {
1526    for (i = 0; i < nmlngth; i++)
1527      putc(nayme[Hyptrav->r->index - 1][i], outfile);
1528  } else
1529    fprintf(outfile, "%4ld      ", Hyptrav->r->index - spp);
1530  if (Hyptrav->nonzero)
1531    fprintf(outfile, "   yes    ");
1532  else if (*unknown)
1533    fprintf(outfile, "    ?     ");
1534  else
1535    fprintf(outfile, "   no     ");
1536  for (j = 1; j <= (chars); j++) {
1537    newline(j, 40, (int)(nmlngth + 17));
1538    k = (j - 1) / bits + 1;
1539    l = (j - 1) % bits + 1;
1540    dot = (((1L << l) & dohyp[k - 1]) == 0 && guess[j - 1] == '?');
1541    s0 = (((1L << l) & Hyptrav->r->statezero[k - 1]) != 0);
1542    s1 = (((1L << l) & Hyptrav->r->stateone[k - 1]) != 0);
1543    a0 = (((1L << l) & Hyptrav->zerobelow->bits_[k - 1]) != 0);
1544    a1 = (((1L << l) & Hyptrav->onebelow->bits_[k - 1]) != 0);
1545    dot = (dot || (a1 == s1 && a0 == s0));
1546    if (dot)
1547      putc('.', outfile);
1548    else {
1549      if (s0) {
1550        if (s1)
1551          putc('P', outfile);
1552        else
1553          putc('0', outfile);
1554      } else if (s1)
1555        putc('1', outfile);
1556      else
1557        putc('?', outfile);
1558    }
1559    if (j % 5 == 0)
1560      putc(' ', outfile);
1561  }
1562  putc('\n', outfile);
1563}  /* hyprint */
1564
1565void hyptrav(r_,unknown,dohyp)
1566node *r_;
1567boolean *unknown;
1568bitptr dohyp;
1569{
1570  /*  compute, print out states at one interior node */
1571  struct LOC_hyptrav HypVars;
1572  long i;
1573
1574  HypVars.r = r_;
1575  gnu(&HypVars.zerobelow);
1576  gnu(&HypVars.onebelow);
1577  if (!HypVars.r->tip)
1578    correct(HypVars.r);
1579  HypVars.bottom = (HypVars.r->back == NULL);
1580  HypVars.nonzero = false;
1581  if (HypVars.bottom) {
1582    memcpy(HypVars.zerobelow->bits_, zeroanc, words*sizeof(long));
1583    memcpy(HypVars.onebelow->bits_, oneanc, words*sizeof(long));
1584  } else {
1585    memcpy(HypVars.zerobelow->bits_,
1586          treenode[HypVars.r->back->index - 1]->statezero, words*sizeof(long));
1587    memcpy(HypVars.onebelow->bits_,
1588          treenode[HypVars.r->back->index - 1]->stateone, words*sizeof(long));
1589  }
1590  for (i = 0; i < (words); i++)
1591    HypVars.nonzero = (HypVars.nonzero ||
1592                      ((HypVars.r->stateone[i] & HypVars.zerobelow->bits_[i])
1593                      | (HypVars.r->statezero[i]
1594                        & HypVars.onebelow->bits_[i])) != 0);
1595  hyprint(&HypVars,unknown,dohyp);
1596  if (!HypVars.r->tip) {
1597    hyptrav(HypVars.r->next->back, unknown,dohyp);
1598    hyptrav(HypVars.r->next->next->back, unknown,dohyp);
1599  }
1600  chuck(HypVars.zerobelow);
1601  chuck(HypVars.onebelow);
1602}  /* hyptrav */
1603
1604void hypstates()
1605{
1606  boolean unknown  = false;
1607  bitptr dohyp;
1608  /* fill in and describe states at interior nodes */
1609  long i, j, k;
1610
1611  for (i = 0; i < (words); i++) {
1612    zeroanc[i] = 0;
1613    oneanc[i] = 0;
1614  }
1615  for (i = 0; i < (chars); i++) {
1616    j = i / bits + 1;
1617    k = i % bits + 1;
1618    if (guess[i] == '0')
1619      zeroanc[j - 1] = ((long)zeroanc[j - 1]) | (1L << k);
1620    if (guess[i] == '1')
1621      oneanc[j - 1] = ((long)oneanc[j - 1]) | (1L << k);
1622    unknown = (unknown || guess[i] == '?');
1623  }
1624  dohyp = (bitptr)Malloc(words*sizeof(long));
1625  for (i = 0; i < words; i++)
1626    dohyp[i] = zeroanc[i] | oneanc[i];
1627  filltrav(root);
1628  fprintf(outfile, "From    To     Any Steps?");
1629  fprintf(outfile, "    State at upper node\n");
1630  fprintf(outfile, "                            ");
1631  fprintf(outfile, " ( . means same as in the node below it on tree)\n\n");
1632  hyptrav(root, &unknown,dohyp);
1633  free(dohyp);
1634}  /* hypstates */
1635
1636void treeout(p)
1637node *p;
1638{
1639  /* write out file with representation of final tree */
1640  long i, n;
1641  Char c;
1642
1643  if (p->tip) {
1644    n = 0;
1645    for (i = 1; i <= nmlngth; i++) {
1646      if (nayme[p->index - 1][i - 1] != ' ')
1647        n = i;
1648    }
1649    for (i = 0; i < n; i++) {
1650      c = nayme[p->index - 1][i];
1651      if (c == ' ')
1652        c = '_';
1653      putc(c, treefile);
1654    }
1655    col += n;
1656  } else {
1657    putc('(', treefile);
1658    col++;
1659    treeout(p->next->back);
1660    putc(',', treefile);
1661    col++;
1662    if (col > 65) {
1663      putc('\n', treefile);
1664      col = 0;
1665    }
1666    treeout(p->next->next->back);
1667    putc(')', treefile);
1668    col++;
1669  }
1670  if (p != root)
1671    return;
1672  if (nextree > 2)
1673    fprintf(treefile, "[%6.4f];\n", 1.0 / (nextree - 1));
1674  else
1675    fprintf(treefile, ";\n");
1676}  /* treeout */
1677
1678void describe()
1679{
1680  /* prints ancestors, steps and table of numbers of steps in
1681     each character */
1682  long i, j, k;
1683
1684  if (treeprint)
1685    fprintf(outfile, "\nrequires a total of %10.3f\n", -like);
1686  if (stepbox) {
1687    putc('\n', outfile);
1688    if (weights)
1689      fprintf(outfile, " weighted");
1690    if (dollo)
1691      fprintf(outfile, " reversions ");
1692    else
1693      fprintf(outfile, " polymorphisms ");
1694    fprintf(outfile, "in each character:\n");
1695    fprintf(outfile, "      ");
1696    for (i = 0; i <= 9; i++)
1697      fprintf(outfile, "%4ld", i);
1698    fprintf(outfile, "\n     *-----------------------------------------\n");
1699    for (i = 0; i <= (chars / 10); i++) {
1700      fprintf(outfile, "%5ld", i * 10);
1701      putc('!', outfile);
1702      for (j = 0; j <= 9; j++) {
1703        k = i * 10 + j;
1704        if (k == 0 || k > chars)
1705          fprintf(outfile, "    ");
1706        else
1707          fprintf(outfile, "%4ld", numsteps[k - 1] + extras[k - 1]);
1708      }
1709      putc('\n', outfile);
1710    }
1711  }
1712  putc('\n', outfile);
1713  if (questions) {
1714    fprintf(outfile, "best guesses of ancestral states:\n");
1715    fprintf(outfile, "      ");
1716    for (i = 0; i <= 9; i++)
1717      fprintf(outfile, "%2ld", i);
1718    fprintf(outfile, "\n     *--------------------\n");
1719    for (i = 0; i <= (chars / 10); i++) {
1720      putc(' ', outfile);
1721      fprintf(outfile, "%4ld!", i * 10);
1722      for (j = 0; j <= 9; j++) {
1723        if (i * 10 + j == 0 || i * 10 + j > chars)
1724          fprintf(outfile, "  ");
1725        else
1726          fprintf(outfile, " %c", guess[i * 10 + j - 1]);
1727      }
1728      putc('\n', outfile);
1729    }
1730    putc('\n', outfile);
1731  }
1732  if (ancseq) {
1733    hypstates();
1734    putc('\n', outfile);
1735  }
1736  putc('\n', outfile);
1737  if (trout) {
1738    col = 0;
1739    treeout(root);
1740  }
1741}  /* describe */
1742
1743
1744void maketree()
1745{
1746  /* constructs a binary tree from the pointers in treenode.
1747     adds each node at location which yields highest "likelihood"
1748     then rearranges the tree for greatest "likelihood" */
1749  long i, j, k, numtrees, num, sumw;
1750  double gotlike, sum, sum2, sd;
1751  node *item, *nufork, *dummy;
1752  double TEMP;
1753
1754  steps = (bitptr)Malloc(words*sizeof(long));
1755  fullset = (1L << (bits + 1)) - (1L << 1);
1756  if (!usertree) {
1757    for (i = 1; i <= (spp); i++)
1758      enterorder[i - 1] = i;
1759    if (jumble) {
1760      for (i = 0; i < (spp); i++) {
1761        j = (long)(randum(seed) * spp) + 1;
1762        k = enterorder[j - 1];
1763        enterorder[j - 1] = enterorder[i];
1764        enterorder[i] = k;
1765      }
1766    }
1767    root = treenode[enterorder[0] - 1];
1768    add(treenode[enterorder[0] - 1], treenode[enterorder[1] - 1],
1769        treenode[spp]);
1770    if (progress) {
1771      printf("\nAdding species:\n");
1772      printf("   ");
1773      for (i = 0; i < nmlngth; i++)
1774        putchar(nayme[enterorder[0] - 1][i]);
1775      printf("\n   ");
1776      for (i = 0; i < nmlngth; i++)
1777        putchar(nayme[enterorder[1] - 1][i]);
1778      putchar('\n');
1779    }
1780    lastrearr = false;
1781    for (i = 3; i <= (spp); i++) {
1782      bestyet = -32000.0;
1783      item = treenode[enterorder[i - 1] - 1];
1784      nufork = treenode[spp + i - 2];
1785      addpreorder(root, item, nufork);
1786      add(there, item, nufork);
1787      like = bestyet;
1788      rearrange(&root);
1789      if (progress) {
1790        printf("   ");
1791        for (j = 0; j < nmlngth; j++)
1792          putchar(nayme[enterorder[i - 1] - 1][j]);
1793        putchar('\n');
1794      }
1795      lastrearr = (i == spp);
1796      if (lastrearr) {
1797        if (progress) {
1798          printf("\nDoing global rearrangements\n");
1799          printf("  !");
1800          for (j = 1; j <= (nonodes); j++)
1801            putchar('-');
1802          printf("!\n");
1803        }
1804        bestlike = bestyet;
1805        if (jumb == 1) {
1806          bstlike2 = bestlike;
1807          nextree = 1;
1808        }
1809        do {
1810          if (progress)
1811            printf("   ");
1812          gotlike = bestlike;
1813          for (j = 0; j < (nonodes); j++) {
1814            bestyet = -32000.0;
1815            item = treenode[j];
1816            if (item != root) {
1817              nufork = treenode[j]->back;
1818              re_move(&item, &nufork);
1819              there = root;
1820              addpreorder(root, item, nufork);
1821              add(there, item, nufork);
1822            }
1823            if (progress)
1824              putchar('.');
1825          }
1826          if (progress)
1827            putchar('\n');
1828        } while (bestlike > gotlike);
1829      }
1830    }
1831    if (progress)
1832      putchar('\n');
1833    for (i = spp - 1; i >= 1; i--)
1834      re_move(&treenode[i], &dummy);
1835    if (jumb == njumble) {
1836      if (treeprint) {
1837        putc('\n', outfile);
1838        if (nextree == 2)
1839          fprintf(outfile, "One most parsimonious tree found:\n");
1840        else
1841          fprintf(outfile, "%6ld trees in all found\n", nextree - 1);
1842      }
1843      if (nextree > maxtrees + 1) {
1844        if (treeprint)
1845          fprintf(outfile, "here are the first%4ld of them\n", (long)maxtrees);
1846        nextree = maxtrees + 1;
1847      }
1848      if (treeprint)
1849        putc('\n', outfile);
1850      for (i = 0; i <= (nextree - 2); i++) {
1851        root = treenode[0];
1852        add(treenode[0], treenode[1], treenode[spp]);
1853        for (j = 3; j <= spp; j++) {
1854          add(treenode[bestrees[i][j - 1] - 1], treenode[j - 1],
1855              treenode[spp + j - 2]);}
1856        evaluate(root);
1857        printree();
1858        describe();
1859        for (j = 1; j < (spp); j++)
1860          re_move(&treenode[j], &dummy);
1861      }
1862    }
1863  } else {
1864    fscanf(infile, "%ld%*[^\n]", &numtrees);
1865    getc(infile);
1866    if (treeprint) {
1867      fprintf(outfile, "User-defined tree");
1868      if (numtrees > 1)
1869        putc('s', outfile);
1870      fprintf(outfile, ":\n");
1871    }
1872    which = 1;
1873    while (which <= numtrees) {
1874      treeread();
1875      evaluate(root);
1876      printree();
1877      describe();
1878      which++;
1879    }
1880    fprintf(outfile, "\n\n");
1881    if (numtrees > 1 && chars > 1) {
1882      if (numtrees > maxuser) {
1883        printf("TOO MANY USER-DEFINED TREES");
1884        printf("  test performed on only the first%4ld of them\n",
1885               (long)maxuser);
1886      }
1887      fprintf(outfile, "Tree    Steps   Diff Steps   Its S.D.");
1888      fprintf(outfile, "   Significantly worse?\n\n");
1889      if (numtrees > maxuser)
1890        num = maxuser;
1891      else
1892        num = numtrees;
1893      for (which = 1; which <= num; which++) {
1894        fprintf(outfile, "%4ld%10.1f", which, nsteps[which - 1]);
1895        if (which == minwhich)
1896          fprintf(outfile, "  <------- best\n");
1897        else {
1898          sumw = 0;
1899          sum = 0.0;
1900          sum2 = 0.0;
1901          for (j = 0; j < (chars); j++) {
1902            if (weight[j] > 0) {
1903              sumw += weight[j];
1904              sum += fsteps[which - 1][j] - fsteps[minwhich - 1][j];
1905              TEMP = fsteps[which - 1][j] - fsteps[minwhich - 1][j];
1906              sum2 += TEMP * TEMP;
1907            }
1908          }
1909          TEMP = sum / sumw;
1910          sd = sqrt(sumw / (sumw - 1.0) * (sum2 - sum * sum / sumw));
1911          fprintf(outfile, "%8.1f%15.5f",
1912                  nsteps[which - 1] - minsteps, sd);
1913          if (sum > 1.95996 * sd)
1914            fprintf(outfile, "           Yes\n");
1915          else
1916            fprintf(outfile, "           No\n");
1917        }
1918      }
1919      fprintf(outfile, "\n\n");
1920    }
1921  }
1922  if (jumb == njumble) {
1923    if (progress) {
1924      printf("Output written to output file\n\n");
1925      if (trout)
1926        printf("Trees also written onto tree file\n\n");
1927    }
1928    free(steps);
1929  }
1930}  /* maketree */
1931
1932
1933main(argc, argv)
1934int argc;
1935Char *argv[];
1936{  /* Dollo or polymorphism parsimony by uphill search */
1937char infilename[100],outfilename[100],trfilename[100];
1938#ifdef MAC
1939  macsetup("Dollop","");
1940#endif
1941  /* reads in spp, chars, and the data. Then calls maketree to
1942     construct the tree */
1943  openfile(&infile,INFILE,"r",argv[0],infilename);
1944  openfile(&outfile,OUTFILE,"w",argv[0],outfilename);
1945
1946  ibmpc = ibmpc0;
1947  ansi = ansi0;
1948  vt52 = vt520;
1949  garbage = NULL;
1950  mulsets = false;
1951  datasets = 1;
1952  firstset = true;
1953  doinit();
1954  if (trout)
1955    openfile(&treefile,TREEFILE,"w",argv[0],NULL);
1956  extras = (steptr)Malloc(chars*sizeof(long));
1957  weight = (steptr)Malloc(chars*sizeof(long));
1958  threshwt = (double *)Malloc(chars*sizeof(double));
1959  if (usertree) {
1960    fsteps = (double **)Malloc(maxuser*sizeof(double *));
1961    for (j = 1; j <= maxuser; j++)
1962      fsteps[j - 1] = (double *)Malloc(chars*sizeof(double));
1963  }
1964  bestrees = (long **)Malloc(maxtrees*(sizeof(long *)));
1965  for (j = 1; j <= maxtrees; j++)
1966    bestrees[j - 1] = (long *)Malloc(spp*sizeof(long));
1967  numsteps = (steptr)Malloc(chars*sizeof(long));
1968  nayme = (Char **)Malloc(spp*sizeof(Char *));
1969  for (j = 1; j <= spp; j++)
1970    nayme[j - 1] = (Char *)Malloc(nmlngth*sizeof(Char));
1971  enterorder = (long *)Malloc(spp*sizeof(long));
1972  naymes = (boolean *)Malloc(spp*sizeof(boolean));
1973  place = (long *)Malloc(nonodes*sizeof(long));
1974  ancone = (boolean *)Malloc(chars*sizeof(boolean));
1975  anczero = (boolean *)Malloc(chars*sizeof(boolean));
1976  ancone0 = (boolean *)Malloc(chars*sizeof(boolean));
1977  anczero0 = (boolean *)Malloc(chars*sizeof(boolean));
1978  numsone = (steptr)Malloc(chars*sizeof(long));
1979  numszero = (steptr)Malloc(chars*sizeof(long));
1980  guess = (Char *)Malloc(chars*sizeof(Char));
1981  zeroanc = (bitptr)Malloc(words*sizeof(long));
1982  oneanc = (bitptr)Malloc(words*sizeof(long));
1983  for (ith = 1; ith <= (datasets); ith++) {
1984    doinput();
1985    if (ith == 1)
1986      firstset = false;
1987    if (datasets > 1) {
1988      fprintf(outfile, "Data set # %ld:\n\n",ith);
1989      if (progress)
1990        printf("\nData set # %ld:\n",ith);
1991    }
1992    for (jumb = 1; jumb <= njumble; jumb++)
1993      maketree();
1994  }
1995  FClose(infile);
1996  FClose(outfile);
1997  FClose(treefile);
1998#ifdef MAC
1999  fixmacfile(infilename);
2000  fixmacfile(outfilename);
2001#endif
2002  exit(0);
2003}  /* Dollo or polymorphism parsimony by uphill search */
2004
2005
2006int eof(f)
2007FILE *f;
2008{
2009    register int ch;
2010
2011    if (feof(f))
2012        return 1;
2013    if (f == stdin)
2014        return 0;
2015    ch = getc(f);
2016    if (ch == EOF)
2017        return 1;
2018    ungetc(ch, f);
2019    return 0;
2020}
2021
2022
2023int eoln(f)
2024FILE *f;
2025{
2026    register int ch;
2027
2028    ch = getc(f);
2029    if (ch == EOF)
2030        return 1;
2031    ungetc(ch, f);
2032    return (ch == '\n');
2033}
2034
2035void memerror()
2036{
2037printf("Error allocating memory\n");
2038exit(-1);
2039}
2040
2041MALLOCRETURN *mymalloc(x)
2042long x;
2043{
2044MALLOCRETURN *mem;
2045mem = (MALLOCRETURN *)malloc((size_t)x);
2046if (!mem)
2047  memerror();
2048else
2049  return (MALLOCRETURN *)mem;
2050}
2051
Note: See TracBrowser for help on using the repository browser.