source: branches/profile/GDE/PHYLIP/disc.c

Last change on this file was 2175, checked in by westram, 20 years ago

upgrade to PHYLIP 3.6

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 24.9 KB
Line 
1#include "phylip.h"
2#include "disc.h"
3
4/* version 3.6. (c) Copyright 1993-2002 by the University of Washington.
5   Written by Joseph Felsenstein, Akiko Fuseki, Sean Lamont, and Andrew Keeffe.
6   Permission is granted to copy and use this program provided no fee is
7   charged for it and provided that this copyright notice is not removed. */
8
9long chars, nonodes, nextree, which;
10/*  nonodes = number of nodes in tree                                        *
11 *  chars = number of binary characters                                      *
12 *  words = number of words needed to represent characters of one organism   */
13steptr weight, extras;
14boolean printdata;
15
16
17void inputdata(pointptr treenode,boolean dollo,boolean printdata,FILE *outfile)
18{
19  /* input the names and character state data for species */
20  /* used in Dollop, Dolpenny, Dolmove, & Move */
21  long i, j, l;
22  char k;
23  Char charstate;
24  /* possible states are '0', '1', 'P', 'B', and '?' */
25
26  if (printdata)
27    headings(chars, "Characters", "----------");
28  for (i = 0; i < (chars); i++)
29    extras[i] = 0;
30  for (i = 1; i <= spp; i++) {
31    initname(i-1);
32    if (printdata) {
33      for (j = 0; j < nmlngth; j++)
34        putc(nayme[i - 1][j], outfile);
35      fprintf(outfile, "   ");
36    }
37    for (j = 0; j < (words); j++) {
38      treenode[i - 1]->stateone[j] = 0;
39      treenode[i - 1]->statezero[j] = 0;
40    }
41    for (j = 1; j <= (chars); j++) {
42      k = (j - 1) % bits + 1;
43      l = (j - 1) / bits + 1;
44      do {
45        if (eoln(infile)) 
46          scan_eoln(infile);
47        charstate = gettc(infile);
48      } while (charstate == ' ');
49      if (charstate == 'b')
50        charstate = 'B';
51      if (charstate == 'p')
52        charstate = 'P';
53      if (charstate != '0' && charstate != '1' && charstate != '?' &&
54          charstate != 'P' && charstate != 'B') {
55        printf("\n\nERROR: Bad character state: %c ",charstate);
56        printf("at character %ld of species %ld\n\n", j, i);
57        exxit(-1);
58      }
59      if (printdata) {
60        newline(outfile, j, 55, nmlngth + 3);
61        putc(charstate, outfile);
62        if (j % 5 == 0)
63          putc(' ', outfile);
64      }
65      if (charstate == '1')
66        treenode[i - 1]->stateone[l - 1] =
67          ((long)treenode[i - 1]->stateone[l - 1]) | (1L << k);
68      if (charstate == '0')
69        treenode[i - 1]->statezero[l - 1] =
70          ((long)treenode[i - 1]->statezero[l - 1]) | (1L << k);
71      if (charstate == 'P' || charstate == 'B') {
72        if (dollo)
73          extras[j - 1] += weight[j - 1];
74        else {
75          treenode[i - 1]->stateone[l - 1] =
76            ((long)treenode[i - 1]->stateone[l - 1]) | (1L << k);
77          treenode[i - 1]->statezero[l - 1] =
78            ((long)treenode[i - 1]->statezero[l - 1]) | (1L << k);
79        }
80      }
81    }
82    scan_eoln(infile);
83    if (printdata)
84      putc('\n', outfile);
85  }
86  if (printdata)
87    fprintf(outfile, "\n\n");
88}  /* inputdata */
89
90
91void inputdata2(pointptr2 treenode)
92{
93  /* input the names and character state data for species */
94  /* used in Mix & Penny */
95  long i, j, l;
96  char k;
97  Char charstate;
98  /* possible states are '0', '1', 'P', 'B', and '?' */
99
100  if (printdata)
101    headings(chars, "Characters", "----------");
102  for (i = 0; i < (chars); i++)
103    extras[i] = 0;
104  for (i = 1; i <= spp; i++) {
105    initname(i-1);
106    if (printdata) {
107      for (j = 0; j < nmlngth; j++)
108        putc(nayme[i - 1][j], outfile);
109    }
110    fprintf(outfile, "   ");
111    for (j = 0; j < (words); j++) {
112      treenode[i - 1]->fulstte1[j] = 0;
113      treenode[i - 1]->fulstte0[j] = 0;
114      treenode[i - 1]->empstte1[j] = 0;
115      treenode[i - 1]->empstte0[j] = 0;
116    }
117    for (j = 1; j <= (chars); j++) {
118      k = (j - 1) % bits + 1;
119      l = (j - 1) / bits + 1;
120      do {
121        if (eoln(infile)) 
122          scan_eoln(infile);
123        charstate = gettc(infile);
124        if (charstate == '\n')
125          charstate = ' ';
126      } while (charstate == ' ');
127      if (charstate == 'b')          charstate = 'B';
128      if (charstate == 'p')          charstate = 'P';
129      if (charstate != '0' && charstate != '1' && charstate != '?' &&
130          charstate != 'P' && charstate != 'B') {
131        printf("\n\nERROR: Bad character state: %c ",charstate);
132        printf("at character %ld of species %ld\n\n", j, i);
133        exxit(-1);
134      }
135      if (printdata) {
136        newline(outfile, j, 55, nmlngth + 3);
137        putc(charstate, outfile);
138        if (j % 5 == 0)
139          putc(' ', outfile);
140      }
141      if (charstate == '1') {
142        treenode[i-1]->fulstte1[l-1] =
143          ((long)treenode[i-1]->fulstte1[l-1]) | (1L << k);
144        treenode[i-1]->empstte1[l-1] =
145          treenode[i-1]->fulstte1[l-1];
146      }
147      if (charstate == '0') {
148        treenode[i-1]->fulstte0[l-1] =
149          ((long)treenode[i-1]->fulstte0[l-1]) | (1L << k);
150        treenode[i-1]->empstte0[l-1] =
151          treenode[i-1]->fulstte0[l-1];
152      }
153      if (charstate == 'P' || charstate == 'B')
154        extras[j-1] += weight[j-1];
155    }
156    scan_eoln(infile);
157    if (printdata)
158      putc('\n', outfile);
159  }
160  fprintf(outfile, "\n\n");
161}  /* inputdata2 */
162
163
164void alloctree(pointptr *treenode)
165{
166  /* allocate tree nodes dynamically */
167  /* used in dollop, dolmove, dolpenny, & move */
168  long i, j;
169  node *p, *q;
170
171  (*treenode) = (pointptr)Malloc(nonodes*sizeof(node *));
172  for (i = 0; i < (spp); i++) {
173    (*treenode)[i] = (node *)Malloc(sizeof(node));
174    (*treenode)[i]->stateone = (bitptr)Malloc(words*sizeof(long));
175    (*treenode)[i]->statezero = (bitptr)Malloc(words*sizeof(long));
176  }
177  for (i = spp; i < (nonodes); i++) {
178    q = NULL;
179    for (j = 1; j <= 3; j++) {
180      p = (node *)Malloc(sizeof(node));
181      p->stateone = (bitptr)Malloc(words*sizeof(long));
182      p->statezero = (bitptr)Malloc(words*sizeof(long));
183      p->next = q;
184      q = p;
185    }
186    p->next->next->next = p;
187    (*treenode)[i] = p;
188  }
189}  /* alloctree */
190
191
192void alloctree2(pointptr2 *treenode)
193{
194  /* allocate tree nodes dynamically */
195  /* used in mix & penny */
196  long i, j;
197  node2 *p, *q;
198
199  (*treenode) = (pointptr2)Malloc(nonodes*sizeof(node2 *));
200  for (i = 0; i < (spp); i++) {
201    (*treenode)[i] = (node2 *)Malloc(sizeof(node2));
202    (*treenode)[i]->fulstte1 = (bitptr)Malloc(words*sizeof(long));
203    (*treenode)[i]->fulstte0 = (bitptr)Malloc(words*sizeof(long));
204    (*treenode)[i]->empstte1 = (bitptr)Malloc(words*sizeof(long));
205    (*treenode)[i]->empstte0 = (bitptr)Malloc(words*sizeof(long));
206    (*treenode)[i]->fulsteps = (bitptr)Malloc(words*sizeof(long));
207    (*treenode)[i]->empsteps = (bitptr)Malloc(words*sizeof(long));
208  }
209  for (i = spp; i < (nonodes); i++) {
210    q = NULL;
211    for (j = 1; j <= 3; j++) {
212      p = (node2 *)Malloc(sizeof(node2));
213      p->fulstte1 = (bitptr)Malloc(words*sizeof(long));
214      p->fulstte0 = (bitptr)Malloc(words*sizeof(long));
215      p->empstte1 = (bitptr)Malloc(words*sizeof(long));
216      p->empstte0 = (bitptr)Malloc(words*sizeof(long));
217      p->fulsteps = (bitptr)Malloc(words*sizeof(long));
218      p->empsteps = (bitptr)Malloc(words*sizeof(long));
219      p->next = q;
220      q = p;
221    }
222    p->next->next->next = p;
223    (*treenode)[i] = p;
224  }
225} /* alloctree2 */
226
227
228void setuptree(pointptr treenode)
229{
230  /* initialize tree nodes */
231  /* used in dollop, dolmove, dolpenny, & move */
232  long i;
233  node *p;
234
235  for (i = 1; i <= (nonodes); i++) {
236    treenode[i-1]->back = NULL;
237    treenode[i-1]->tip = (i <= spp);
238    treenode[i-1]->index = i;
239    if (i > spp) {
240      p = treenode[i-1]->next;
241      while (p != treenode[i-1]) {
242        p->back = NULL;
243        p->tip = false;
244        p->index = i;
245        p = p->next;
246      }
247    }
248  }
249} /* setuptree */
250
251
252void setuptree2(pointptr2 treenode)
253{
254  /* initialize tree nodes */
255  /* used in mix & penny */
256  long i;
257  node2 *p;
258
259  for (i = 1; i <= (nonodes); i++) {
260    treenode[i-1]->back = NULL;
261    treenode[i-1]->tip = (i <= spp);
262    treenode[i-1]->index = i;
263    if (i > spp) {
264      p = treenode[i-1]->next;
265      while (p != treenode[i-1]) {
266        p->back = NULL;
267        p->tip = false;
268        p->index = i;
269        p = p->next;
270      }
271    }
272  }
273} /* setuptree2 */
274
275
276void inputancestors(boolean *anczero0, boolean *ancone0)
277{
278  /* reads the ancestral states for each character */
279  /* used in dollop, dolmove, dolpenny, mix, move, & penny */
280  long i;
281  Char ch;
282
283  for (i = 1; i < nmlngth; i++)
284    gettc(infile);
285  for (i = 0; i < (chars); i++) {
286    anczero0[i] = true;
287    ancone0[i] = true;
288    do {
289      if (eoln(infile))
290        scan_eoln(infile);
291      ch = gettc(infile);
292      if (ch == '\n')
293        ch = ' ';
294    } while (ch == ' ');
295    if (ch == 'p')
296      ch = 'P';
297    if (ch == 'b')
298      ch = 'B';
299    if (strchr("10PB?",ch) != NULL){
300      anczero0[i] = (ch == '1') ? false : anczero0[i];
301      ancone0[i] = (ch == '0') ? false : ancone0[i];
302    } else {
303      printf("BAD ANCESTOR STATE: %cAT CHARACTER %4ld\n", ch, i + 1);
304      exxit(-1);
305    }
306  }
307  scan_eoln(infile);
308}  /* inputancestors */
309
310void inputancestorsnew(boolean *anczero0, boolean *ancone0)
311{
312  /* reads the ancestral states for each character */
313  /* used in dollop, dolmove, dolpenny, mix, move, & penny */
314  long i;
315  Char ch;
316
317  for (i = 0; i < (chars); i++) {
318    anczero0[i] = true;
319    ancone0[i] = true;
320    do {
321      if (eoln(ancfile))
322        scan_eoln(ancfile);
323      ch = gettc(ancfile);
324      if (ch == '\n')
325        ch = ' ';
326    } while (ch == ' ');
327    if (ch == 'p')
328      ch = 'P';
329    if (ch == 'b')
330      ch = 'B';
331    if (strchr("10PB?",ch) != NULL){
332      anczero0[i] = (ch == '1') ? false : anczero0[i];
333      ancone0[i] = (ch == '0') ? false : ancone0[i];
334    } else {
335      printf("BAD ANCESTOR STATE: %cAT CHARACTER %4ld\n", ch, i + 1);
336      exxit(-1);
337    }
338  }
339  scan_eoln(ancfile);
340}  /* inputancestorsnew */
341
342
343void printancestors(FILE *filename, boolean *anczero, boolean *ancone)
344{
345  /* print out list of ancestral states */
346  /* used in dollop, dolmove, dolpenny, mix, move, & penny */
347  long i;
348
349  fprintf(filename, "    Ancestral states:\n");
350  for (i = 1; i <= nmlngth + 3; i++)
351    putc(' ', filename);
352  for (i = 1; i <= (chars); i++) {
353    newline(filename, i, 55, nmlngth + 3);
354    if (ancone[i-1] && anczero[i-1])
355      putc('?', filename);
356    else if (ancone[i-1])
357      putc('1', filename);
358    else
359      putc('0', filename);
360    if (i % 5 == 0)
361      putc(' ', filename);
362  }
363  fprintf(filename, "\n\n");
364}  /* printancestor */
365
366
367void add(node *below, node *newtip, node *newfork, node **root,
368                        pointptr treenode)
369{
370  /* inserts the nodes newfork and its left descendant, newtip,
371     to the tree.  below becomes newfork's right descendant.
372     The global variable root is also updated */
373  /* used in dollop & dolpenny */
374
375  if (below != treenode[below->index - 1])
376    below = treenode[below->index - 1];
377  if (below->back != NULL)
378    below->back->back = newfork;
379  newfork->back = below->back;
380  below->back = newfork->next->next;
381  newfork->next->next->back = below;
382  newfork->next->back = newtip;
383  newtip->back = newfork->next;
384  if (*root == below)
385    *root = newfork;
386}  /* add */
387
388
389void add2(node *below, node *newtip, node *newfork, node **root,
390                        boolean restoring, boolean wasleft, pointptr treenode)
391{
392  /* inserts the nodes newfork and its left descendant, newtip,
393     to the tree.   below becomes newfork's right descendant */
394  /* used in move & dolmove */
395  boolean putleft;
396  node *leftdesc, *rtdesc;
397
398  if (below != treenode[below->index - 1])
399    below = treenode[below->index - 1];
400  if (below->back != NULL)
401    below->back->back = newfork;
402  newfork->back = below->back;
403  putleft = true;
404  if (restoring)
405    putleft = wasleft;
406  if (putleft) {
407    leftdesc = newtip;
408    rtdesc = below;
409  } else {
410    leftdesc = below;
411    rtdesc = newtip;
412  }
413  rtdesc->back = newfork->next->next;
414  newfork->next->next->back = rtdesc;
415  newfork->next->back = leftdesc;
416  leftdesc->back = newfork->next;
417  if (*root == below)
418    *root = newfork;
419  (*root)->back = NULL;
420}  /* add2 */
421
422
423void add3(node2 *below, node2 *newtip, node2 *newfork, node2 **root,
424                        pointptr2 treenode)
425{
426  /* inserts the nodes newfork and its left descendant, newtip,
427     to the tree.  below becomes newfork's right descendant.
428     The global variable root is also updated */
429  /* used in mix & penny */
430  node2 *p;
431
432  if (below != treenode[below->index - 1])
433    below = treenode[below->index - 1];
434  if (below->back != NULL)
435    below->back->back = newfork;
436  newfork->back = below->back;
437  below->back = newfork->next->next;
438  newfork->next->next->back = below;
439  newfork->next->back = newtip;
440  newtip->back = newfork->next;
441  if (*root == below)
442    *root = newfork;
443  (*root)->back = NULL;
444  p = newfork;
445  do {
446    p->visited = false;
447    p = p->back;
448    if (p != NULL) p = treenode[p->index - 1];
449  } while (p != NULL);
450}  /* add3 */
451
452
453void re_move(node **item, node **fork, node **root, pointptr treenode)
454{
455  /* removes nodes item and its ancestor, fork, from the tree.
456     the new descendant of fork's ancestor is made to be
457     fork's second descendant (other than item).  Also
458     returns pointers to the deleted nodes, item and fork.
459     The global variable root is also updated */
460  /* used in dollop & dolpenny */
461  node *p, *q;
462
463  if ((*item)->back == NULL) {
464    *fork = NULL;
465    return;
466  }
467  *fork = treenode[(*item)->back->index - 1];
468  if (*root == *fork) {
469    if (*item == (*fork)->next->back)
470      *root = (*fork)->next->next->back;
471    else
472      *root = (*fork)->next->back;
473  }
474  p = (*item)->back->next->back;
475  q = (*item)->back->next->next->back;
476  if (p != NULL)
477    p->back = q;
478  if (q != NULL)
479    q->back = p;
480  (*fork)->back = NULL;
481  p = (*fork)->next;
482  while (p != *fork) {
483    p->back = NULL;
484    p = p->next;
485  }
486  (*item)->back = NULL;
487}  /* re_move */
488
489
490void re_move2(node **item, node **fork, node **root, boolean *wasleft,
491                        pointptr treenode)
492{
493  /* removes nodes item and its ancestor, fork, from the tree.
494     the new descendant of fork's ancestor is made to be
495     fork's second descendant (other than item).   Also
496     returns pointers to the deleted nodes, item and fork */
497  /* used in move & dolmove */
498  node *p, *q;
499
500  if ((*item)->back == NULL) {
501    *fork = NULL;
502    return;
503  }
504  *fork = treenode[(*item)->back->index - 1];
505  if (*item == (*fork)->next->back) {
506    if (*root == *fork)
507      *root = (*fork)->next->next->back;
508    (*wasleft) = true;
509  } else {
510    if (*root == *fork)
511      *root = (*fork)->next->back;
512    (*wasleft) = false;
513  }
514  p = (*item)->back->next->back;
515  q = (*item)->back->next->next->back;
516  if (p != NULL)
517    p->back = q;
518  if (q != NULL)
519    q->back = p;
520  (*fork)->back = NULL;
521  p = (*fork)->next;
522  while (p != *fork) {
523    p->back = NULL;
524    p = p->next;
525  }
526  (*item)->back = NULL;
527}  /* re_move2 */
528
529
530void re_move3(node2 **item, node2 **fork, node2 **root, pointptr2 treenode)
531{
532  /* removes nodes item and its ancestor, fork, from the tree.
533     the new descendant of fork's ancestor is made to be
534     fork's second descendant (other than item).  Also
535     returns pointers to the deleted nodes, item and fork.
536     The global variable *root is also updated */
537  /* used in mix & penny */
538  node2 *p, *q;
539
540  if ((*item)->back == NULL) {
541    *fork = NULL;
542    return;
543  }
544  *fork = treenode[(*item)->back->index - 1];
545  if (*root == *fork) {
546    if (*item == (*fork)->next->back)
547      *root = (*fork)->next->next->back;
548    else
549      *root = (*fork)->next->back;
550  }
551  p = (*item)->back->next->back;
552  q = (*item)->back->next->next->back;
553  if (p != NULL)
554    p->back = q;
555  if (q != NULL)
556    q->back = p;
557  q = (*fork)->back;
558  (*fork)->back = NULL;
559  p = (*fork)->next;
560  while (p != *fork) {
561    p->back = NULL;
562    p = p->next;
563  }
564  (*item)->back = NULL;
565  if (q != NULL) q = treenode[q->index - 1];
566  while (q != NULL) {
567    q-> visited = false;
568    q = q->back;
569    if (q != NULL) q = treenode[q->index - 1];
570  }
571}  /* re_move3 */
572
573
574void coordinates(node *p, long *tipy, double f, long *fartemp)
575{
576  /* establishes coordinates of nodes */
577  /* used in dollop, dolpenny, dolmove, & move */
578  node *q, *first, *last;
579
580  if (p->tip) {
581    p->xcoord = 0;
582    p->ycoord = *tipy;
583    p->ymin = *tipy;
584    p->ymax = *tipy;
585    *tipy += down;
586    return;
587  }
588  q = p->next;
589  do {
590    coordinates(q->back, tipy, f, fartemp);
591    q = q->next;
592  } while (p != q);
593  first = p->next->back;
594  q = p->next;
595  while (q->next != p)
596    q = q->next;
597  last = q->back;
598  p->xcoord = (last->ymax - first->ymin) * f;
599  p->ycoord = (first->ycoord + last->ycoord) / 2;
600  p->ymin = first->ymin;
601  p->ymax = last->ymax;
602  if (p->xcoord > *fartemp)
603    *fartemp = p->xcoord;
604}  /* coordinates */
605
606void coordinates2(node2 *p, long *tipy)
607{
608  /* establishes coordinates2 of nodes */
609  node2 *q, *first, *last;
610
611  if (p->tip) {
612    p->xcoord = 0;
613    p->ycoord = *tipy;
614    p->ymin = *tipy;
615    p->ymax = *tipy;
616    (*tipy) += down;
617    return;
618  }
619  q = p->next;
620  do {
621    coordinates2(q->back, tipy);
622    q = q->next;
623  } while (p != q);
624  first = p->next->back;
625  q = p->next;
626  while (q->next != p)
627    q = q->next;
628  last = q->back;
629  p->xcoord = last->ymax - first->ymin;
630  p->ycoord = (first->ycoord + last->ycoord) / 2;
631  p->ymin = first->ymin;
632  p->ymax = last->ymax;
633}  /* coordinates2 */
634
635
636void treeout(node *p, long nextree, long *col, node *root)
637{
638  /* write out file with representation of final tree */
639  /* used in dollop, dolmove, dolpenny, & move */
640  long i, n;
641  Char c;
642  node *q;
643
644  if (p->tip) {
645    n = 0;
646    for (i = 1; i <= nmlngth; i++) {
647      if (nayme[p->index - 1][i - 1] != ' ')
648        n = i;
649    }
650    for (i = 0; i < n; i++) {
651      c = nayme[p->index - 1][i];
652      if (c == ' ')
653        c = '_';
654      putc(c, outtree);
655    }
656    *col += n;
657  } else {
658    q = p->next;
659    putc('(', outtree);
660    (*col)++;
661    while (q != p) {
662      treeout(q->back, nextree, col, root);
663      q = q->next;
664      if (q == p)
665        break;
666      putc(',', outtree);
667      (*col)++;
668      if (*col > 65) {
669        putc('\n', outtree);
670        *col = 0;
671      }
672    }
673    putc(')', outtree);
674    (*col)++;
675  }
676  if (p != root)
677    return;
678  if (nextree > 2)
679    fprintf(outtree, "[%6.4f];\n", 1.0 / (nextree - 1));
680  else
681    fprintf(outtree, ";\n");
682}  /* treeout */
683
684void treeout2(node2 *p, long *col, node2 *root)
685{
686  /* write out file with representation of final tree */
687  /* used in mix & penny */
688  long i, n;
689  Char c;
690
691  if (p->tip) {
692    n = 0;
693    for (i = 1; i <= nmlngth; i++) {
694      if (nayme[p->index - 1][i - 1] != ' ')
695        n = i;
696    }
697    for (i = 0; i < n; i++) {
698      c = nayme[p->index - 1][i];
699      if (c == ' ')
700        c = '_';
701      putc(c, outtree);
702    }
703    *col += n;
704  } else {
705    putc('(', outtree);
706    (*col)++;
707    treeout2(p->next->back, col, root);
708    putc(',', outtree);
709    (*col)++;
710    if (*col > 65) {
711      putc('\n', outtree);
712      *col = 0;
713    }
714    treeout2(p->next->next->back, col, root);
715    putc(')', outtree);
716    (*col)++;
717  }
718  if (p != root)
719    return;
720  if (nextree > 2)
721    fprintf(outtree, "[%6.4f];\n", 1.0 / (nextree - 1));
722  else
723    fprintf(outtree, ";\n");
724}  /* treeout2 */
725
726
727void standev(long numtrees, long minwhich, double minsteps,
728                        double *nsteps, double **fsteps, longer seed)
729{  /* compute and write standard deviation of user trees */
730   /* used in pars */
731  long i, j, k;
732  double wt, sumw, sum, sum2, sd;
733  double temp;
734  double **covar, *P, *f;
735
736#define SAMPLES 1000
737#define MAXSHIMOTREES 1000
738/* ????? if numtrees too big for Shimo, truncate */
739  if (numtrees > maxuser) {
740    printf("TOO MANY USER-DEFINED TREES");
741    printf("  test only performed in the first %ld of them\n", (long)maxuser);
742  } else
743  if (numtrees == 2) {
744    fprintf(outfile, "Kishino-Hasegawa-Templeton test\n\n");
745    fprintf(outfile, "Tree    Steps   Diff Steps   Its S.D.");
746    fprintf(outfile, "   Significantly worse?\n\n");
747    which = 1;
748    while (which <= numtrees) {
749      fprintf(outfile, "%3ld%10.1f", which, nsteps[which - 1]);
750      if (minwhich == which)
751        fprintf(outfile, "  <------ best\n");
752      else {
753        sumw = 0.0;
754        sum = 0.0;
755        sum2 = 0.0;
756        for (i = 0; i < chars; i++) {
757          if (weight[i] > 0) {
758            wt = weight[i];
759            sumw += wt;
760            temp = (fsteps[which - 1][i] - fsteps[minwhich - 1][i]);
761            sum += temp;
762            sum2 += temp * temp / wt;
763          }
764        }
765        temp = sum / sumw;
766        sd = sqrt(sumw / (sumw - 1.0) * (sum2 - temp * temp));
767        fprintf(outfile, "%10.1f%12.4f",
768                (nsteps[which - 1] - minsteps) / 10, sd);
769        if (sum > 1.95996 * sd)
770          fprintf(outfile, "           Yes\n");
771        else
772          fprintf(outfile, "           No\n");
773      }
774      which++;
775    }
776    fprintf(outfile, "\n\n");
777  } else {           /* Shimodaira-Hasegawa test using normal approximation */
778    fprintf(outfile, "Shimodaira-Hasegawa test\n\n");
779    covar = (double **)Malloc(numtrees*sizeof(double *)); 
780    for (i = 0; i < numtrees; i++)
781      covar[i] = (double *)Malloc(numtrees*sizeof(double)); 
782    sumw = 0.0;
783    for (i = 0; i < chars; i++)
784      sumw += weight[i];
785    for (i = 0; i < numtrees; i++) {        /* compute covariances of trees */
786      sum = nsteps[i]/sumw;
787      for (j = 0; j <=i; j++) {
788        sum2 = nsteps[j]/sumw;
789        temp = 0.0;
790        for (k = 0; k < chars; k++) {
791          if (weight[k] > 0)
792            temp = temp + weight[k]*(fsteps[i][k]-sum) *(fsteps[j][k]-sum2);
793        }
794        covar[i][j] = temp;
795        if (i != j)
796          covar[j][i] = temp;
797      }
798    }
799    for (i = 0; i < numtrees; i++) { /* in-place Cholesky decomposition
800                                        of trees x trees covariance matrix */
801      sum = 0.0;
802      for (j = 0; j <= i-1; j++)
803        sum = sum + covar[i][j] * covar[i][j];
804      if (covar[i][i]-sum <= 0.0)
805        temp = 0.0;
806      else 
807        temp = sqrt(covar[i][i] - sum);
808      covar[i][i] = temp;
809      for (j = i+1; j < numtrees; j++) {
810        sum = 0.0;
811        for (k = 0; k < i; k++)
812          sum = sum + covar[i][k] * covar[j][k];
813        if (fabs(temp) < 1.0E-12)
814          covar[j][i] = 0.0;
815        else 
816          covar[j][i] = (covar[j][i] - sum)/temp;
817      }
818    }
819    f = (double *)Malloc(numtrees*sizeof(double)); /* vector of P's of trees */
820    P = (double *)Malloc(numtrees*sizeof(double)); /* vector of P's of trees */
821    for (i = 0; i < numtrees; i++)
822      P[i] = 0.0;
823    sum2 = nsteps[0];               /* sum2 will be smallest # of steps */
824    for (i = 1; i < numtrees; i++)
825      if (sum2 > nsteps[i])
826        sum2 = nsteps[i];
827    for (i = 1; i < SAMPLES; i++) {           /* loop over resampled trees */
828      for (j = 0; j < numtrees; j++) {        /* draw vectors */
829        sum = 0.0;
830        for (k = 0; k <= j; k++)
831          sum += normrand(seed)*covar[j][k];
832        f[j] = sum;
833      }
834      sum = f[1];
835      for (j = 1; j < numtrees; j++)          /* get min of vector */
836        if (f[j] < sum)
837          sum = f[j];
838      for (j = 0; j < numtrees; j++)          /* accumulate P's */
839        if (nsteps[j]-sum2 < f[j] - sum)
840          P[j] += 1.0/SAMPLES;
841    }
842    fprintf(outfile, "Tree    Steps   Diff Steps   P value");
843    fprintf(outfile, "   Significantly worse?\n\n");
844    for (i = 0; i < numtrees; i++) {
845      fprintf(outfile, "%3ld%10.1f", i+1, nsteps[i]);
846      if ((minwhich-1) == i)
847        fprintf(outfile, "  <------ best\n");
848      else {
849        fprintf(outfile, "  %9.1f %10.3f", nsteps[i]-sum2, P[i]);
850        if (P[i] < 0.05)
851          fprintf(outfile, "           Yes\n");
852        else
853          fprintf(outfile, "           No\n");
854      }
855    }
856  fprintf(outfile, "\n");
857  free(P);             /* free the variables we Malloc'ed */
858  free(f);
859  for (i = 0; i < numtrees; i++)
860    free(covar[i]);
861  free(covar);
862  }
863}  /* standev */
864
865
866void guesstates(Char *guess)
867{ /* write best guesses of ancestral states */
868  /* used in dollop, dolpenny, mix, & penny */
869  long i, j;
870
871  fprintf(outfile, "best guesses of ancestral states:\n");
872  fprintf(outfile, "      ");
873  for (i = 0; i <= 9; i++)
874    fprintf(outfile, "%2ld", i);
875  fprintf(outfile, "\n     *--------------------\n");
876  for (i = 0; i <= (chars / 10); i++) {
877    fprintf(outfile, "%5ld!", i * 10);
878    for (j = 0; j <= 9; j++) {
879      if (i * 10 + j == 0 || i * 10 + j > chars)
880        fprintf(outfile, "  ");
881      else
882        fprintf(outfile, " %c", guess[i * 10 + j - 1]);
883    }
884    putc('\n', outfile);
885  }
886  putc('\n', outfile);
887}  /* guesstates */
888
889
890void freegarbage(gbit **garbage)
891{
892  /* used in dollop, dolpenny, mix, & penny */
893  gbit *p;
894
895  while (*garbage) {
896    p = *garbage;
897    *garbage = (*garbage)->next;
898    free(p->bits_);
899    free(p);
900  }
901}  /* freegarbage */
902
903
904void disc_gnu(gbit **p, gbit **grbg)
905{
906  /* this is a do-it-yourself garbage collectors for move
907     Make a new node or pull one off the garbage list */
908
909  if (*grbg != NULL) {
910    *p = *grbg;
911    *grbg = (*grbg)->next;
912  } else {
913    *p = (gbit *)Malloc(sizeof(gbit));
914    (*p)->bits_ = (bitptr)Malloc(words*sizeof(long));
915  }
916  (*p)->next       = NULL;
917}  /* disc_gnu */
918
919
920void disc_chuck(gbit *p, gbit **grbg)
921{
922  /* collect garbage on p -- put it on front of garbage list */
923  p->next = *grbg;
924  *grbg = p;
925}  /* disc_chuck */
926
Note: See TracBrowser for help on using the repository browser.