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