1 /*Modified by Christian Zmasek. Use at your own risk.*/
7 /* version 3.6. (c) Copyright 1993-2004 by the University of Washington.
8 Written by Joseph Felsenstein, Akiko Fuseki, Sean Lamont, Andrew Keeffe,
9 Mike Palczewski, Doug Buxton and Dan Fineman.
10 Permission is granted to copy and use this program provided no fee is
11 charged for it and provided that this copyright notice is not removed. */
13 #define VERSION "3.65"
15 /* machine-specific stuff:
16 based on a number of factors in the library stdlib.h, we will try
17 to determine what kind of machine/compiler this program is being
18 built on. However, it doesn't always succeed. However, if you have
19 ANSI conforming C, it will probably work.
21 We will try to figure out machine type
22 based on defines in stdio, and compiler-defined things as well.: */
29 void phyClearScreen(void);
30 void phySaveConsoleAttributes(void);
31 void phySetConsoleAttributes(void);
32 void phyRestoreConsoleAttributes(void);
33 void phyFillScreenColor(void);
54 #define INFILE "infile data"
55 #define OUTFILE "outfile data"
56 #define FONTFILE "fontfile data"
57 #define PLOTFILE "plotfile data"
58 #define INTREE "intree data"
59 #define INTREE2 "intree data 2"
60 #define OUTTREE "outtree data"
61 #define CATFILE "categories data"
62 #define WEIGHTFILE "weights data"
63 #define ANCFILE "ancestors data"
64 #define MIXFILE "mixture data"
65 #define FACTFILE "factors data"
68 #define INFILE "infile"
69 #define OUTFILE "outfile"
70 #define FONTFILE "fontfile" /* on unix this might be /usr/local/lib/fontfile */
71 #define PLOTFILE "plotfile"
72 #define INTREE "intree"
73 #define INTREE2 "intree2"
74 #define OUTTREE "outtree"
75 #define CATFILE "categories"
76 #define WEIGHTFILE "weights"
77 #define ANCFILE "ancestors"
78 #define MIXFILE "mixture"
79 #define FACTFILE "factors"
82 #ifdef L_ctermid /* try and detect for sysV or V7. */
92 # if defined(_STDLIB_H_) || defined(_H_STDLIB) || defined(H_SCCSID) || defined(unix)
94 # define MACHINE_TYPE "BSD Unix C"
101 #define MACHINE_TYPE "VAX/VMS C"
110 /* watcom-c has graphics library calls that are almost identical to *
111 * quick-c, so the "QUICKC" symbol name stays. */
115 #define MACHINE_TYPE "MS-DOS / Quick C"
122 #define MACHINE_TYPE "MS-DOS /Microsoft C "
123 #define DOS /* DOS is always defined if on a DOS machine */
124 #define MSC /* MSC is defined for microsoft C */
127 #ifdef __MSDOS__ /* TURBO c compiler, ONLY (no other DOS C compilers) */
131 #include <graphics.h>
134 #ifdef DJGPP /* DJ Delorie's original gnu C/C++ port */
135 #include <graphics.h>
139 #define MACHINE_TYPE "ANSI C"
143 #define MALLOCRETURN void
145 #define MALLOCRETURN void
148 #define signed /* signed doesn't exist in VMS */
151 /* default screen types */
152 /* if on a DOS but not a Windows system can use IBM PC screen controls */
156 #define ANSICRT false
159 /* if on a Mac cannot use screen controls */
162 #define ANSICRT false
164 /* if on a Windows system can use IBM PC screen controls */
167 #define ANSICRT false
169 /* otherwise, let's assume we are on a Linux or Unix system
170 with ANSI terminal controls */
182 #define MALLOCRETURN void
198 #include "interface.h"
205 /* directory delimiters */
207 #define DELIMITER ':'
210 #define DELIMITER '\\'
212 #define DELIMITER '/'
217 #define FClose(file) if (file) fclose(file) ; file=NULL
218 #define Malloc(x) mymalloc((long)x)
220 typedef void *Anyptr;
221 #define Signed signed
223 #define Volatile volatile
224 #define Char char /* Characters (not bytes) */
225 #define Static static /* Private global funcs and vars */
226 #define Local static /* Nested functions */
228 typedef unsigned char boolean;
234 MALLOCRETURN *mymalloc(long);
236 #define FNMLNGTH 200 /* length of array to store a file name */
237 #define MAXNCH 26 /*changed from to 20 to 26 by CZ 2006-07-28 */
238 #define nmlngth 26 /*changed from to 10 to 26 by CZ 2006-07-28 */ /* number of characters in species name */
239 #define maxcategs 9 /* maximum number of site types */
240 #define maxcategs2 11 /* maximum number of site types + 2 */
244 #define MAXSHIMOTREES 100
246 #define smoothings 4 /* number of passes through smoothing algorithm */
247 #define iterations 4 /* number of iterates for each branch */
248 #define epsilon 0.0001 /* small number used in makenewv */
249 #define EPSILON 0.00001 /* small number used in hermite root-finding */
250 #define initialv 0.1 /* starting branch length unless otherwise */
251 #define over 60 /* maximum width all branches of tree on screen */
252 #define SQRTPI 1.7724538509055160273
253 #define SQRT2 1.4142135623730950488
255 #define NLRSAVES 5 /* number of views that need to be saved during local *
258 typedef long *steptr;
259 typedef long longer[6];
260 typedef char naym[MAXNCH];
261 typedef long *bitptr;
262 typedef double raterootarray[maxcategs2][maxcategs2];
264 typedef struct bestelm {
271 extern FILE *infile, *outfile, *intree, *intree2, *outtree,
272 *weightfile, *catfile, *ancfile, *mixfile, *factfile;
273 extern long spp, words, bits;
274 extern boolean ibmpc, ansi, tranvsp;
275 extern naym *nayme; /* names of species */
278 #define ebcdic EBCDIC
280 typedef Char plotstring[MAXNCH];
282 /* Approx. 1GB, used to test for memory request errors */
283 #define TOO_MUCH_MEMORY 1000000000
286 /* The below pre-processor commands define the type used to store
287 group arrays. We can't use #elif for metrowerks, so we use
288 cascaded if statements */
291 /* minimum double we feel safe with, anything less will be considered
293 #define MIN_DOUBLE 10e-100
295 /* K&R says that there should be a plus in front of the number, but no
296 machine we've seen actually uses one; we'll include it just in
298 #define MAX_32BITS 2147483647
299 #define MAX_32BITS_PLUS +2147483647
301 /* If ints are 4 bytes, use them */
302 #if INT_MAX == MAX_32BITS
303 typedef int group_type;
306 #if INT_MAX == MAX_32BITS_PLUS
307 typedef int group_type;
310 /* Else, if longs are 4 bytes, use them */
311 #if LONG_MAX == MAX_32BITS
312 typedef long group_type;
315 #if LONG_MAX == MAX_32BITS_PLUS
316 typedef long group_type;
318 /* Default to longs */
320 typedef long group_type;
327 /* for many programs */
329 #define maxuser 1000 /* maximum number of user-defined trees */
331 typedef Char **sequence;
338 alanine, arginine, asparagine, aspartic, cysteine,
339 glutamine, glutamic, glycine, histidine, isoleucine,
340 leucine, lysine, methionine, phenylalanine, proline,
341 serine, threonine, tryptophan, tyrosine, valine
347 zero = 0, one, two, three, four, five, six, seven
353 ala, arg, asn, asp, cys, gln, glu, gly, his, ileu, leu, lys, met, phe, pro,
354 ser1, ser2, thr, trp, tyr, val, del, stop, asx, glx, ser, unk, quest
357 typedef double sitelike[(long)T - (long)A + 1]; /* used in dnaml, dnadist */
358 typedef double psitelike[(long)valine - (long)alanine + 1];
361 typedef long *baseptr; /* baseptr used in dnapars, dnacomp & dnapenny */
362 typedef long *baseptr2; /* baseptr used in dnamove */
363 typedef unsigned char *discbaseptr; /* discbaseptr used in pars */
364 typedef sitelike *ratelike; /* used in dnaml ... */
365 typedef psitelike *pratelike; /* used in proml */
366 typedef ratelike *phenotype; /* phenotype used in dnaml, dnamlk, dnadist */
367 typedef pratelike *pphenotype; /* phenotype used in proml */
368 typedef double *sitelike2;
369 typedef sitelike2 *phenotype2; /* phenotype2 used in restml */
370 typedef double *phenotype3; /* for continuous char programs */
372 typedef double *vector; /* used in distance programs */
374 typedef long nucarray[(long)O - (long)A + 1];
375 typedef long discnucarray[(long)seven - (long)zero + 1];
377 typedef enum { nocollap, tocollap, undefined } collapstates;
379 typedef enum { bottom, nonbottom, hslength, tip, iter, length,
380 hsnolength, treewt, unittrwt } initops;
383 typedef double **transmatrix;
384 typedef transmatrix *transptr; /* transptr used in restml */
386 typedef long sitearray[3];
387 typedef sitearray *seqptr; /* seqptr used in protpars */
389 typedef struct node {
390 struct node *next, *back;
392 long naymlength, tipsabove, index;
393 double times_in_tree; /* Previously known as cons_index */
394 double xcoord, ycoord;
395 long long_xcoord, long_ycoord; /* for use in cons. */
396 double oldlen, length, r, theta, oldtheta, width, depth,
397 tipdist, lefttheta, righttheta;
398 group_type *nodeset; /* used by accumulate -plc */
399 long ymin, ymax; /* used by printree -plc */
400 boolean haslength; /* haslength used in dnamlk */
401 boolean iter; /* iter used in dnaml, fitch & restml */
402 boolean initialized; /* initialized used in dnamlk & restml */
403 long branchnum; /* branchnum used in restml */
404 phenotype x; /* x used in dnaml, dnamlk, dnadist */
405 phenotype2 x2; /* x2 used in restml */
406 phenotype3 view; /* contml etc */
407 pphenotype protx; /* protx used in proml */
408 aas *seq; /* the sequence used in protpars */
409 seqptr siteset; /* temporary storage for aa's used in protpars*/
410 double v, deltav, ssq; /* ssq used only in contrast */
411 double bigv; /* bigv used in contml */
412 double tyme, oldtyme; /* used in dnamlk */
413 double t; /* time in kitsch */
414 boolean sametime; /* bookkeeps scrunched nodes in kitsch */
415 double weight; /* weight of node used by scrunch in kitsch */
416 boolean processed; /* used by evaluate in kitsch */
417 boolean deleted; /* true if node is deleted (retree) */
418 boolean hasname; /* true if tip has a name (retree) */
419 double beyond; /* distance beyond this node to most distant tip */
421 boolean deadend; /* true if no undeleted nodes beyond this node */
423 boolean onebranch; /* true if there is one undeleted node beyond */
424 /* this node (retree) */
425 struct node *onebranchnode;
426 /* if there is, a pointer to that node (retree)*/
427 double onebranchlength; /* if there is, the distance from here to there*/
429 boolean onebranchhaslength; /* true if there is a valid combined length*/
430 /* from here to there (retree) */
431 collapstates collapse; /* used in dnapars & dnacomp */
433 boolean bottom; /* used in dnapars & dnacomp, disc char */
434 boolean visited; /* used in dnapars & dnacomp disc char */
435 baseptr base; /* the sequence in dnapars/comp/penny */
436 discbaseptr discbase; /* the sequence in pars */
437 baseptr2 base2; /* the sequence in dnamove */
438 baseptr oldbase; /* record previous sequence */
439 discbaseptr olddiscbase; /* record previous sequence */
440 long numdesc; /* number of immediate descendants */
441 nucarray *numnuc; /* bookkeeps number of nucleotides */
442 discnucarray *discnumnuc; /* bookkeeps number of nucleotides */
443 steptr numsteps; /* bookkeeps steps */
444 steptr oldnumsteps; /* record previous steps */
445 double sumsteps; /* bookkeeps sum of steps */
446 nucarray cumlengths; /* bookkeeps cummulative minimum lengths */
447 discnucarray disccumlengths; /* bookkeeps cummulative minimum lengths */
448 nucarray numreconst; /* bookkeeps number of reconstructions */
449 discnucarray discnumreconst; /* bookkeeps number of reconstructions */
450 vector d, w; /* for distance matrix programs */
451 double dist; /* dist used in fitch */
452 bitptr stateone, statezero; /* discrete char programs */
453 long maxpos; /* maxpos used in Clique */
454 Char state; /* state used in Dnamove, Dolmove & Move */
455 double* underflows; /* used to record underflow */
458 typedef node **pointarray;
460 typedef struct tree {
463 transptr trans; /* restml */
464 long *freetrans; /* restml */
465 long transindex; /* restml */
470 typedef void (*initptr)(node **, node **, node *, long, long,
471 long *, long *, initops, pointarray,
472 pointarray, Char *, Char *, FILE *);
475 /* function prototypes */
476 void scan_eoln(FILE *);
477 boolean eoff(FILE *);
478 boolean eoln(FILE *);
479 int filexists(char *);
480 const char* get_command_name (const char *);
481 void getstryng(char *);
482 void openfile(FILE **,const char *,const char *,const char *,const char *,
484 void cleerhome(void);
485 void loopcount(long *, long);
486 double randum(longer);
487 void randumize(longer, long *);
488 double normrand(longer);
489 long readlong(const char *);
491 void uppercase(Char *);
492 void initseed(long *, long *, longer);
493 void initjumble(long *, long *, longer, long *);
494 void initoutgroup(long *, long);
495 void initthreshold(double *);
496 void initcatn(long *);
497 void initcategs(long, double *);
498 void initprobcat(long, double *, double *);
499 double logfac (long);
500 double halfroot(double (*func)(long , double), long, double, double);
501 double hermite(long, double);
502 void initlaguerrecat(long, double, double *, double *);
503 void root_hermite(long, double *);
504 void hermite_weight(long, double *, double *);
505 void inithermitcat(long, double, double *, double *);
506 void lgr(long, double, raterootarray);
507 double glaguerre(long, double, double);
508 void initgammacat(long, double, double *, double *);
509 void inithowmany(long *, long);
510 void inithowoften(long *);
512 void initlambda(double *);
513 void initfreqs(double *, double *, double *, double *);
514 void initratio(double *);
515 void initpower(double *);
516 void initdatasets(long *);
517 void justweights(long *);
518 void initterminal(boolean *, boolean *);
519 void initnumlines(long *);
520 void initbestrees(bestelm *, long, boolean);
521 void newline(FILE *, long, long, long);
523 void inputnumbers(long *, long *, long *, long);
524 void inputnumbersold(long *, long *, long *, long);
525 void inputnumbers2(long *, long *, long n);
526 void inputnumbers3(long *, long *);
527 void samenumsp(long *, long);
528 void samenumsp2(long);
529 void readoptions(long *, const char *);
530 void matchoptions(Char *, const char *);
531 void inputweights(long, steptr, boolean *);
532 void inputweightsold(long, steptr, boolean *);
533 void inputweights2(long, long, long *, steptr, boolean *, const char *);
534 void printweights(FILE *, long, long, steptr, const char *);
536 void inputcategs(long, long, steptr, long, const char *);
537 void printcategs(FILE *, long, steptr, const char *);
538 void inputfactors(long, Char *, boolean *);
539 void inputfactorsnew(long, Char *, boolean *);
540 void printfactors(FILE *, long, Char *, const char *);
541 void headings(long, const char *, const char *);
543 void findtree(boolean *,long *,long,long *,bestelm *);
544 void addtree(long,long *,boolean,long *,bestelm *);
545 long findunrearranged(bestelm *, long, boolean);
546 boolean torearrange(bestelm *, long);
548 void reducebestrees(bestelm *, long *);
549 void shellsort(double *, long *, long);
550 void getch(Char *, long *, FILE *);
551 void getch2(Char *, long *);
552 void findch(Char, Char *, long);
553 void findch2(Char, long *, long *, Char *);
554 void findch3(Char, Char *, long, long);
555 void processlength(double *,double *,Char *,boolean *,FILE *,long *);
556 void writename(long, long, long *);
559 void odd_malloc(long);
561 void gnu(node **, node **);
562 void chuck(node **, node *);
563 void zeronumnuc(node *, long);
564 void zerodiscnumnuc(node *, long);
565 void allocnontip(node *, long *, long);
566 void allocdiscnontip(node *, long *, unsigned char *, long );
567 void allocnode(node **, long *, long);
568 void allocdiscnode(node **, long *, unsigned char *, long );
569 void gnutreenode(node **, node **, long, long, long *);
570 void gnudisctreenode(node **, node **, long , long, long *,
573 void chucktreenode(node **, node *);
574 void setupnode(node *, long);
575 long count_sibs (node *);
576 void inittrav (node *);
577 void commentskipper(FILE ***, long *);
578 long countcomma(FILE **, long *);
579 long countsemic(FILE **);
580 void hookup(node *, node *);
581 void link_trees(long, long , long, pointarray);
582 void allocate_nodep(pointarray *, FILE **, long *);
584 void malloc_pheno(node *, long, long);
585 void malloc_ppheno(node *, long, long);
586 long take_name_from_tree (Char *, Char *, FILE *);
587 void match_names_to_data (Char *, pointarray, node **, long);
588 void addelement(node **, node *, Char *, long *, FILE *, pointarray,
589 boolean *, boolean *, pointarray, long *, long *, boolean *,
590 node **, initptr,boolean,long);
591 void treeread (FILE *, node **, pointarray, boolean *, boolean *,
592 pointarray, long *, boolean *, node **, initptr,boolean,long);
593 void addelement2(node *, Char *, long *, FILE *, pointarray, boolean,
594 double *, boolean *, long *, long *, long, boolean *,boolean,
596 void treeread2 (FILE *, node **, pointarray, boolean, double *,
597 boolean *, boolean *, long *,boolean,long);
599 void countup(long *loopcount, long maxcount);
600 char gettc(FILE* file);
601 void unroot_r(node* p,node ** nodep, long nonodes);
602 void unroot(tree* t,long nonodes);
603 void unroot_here(node* root, node** nodep, long nonodes);
604 void clear_connections(tree *t, long nonodes);
605 void init(int argc, char** argv);
607 #endif /* _PHYLIP_H_ */