-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcons.c
executable file
·1457 lines (1333 loc) · 37.6 KB
/
cons.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "phylip.h"
#include "cons.h"
int tree_pairing;
Char outfilename[FNMLNGTH], intreename[FNMLNGTH], intree2name[FNMLNGTH], outtreename[FNMLNGTH];
node *root;
long numopts, outgrno, col, setsz;
long maxgrp; /* max. no. of groups in all trees found */
boolean trout, firsttree, noroot, outgropt, didreroot, prntsets,
progress, treeprint, goteof, strict, mr=false, mre=false,
ml=false; /* initialized all false for Treedist */
pointarray nodep;
pointarray treenode;
group_type **grouping, **grping2, **group2;/* to store groups found */
double *lengths, *lengths2;
long **order, **order2, lasti;
group_type *fullset;
node *grbg;
long tipy;
double **timesseen, **tmseen2, **times2 ;
double trweight, ntrees, mlfrac;
/* prototypes */
void censor(void);
boolean compatible(long, long);
void elimboth(long);
void enternohash(group_type*, long*);
void enterpartition (group_type*, long*);
void reorient(node* n);
/* begin hash table code */
#define NUM_BUCKETS 100
typedef struct namenode {
struct namenode *next;
plotstring naym;
int hitCount;
} namenode;
typedef namenode **hashtype;
hashtype hashp;
long namesGetBucket(plotstring);
void namesAdd(plotstring);
boolean namesSearch(plotstring);
void namesDelete(plotstring);
void namesClearTable(void);
void namesCheckTable(void);
void missingnameRecurs(node *p);
/**
* namesGetBucket - return the bucket for a given name
*/
long namesGetBucket(plotstring searchname) {
long i;
long sum = 0;
for (i = 0; (i < MAXNCH) && (searchname[i] != '\0'); i++) {
sum += searchname[i];
}
return (sum % NUM_BUCKETS);
}
/**
* namesAdd - add a name to the hash table
*
* The argument is added at the head of the appropriate linked list. No
* checking is done for duplicates. The caller can call
* namesSearch to check for an existing name prior to calling
* namesAdd.
*/
void namesAdd(plotstring addname) {
long bucket = namesGetBucket(addname);
namenode *hp, *temp;
temp = hashp[bucket];
hashp[bucket] = (namenode *)Malloc(sizeof(namenode));
hp = hashp[bucket];
strcpy(hp->naym, addname);
hp->next = temp;
hp->hitCount = 0;
}
/**
* namesSearch - search for a name in the hash table
*
* Return true if the name is found, else false.
*/
boolean namesSearch(plotstring searchname) {
long i = namesGetBucket(searchname);
namenode *p;
p = hashp[i];
if (p == NULL) {
return false;
}
do {
if (strcmp(searchname,p->naym) == 0) {
p->hitCount++;
return true;
}
p = p->next;
} while (p != NULL);
return false;
}
/**
* Go through hash table and check that the hit count on all entries is one.
* If it is zero, then a species was missed, if it is two, then there is a
* duplicate species.
*/
void namesCheckTable(void) {
namenode *p;
long i;
for (i=0; i< NUM_BUCKETS; i++) {
p = hashp[i];
while (p != NULL){
if(p->hitCount >1){
printf("\n\nERROR in user tree: duplicate name found: ");
puts(p->naym);
printf("\n\n");
exxit(-1);
} else if(p->hitCount == 0){
printf("\n\nERROR in user tree: name %s not found\n\n\n",
p->naym);
exxit(-1);
}
p->hitCount = 0;
p = p->next;
}
}
}
/**
* namesClearTable - empty names out of the table and
* return allocated memory
*/
void namesClearTable(void) {
long i;
namenode *p, *temp;
for (i=0; i< NUM_BUCKETS; i++) {
p = hashp[i];
if (p != NULL) {
do {
temp = p;
p = p->next;
free(temp);
} while (p != NULL);
hashp[i] = NULL;
}
}
}
/* end hash table code */
void initconsnode(node **p, node **grbg, node *q, long len, long nodei,
long *ntips, long *parens, initops whichinit,
pointarray treenode, pointarray nodep, Char *str,
Char *ch, FILE *intree)
{
/* initializes a node */
long i;
char c;
boolean minusread;
double valyew, divisor, fracchange;
switch (whichinit) {
case bottom:
gnu(grbg, p);
(*p)->index = nodei;
(*p)->tip = false;
for (i=0; i<MAXNCH; i++)
(*p)->nayme[i] = '\0';
nodep[(*p)->index - 1] = (*p);
(*p)->v = 0;
break;
case nonbottom:
gnu(grbg, p);
(*p)->index = nodei;
(*p)->v = 0;
break;
case tip:
(*ntips)++;
gnu(grbg, p);
nodep[(*ntips) - 1] = *p;
setupnode(*p, *ntips);
(*p)->tip = true;
strncpy ((*p)->nayme, str, MAXNCH);
if (firsttree && prntsets) {
fprintf(outfile, " %ld. ", *ntips);
for (i = 0; i < len; i++)
putc(str[i], outfile);
putc('\n', outfile);
if ((*ntips > 0) && (((*ntips) % 10) == 0))
putc('\n', outfile);
}
(*p)->v = 0;
break;
case length:
processlength(&valyew, &divisor, ch, &minusread, intree, parens);
fracchange = 1.0;
(*p)->v = valyew / divisor / fracchange;
break;
case treewt:
if (!eoln(intree)) {
fscanf(intree, "%lf", &trweight);
getch(ch, parens, intree);
if (*ch != ']') {
printf("\n\nERROR: Missing right square bracket\n\n");
exxit(-1);
} else {
getch(ch, parens, intree);
if (*ch != ';') {
printf("\n\nERROR: Missing semicolon after square brackets\n\n");
exxit(-1);
}
}
}
break;
case unittrwt:
/* This comes not only when setting trweight but also at the end of
* any tree. The following code saves the current position in a
* file and reads to a new line. If there is a new line then we're
* at the end of tree, otherwise warn the user. This function should
* really leave the file alone, so once we're done with 'intree'
* we seek the position back so that it doesn't look like we did
* anything */
trweight = 1.0 ;
i = ftell (intree);
c = ' ';
while (c == ' ') {
if (eoff(intree)) {
fseek(intree,i,SEEK_SET);
return;
}
c = gettc(intree);
}
fseek(intree,i,SEEK_SET);
if ( c != '\n' && c!= '\r')
printf("WARNING: Tree weight set to 1.0\n");
if ( c == '\r' )
if ( (c == gettc(intree)) != '\n')
ungetc(c, intree);
break;
case hsnolength:
(*p)->v = -1; /* signal value that a length is missing */
break;
default: /* cases hslength, iter, hsnolength */
break; /* should there be an error message here?*/
}
} /* initconsnode */
void censor(void)
{
/* delete groups that are too rare to be in the consensus tree */
long i;
i = 1;
do {
if (timesseen[i-1])
if (!(mre || (mr && (2*(*timesseen[i-1]) > ntrees))
|| (ml && ((*timesseen[i-1]) > mlfrac*ntrees))
|| (strict && ((*timesseen[i-1]) == ntrees)))) {
free(grouping[i - 1]);
free(timesseen[i - 1]);
grouping[i - 1] = NULL;
timesseen[i - 1] = NULL;
}
i++;
} while (i < maxgrp);
} /* censor */
void compress(long *n)
{
/* push all the nonempty subsets to the front end of their array */
long i, j;
i = 1;
j = 1;
do {
while (grouping[i - 1] != NULL)
i++;
if (j <= i)
j = i + 1;
while ((grouping[j - 1] == NULL) && (j < maxgrp))
j++;
if (j < maxgrp) {
grouping[i - 1] = (group_type *)Malloc(setsz * sizeof(group_type));
timesseen[i - 1] = (double *)Malloc(sizeof(double));
memcpy(grouping[i - 1], grouping[j - 1], setsz * sizeof(group_type));
*timesseen[i - 1] = *timesseen[j - 1];
free(grouping[j - 1]);
free(timesseen[j - 1]);
grouping[j - 1] = NULL;
timesseen[j - 1] = NULL;
}
} while (j != maxgrp);
(*n) = i - 1;
} /* compress */
void sort(long n)
{
/* Shell sort keeping grouping, timesseen in same order */
long gap, i, j;
group_type *stemp;
double rtemp;
gap = n / 2;
stemp = (group_type *)Malloc(setsz * sizeof(group_type));
while (gap > 0) {
for (i = gap + 1; i <= n; i++) {
j = i - gap;
while (j > 0) {
if (*timesseen[j - 1] < *timesseen[j + gap - 1]) {
memcpy(stemp, grouping[j - 1], setsz * sizeof(group_type));
memcpy(grouping[j - 1], grouping[j + gap - 1], setsz * sizeof(group_type));
memcpy(grouping[j + gap - 1], stemp, setsz * sizeof(group_type));
rtemp = *timesseen[j - 1];
*timesseen[j - 1] = *timesseen[j + gap - 1];
*timesseen[j + gap - 1] = rtemp;
}
j -= gap;
}
}
gap /= 2;
}
free(stemp);
} /* sort */
boolean compatible(long i, long j)
{
/* are groups i and j compatible? */
boolean comp;
long k;
comp = true;
for (k = 0; k < setsz; k++)
if ((grouping[i][k] & grouping[j][k]) != 0)
comp = false;
if (!comp) {
comp = true;
for (k = 0; k < setsz; k++)
if ((grouping[i][k] & ~grouping[j][k]) != 0)
comp = false;
if (!comp) {
comp = true;
for (k = 0; k < setsz; k++)
if ((grouping[j][k] & ~grouping[i][k]) != 0)
comp = false;
if (!comp) {
comp = noroot;
if (comp) {
for (k = 0; k < setsz; k++)
if ((fullset[k] & ~grouping[i][k] & ~grouping[j][k]) != 0)
comp = false;
}
}
}
}
return comp;
} /* compatible */
void eliminate(long *n, long *n2)
{
/* eliminate groups incompatible with preceding ones */
long i, j, k;
boolean comp;
for (i = 2; i <= (*n); i++) {
comp = true;
for (j = 0; comp && (j <= i - 2); j++) {
if ((timesseen[j] != NULL) && *timesseen[j] > 0) {
comp = compatible(i-1,j);
if (!comp) {
(*n2)++;
times2[(*n2) - 1] = (double *)Malloc(sizeof(double));
group2[(*n2) - 1] = (group_type *)Malloc(setsz * sizeof(group_type));
*times2[(*n2) - 1] = *timesseen[i - 1];
memcpy(group2[(*n2) - 1], grouping[i - 1], setsz * sizeof(group_type));
*timesseen[i - 1] = 0.0;
for (k = 0; k < setsz; k++)
grouping[i - 1][k] = 0;
}
}
}
if (*timesseen[i - 1] == 0.0) {
free(grouping[i - 1]);
free(timesseen[i - 1]);
timesseen[i - 1] = NULL;
grouping[i - 1] = NULL;
}
}
} /* eliminate */
void printset(long n)
{
/* print out the n sets of species */
long i, j, k, size;
boolean noneprinted;
fprintf(outfile, "\nSet (species in order) ");
for (i = 1; i <= spp - 25; i++)
putc(' ', outfile);
fprintf(outfile, " How many times out of %7.2f\n\n", ntrees);
noneprinted = true;
for (i = 0; i < n; i++) {
if ((timesseen[i] != NULL) && (*timesseen[i] > 0)) {
size = 0;
k = 0;
for (j = 1; j <= spp; j++) {
if (j == ((k+1)*SETBITS+1)) k++;
if (((1L << (j - 1 - k*SETBITS)) & grouping[i][k]) != 0)
size++;
}
if (size != 1 && !(noroot && size >= (spp-1))) {
noneprinted = false;
k = 0;
for (j = 1; j <= spp; j++) {
if (j == ((k+1)*SETBITS+1)) k++;
if (((1L << (j - 1 - k*SETBITS)) & grouping[i][k]) != 0)
putc('*', outfile);
else
putc('.', outfile);
if (j % 10 == 0)
putc(' ', outfile);
}
for (j = 1; j <= 23 - spp; j++)
putc(' ', outfile);
fprintf(outfile, " %5.2f\n", *timesseen[i]);
}
}
}
if (noneprinted)
fprintf(outfile, " NONE\n");
} /* printset */
void bigsubset(group_type *st, long n)
{
/* Find a maximal subset of st among the n groupings,
to be the set at the base of the tree. */
long i, j;
group_type *su;
boolean max, same;
su = (group_type *)Malloc(setsz * sizeof(group_type));
for (i = 0; i < setsz; i++)
su[i] = 0;
for (i = 0; i < n; i++) {
max = true;
for (j = 0; j < setsz; j++)
if ((grouping[i][j] & ~st[j]) != 0)
max = false;
if (max) {
same = true;
for (j = 0; j < setsz; j++)
if (grouping[i][j] != st[j])
same = false;
max = !same;
}
if (max) {
for (j = 0; j < setsz; j ++)
if ((su[j] & ~grouping[i][j]) != 0)
max = false;
if (max) {
same = true;
for (j = 0; j < setsz; j ++)
if (su[j] != grouping[i][j])
same = false;
max = !same;
}
if (max)
memcpy(su, grouping[i], setsz * sizeof(group_type));
}
}
memcpy(st, su, setsz * sizeof(group_type));
free(su);
} /* bigsubset */
void recontraverse(node **p, group_type *st, long n, long *nextnode)
{
/* traverse to add next node to consensus tree */
long i, j = 0, k = 0, l = 0;
boolean found, same = 0, zero, zero2;
group_type *tempset, *st2;
node *q, *r;
for (i = 1; i <= spp; i++) { /* count species in set */
if (i == ((l+1)*SETBITS+1)) l++;
if (((1L << (i - 1 - l*SETBITS)) & st[l]) != 0) {
k++; /* k is the number of species in the set */
j = i; /* j is set to last species in the set */
}
}
if (k == 1) { /* if only 1, set up that tip */
*p = nodep[j - 1];
(*p)->tip = true;
(*p)->index = j;
return;
}
gnu(&grbg, p); /* otherwise make interior node */
(*p)->tip = false;
(*p)->index = *nextnode;
nodep[*nextnode - 1] = *p;
(*nextnode)++;
(*p)->deltav = 0.0;
for (i = 0; i < n; i++) { /* go through all sets */
same = true; /* to find one which is this one */
for (j = 0; j < setsz; j++)
if (grouping[i][j] != st[j])
same = false;
if (same)
(*p)->deltav = *timesseen[i];
}
tempset = (group_type *)Malloc(setsz * sizeof(group_type));
memcpy(tempset, st, setsz * sizeof(group_type));
q = *p;
st2 = (group_type *)Malloc(setsz * sizeof(group_type));
memcpy(st2, st, setsz * sizeof(group_type));
zero = true; /* having made two copies of the set ... */
for (j = 0; j < setsz; j++) /* see if they are empty */
if (tempset[j] != 0)
zero = false;
if (!zero)
bigsubset(tempset, n); /* find biggest set within it */
zero = zero2 = false; /* ... tempset is that subset */
while (!zero && !zero2) {
zero = zero2 = true;
for (j = 0; j < setsz; j++) {
if (st2[j] != 0)
zero = false;
if (tempset[j] != 0)
zero2 = false;
}
if (!zero && !zero2) {
gnu(&grbg, &q->next);
q->next->index = q->index;
q = q->next;
q->tip = false;
r = *p;
recontraverse(&q->back, tempset, n, nextnode); /* put it on tree */
*p = r;
q->back->back = q;
for (j = 0; j < setsz; j++)
st2[j] &= ~tempset[j]; /* remove that subset from the set */
memcpy(tempset, st2, setsz * sizeof(group_type)); /* that becomes set */
found = false;
i = 1;
while (!found && i <= n) {
if (grouping[i - 1] != 0) {
same = true;
for (j = 0; j < setsz; j++)
if (grouping[i - 1][j] != tempset[j])
same = false;
}
if ((grouping[i - 1] != 0) && same)
found = true;
else
i++;
}
zero = true;
for (j = 0; j < setsz; j++)
if (tempset[j] != 0)
zero = false;
if (!zero && !found)
bigsubset(tempset, n);
}
}
q->next = *p;
free(tempset);
free(st2);
} /* recontraverse */
void reconstruct(long n)
{
/* reconstruct tree from the subsets */
long nextnode;
group_type *s;
nextnode = spp + 1;
s = (group_type *)Malloc(setsz * sizeof(group_type));
memcpy(s, fullset, setsz * sizeof(group_type));
recontraverse(&root, s, n, &nextnode);
free(s);
} /* reconstruct */
void coordinates(node *p, long *tipy)
{
/* establishes coordinates of nodes */
node *q, *first, *last;
long maxx;
if (p->tip) {
p->xcoord = 0;
p->ycoord = *tipy;
p->ymin = *tipy;
p->ymax = *tipy;
(*tipy) += down;
return;
}
q = p->next;
maxx = 0;
while (q != p) {
coordinates(q->back, tipy);
if (!q->back->tip) {
if (q->back->xcoord > maxx)
maxx = q->back->xcoord;
}
q = q->next;
}
first = p->next->back;
q = p;
while (q->next != p)
q = q->next;
last = q->back;
p->xcoord = maxx + OVER;
p->ycoord = (long)((first->ycoord + last->ycoord) / 2);
p->ymin = first->ymin;
p->ymax = last->ymax;
} /* coordinates */
void drawline(long i)
{
/* draws one row of the tree diagram by moving up tree */
node *p, *q;
long n, j;
boolean extra, done, trif;
node *r, *first = NULL, *last = NULL;
boolean found;
p = root;
q = root;
fprintf(outfile, " ");
extra = false;
trif = false;
do {
if (!p->tip) {
found = false;
r = p->next;
while (r != p && !found) {
if (i >= r->back->ymin && i <= r->back->ymax) {
q = r->back;
found = true;
} else
r = r->next;
}
first = p->next->back;
r = p;
while (r->next != p)
r = r->next;
last = r->back;
}
done = (p->tip || p == q);
n = p->xcoord - q->xcoord;
if (extra) {
n--;
extra = false;
}
if (q->ycoord == i && !done) {
if (trif)
putc('-', outfile);
else
putc('+', outfile);
trif = false;
if (!q->tip) {
for (j = 1; j <= n - 7; j++)
putc('-', outfile);
if (noroot && (root->next->next->next == root) &&
(((root->next->back == q) && root->next->next->back->tip)
|| ((root->next->next->back == q) && root->next->back->tip)))
fprintf(outfile, "------|");
else {
if (!strict) { /* write number of times seen */
if (q->deltav >= 100)
fprintf(outfile, "%5.1f-|", (double)q->deltav);
else if (q->deltav >= 10)
fprintf(outfile, "-%4.1f-|", (double)q->deltav);
else
fprintf(outfile, "--%3.1f-|", (double)q->deltav);
} else
fprintf(outfile, "------|");
}
extra = true;
trif = true;
} else {
for (j = 1; j < n; j++)
putc('-', outfile);
}
} else if (!p->tip && last->ycoord > i && first->ycoord < i &&
(i != p->ycoord || p == root)) {
putc('|', outfile);
for (j = 1; j < n; j++)
putc(' ', outfile);
} else {
for (j = 1; j <= n; j++)
putc(' ', outfile);
if (trif)
trif = false;
}
if (q != p)
p = q;
} while (!done);
if (p->ycoord == i && p->tip) {
for (j = 0; (j < MAXNCH) && (p->nayme[j] != '\0'); j++)
putc(p->nayme[j], outfile);
}
putc('\n', outfile);
} /* drawline */
void printree()
{
/* prints out diagram of the tree */
long i;
long tipy;
if (treeprint) {
fprintf(outfile, "\nCONSENSUS TREE:\n");
if (mr || mre || ml) {
if (noroot) {
fprintf(outfile, "the numbers on the branches indicate the number\n");
fprintf(outfile, "of times the partition of the species into the two sets\n");
fprintf(outfile, "which are separated by that branch occurred\n");
} else {
fprintf(outfile, "the numbers forks indicate the number\n");
fprintf(outfile, "of times the group consisting of the species\n");
fprintf(outfile, "which are to the right of that fork occurred\n");
}
fprintf(outfile, "among the trees, out of %6.2f trees\n", ntrees);
if (ntrees <= 1.001)
fprintf(outfile, "(trees had fractional weights)\n");
}
tipy = 1;
coordinates(root, &tipy);
putc('\n', outfile);
for (i = 1; i <= tipy - down; i++)
drawline(i);
putc('\n', outfile);
}
if (noroot) {
fprintf(outfile, "\n remember:");
if (didreroot)
fprintf(outfile, " (though rerooted by outgroup)");
fprintf(outfile, " this is an unrooted tree!\n");
}
putc('\n', outfile);
} /* printree */
void enternohash(group_type *s, long *n)
{
/* if set s is already there, enter it into groupings in the next slot
(without hash-coding). n is number of sets stored there and is updated */
long i, j;
boolean found;
found = false;
for (i = 0; i < (*n); i++) { /* go through looking whether it is there */
found = true;
for (j = 0; j < setsz; j++) { /* check both parts of partition */
found = found && (grouping[i][j] == s[j]);
found = found && (group2[i][j] == (fullset[j] & (~s[j])));
}
if (found)
break;
}
if (!found) { /* if not, add it to the slot after the end,
which must be empty */
grouping[i] = (group_type *)Malloc(setsz * sizeof(group_type));
timesseen[i] = (double *)Malloc(sizeof(double));
group2[i] = (group_type *)Malloc(setsz * sizeof(group_type));
for (j = 0; j < setsz; j++)
grouping[i][j] = s[j];
*timesseen[i] = 1;
(*n)++;
}
} /* enternohash */
void enterpartition (group_type *s1, long *n)
{
/* try to put this partition in list of partitions. If implied by others,
don't bother. If others implied by it, replace them. If this one
vacuous because only one element in s1, forget it */
long i, j;
boolean found;
/* this stuff all to be rewritten but left here so pieces can be used */
found = false;
for (i = 0; i < (*n); i++) { /* go through looking whether it is there */
found = true;
for (j = 0; j < setsz; j++) { /* check both parts of partition */
found = found && (grouping[i][j] == s1[j]);
found = found && (group2[i][j] == (fullset[j] & (~s1[j])));
}
if (found)
break;
}
if (!found) { /* if not, add it to the slot after the end,
which must be empty */
grouping[i] = (group_type *)Malloc(setsz * sizeof(group_type));
timesseen[i] = (double *)Malloc(sizeof(double));
group2[i] = (group_type *)Malloc(setsz * sizeof(group_type));
for (j = 0; j < setsz; j++)
grouping[i][j] = s1[j];
*timesseen[i] = 1;
(*n)++;
}
} /* enterpartition */
void elimboth(long n)
{
/* for Adams case: eliminate pairs of groups incompatible with each other */
long i, j;
boolean comp;
for (i = 0; i < n-1; i++) {
for (j = i+1; j < n; j++) {
comp = compatible(i,j);
if (!comp) {
*timesseen[i] = 0.0;
*timesseen[j] = 0.0;
}
}
if (*timesseen[i] == 0.0) {
free(grouping[i]);
free(timesseen[i]);
timesseen[i] = NULL;
grouping[i] = NULL;
}
}
if (*timesseen[n-1] == 0.0) {
free(grouping[n-1]);
free(timesseen[n-1]);
timesseen[n-1] = NULL;
grouping[n-1] = NULL;
}
} /* elimboth */
void consensus(pattern_elm ***pattern_array, long trees_in)
{
long i, n, n2, tipy;
group2 = (group_type **) Malloc(maxgrp*sizeof(group_type *));
for (i = 0; i < maxgrp; i++)
group2[i] = NULL;
times2 = (double **)Malloc(maxgrp*sizeof(double *));
for (i = 0; i < maxgrp; i++)
times2[i] = NULL;
n2 = 0;
censor(); /* drop groups that are too rare */
compress(&n); /* push everybody to front of array */
if (!strict) { /* drop those incompatible, if any */
sort(n);
eliminate(&n, &n2);
compress(&n);
}
reconstruct(n);
tipy = 1;
coordinates(root, &tipy);
if (prntsets) {
fprintf(outfile, "\nSets included in the consensus tree\n");
printset(n);
for (i = 0; i < n2; i++) {
if (!grouping[i]) {
grouping[i] = (group_type *)Malloc(setsz * sizeof(group_type));
timesseen[i] = (double *)Malloc(sizeof(double));
}
memcpy(grouping[i], group2[i], setsz * sizeof(group_type));
*timesseen[i] = *times2[i];
}
n = n2;
fprintf(outfile, "\n\nSets NOT included in consensus tree:");
if (n2 == 0)
fprintf(outfile, " NONE\n");
else {
putc('\n', outfile);
printset(n);
}
}
putc('\n', outfile);
if (strict)
fprintf(outfile, "\nStrict consensus tree\n");
if (mre)
fprintf(outfile, "\nExtended majority rule consensus tree\n");
if (ml) {
fprintf(outfile, "\nM consensus tree (l = %4.2f)\n", mlfrac);
fprintf(outfile, " l\n");
}
if (mr)
fprintf(outfile, "\nMajority rule consensus tree\n");
printree();
free(nayme);
for (i = 0; i < maxgrp; i++)
free(grouping[i]);
free(grouping);
for (i = 0; i < maxgrp; i++)
free(order[i]);
free(order);
for (i = 0; i < maxgrp; i++)
if (timesseen[i] != NULL)
free(timesseen[i]);
free(timesseen);
} /* consensus */
void rehash()
{
group_type *s;
long i, j, k;
double temp, ss, smult;
boolean done;
smult = (sqrt(5.0) - 1) / 2;
s = (group_type *)Malloc(setsz * sizeof(group_type));
for (i = 0; i < maxgrp/2; i++) {
k = *order[i];
memcpy(s, grouping[k], setsz * sizeof(group_type));
ss = 0.0;
for (j = 0; j < setsz; j++)
ss += s[j] /* pow(2, SETBITS*j)*/;
temp = ss * smult;
j = (long)(maxgrp * (temp - floor(temp)));
done = false;
while (!done) {
if (!grping2[j]) {
grping2[j] = (group_type *)Malloc(setsz * sizeof(group_type));
order2[i] = (long *)Malloc(sizeof(long));
tmseen2[j] = (double *)Malloc(sizeof(double));
memcpy(grping2[j], grouping[k], setsz * sizeof(group_type));
*tmseen2[j] = *timesseen[k];
*order2[i] = j;
grouping[k] = NULL;
timesseen[k] = NULL;
order[i] = NULL;
done = true;
} else {
j++;
if (j >= maxgrp) j -= maxgrp;
}
}
}
free(s);
} /* rehash */
void enternodeset(node* r)
{ /* enter a set of species into the hash table */
long i, j, start;
double ss, n;
boolean done, same;
double times ;
group_type *s;
s = r->nodeset;
same = true;
for (i = 0; i < setsz; i++)
if (s[i] != fullset[i])
same = false;
if (same)
return;
times = trweight;
ss = 0.0; /* compute the hashcode for the set */
n = ((sqrt(5.0) - 1.0) / 2.0); /* use an irrational multiplier */
for (i = 0; i < setsz; i++)
ss += s[i] * n;
i = (long)(maxgrp * (ss - floor(ss))) + 1; /* use fractional part of code */
start = i;
done = false; /* go through seeing if it is there */
while (!done) {
if (grouping[i - 1]) { /* ... i.e. if group is absent, or */
same = false; /* (will be false if timesseen = 0) */
if (!(timesseen[i-1] == 0)) { /* ... if timesseen = 0 */
same = true;
for (j = 0; j < setsz; j++) {
if (s[j] != grouping[i - 1][j])
same = false;