initial commit
[jalview.git] / forester / archive / RIO / others / phylip_mod / src / phylip.h
1 /*Modified by Christian Zmasek. Use at your own risk.*/
2
3
4 #ifndef _PHYLIP_H_
5 #define _PHYLIP_H_
6
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. */
12
13 #define VERSION "3.65"
14
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.
20
21    We will try to figure out machine type
22    based on defines in stdio, and compiler-defined things as well.: */
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #ifdef WIN32
27 #include <windows.h>
28
29 void phyClearScreen(void);
30 void phySaveConsoleAttributes(void);
31 void phySetConsoleAttributes(void);
32 void phyRestoreConsoleAttributes(void);
33 void phyFillScreenColor(void);
34
35 #endif
36
37 #ifdef  GNUDOS
38 #define DJGPP
39 #define DOS
40 #endif
41
42 #ifdef THINK_C
43 #define MAC
44 #endif
45 #ifdef __MWERKS__
46 #ifndef WIN32
47 #define MAC
48 #endif
49 #endif
50
51 #ifdef __CMS_OPEN
52 #define CMS
53 #define EBCDIC true
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"
66 #else
67 #define EBCDIC false
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"
80 #endif
81
82 #ifdef L_ctermid            /* try and detect for sysV or V7. */
83 #define SYSTEM_FIVE
84 #endif
85
86 #ifdef sequent
87 #define SYSTEM_FIVE
88 #endif
89
90 #ifndef SYSTEM_FIVE
91 #include <stdlib.h>
92 # if defined(_STDLIB_H_) || defined(_H_STDLIB) || defined(H_SCCSID) || defined(unix)
93 # define UNIX
94 # define MACHINE_TYPE "BSD Unix C"
95 # endif
96 #endif
97
98
99 #ifdef __STDIO_LOADED
100 #define VMS
101 #define MACHINE_TYPE "VAX/VMS C"
102 #endif
103
104 #ifdef __WATCOMC__
105 #define QUICKC
106 #define WATCOM
107 #define DOS
108 #include "graph.h"
109 #endif
110 /* watcom-c has graphics library calls that are almost identical to    *
111  * quick-c, so the "QUICKC" symbol name stays.                         */
112
113
114 #ifdef _QC
115 #define MACHINE_TYPE "MS-DOS / Quick C"
116 #define QUICKC
117 #include "graph.h"
118 #define DOS
119 #endif
120
121 #ifdef _DOS_MODE
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              */
125 #endif
126
127 #ifdef __MSDOS__      /* TURBO c compiler, ONLY (no other DOS C compilers) */
128 #define DOS
129 #define TURBOC
130 #include <stdlib.h>
131 #include <graphics.h>
132 #endif
133
134 #ifdef DJGPP          /* DJ Delorie's original gnu  C/C++ port */
135 #include <graphics.h>
136 #endif
137
138 #ifndef MACHINE_TYPE
139 #define MACHINE_TYPE "ANSI C"
140 #endif
141
142 #ifdef DOS
143 #define MALLOCRETURN void 
144 #else
145 #define MALLOCRETURN void
146 #endif
147 #ifdef VMS
148 #define signed /* signed doesn't exist in VMS */
149 #endif
150
151 /* default screen types */
152 /*  if on a DOS but not a Windows system can use IBM PC screen controls */
153 #ifdef DOS
154 #ifndef WIN32
155 #define IBMCRT true
156 #define ANSICRT false
157 #endif
158 #endif
159 /*  if on a Mac cannot use screen controls */
160 #ifdef MAC
161 #define IBMCRT false 
162 #define ANSICRT false
163 #endif
164 /*  if on a Windows system can use IBM PC screen controls */
165 #ifdef WIN32
166 #define IBMCRT true 
167 #define ANSICRT false
168 #endif
169 /* otherwise, let's assume we are on a Linux or Unix system
170    with ANSI terminal controls */
171 #ifndef MAC
172 #ifndef DOS
173 #ifndef WIN32
174 #define IBMCRT false 
175 #define ANSICRT true
176 #endif
177 #endif
178 #endif
179
180 #ifdef DJGPP
181 #undef MALLOCRETURN
182 #define MALLOCRETURN void
183 #endif
184
185
186 /* includes: */
187 #ifdef UNIX
188 #include <strings.h>
189 #else
190 #include <string.h>
191 #endif
192
193 #include <math.h>
194 #include <ctype.h>
195
196 #ifdef MAC
197 #ifdef DRAW
198 #include "interface.h"
199 #else
200 #include "macface.h"
201 #endif
202 #define getch gettch
203 #endif
204
205 /* directory delimiters */
206 #ifdef MAC
207 #define DELIMITER ':'
208 #else 
209 #ifdef WIN32
210 #define DELIMITER '\\'
211 #else 
212 #define DELIMITER '/'
213 #endif
214 #endif
215
216
217 #define FClose(file) if (file) fclose(file) ; file=NULL
218 #define Malloc(x) mymalloc((long)x)
219
220 typedef void *Anyptr;
221 #define Signed     signed
222 #define Const     const
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 */
227
228 typedef unsigned char boolean;
229
230 #define true    1
231 #define false   0
232 #define SETBITS 31
233
234 MALLOCRETURN    *mymalloc(long);
235
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 */
241 #define point           "."
242 #define pointe          '.'
243 #define down            2
244 #define MAXSHIMOTREES 100
245
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
254
255 #define NLRSAVES 5 /* number of views that need to be saved during local  *
256                     * rearrangement                                       */
257
258 typedef long *steptr;
259 typedef long longer[6];
260 typedef char naym[MAXNCH];
261 typedef long *bitptr;
262 typedef double raterootarray[maxcategs2][maxcategs2];
263
264 typedef struct bestelm {
265   long *btree;
266   boolean gloreange;
267   boolean locreange;
268   boolean collapse;
269 } bestelm;
270
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 */
276
277
278 #define ebcdic          EBCDIC
279
280 typedef Char plotstring[MAXNCH];
281
282 /* Approx. 1GB, used to test for memory request errors */
283 #define TOO_MUCH_MEMORY 1000000000
284
285
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 */
289 #include <limits.h>
290
291 /* minimum double we feel safe with, anything less will be considered 
292    underflow */
293 #define MIN_DOUBLE 10e-100
294
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
297    case. */
298 #define MAX_32BITS        2147483647
299 #define MAX_32BITS_PLUS  +2147483647
300
301 /* If ints are 4 bytes, use them */
302 #if INT_MAX == MAX_32BITS
303 typedef int  group_type;
304
305 #else  
306      #if INT_MAX == MAX_32BITS_PLUS
307      typedef int  group_type;
308
309      #else 
310           /* Else, if longs are 4 bytes, use them */
311           #if LONG_MAX == MAX_32BITS
312           typedef long group_type;
313
314           #else 
315                #if LONG_MAX == MAX_32BITS_PLUS
316                 typedef long group_type;
317
318                /* Default to longs */
319                #else
320                     typedef long group_type;
321                #endif
322
323           #endif
324      #endif
325 #endif
326
327 /* for many programs */
328
329 #define maxuser         1000  /* maximum number of user-defined trees    */
330
331 typedef Char **sequence;
332
333 typedef enum {
334   A, C, G, T, O
335 } bases;
336
337 typedef enum {
338   alanine, arginine, asparagine, aspartic, cysteine, 
339   glutamine, glutamic, glycine, histidine, isoleucine,
340   leucine, lysine, methionine, phenylalanine, proline,
341   serine, threonine, tryptophan, tyrosine, valine
342 } acids;
343
344 /* for Pars */
345
346 typedef enum {
347   zero = 0, one, two, three, four, five, six, seven
348 } discbases;
349
350 /* for Protpars */
351
352 typedef enum {
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
355 } aas;
356
357 typedef double sitelike[(long)T - (long)A + 1];   /* used in dnaml, dnadist */
358 typedef double psitelike[(long)valine - (long)alanine + 1];
359                              /* used in proml                                    */        
360      
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 */
371
372 typedef double *vector;                     /* used in distance programs    */
373
374 typedef long nucarray[(long)O - (long)A + 1];
375 typedef long discnucarray[(long)seven - (long)zero + 1];
376
377 typedef enum { nocollap, tocollap, undefined } collapstates;
378
379 typedef enum { bottom, nonbottom, hslength, tip, iter, length,
380                  hsnolength, treewt, unittrwt } initops;
381
382
383 typedef double **transmatrix;
384 typedef transmatrix *transptr;                /* transptr used in restml */
385
386 typedef long sitearray[3];
387 typedef sitearray *seqptr;                    /* seqptr used in protpars */
388
389 typedef struct node {
390   struct node *next, *back;
391   plotstring nayme;
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 */
420                             /* (retree) */
421   boolean deadend;          /* true if no undeleted nodes beyond this node */
422                             /* (retree) */
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*/
428                                 /* (retree)                                */
429   boolean onebranchhaslength;   /* true if there is a valid combined length*/
430                                  /* from here to there (retree)            */
431   collapstates collapse;         /* used in dnapars & dnacomp              */
432   boolean tip;
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               */
456 } node;
457
458 typedef node **pointarray;
459
460 typedef struct tree {
461   pointarray nodep;
462   double likelihood;
463   transptr trans;                 /* restml */
464   long *freetrans;                /* restml */
465   long transindex;                /* restml */
466   node *start;                    
467   node *root;                    
468 } tree;
469
470 typedef void (*initptr)(node **, node **, node *, long, long,
471                          long *, long *, initops, pointarray,
472                          pointarray, Char *, Char *, FILE *);
473
474 #ifndef OLDC
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 *,
483                 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 *);
490
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 *);
511
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);
522
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 *);
535
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 *);
542 void   initname(long);
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);
547
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 *);
557 void   memerror(void);
558
559 void   odd_malloc(long);
560
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 *,
571                 unsigned char *);
572
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  *);
583   
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,
595                 long);
596 void   treeread2 (FILE *, node **, pointarray, boolean, double *,
597                 boolean *, boolean *, long *,boolean,long);
598 void   exxit (int);
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);
606 #endif /* OLDC */
607 #endif /* _PHYLIP_H_ */