Debian Codegraph @

csrc/protpars.c

raw symbols SCIP
1 #include "phylip.h"
2 #include "seq.h"
3 #include <unistd.h>
4 #include <setjmp.h>
5
6 /*
7 derived from file protpars.c of PHYLIP version 3.696 with the following copyright:
8
9 version 3.696.
10 Written by Joseph Felsenstein, Akiko Fuseki, Sean Lamont, and Andrew Keeffe.
11
12 Copyright (c) 1993-2014, Joseph Felsenstein.
13 All rights reserved.
14
15 Redistribution and use in source and binary forms, with or without
16 modification, are permitted provided that the following conditions are met:
17
18 1. Redistributions of source code must retain the above copyright notice,
19 this list of conditions and the following disclaimer.
20
21 2. Redistributions in binary form must reproduce the above copyright notice,
22 this list of conditions and the following disclaimer in the documentation
23 and/or other materials provided with the distribution.
24
25 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
29 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 POSSIBILITY OF SUCH DAMAGE.
36 */
37
38 #define maxtrees 100 /* maximum number of tied trees stored */
39 //static int maxtrees;
40 static int nmlngth;
41
42 static jmp_buf lngjmp_env;
43
44 typedef enum {
45 universal, ciliate, mito, vertmito, flymito, yeastmito
46 } codetype;
47
48 /* nodes will form a binary tree */
49
50 typedef struct gseq {
51 seqptr seq;
52 struct gseq *next;
53 } gseq;
54
55 /* function prototypes */
56 extern int tree_build_interrupted;
57 extern char *create_tmp_filename_from_C(void);
58 extern FILE *fl_fopen_from_C(const char *fname, const char *mode);
59 extern int fl_unlink_from_C(const char*fname);
60 void padtosize(char *pname, char *name, int length);
61 //void protgnu(gseq **);
62 //void protchuck(gseq *);
63 void code(void);
64 void setup(void);
65 static void getoptions(void);
66 void protalloctree(void);
67 static void protfreetree();
68 static void allocrest(void);
69 static void doinit(int, int, char*);
70 //void protinputdata(void);
71
72 void protmakevalues(void);
73 static void doinput(char** seq, char** seqname);
74 void protfillin(node *, node *, node *);
75 void protpreorder(node *);
76 void protadd(node *, node *, node *);
77 void protre_move(node **, node **);
78 static void evaluate(node *);
79 void protpostorder(node *);
80 void protreroot(node *);
81 void protsavetraverse(node *, long *, boolean *);
82
83 void protsavetree(long *, boolean *);
84 static void tryadd(node *, node **, node **);
85 static void addpreorder(node *, node *, node *);
86 static void tryrearr(node *, boolean *);
87 static void repreorder(node *, boolean *);
88 static void rearrange(node **);
89 void protgetch(Char *);
90 void protaddelement(node **, long *, long *, boolean *);
91 void prottreeread(void);
92 void protancestset(long *, long *, long *, long *, long *);
93
94 void prothyprint(long , long , boolean *, node *, boolean *, boolean *);
95 void prothyptrav(node *, sitearray *, long, long, long *, boolean *,
96 sitearray);
97 //void prothypstates(long *);
98 static void describe(void);
99 static void maketree(char*);
100 void reallocnode(node* p);
101 static void reallocchars(void);
102 extern void awake_from_C(void);
103 /* function prototypes */
104
105
106
107 static Char *outtreename;
108 static node *root;
109 static long chars, col, msets, ith, njumble, jumb;
110 /* chars = number of sites in actual sequences */
111 static boolean jumble, usertree, weights, thresh, trout, progress, stepbox,
112 justwts, ancseq, mulsets, firstset;
113 codetype whichcode;
114 long fullset, fulldel;
115 static pointarray treenode; /* pointers to all nodes in tree */
116 static double threshold;
117 static steptr threshwt;
118 static longer seed;
119 static long *enterorder;
120 sitearray translate[(long)quest - (long)ala + 1];
121 aas trans[4][4][4];
122 static long **fsteps;
123 static bestelm *bestrees;
124 boolean dummy;
125 static gseq *garbage;
126 static node *temp, *temp1;
127 Char ch;
128 aas tmpa;
129 static char *progname;
130
131 /* Local variables for maketree, propagated globally for c version: */
132 static long minwhich;
133 static double like, bestyet, bestlike, minsteps, bstlike2;
134 static boolean lastrearr, recompute;
135 static node *there;
136 static double nsteps[maxuser];
137 static long *place;
138 static boolean *names;
139
140
141 /*void protgnu(gseq **p)
142 {
143 * this and the following are do-it-yourself garbage collectors.
144 Make a new node or pull one off the garbage list *
145 if (garbage != NULL) {
146 *p = garbage;
147 free((*p)->seq);
148 (*p)->seq = (seqptr)Malloc(chars*sizeof(sitearray));
149 garbage = garbage->next;
150 } else {
151 *p = (gseq *)Malloc(sizeof(gseq));
152 (*p)->seq = (seqptr)Malloc(chars*sizeof(sitearray));
153 }
154 (*p)->next = NULL;
155 } * protgnu *
156
157
158 void protchuck(gseq *p)
159 {
160 * collect garbage on p -- put it on front of garbage list *
161 p->next = garbage;
162 garbage = p;
163 } * protchuck */
164
165
166 void code()
167 {
168 /* make up table of the code 1 = u, 2 = c, 3 = a, 4 = g */
169 trans[0][0][0] = phe;
170 trans[0][0][1] = phe;
171 trans[0][0][2] = leu;
172 trans[0][0][3] = leu;
173 trans[0][1][0] = ser1;
174 trans[0][1][1] = ser1;
175 trans[0][1][2] = ser1;
176 trans[0][1][3] = ser1;
177 trans[0][2][0] = tyr;
178 trans[0][2][1] = tyr;
179 trans[0][2][2] = stop;
180 trans[0][2][3] = stop;
181 trans[0][3][0] = cys;
182 trans[0][3][1] = cys;
183 trans[0][3][2] = stop;
184 trans[0][3][3] = trp;
185 trans[1][0][0] = leu;
186 trans[1][0][1] = leu;
187 trans[1][0][2] = leu;
188 trans[1][0][3] = leu;
189 trans[1][1][0] = pro;
190 trans[1][1][1] = pro;
191 trans[1][1][2] = pro;
192 trans[1][1][3] = pro;
193 trans[1][2][0] = his;
194 trans[1][2][1] = his;
195 trans[1][2][2] = gln;
196 trans[1][2][3] = gln;
197 trans[1][3][0] = arg;
198 trans[1][3][1] = arg;
199 trans[1][3][2] = arg;
200 trans[1][3][3] = arg;
201 trans[2][0][0] = ileu;
202 trans[2][0][1] = ileu;
203 trans[2][0][2] = ileu;
204 trans[2][0][3] = met;
205 trans[2][1][0] = thr;
206 trans[2][1][1] = thr;
207 trans[2][1][2] = thr;
208 trans[2][1][3] = thr;
209 trans[2][2][0] = asn;
210 trans[2][2][1] = asn;
211 trans[2][2][2] = lys;
212 trans[2][2][3] = lys;
213 trans[2][3][0] = ser2;
214 trans[2][3][1] = ser2;
215 trans[2][3][2] = arg;
216 trans[2][3][3] = arg;
217 trans[3][0][0] = val;
218 trans[3][0][1] = val;
219 trans[3][0][2] = val;
220 trans[3][0][3] = val;
221 trans[3][1][0] = ala;
222 trans[3][1][1] = ala;
223 trans[3][1][2] = ala;
224 trans[3][1][3] = ala;
225 trans[3][2][0] = asp;
226 trans[3][2][1] = asp;
227 trans[3][2][2] = glu;
228 trans[3][2][3] = glu;
229 trans[3][3][0] = gly;
230 trans[3][3][1] = gly;
231 trans[3][3][2] = gly;
232 trans[3][3][3] = gly;
233 if (whichcode == mito)
234 trans[0][3][2] = trp;
235 if (whichcode == vertmito) {
236 trans[0][3][2] = trp;
237 trans[2][3][2] = stop;
238 trans[2][3][3] = stop;
239 trans[2][0][2] = met;
240 }
241 if (whichcode == flymito) {
242 trans[0][3][2] = trp;
243 trans[2][0][2] = met;
244 trans[2][3][2] = ser2;
245 }
246 if (whichcode == yeastmito) {
247 trans[0][3][2] = trp;
248 trans[1][0][2] = thr;
249 trans[2][0][2] = met;
250 }
251 } /* code */
252
253
254 void setup()
255 {
256 /* set up set table to get aasets from aas */
257 aas a, b;
258 long i, j, k, l, s;
259
260 for (a = ala; (long)a <= (long)stop; a = (aas)((long)a + 1)) {
261 translate[(long)a - (long)ala][0] = 1L << ((long)a);
262 translate[(long)a - (long)ala][1] = 1L << ((long)a);
263 }
264 for (i = 0; i <= 3; i++) {
265 for (j = 0; j <= 3; j++) {
266 for (k = 0; k <= 3; k++) {
267 for (l = 0; l <= 3; l++) {
268 translate[(long)trans[i][j][k]][1] |= (1L << (long)trans[l][j][k]);
269 translate[(long)trans[i][j][k]][1] |= (1L << (long)trans[i][l][k]);
270 translate[(long)trans[i][j][k]][1] |= (1L << (long)trans[i][j][l]);
271 }
272 }
273 }
274 }
275 translate[(long)del - (long)ala][1] = 1L << ((long)del);
276 fulldel = (1L << ((long)stop + 1)) - (1L << ((long)ala));
277 fullset = fulldel & (~(1L << ((long)del)));
278 translate[(long)asx - (long)ala][0]
279 = (1L << ((long)asn)) | (1L << ((long)asp));
280 translate[(long)glx - (long)ala][0]
281 = (1L << ((long)gln)) | (1L << ((long)glu));
282 translate[(long)ser - (long)ala][0]
283 = (1L << ((long)ser1)) | (1L << ((long)ser2));
284 translate[(long)unk - (long)ala][0] = fullset;
285 translate[(long)quest - (long)ala][0] = fulldel;
286 translate[(long)asx - (long)ala][1] = translate[(long)asn - (long)ala][1]
287 | translate[(long)asp - (long)ala][1];
288 translate[(long)glx - (long)ala][1] = translate[(long)gln - (long)ala][1]
289 | translate[(long)glu - (long)ala][1];
290 translate[(long)ser - (long)ala][1] = translate[(long)ser1 - (long)ala][1]
291 | translate[(long)ser2 - (long)ala][1];
292 translate[(long)unk - (long)ala][1] = fullset;
293 translate[(long)quest - (long)ala][1] = fulldel;
294 for (a = ala; (long)a <= (long)quest; a = (aas)((long)a + 1)) {
295 s = 0;
296 for (b = ala; (long)b <= (long)stop; b = (aas)((long)b + 1)) {
297 if (((1L << ((long)b)) & translate[(long)a - (long)ala][1]) != 0)
298 s |= translate[(long)b - (long)ala][1];
299 }
300 translate[(long)a - (long)ala][2] = s;
301 }
302 } /* setup */
303
304
305 static void getoptions()
306 {
307 /* interactively set options */
308 //long loopcount, loopcount2;
309 //Char ch, ch2;
310
311 //fprintf(outfile, "\nProtein parsimony algorithm, version %s\n\n",VERSION);
312 //putchar('\n');
313 jumble = false;
314 njumble = 1;
315 outgrno = 1;
316 outgropt = false;
317 thresh = false;
318 trout = true;
319 usertree = false;
320 weights = false;
321 whichcode = universal;
322 printdata = false;
323 progress = false;
324 treeprint = false;
325 stepbox = false;
326 ancseq = false;
327 dotdiff = true;
328 interleaved = true;
329 /*loopcount = 0;
330 for (;;) {
331 cleerhome();
332 printf("\nProtein parsimony algorithm, version %s\n\n",VERSION);
333 printf("Setting for this run:\n");
334 printf(" U Search for best tree? %s\n",
335 (usertree ? "No, use user trees in input file" : "Yes"));
336 if (!usertree) {
337 printf(" J Randomize input order of sequences?");
338 if (jumble)
339 printf(" Yes (seed =%8ld,%3ld times)\n", inseed0, njumble);
340 else
341 printf(" No. Use input order\n");
342 }
343 printf(" O Outgroup root?");
344 if (outgropt)
345 printf(" Yes, at sequence number%3ld\n", outgrno);
346 else
347 printf(" No, use as outgroup species%3ld\n", outgrno);
348 printf(" T Use Threshold parsimony?");
349 if (thresh)
350 printf(" Yes, count steps up to%4.1f per site\n", threshold);
351 else
352 printf(" No, use ordinary parsimony\n");
353 printf(" C Use which genetic code? %s\n",
354 (whichcode == universal) ? "Universal" :
355 (whichcode == ciliate) ? "Ciliate" :
356 (whichcode == mito) ? "Universal mitochondrial" :
357 (whichcode == vertmito) ? "Vertebrate mitochondrial" :
358 (whichcode == flymito) ? "Fly mitochondrial" :
359 (whichcode == yeastmito) ? "Yeast mitochondrial" : "");
360 printf(" W Sites weighted? %s\n",
361 (weights ? "Yes" : "No"));
362 printf(" M Analyze multiple data sets?");
363 if (mulsets)
364 printf(" Yes, %2ld %s\n", msets,
365 (justwts ? "sets of weights" : "data sets"));
366 else
367 printf(" No\n");
368 printf(" I Input sequences interleaved? %s\n",
369 (interleaved ? "Yes" : "No, sequential"));
370 printf(" 0 Terminal type (IBM PC, ANSI, none)? %s\n",
371 (ibmpc ? "IBM PC" : ansi ? "ANSI" : "(none)"));
372 printf(" 1 Print out the data at start of run %s\n",
373 (printdata ? "Yes" : "No"));
374 printf(" 2 Print indications of progress of run %s\n",
375 (progress ? "Yes" : "No"));
376 printf(" 3 Print out tree %s\n",
377 (treeprint ? "Yes" : "No"));
378 printf(" 4 Print out steps in each site %s\n",
379 (stepbox ? "Yes" : "No"));
380 printf(" 5 Print sequences at all nodes of tree %s\n",
381 (ancseq ? "Yes" : "No"));
382 if (ancseq || printdata)
383 printf(" . Use dot-differencing to display them %s\n",
384 dotdiff ? "Yes" : "No");
385 printf(" 6 Write out trees onto tree file? %s\n",
386 (trout ? "Yes" : "No"));
387 if(weights && justwts){
388 printf(
389 "WARNING: W option and Multiple Weights options are both on. ");
390 printf(
391 "The W menu option is unnecessary and has no additional effect. \n");
392 }
393 printf(
394 "\nAre these settings correct? (type Y or the letter for one to change)\n");
395 fflush(stdout);
396 scanf("%c%*[^\n]", &ch);
397 getchar();
398 uppercase(&ch);
399 if (ch == 'Y')
400 break;
401 if (((!usertree) && (strchr("WCJOTUMI12345.60", ch) != NULL))
402 || (usertree && ((strchr("WCOTUMI12345.60", ch) != NULL)))){
403 switch (ch) {
404
405 case 'J':
406 jumble = !jumble;
407 if (jumble)
408 initjumble(&inseed, &inseed0, seed, &njumble);
409 else njumble = 1;
410 break;
411
412 case 'W':
413 weights = !weights;
414 break;
415
416 case 'O':
417 outgropt = !outgropt;
418 if (outgropt)
419 initoutgroup(&outgrno, spp);
420 else outgrno = 1;
421 break;
422
423 case 'T':
424 thresh = !thresh;
425 if (thresh)
426 initthreshold(&threshold);
427 break;
428
429 case 'C':
430 printf("\nWhich genetic code?\n");
431 printf(" type for\n\n");
432 printf(" U Universal\n");
433 printf(" M Mitochondrial\n");
434 printf(" V Vertebrate mitochondrial\n");
435 printf(" F Fly mitochondrial\n");
436 printf(" Y Yeast mitochondrial\n\n");
437 loopcount2 = 0;
438 do {
439 printf("type U, M, V, F, or Y\n");
440 fflush(stdout);
441 scanf("%c%*[^\n]", &ch);
442 getchar();
443 if (ch == '\n')
444 ch = ' ';
445 uppercase(&ch);
446 countup(&loopcount2, 10);
447 } while (ch != 'U' && ch != 'M' && ch != 'V'
448 && ch != 'F' && ch != 'Y');
449 switch (ch) {
450
451 case 'U':
452 whichcode = universal;
453 break;
454
455 case 'M':
456 whichcode = mito;
457 break;
458
459 case 'V':
460 whichcode = vertmito;
461 break;
462
463 case 'F':
464 whichcode = flymito;
465 break;
466
467 case 'Y':
468 whichcode = yeastmito;
469 break;
470 }
471 break;
472
473 case 'M':
474 mulsets = !mulsets;
475 if (mulsets){
476 printf("Multiple data sets or multiple weights?");
477 loopcount2 = 0;
478 do {
479 printf(" (type D or W)\n");
480 #ifdef _WIN32
481 phyFillScreenColor();
482 #endif
483 fflush(stdout);
484 scanf("%c%*[^\n]", &ch2);
485 getchar();
486 if (ch2 == '\n')
487 ch2 = ' ';
488 uppercase(&ch2);
489 countup(&loopcount2, 10);
490 } while ((ch2 != 'W') && (ch2 != 'D'));
491 justwts = (ch2 == 'W');
492 if (justwts)
493 justweights(&msets);
494 else
495 initdatasets(&msets);
496 if (!jumble) {
497 jumble = true;
498 initjumble(&inseed, &inseed0, seed, &njumble);
499 }
500 }
501 break;
502
503 case 'I':
504 interleaved = !interleaved;
505 break;
506
507 case 'U':
508 usertree = !usertree;
509 break;
510
511 case '0':
512 initterminal(&ibmpc, &ansi);
513 break;
514
515 case '1':
516 printdata = !printdata;
517 break;
518
519 case '2':
520 progress = !progress;
521 break;
522
523 case '3':
524 treeprint = !treeprint;
525 break;
526
527 case '4':
528 stepbox = !stepbox;
529 break;
530
531 case '5':
532 ancseq = !ancseq;
533 break;
534
535 case '.':
536 dotdiff = !dotdiff;
537 break;
538
539 case '6':
540 trout = !trout;
541 break;
542 }
543 } else
544 printf("Not a possible option!\n");
545 countup(&loopcount, 100);
546 }*/
547 } /* getoptions */
548
549
550 void protalloctree()
551 { /* allocate treenode dynamically */
552 long i, j;
553 node *p, *q;
554
555 treenode = (pointarray)Malloc(nonodes*sizeof(node *));
556 for (i = 0; i < (spp); i++) {
557 treenode[i] = (node *)Malloc(sizeof(node));
558 treenode[i]->numsteps = (steptr)Malloc(chars*sizeof(long));
559 treenode[i]->siteset = (seqptr)Malloc(chars*sizeof(sitearray));
560 treenode[i]->seq = (aas *)Malloc(chars*sizeof(aas));
561 }
562 for (i = spp; i < (nonodes); i++) {
563 q = NULL;
564 for (j = 1; j <= 3; j++) {
565 p = (node *)Malloc(sizeof(node));
566 p->numsteps = (steptr)Malloc(chars*sizeof(long));
567 p->siteset = (seqptr)Malloc(chars*sizeof(sitearray));
568 p->seq = (aas *)Malloc(chars*sizeof(aas));
569 p->next = q;
570 q = p;
571 }
572 p->next->next->next = p;
573 treenode[i] = p;
574 }
575 } /* protalloctree */
576
577
578 static void protfreetree()
579 {
580 int i;
581 node *p;
582 for (i = 0; i < nonodes; i++) {
583 p = treenode[i];
584 free(p->numsteps);
585 free(p->siteset);
586 free(p->seq);
587 free(p);
588 }
589 free(treenode);
590
591 for (i = 1; i <= maxtrees; i++) free(bestrees[i-1].btree);
592 free(bestrees);
593 free(enterorder);
594 free(place);
595 free(weight);
596 free(threshwt);
597 free(temp->numsteps);
598 free(temp->siteset);
599 free(temp->seq);
600 free(temp);
601 free(temp1->numsteps);
602 free(temp1->siteset);
603 free(temp1->seq);
604 free(temp1);
605 if (usertree) {
606 for (i = 0; i < maxuser; i++) {
607 free(fsteps[i]);
608 }
609 free(fsteps);
610 }
611 }
612
613
614 void reallocnode(node* p)
615 {
616 free(p->numsteps);
617 free(p->siteset);
618 free(p->seq);
619 p->numsteps = (steptr)Malloc(chars*sizeof(long));
620 p->siteset = (seqptr)Malloc(chars*sizeof(sitearray));
621 p->seq = (aas *)Malloc(chars*sizeof(aas));
622 }
623
624
625 static void reallocchars(void)
626 { /* reallocates variables that are dependand on the number of chars
627 * do we need to reallocate the garbage list too? */
628 long i;
629 node *p;
630
631 if (usertree)
632 for (i = 0; i < maxuser; i++) {
633 free(fsteps[i]);
634 fsteps[i] = (long *)Malloc(chars*sizeof(long));
635 }
636
637 for (i = 0; i < nonodes; i++) {
638 reallocnode(treenode[i]);
639 if (i >= spp) {
640 p=treenode[i]->next;
641 while (p != treenode[i]) {
642 reallocnode(p);
643 p = p->next;
644 }
645 }
646 }
647
648 free(weight);
649 free(threshwt);
650 free(temp->numsteps);
651 free(temp->siteset);
652 free(temp->seq);
653 free(temp1->numsteps);
654 free(temp1->siteset);
655 free(temp1->seq);
656
657 weight = (steptr)Malloc(chars*sizeof(long));
658 threshwt = (steptr)Malloc(chars*sizeof(long));
659 temp->numsteps = (steptr)Malloc(chars*sizeof(long));
660 temp->siteset = (seqptr)Malloc(chars*sizeof(sitearray));
661 temp->seq = (aas *)Malloc(chars*sizeof(aas));
662 temp1->numsteps = (steptr)Malloc(chars*sizeof(long));
663 temp1->siteset = (seqptr)Malloc(chars*sizeof(sitearray));
664 temp1->seq = (aas *)Malloc(chars*sizeof(aas));
665 }
666
667
668 static void allocrest()
669 { /* allocate remaining global arrays and variables dynamically */
670 long i;
671
672 if (usertree) {
673 fsteps = (long **)Malloc(maxuser*sizeof(long *));
674 for (i = 0; i < maxuser; i++)
675 fsteps[i] = (long *)Malloc(chars*sizeof(long));
676 }
677 bestrees = (bestelm *)Malloc(maxtrees*sizeof(bestelm));
678 for (i = 1; i <= maxtrees; i++)
679 bestrees[i - 1].btree = (long *)Malloc(spp*sizeof(long));
680 //nayme = (naym *)Malloc(spp*sizeof(naym));
681 enterorder = (long *)Malloc(spp*sizeof(long));
682 place = (long *)Malloc(nonodes*sizeof(long));
683 weight = (steptr)Malloc(chars*sizeof(long));
684 threshwt = (steptr)Malloc(chars*sizeof(long));
685 temp = (node *)Malloc(sizeof(node));
686 temp->numsteps = (steptr)Malloc(chars*sizeof(long));
687 temp->siteset = (seqptr)Malloc(chars*sizeof(sitearray));
688 temp->seq = (aas *)Malloc(chars*sizeof(aas));
689 temp1 = (node *)Malloc(sizeof(node));
690 temp1->numsteps = (steptr)Malloc(chars*sizeof(long));
691 temp1->siteset = (seqptr)Malloc(chars*sizeof(sitearray));
692 temp1->seq = (aas *)Malloc(chars*sizeof(aas));
693 } /* allocrest */
694
695
696 static void doinit(int myspp, int mychars, char* toevaluate)
697 {
698 /* initializes variables */
699
700 //inputnumbers(&spp, &chars, &nonodes, 1);
701 spp = myspp;
702 chars = mychars;
703 nonodes = (spp * 2 - 1);
704 getoptions();
705 usertree = (toevaluate != NULL);
706 /*if (printdata)
707 fprintf(outfile, "%2ld species, %3ld sites\n\n", spp, chars);*/
708 protalloctree();
709 allocrest();
710 } /* doinit*/
711
712
713 /*void protinputdata()
714 {
715 * input the names and sequences for each species *
716 long i, j, k, l, aasread, aasnew = 0;
717 Char charstate;
718 boolean allread, done;
719 aas aa; * temporary amino acid for input *
720
721 if (printdata)
722 headings(chars, "Sequences", "---------");
723 aasread = 0;
724 allread = false;
725 while (!(allread)) {
726 * eat white space -- if the separator line has spaces on it*
727 do {
728 charstate = gettc(infile);
729 } while (charstate == ' ' || charstate == '\t');
730 ungetc(charstate, infile);
731 if (eoln(infile)) {
732 scan_eoln(infile);
733 }
734 i = 1;
735 while (i <= spp) {
736 if ((interleaved && aasread == 0) || !interleaved)
737 initname(i - 1);
738 j = interleaved ? aasread : 0;
739 done = false;
740 while (!done && !eoff(infile)) {
741 if (interleaved)
742 done = true;
743 while (j < chars && !(eoln(infile) || eoff(infile))) {
744 charstate = gettc(infile);
745 if (charstate == '\n' || charstate == '\t')
746 charstate = ' ';
747 if (charstate == ' ' || (charstate >= '0' && charstate <= '9'))
748 continue;
749 uppercase(&charstate);
750 if ((!isalpha(charstate) && charstate != '?' &&
751 charstate != '-' && charstate != '*') || charstate == 'J' ||
752 charstate == 'O' || charstate == 'U') {
753 printf("WARNING -- BAD AMINO ACID:%c",charstate);
754 printf(" AT POSITION%5ld OF SPECIES %3ld\n",j,i);
755 exxit(-1);
756 }
757 j++;
758 aa = (charstate == 'A') ? ala :
759 (charstate == 'B') ? asx :
760 (charstate == 'C') ? cys :
761 (charstate == 'D') ? asp :
762 (charstate == 'E') ? glu :
763 (charstate == 'F') ? phe :
764 (charstate == 'G') ? gly : aa;
765 aa = (charstate == 'H') ? his :
766 (charstate == 'I') ? ileu :
767 (charstate == 'K') ? lys :
768 (charstate == 'L') ? leu :
769 (charstate == 'M') ? met :
770 (charstate == 'N') ? asn :
771 (charstate == 'P') ? pro :
772 (charstate == 'Q') ? gln :
773 (charstate == 'R') ? arg : aa;
774 aa = (charstate == 'S') ? ser :
775 (charstate == 'T') ? thr :
776 (charstate == 'V') ? val :
777 (charstate == 'W') ? trp :
778 (charstate == 'X') ? unk :
779 (charstate == 'Y') ? tyr :
780 (charstate == 'Z') ? glx :
781 (charstate == '*') ? stop :
782 (charstate == '?') ? quest:
783 (charstate == '-') ? del : aa;
784
785 treenode[i - 1]->seq[j - 1] = aa;
786 memcpy(treenode[i - 1]->siteset[j - 1],
787 translate[(long)aa - (long)ala], sizeof(sitearray));
788 }
789 if (interleaved)
790 continue;
791 if (j < chars)
792 scan_eoln(infile);
793 else if (j == chars)
794 done = true;
795 }
796 if (interleaved && i == 1)
797 aasnew = j;
798 scan_eoln(infile);
799 if ((interleaved && j != aasnew) || ((!interleaved) && j != chars)){
800 printf("ERROR: SEQUENCES OUT OF ALIGNMENT\n");
801 exxit(-1);}
802 i++;
803 }
804 if (interleaved) {
805 aasread = aasnew;
806 allread = (aasread == chars);
807 } else
808 allread = (i > spp);
809 }
810 if (printdata) {
811 for (i = 1; i <= ((chars - 1) / 60 + 1); i++) {
812 for (j = 1; j <= (spp); j++) {
813 for (k = 0; k < nmlngth; k++)
814 putc(nayme[j - 1][k], outfile);
815 fprintf(outfile, " ");
816 l = i * 60;
817 if (l > chars)
818 l = chars;
819 for (k = (i - 1) * 60 + 1; k <= l; k++) {
820 if (j > 1 && treenode[j - 1]->seq[k - 1] == treenode[0]->seq[k - 1])
821 charstate = '.';
822 else {
823 tmpa = treenode[j-1]->seq[k-1];
824 charstate = (tmpa == ala) ? 'A' :
825 (tmpa == asx) ? 'B' :
826 (tmpa == cys) ? 'C' :
827 (tmpa == asp) ? 'D' :
828 (tmpa == glu) ? 'E' :
829 (tmpa == phe) ? 'F' :
830 (tmpa == gly) ? 'G' :
831 (tmpa == his) ? 'H' :
832 (tmpa ==ileu) ? 'I' :
833 (tmpa == lys) ? 'K' :
834 (tmpa == leu) ? 'L' : charstate;
835 charstate = (tmpa == met) ? 'M' :
836 (tmpa == asn) ? 'N' :
837 (tmpa == pro) ? 'P' :
838 (tmpa == gln) ? 'Q' :
839 (tmpa == arg) ? 'R' :
840 (tmpa == ser) ? 'S' :
841 (tmpa ==ser1) ? 'S' :
842 (tmpa ==ser2) ? 'S' : charstate;
843 charstate = (tmpa == thr) ? 'T' :
844 (tmpa == val) ? 'V' :
845 (tmpa == trp) ? 'W' :
846 (tmpa == unk) ? 'X' :
847 (tmpa == tyr) ? 'Y' :
848 (tmpa == glx) ? 'Z' :
849 (tmpa == del) ? '-' :
850 (tmpa ==stop) ? '*' :
851 (tmpa==quest) ? '?' : charstate;
852 }
853 putc(charstate, outfile);
854 if (k % 10 == 0 && k % 60 != 0)
855 putc(' ', outfile);
856 }
857 putc('\n', outfile);
858 }
859 putc('\n', outfile);
860 }
861 putc('\n', outfile);
862 }
863 putc('\n', outfile);
864 } * protinputdata */
865
866
867 static void convertprotseq(char **seq)
868 {
869 aas aa;
870 char charstate;
871 int i, j;
872 for (i = 1; i <= spp; i++) {
873 for (j = 1; j <= chars; j++) {
874 aa = unk;
875 charstate = seq[i-1][j-1];
876 aa = (charstate == 'A') ? ala :
877 (charstate == 'B') ? asx :
878 (charstate == 'C') ? cys :
879 (charstate == 'D') ? asp :
880 (charstate == 'E') ? glu :
881 (charstate == 'F') ? phe :
882 (charstate == 'G') ? gly : aa;
883 aa = (charstate == 'H') ? his :
884 (charstate == 'I') ? ileu :
885 (charstate == 'K') ? lys :
886 (charstate == 'L') ? leu :
887 (charstate == 'M') ? met :
888 (charstate == 'N') ? asn :
889 (charstate == 'P') ? pro :
890 (charstate == 'Q') ? gln :
891 (charstate == 'R') ? arg : aa;
892 aa = (charstate == 'S') ? ser :
893 (charstate == 'T') ? thr :
894 (charstate == 'V') ? val :
895 (charstate == 'W') ? trp :
896 (charstate == 'X') ? unk :
897 (charstate == 'Y') ? tyr :
898 (charstate == 'Z') ? glx :
899 (charstate == '*') ? stop :
900 (charstate == '?') ? quest:
901 (charstate == '-') ? del : aa;
902
903 treenode[i - 1]->seq[j - 1] = aa;
904 memcpy(treenode[i - 1]->siteset[j - 1],
905 translate[(long)aa - (long)ala], sizeof(sitearray));
906 }
907 }
908 }
909
910
911 void protmakevalues()
912 {
913 /* set up fractional likelihoods at tips */
914 long i, j;
915 node *p;
916
917 for (i = 1; i <= nonodes; i++) {
918 treenode[i - 1]->back = NULL;
919 treenode[i - 1]->tip = (i <= spp);
920 treenode[i - 1]->index = i;
921 for (j = 0; j < (chars); j++)
922 treenode[i - 1]->numsteps[j] = 0;
923 if (i > spp) {
924 p = treenode[i - 1]->next;
925 while (p != treenode[i - 1]) {
926 p->back = NULL;
927 p->tip = false;
928 p->index = i;
929 for (j = 0; j < (chars); j++)
930 p->numsteps[j] = 0;
931 p = p->next;
932 }
933 }
934 }
935 } /* protmakevalues */
936
937
938 static void doinput(char** seq, char** seqname)
939 {
940 /* reads the input data */
941 long i;
942
943 if (justwts) {
944 if (firstset) {
945 //protinputdata();
946 convertprotseq(seq);
947 nayme = seqname;
948 }
949 /*for (i = 0; i < chars; i++)
950 weight[i] = 1;
951 inputweights(chars, weight, &weights);
952 if (justwts) {
953 fprintf(outfile, "\n\nWeights set # %ld:\n\n", ith);
954 if (progress)
955 printf("\nWeights set # %ld:\n\n", ith);
956 }
957 if (printdata)
958 printweights(outfile, 0, chars, weight, "Sites");*/
959 } else {
960 if (!firstset){
961 //samenumsp(&chars, ith);
962 reallocchars();
963 }
964 /*for (i = 0; i < chars; i++)
965 weight[i] = 1;
966 if (weights) {
967 inputweights(chars, weight, &weights);
968 }
969 if (weights)
970 printweights(outfile, 0, chars, weight, "Sites");*/
971 //protinputdata();
972 convertprotseq(seq);
973 nayme = seqname;
974 }
975 if(!thresh)
976 threshold = spp * 3.0;
977 for (i = 0 ; i < (chars) ; i++){
978 weight[i]*=10;
979 threshwt[i] = (long)(threshold * weight[i] + 0.5);
980 }
981 nmlngth = 0;
982 for (i = 0; i < spp; i++) {
983 if(strlen(seqname[i]) > nmlngth) nmlngth = strlen(seqname[i]);
984 }
985
986 protmakevalues();
987 } /* doinput */
988
989
990 void protfillin(node *p, node *left, node *rt)
991 {
992 /* sets up for each node in the tree the aa set for site m
993 at that point and counts the changes. The program
994 spends much of its time in this function */
995 boolean counted, done;
996 aas aa;
997 long s = 0;
998 sitearray ls, rs, qs;
999 long i, j, m, n;
1000
1001 for (m = 0; m < chars; m++) {
1002 if (left != NULL)
1003 memcpy(ls, left->siteset[m], sizeof(sitearray));
1004 if (rt != NULL)
1005 memcpy(rs, rt->siteset[m], sizeof(sitearray));
1006 if (left == NULL) {
1007 n = rt->numsteps[m];
1008 memcpy(qs, rs, sizeof(sitearray));
1009 }
1010 else if (rt == NULL) {
1011 n = left->numsteps[m];
1012 memcpy(qs, ls, sizeof(sitearray));
1013 }
1014 else {
1015 n = left->numsteps[m] + rt->numsteps[m];
1016 if ((ls[0] == rs[0]) && (ls[1] == rs[1]) && (ls[2] == rs[2])) {
1017 qs[0] = ls[0];
1018 qs[1] = ls[1];
1019 qs[2] = ls[2];
1020 }
1021 else {
1022 counted = false;
1023 for (i = 0; (!counted) && (i <= 3); i++) {
1024 switch (i) {
1025
1026 case 0:
1027 s = ls[0] & rs[0];
1028 break;
1029
1030 case 1:
1031 s = (ls[0] & rs[1]) | (ls[1] & rs[0]);
1032 break;
1033
1034 case 2:
1035 s = (ls[0] & rs[2]) | (ls[1] & rs[1]) | (ls[2] & rs[0]);
1036 break;
1037
1038 case 3:
1039 s = ls[0] | (ls[1] & rs[2]) | (ls[2] & rs[1]) | rs[0];
1040 break;
1041
1042 }
1043 if (s != 0) {
1044 qs[0] = s;
1045 counted = true;
1046 } else
1047 n += weight[m];
1048 }
1049 switch (i) {
1050 case 1:
1051 qs[1] = qs[0] | (ls[0] & rs[1]) | (ls[1] & rs[0]);
1052 qs[2] = qs[1] | (ls[0] & rs[2]) | (ls[1] & rs[1]) | (ls[2] & rs[0]);
1053 break;
1054 case 2:
1055 qs[1] = qs[0] | (ls[0] & rs[2]) | (ls[1] & rs[1]) | (ls[2] & rs[0]);
1056 qs[2] = qs[1] | ls[0] | (ls[1] & rs[2]) | (ls[2] & rs[1]) | rs[0];
1057 break;
1058 case 3:
1059 qs[1] = qs[0] | ls[0] | (ls[1] & rs[2]) | (ls[2] & rs[1]) | rs[0];
1060 qs[2] = qs[1] | ls[1] | (ls[2] & rs[2]) | rs[1];
1061 break;
1062 case 4:
1063 qs[1] = qs[0] | ls[1] | (ls[2] & rs[2]) | rs[1];
1064 qs[2] = qs[1] | ls[2] | rs[2];
1065 break;
1066 }
1067 for (aa = ala; (long)aa <= (long)stop; aa = (aas)((long)aa + 1)) {
1068 done = false;
1069 for (i = 0; (!done) && (i <= 1); i++) {
1070 if (((1L << ((long)aa)) & qs[i]) != 0) {
1071 for (j = i+1; j <= 2; j++)
1072 qs[j] |= translate[(long)aa - (long)ala][j-i];
1073 done = true;
1074 }
1075 }
1076 }
1077 }
1078 }
1079 p->numsteps[m] = n;
1080 memcpy(p->siteset[m], qs, sizeof(sitearray));
1081 }
1082 } /* protfillin */
1083
1084
1085 void protpreorder(node *p)
1086 {
1087 /* recompute number of steps in preorder taking both ancestoral and
1088 descendent steps into account */
1089 if (p != NULL && !p->tip) {
1090 protfillin (p->next, p->next->next->back, p->back);
1091 protfillin (p->next->next, p->back, p->next->back);
1092 protpreorder (p->next->back);
1093 protpreorder (p->next->next->back);
1094 }
1095 } /* protpreorder */
1096
1097
1098 void protadd(node *below, node *newtip, node *newfork)
1099 {
1100 /* inserts the nodes newfork and its left descendant, newtip,
1101 to the tree. below becomes newfork's right descendant */
1102
1103 if (below != treenode[below->index - 1])
1104 below = treenode[below->index - 1];
1105 if (below->back != NULL)
1106 below->back->back = newfork;
1107 newfork->back = below->back;
1108 below->back = newfork->next->next;
1109 newfork->next->next->back = below;
1110 newfork->next->back = newtip;
1111 newtip->back = newfork->next;
1112 if (root == below)
1113 root = newfork;
1114 root->back = NULL;
1115
1116 if (recompute) {
1117 protfillin (newfork, newfork->next->back, newfork->next->next->back);
1118 protpreorder(newfork);
1119 if (newfork != root)
1120 protpreorder(newfork->back);
1121 }
1122 } /* protadd */
1123
1124
1125 void protre_move(node **item, node **fork)
1126 {
1127 /* removes nodes item and its ancestor, fork, from the tree.
1128 the new descendant of fork's ancestor is made to be
1129 fork's second descendant (other than item). Also
1130 returns pointers to the deleted nodes, item and fork */
1131 node *p, *q, *other;
1132
1133 if ((*item)->back == NULL) {
1134 *fork = NULL;
1135 return;
1136 }
1137 *fork = treenode[(*item)->back->index - 1];
1138 if ((*item) == (*fork)->next->back)
1139 other = (*fork)->next->next->back;
1140 else other = (*fork)->next->back;
1141 if (root == *fork)
1142 root = other;
1143 p = (*item)->back->next->back;
1144 q = (*item)->back->next->next->back;
1145 if (p != NULL) p->back = q;
1146 if (q != NULL) q->back = p;
1147 (*fork)->back = NULL;
1148 p = (*fork)->next;
1149 do {
1150 p->back = NULL;
1151 p = p->next;
1152 } while (p != (*fork));
1153 (*item)->back = NULL;
1154 if (recompute) {
1155 protpreorder(other);
1156 if (other != root) protpreorder(other->back);
1157 }
1158 } /* protre_move */
1159
1160
1161 static void evaluate(node *r)
1162 {
1163 /* determines the number of steps needed for a tree. this is the
1164 minimum number of steps needed to evolve sequences on this tree */
1165 long i, steps, term;
1166 double sum;
1167
1168 sum = 0.0;
1169 for (i = 0; i < (chars); i++) {
1170 steps = r->numsteps[i];
1171 if (steps <= threshwt[i])
1172 term = steps;
1173 else
1174 term = threshwt[i];
1175 sum += term;
1176 if (usertree && which <= maxuser)
1177 fsteps[which - 1][i] = term;
1178 }
1179 if (usertree && which <= maxuser) {
1180 nsteps[which - 1] = sum;
1181 if (which == 1) {
1182 minwhich = 1;
1183 minsteps = sum;
1184 } else if (sum < minsteps) {
1185 minwhich = which;
1186 minsteps = sum;
1187 }
1188 }
1189 like = -sum;
1190 } /* evaluate */
1191
1192
1193 void protpostorder(node *p)
1194 {
1195 /* traverses a binary tree, calling PROCEDURE fillin at a
1196 node's descendants before calling fillin at the node */
1197 if (p->tip)
1198 return;
1199 protpostorder(p->next->back);
1200 protpostorder(p->next->next->back);
1201 protfillin(p, p->next->back, p->next->next->back);
1202 } /* protpostorder */
1203
1204
1205 void protreroot(node *outgroup)
1206 {
1207 /* reorients tree, putting outgroup in desired position. */
1208 node *p, *q;
1209
1210 if (outgroup->back->index == root->index)
1211 return;
1212 p = root->next;
1213 q = root->next->next;
1214 p->back->back = q->back;
1215 q->back->back = p->back;
1216 p->back = outgroup;
1217 q->back = outgroup->back;
1218 outgroup->back->back = q;
1219 outgroup->back = p;
1220 } /* protreroot */
1221
1222
1223 void protsavetraverse(node *p, long *pos, boolean *found)
1224 {
1225 /* sets BOOLEANs that indicate which way is down */
1226 p->bottom = true;
1227 if (p->tip)
1228 return;
1229 p->next->bottom = false;
1230 protsavetraverse(p->next->back, pos,found);
1231 p->next->next->bottom = false;
1232 protsavetraverse(p->next->next->back, pos,found);
1233 } /* protsavetraverse */
1234
1235
1236 void protsavetree(long *pos, boolean *found)
1237 {
1238 /* record in place where each species has to be
1239 added to reconstruct this tree */
1240 long i, j;
1241 node *p;
1242 boolean done;
1243
1244 protreroot(treenode[outgrno - 1]);
1245 protsavetraverse(root, pos,found);
1246 for (i = 0; i < (nonodes); i++)
1247 place[i] = 0;
1248 place[root->index - 1] = 1;
1249 for (i = 1; i <= (spp); i++) {
1250 p = treenode[i - 1];
1251 while (place[p->index - 1] == 0) {
1252 place[p->index - 1] = i;
1253 while (!p->bottom)
1254 p = p->next;
1255 p = p->back;
1256 }
1257 if (i > 1) {
1258 place[i - 1] = place[p->index - 1];
1259 j = place[p->index - 1];
1260 done = false;
1261 while (!done) {
1262 place[p->index - 1] = spp + i - 1;
1263 while (!p->bottom)
1264 p = p->next;
1265 p = p->back;
1266 done = (p == NULL);
1267 if (!done)
1268 done = (place[p->index - 1] != j);
1269 }
1270 }
1271 }
1272 } /* protsavetree */
1273
1274
1275 static void tryadd(node *p, node **item, node **nufork)
1276 {
1277 /* temporarily adds one fork and one tip to the tree.
1278 if the location where they are added yields greater
1279 "likelihood" than other locations tested up to that
1280 time, then keeps that location as there */
1281 long pos;
1282 boolean found;
1283 node *rute, *q;
1284
1285 if (p == root)
1286 protfillin(temp, *item, p);
1287 else {
1288 protfillin(temp1, *item, p);
1289 protfillin(temp, temp1, p->back);
1290 }
1291 evaluate(temp);
1292 if (lastrearr) {
1293 if (like < bestlike) {
1294 if ((*item) == (*nufork)->next->next->back) {
1295 q = (*nufork)->next;
1296 (*nufork)->next = (*nufork)->next->next;
1297 (*nufork)->next->next = q;
1298 q->next = (*nufork);
1299 }
1300 }
1301 else if (like >= bstlike2) {
1302 recompute = false;
1303 protadd(p, (*item), (*nufork));
1304 rute = root->next->back;
1305 protsavetree(&pos,&found);
1306 protreroot(rute);
1307 if (like > bstlike2) {
1308 bestlike = bstlike2 = like;
1309 pos = 1;
1310 nextree = 1;
1311 addtree(pos, &nextree, dummy, place, bestrees);
1312 } else {
1313 pos = 0;
1314 findtree(&found, &pos, nextree, place, bestrees);
1315 if (!found) {
1316 if (nextree <= maxtrees)
1317 addtree(pos, &nextree, dummy, place, bestrees);
1318 }
1319 }
1320 protre_move (item, nufork);
1321 recompute = true;
1322 }
1323 }
1324 if (like >= bestyet) {
1325 bestyet = like;
1326 there = p;
1327 }
1328 } /* tryadd */
1329
1330
1331 static void addpreorder(node *p, node *item, node *nufork)
1332 {
1333 /* traverses a binary tree, calling PROCEDURE tryadd
1334 at a node before calling tryadd at its descendants */
1335
1336 if (p == NULL)
1337 return;
1338 tryadd(p, &item,&nufork);
1339 if (!p->tip) {
1340 addpreorder(p->next->back, item, nufork);
1341 addpreorder(p->next->next->back, item, nufork);
1342 }
1343 } /* addpreorder */
1344
1345
1346 static void tryrearr(node *p, boolean *success)
1347 {
1348 /* evaluates one rearrangement of the tree.
1349 if the new tree has greater "likelihood" than the old
1350 one sets success := TRUE and keeps the new tree.
1351 otherwise, restores the old tree */
1352 node *frombelow, *whereto, *forknode, *q;
1353 double oldlike;
1354
1355 if (p->back == NULL)
1356 return;
1357 forknode = treenode[p->back->index - 1];
1358 if (forknode->back == NULL)
1359 return;
1360 oldlike = bestyet;
1361 if (p->back->next->next == forknode)
1362 frombelow = forknode->next->next->back;
1363 else
1364 frombelow = forknode->next->back;
1365 whereto = treenode[forknode->back->index - 1];
1366 if (whereto->next->back == forknode)
1367 q = whereto->next->next->back;
1368 else
1369 q = whereto->next->back;
1370 protfillin(temp1, frombelow, q);
1371 protfillin(temp, temp1, p);
1372 protfillin(temp1, temp, whereto->back);
1373 evaluate(temp1);
1374 if (like - oldlike < LIKE_EPSILON) {
1375 if (p == forknode->next->next->back) {
1376 q = forknode->next;
1377 forknode->next = forknode->next->next;
1378 forknode->next->next = q;
1379 q->next = forknode;
1380 }
1381 }
1382 else {
1383 recompute = false;
1384 protre_move(&p, &forknode);
1385 protfillin(whereto, whereto->next->back, whereto->next->next->back);
1386 recompute = true;
1387 protadd(whereto, p, forknode);
1388 *success = true;
1389 bestyet = like;
1390 }
1391 } /* tryrearr */
1392
1393
1394 static void repreorder(node *p, boolean *success)
1395 {
1396 /* traverses a binary tree, calling PROCEDURE tryrearr
1397 at a node before calling tryrearr at its descendants */
1398 if (p == NULL)
1399 return;
1400 tryrearr(p,success);
1401 if (!p->tip) {
1402 repreorder(p->next->back,success);
1403 repreorder(p->next->next->back,success);
1404 }
1405 } /* repreorder */
1406
1407
1408 static void rearrange(node **r)
1409 {
1410 /* traverses the tree (preorder), finding any local
1411 rearrangement which decreases the number of steps.
1412 if traversal succeeds in increasing the tree's
1413 "likelihood", PROCEDURE rearrange runs traversal again */
1414 boolean success = true;
1415 while (success) {
1416 success = false;
1417 repreorder(*r, &success);
1418 }
1419 } /* rearrange */
1420
1421
1422 void protgetch(Char *c)
1423 {
1424 /* get next nonblank character */
1425 do {
1426 if (eoln(intree))
1427 scan_eoln(intree);
1428 *c = gettc(intree);
1429 if (*c == '\n' || *c == '\t')
1430 *c = ' ';
1431 } while (!(*c != ' ' || eoff(intree)));
1432 } /* protgetch */
1433
1434
1435 void protaddelement(node **p,long *nextnode,long *lparens,boolean *names)
1436 {
1437 /* recursive procedure adds nodes to user-defined tree */
1438 node *q;
1439 long i, n;
1440 boolean found;
1441 Char str[nmlngth + 1];
1442
1443 protgetch(&ch);
1444
1445 if (ch == '(' ) {
1446 if ((*lparens) >= spp - 1) {
1447 printf("\nERROR IN USER TREE: TOO MANY LEFT PARENTHESES\n");
1448 exxit(-1);
1449 }
1450 (*nextnode)++;
1451 (*lparens)++;
1452 q = treenode[(*nextnode) - 1];
1453 protaddelement(&q->next->back, nextnode,lparens,names);
1454 q->next->back->back = q->next;
1455 findch(',', &ch, which);
1456 protaddelement(&q->next->next->back, nextnode,lparens,names);
1457 q->next->next->back->back = q->next->next;
1458 findch(')', &ch, which);
1459 *p = q;
1460 return;
1461 }
1462 /*for (i = 0; i < nmlngth; i++)
1463 str[i] = ' ';*/
1464 n = 1;
1465 do {
1466 /*if (ch == '_')
1467 ch = ' ';*/
1468 str[n - 1] = ch;
1469 if (eoln(intree))
1470 scan_eoln(intree);
1471 ch = gettc(intree);
1472 n++;
1473 } while (ch != ',' && ch != ')' && ch != ':' && n <= nmlngth);
1474 str[n-1] = 0;
1475 n = 1;
1476 do {
1477 int l = strlen(nayme[n-1]);
1478 found = true;
1479 for (i = 0; i <= l; i++)
1480 found = (found && ((str[i] == nayme[n - 1][i]) /*||
1481 ((nayme[n - 1][i] == '_') && (str[i] == ' '))*/ ));
1482 if (found) {
1483 if (names[n - 1] == false) {
1484 *p = treenode[n - 1];
1485 names[n - 1] = true;
1486 } else {
1487 printf("\nERROR IN USER TREE: DUPLICATE NAME FOUND -- ");
1488 for (i = 0; i < l; i++)
1489 putchar(nayme[n - 1][i]);
1490 putchar('\n');
1491 exxit(-1);
1492 }
1493 } else
1494 n++;
1495 } while (!(n > spp || found));
1496 if (n <= spp)
1497 return;
1498 printf("CANNOT FIND SPECIES: %s\n", str);
1499 } /* protaddelement */
1500
1501
1502 void prottreeread()
1503 {
1504 /* read in user-defined tree and set it up */
1505 long nextnode, lparens, i;
1506
1507 root = treenode[spp];
1508 nextnode = spp;
1509 root->back = NULL;
1510 names = (boolean *)Malloc(spp*sizeof(boolean));
1511 for (i = 0; i < (spp); i++)
1512 names[i] = false;
1513 lparens = 0;
1514 protaddelement(&root, &nextnode,&lparens,names);
1515 if (ch == '[') {
1516 do
1517 ch = gettc(intree);
1518 while (ch != ']');
1519 ch = gettc(intree);
1520 }
1521 findch(';', &ch, which);
1522 scan_eoln(intree);
1523 free(names);
1524 } /* prottreeread */
1525
1526
1527 void protancestset(long *a, long *b, long *c, long *d, long *k)
1528 {
1529 /* sets up the aa set array. */
1530 aas aa;
1531 long s, sa, sb;
1532 long i, j, m, n;
1533 boolean counted;
1534
1535 counted = false;
1536 *k = 0;
1537 for (i = 0; i <= 5; i++) {
1538 if (*k < 3) {
1539 s = 0;
1540 if (i > 3)
1541 n = i - 3;
1542 else
1543 n = 0;
1544 for (j = n; j <= (i - n); j++) {
1545 if (j < 3)
1546 sa = a[j];
1547 else
1548 sa = fullset;
1549 for (m = n; m <= (i - j - n); m++) {
1550 if (m < 3)
1551 sb = sa & b[m];
1552 else
1553 sb = sa;
1554 if (i - j - m < 3)
1555 sb &= c[i - j - m];
1556 s |= sb;
1557 }
1558 }
1559 if (counted || s != 0) {
1560 d[*k] = s;
1561 (*k)++;
1562 counted = true;
1563 }
1564 }
1565 }
1566 for (i = 0; i <= 1; i++) {
1567 for (aa = ala; (long)aa <= (long)stop; aa = (aas)((long)aa + 1)) {
1568 if (((1L << ((long)aa)) & d[i]) != 0) {
1569 for (j = i + 1; j <= 2; j++)
1570 d[j] |= translate[(long)aa - (long)ala][j - i];
1571 }
1572 }
1573 }
1574 } /* protancestset */
1575
1576
1577 /*void prothyprint(long b1, long b2, boolean *bottom, node *r,
1578 boolean *nonzero, boolean *maybe)
1579 {
1580 * print out states in sites b1 through b2 at node *
1581 long i;
1582 boolean dot;
1583 Char ch = 0;
1584 aas aa;
1585
1586 if (*bottom) {
1587 if (!outgropt)
1588 fprintf(outfile, " ");
1589 else
1590 fprintf(outfile, "root ");
1591 } else
1592 fprintf(outfile, "%3ld ", r->back->index - spp);
1593 if (r->tip) {
1594 for (i = 0; i < nmlngth; i++)
1595 putc(nayme[r->index - 1][i], outfile);
1596 } else
1597 fprintf(outfile, "%4ld ", r->index - spp);
1598 if (*bottom)
1599 fprintf(outfile, " ");
1600 else if (*nonzero)
1601 fprintf(outfile, " yes ");
1602 else if (*maybe)
1603 fprintf(outfile, " maybe ");
1604 else
1605 fprintf(outfile, " no ");
1606 for (i = b1 - 1; i < b2; i++) {
1607 aa = r->seq[i];
1608 switch (aa) {
1609
1610 case ala:
1611 ch = 'A';
1612 break;
1613
1614 case asx:
1615 ch = 'B';
1616 break;
1617
1618 case cys:
1619 ch = 'C';
1620 break;
1621
1622 case asp:
1623 ch = 'D';
1624 break;
1625
1626 case glu:
1627 ch = 'E';
1628 break;
1629
1630 case phe:
1631 ch = 'F';
1632 break;
1633
1634 case gly:
1635 ch = 'G';
1636 break;
1637
1638 case his:
1639 ch = 'H';
1640 break;
1641
1642 case ileu:
1643 ch = 'I';
1644 break;
1645
1646 case lys:
1647 ch = 'K';
1648 break;
1649
1650 case leu:
1651 ch = 'L';
1652 break;
1653
1654 case met:
1655 ch = 'M';
1656 break;
1657
1658 case asn:
1659 ch = 'N';
1660 break;
1661
1662 case pro:
1663 ch = 'P';
1664 break;
1665
1666 case gln:
1667 ch = 'Q';
1668 break;
1669
1670 case arg:
1671 ch = 'R';
1672 break;
1673
1674 case ser:
1675 ch = 'S';
1676 break;
1677
1678 case ser1:
1679 ch = 'S';
1680 break;
1681
1682 case ser2:
1683 ch = 'S';
1684 break;
1685
1686 case thr:
1687 ch = 'T';
1688 break;
1689
1690 case trp:
1691 ch = 'W';
1692 break;
1693
1694 case tyr:
1695 ch = 'Y';
1696 break;
1697
1698 case val:
1699 ch = 'V';
1700 break;
1701
1702 case glx:
1703 ch = 'Z';
1704 break;
1705
1706 case del:
1707 ch = '-';
1708 break;
1709
1710 case stop:
1711 ch = '*';
1712 break;
1713
1714 case unk:
1715 ch = 'X';
1716 break;
1717
1718 case quest:
1719 ch = '?';
1720 break;
1721 }
1722 if (!(*bottom) && dotdiff)
1723 dot = (r->siteset[i] [0] == treenode[r->back->index - 1]->siteset[i][0]
1724 || ((r->siteset[i][0] &
1725 (~((1L << ((long)ser1)) | (1L << ((long)ser2)) |
1726 (1L << ((long)ser))))) == 0 &&
1727 (treenode[r->back->index - 1]->siteset[i] [0] &
1728 (~((1L << ((long)ser1)) | (1L << ((long)ser2)) |
1729 (1L << ((long)ser))))) == 0));
1730 else
1731 dot = false;
1732 if (dot)
1733 putc('.', outfile);
1734 else
1735 putc(ch, outfile);
1736 if ((i + 1) % 10 == 0)
1737 putc(' ', outfile);
1738 }
1739 putc('\n', outfile);
1740 } * prothyprint *
1741
1742
1743 void prothyptrav(node *r, sitearray *hypset, long b1, long b2, long *k,
1744 boolean *bottom, sitearray nothing)
1745 {
1746 boolean maybe, nonzero;
1747 long i;
1748 aas aa;
1749 long anc = 0, hset;
1750 gseq *ancset, *temparray;
1751
1752 protgnu(&ancset);
1753 protgnu(&temparray);
1754 maybe = false;
1755 nonzero = false;
1756 for (i = b1 - 1; i < b2; i++) {
1757 if (!r->tip) {
1758 protancestset(hypset[i], r->next->back->siteset[i],
1759 r->next->next->back->siteset[i], temparray->seq[i], k);
1760 memcpy(r->siteset[i], temparray->seq[i], sizeof(sitearray));
1761 }
1762 if (!(*bottom))
1763 anc = treenode[r->back->index - 1]->siteset[i][0];
1764 if (!r->tip) {
1765 hset = r->siteset[i][0];
1766 r->seq[i] = quest;
1767 for (aa = ala; (long)aa <= (long)stop; aa = (aas)((long)aa + 1)) {
1768 if (hset == 1L << ((long)aa))
1769 r->seq[i] = aa;
1770 }
1771 if (hset == ((1L << ((long)asn)) | (1L << ((long)asp))))
1772 r->seq[i] = asx;
1773 if (hset == ((1L << ((long)gln)) | (1L << ((long)gly))))
1774 r->seq[i] = glx;
1775 if (hset == ((1L << ((long)ser1)) | (1L << ((long)ser2))))
1776 r->seq[i] = ser;
1777 if (hset == fullset)
1778 r->seq[i] = unk;
1779 }
1780 nonzero = (nonzero || (r->siteset[i][0] & anc) == 0);
1781 maybe = (maybe || r->siteset[i][0] != anc);
1782 }
1783 prothyprint(b1, b2,bottom,r,&nonzero,&maybe);
1784 *bottom = false;
1785 if (!r->tip) {
1786 memcpy(temparray->seq, r->next->back->siteset, chars*sizeof(sitearray));
1787 for (i = b1 - 1; i < b2; i++)
1788 protancestset(hypset[i], r->next->next->back->siteset[i], nothing,
1789 ancset->seq[i], k);
1790 prothyptrav(r->next->back, ancset->seq, b1, b2,k,bottom,nothing );
1791 for (i = b1 - 1; i < b2; i++)
1792 protancestset(hypset[i], temparray->seq[i], nothing, ancset->seq[i],k);
1793 prothyptrav(r->next->next->back, ancset->seq, b1, b2, k,bottom,nothing);
1794 }
1795 protchuck(temparray);
1796 protchuck(ancset);
1797 } * prothyptrav *
1798
1799
1800 void prothypstates(long *k)
1801 {
1802 * fill in and describe states at interior nodes *
1803 boolean bottom;
1804 sitearray nothing;
1805 long i, n;
1806 seqptr hypset;
1807
1808 fprintf(outfile, "\nFrom To Any Steps? State at upper node\n");
1809 fprintf(outfile, " ");
1810 fprintf(outfile, "( . means same as in the node below it on tree)\n\n");
1811 memcpy(nothing, translate[(long)quest - (long)ala], sizeof(sitearray));
1812 hypset = (seqptr)Malloc(chars*sizeof(sitearray));
1813 for (i = 0; i < (chars); i++)
1814 memcpy(hypset[i], nothing, sizeof(sitearray));
1815 bottom = true;
1816 for (i = 1; i <= ((chars - 1) / 40 + 1); i++) {
1817 putc('\n', outfile);
1818 n = i * 40;
1819 if (n > chars)
1820 n = chars;
1821 bottom = true;
1822 prothyptrav(root, hypset, i * 40 - 39, n, k,&bottom,nothing);
1823 }
1824 free(hypset);
1825 } * prothypstates */
1826
1827
1828 static void describe()
1829 {
1830 /* prints ancestors, steps and table of numbers of steps in
1831 each site */
1832 /*long i,j,k;
1833
1834 if (treeprint)
1835 fprintf(outfile, "\nrequires a total of %10.3f\n", like / -10);
1836 if (stepbox) {
1837 putc('\n', outfile);
1838 if (weights)
1839 fprintf(outfile, "weighted ");
1840 fprintf(outfile, "steps in each position:\n");
1841 fprintf(outfile, " ");
1842 for (i = 0; i <= 9; i++)
1843 fprintf(outfile, "%4ld", i);
1844 fprintf(outfile, "\n *-----------------------------------------\n");
1845 for (i = 0; i <= (chars / 10); i++) {
1846 fprintf(outfile, "%5ld", i * 10);
1847 putc('!', outfile);
1848 for (j = 0; j <= 9; j++) {
1849 k = i * 10 + j;
1850 if (k == 0 || k > chars)
1851 fprintf(outfile, " ");
1852 else
1853 fprintf(outfile, "%4ld", root->numsteps[k - 1] / 10);
1854 }
1855 putc('\n', outfile);
1856 }
1857 }
1858 if (ancseq) {
1859 prothypstates(&k);
1860 putc('\n', outfile);
1861 }
1862 putc('\n', outfile);*/
1863 if (trout) {
1864 col = 0;
1865 treeout(root, nextree, &col, root);
1866 }
1867 } /* describe */
1868
1869
1870 static void maketree(char *toevaluate)
1871 {
1872 /* constructs a binary tree from the pointers in treenode.
1873 adds each node at location which yields highest "likelihood"
1874 then rearranges the tree for greatest "likelihood" */
1875 long i, j, numtrees;
1876 double gotlike;
1877 node *item, *nufork, *dummy;
1878
1879 if (setjmp(lngjmp_env)) {
1880 trout = false;
1881 goto way_out;
1882 }
1883
1884 if (!usertree) {
1885 for (i = 1; i <= (spp); i++)
1886 enterorder[i - 1] = i;
1887 if (jumble)
1888 randumize(seed, enterorder);
1889 root = treenode[enterorder[0] - 1];
1890 recompute = true;
1891 protadd(treenode[enterorder[0] - 1], treenode[enterorder[1] - 1],
1892 treenode[spp]);
1893 /*if (progress) {
1894 printf("\nAdding species:\n");
1895 writename(0, 2, enterorder);
1896 }*/
1897 lastrearr = false;
1898 for (i = 3; i <= (spp); i++) {
1899 bestyet = -30.0*spp*chars;
1900 there = root;
1901 item = treenode[enterorder[i - 1] - 1];
1902 nufork = treenode[spp + i - 2];
1903 addpreorder(root, item, nufork);
1904 protadd(there, item, nufork);
1905 like = bestyet;
1906 rearrange(&root);
1907 /*if (progress)
1908 writename(i - 1, 1, enterorder);*/
1909 lastrearr = (i == spp);
1910 if (lastrearr) {
1911 /*if (progress) {
1912 printf("\nDoing global rearrangements\n");
1913 printf(" !");
1914 for (j = 1; j <= nonodes; j++)
1915 if ( j % (( nonodes / 72 ) + 1 ) == 0 )
1916 putchar('-');
1917 printf("!\n");
1918 }*/
1919 bestlike = bestyet;
1920 if (jumb == 1) {
1921 bstlike2 = bestlike = -30.0*spp*chars;
1922 nextree = 1;
1923 }
1924 do {
1925 /*if (progress)
1926 printf(" ");*/
1927 gotlike = bestlike;
1928 for (j = 0; j < (nonodes); j++) {
1929 //bestyet = -30.0*spp*chars;
1930 item = treenode[j];
1931 if (item != root) {
1932 bestyet = -30.0*spp*chars;
1933 nufork = treenode[treenode[j]->back->index - 1];
1934 protre_move(&item, &nufork);
1935 there = root;
1936 addpreorder(root, item, nufork);
1937 protadd(there, item, nufork);
1938 }
1939 /*if (progress) {
1940 if ( j % (( nonodes / 72 ) + 1 ) == 0 )
1941 putchar('.');
1942 fflush(stdout);
1943 }*/
1944 if (tree_build_interrupted) {
1945 longjmp(lngjmp_env, 0);
1946 }
1947 }
1948 /*if (progress)
1949 putchar('\n');*/
1950 } while (bestlike > gotlike);
1951 }
1952 }
1953 /*if (progress)
1954 putchar('\n');*/
1955 for (i = spp - 1; i >= 1; i--)
1956 protre_move(&treenode[i], &dummy);
1957 if (jumb == njumble) {
1958 /*if (treeprint) {
1959 putc('\n', outfile);
1960 if (nextree == 2)
1961 fprintf(outfile, "One most parsimonious tree found:\n");
1962 else
1963 fprintf(outfile, "%6ld trees in all found\n", nextree - 1);
1964 }*/
1965 if (nextree > maxtrees + 1) {
1966 /*if (treeprint)
1967 fprintf(outfile, "here are the first%4ld of them\n", (long)maxtrees);*/
1968 nextree = maxtrees + 1;
1969 }
1970 /*if (treeprint)
1971 putc('\n', outfile);*/
1972 recompute = false;
1973 for (i = 0; i <= (nextree - 2); i++) {
1974 root = treenode[0];
1975 protadd(treenode[0], treenode[1], treenode[spp]);
1976 for (j = 3; j <= (spp); j++)
1977 protadd(treenode[bestrees[i].btree[j - 1] - 1], treenode[j - 1],
1978 treenode[spp + j - 2]);
1979 protreroot(treenode[outgrno - 1]);
1980 protpostorder(root);
1981 evaluate(root);
1982 //printree(root, 1.0);
1983 describe();
1984 for (j = 1; j < (spp); j++)
1985 protre_move(&treenode[j], &dummy);
1986 }
1987 }
1988 } else {
1989 /* Open in binary: ftell() is broken for UNIX line-endings under _WIN32 */
1990 //openfile(&intree,INTREE,"input tree file", "rb",progname,intreename);
1991 //numtrees = countsemic(&intree);
1992 numtrees = 1;
1993 /*if (numtrees > 2)
1994 initseed(&inseed, &inseed0, seed);*/
1995 /*if (treeprint) {
1996 fprintf(outfile, "User-defined tree");
1997 if (numtrees > 1)
1998 putc('s', outfile);
1999 fprintf(outfile, ":\n\n\n\n");
2000 }*/
2001 which = 1;
2002 while (which <= numtrees) {
2003 char *tmpfname = strdup(create_tmp_filename_from_C());
2004 intree = fl_fopen_from_C(tmpfname, "rb+");
2005 fwrite(toevaluate, strlen(toevaluate), 1, intree);
2006 fflush(intree);
2007 fseek(intree, SEEK_SET, 0);
2008 prottreeread();
2009 fclose(intree);
2010 fl_unlink_from_C(tmpfname);
2011 free(tmpfname);
2012 if (outgropt)
2013 protreroot(treenode[outgrno - 1]);
2014 protpostorder(root);
2015 evaluate(root);
2016 //printree(root, 1.0);
2017 describe();
2018 which++;
2019 }
2020 /*printf("\n");
2021 FClose(intree);
2022 putc('\n', outfile);*/
2023 if (numtrees > 1 && chars > 1 )
2024 standev(chars, numtrees, minwhich, minsteps, nsteps, fsteps, seed);
2025 }
2026 /*if (jumb == njumble && progress) {
2027 printf("Output written to file \"%s\"\n", outfilename);
2028 if (trout)
2029 printf("\nTrees also written onto file \"%s\"\n", outtreename);
2030 }*/
2031 way_out:
2032 return;
2033 } /* maketree */
2034
2035
2036 char* protpars(char** seq, char** seqname, int notu, int njumbles, int *pjumble_no, int *steps, char* toevaluate, int arg_maxtrees/*unused*/, int *bt_weights, int unused)
2037 { /* Protein parsimony by uphill search */
2038 //init(argc,argv);
2039 progname = "Protpars";
2040 //openfile(&infile,INFILE,"input file", "r",argv[0],infilename);
2041 //openfile(&outfile,OUTFILE,"output file", "w",argv[0],outfilename);
2042
2043 ibmpc = IBMCRT;
2044 ansi = ANSICRT;
2045 garbage = NULL;
2046 mulsets = false;
2047 msets = 1;
2048 firstset = true;
2049 code();
2050 setup();
2051 doinit(notu, strlen(seq[0]), toevaluate);
2052 if (!toevaluate && njumbles > 0) {
2053 njumble = njumbles;
2054 jumble = true;
2055 }
2056 /*if (weights || justwts)
2057 openfile(&weightfile,WEIGHTFILE,"weights file","r",argv[0],weightfilename);*/
2058 if (trout) {
2059 outtreename = strdup(create_tmp_filename_from_C()); //openfile(&outtree,OUTTREE,"output tree file", "w",argv[0],outtreename);
2060 outtree = fl_fopen_from_C(outtreename, "wb");
2061 }
2062 for (ith = 1; ith <= msets; ith++) {
2063 int i;
2064 if (bt_weights != NULL) for (i = 0; i < chars; i++) weight[i] = bt_weights[i];
2065 else for (i = 0; i < chars; i++) weight[i] = 1;
2066 doinput(seq, seqname);
2067 if (ith == 1)
2068 firstset = false;
2069 /*if (msets > 1 && !justwts) {
2070 fprintf(outfile, "Data set # %ld:\n\n",ith);
2071 if (progress)
2072 printf("Data set # %ld:\n\n",ith);
2073 }*/
2074 for (jumb = 1; jumb <= njumble; jumb++) {
2075 maketree(toevaluate);
2076 if (jumble && pjumble_no) {
2077 *steps = (int)(bestyet / -10);
2078 *pjumble_no = jumb;
2079 awake_from_C();
2080 }
2081 }
2082 protfreetree();
2083 }
2084 /*FClose(infile);
2085 FClose(outfile);
2086 FClose(outtree);
2087 printf("\nDone.\n\n");*/
2088 char *tree = NULL;
2089 FClose(outtree);
2090 if (trout) {
2091 outtree = fl_fopen_from_C(outtreename, "rb"); // read tree from tmp file and return it as a char*
2092 fseek(outtree, 0, SEEK_END);
2093 long L=ftell(outtree);
2094 tree = (char*)malloc(L+1);
2095 fseek(outtree, 0, SEEK_SET);
2096 fread(tree, L, 1, outtree);
2097 tree[L] = 0;
2098 fclose(outtree);
2099 *steps = (int)(like / -10);
2100 }
2101 fl_unlink_from_C(outtreename);
2102 free(outtreename);
2103 return tree;
2104 } /* Protein parsimony by uphill search */