source: tags/initial/GDE/PHYLIP/dnamlk.c

Last change on this file was 2, checked in by oldcode, 24 years ago

Initial revision

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 31.2 KB
Line 
1#include "phylip.h"
2
3/* version 3.56c. (c) Copyright 1986-1993 by the University of Washington
4  and by Joseph Felsenstein.  Written by Joseph Felsenstein.  Permission is
5  granted to copy and use this program provided no fee is charged for it
6  and provided that this copyright notice is not removed. */
7
8#define maxcategs       9    /* maximum number of site types                 */
9#define smoothings      2    /* number of passes through smoothing algorithm */
10#define iterations      10   /* number of iterates for each branch           */
11#define nmlngth         10   /* number of characters max. in species name    */
12
13#define epsilon         0.0001   /* used in makenewv, getthree, update */
14
15#define point           '.'
16
17#define ibmpc0          false
18#define ansi0           true
19#define vt520           false
20
21
22typedef enum {
23  A, C, G, T
24} base;
25typedef double sitelike[(short)T - (short)A + 1];
26typedef sitelike **phenotype;
27typedef double *contribarr;
28typedef char **sequence;
29
30
31typedef short longer[6];
32
33typedef struct node {
34  struct node *next, *back;
35  phenotype x;
36  double tyme, v;
37  short number, xcoord, ycoord, ymin, ymax;
38  boolean tip, haslength, initialized;
39} node;
40
41
42typedef struct tree {
43  node **nodep;
44  double likelihood;
45  node *root;
46} tree;
47
48
49extern short      categ,spp,endsite,sites,nonodes,weightsum,datasets,
50                  njumble,jumb;
51extern double     rate[maxcategs];
52extern double     xi,xv,freqa,freqc,freqg,freqt,freqr,freqy,freqar,freqcy,
53                  freqgr,freqty,lambda,lambda1,fracchange;
54extern boolean    usertree,globle,jumble,lengths,trout,weights,ctgry,auto_,
55                  printdata,progress,treeprint;
56extern tree       curtree,bestree,bestree2;
57extern contribarr probcat;
58extern            double **contribution;
59extern short       *alias,*ally,*location,*aliasweight;
60extern FILE       *infile,*outfile,*treefile;
61extern            Char **naym;
62extern            short *enterorder;
63extern            longer seed;
64
65
66
67/* end inclusion of file dnamlk.h                                         */
68
69
70FILE *infile, *outfile, *treefile;
71short spp, nonodes, sites, weightsum, endsite, categs, inseed,
72            datasets, ith, i, j, l, jumb, njumble;
73/* spp = number of species
74  nonodes = number of nodes in tree
75  sites = number of sites in actual sequences
76  endsite = number of patterns in data
77  numtrees = number of user-defined trees */
78boolean freqsfrom, globle, jumble, lengths, trout, usertree,
79               weights, ctgry, ttr, auto_, printdata, progress, treeprint,
80               mulsets, firstset, interleaved, ibmpc, vt52, ansi;
81tree curtree, bestree, bestree2;
82Char **naym;   /* names of species */
83double xi, xv, ttratio, ttratio0, freqa, freqc, freqg, freqt, freqr,
84              freqy, freqar, freqcy, freqgr, freqty, fracchange, sumrates,
85              lambda,lambda1;
86longer seed;
87short *enterorder;
88sequence y;
89short *weight,*alias,*aliasweight,*ally,*location;
90double rate[maxcategs];
91contribarr probcat;
92double   **contribution;
93
94openfile(fp,filename,mode,application,perm)
95FILE **fp;
96char *filename;
97char *mode;
98char *application;
99char *perm;
100{
101  FILE *of;
102  char file[100];
103  strcpy(file,filename);
104  while (1){
105    of = fopen(file,mode);
106    if (of)
107      break;
108    else {
109      switch (*mode){
110      case 'r':
111        printf("%s:  can't read %s\n",application,file);
112        file[0]='\0';
113        while (file[0] =='\0'){
114          printf("Please enter a new filename>");
115          gets(file);}
116        break;
117      case 'w':
118        printf("%s: can't write %s\n",application,file);
119        file[0] = '\0';
120        while (file[0] =='\0'){
121          printf("Please enter a new filename>");
122          gets(file);}
123        break;
124      }
125    }
126  }
127  *fp=of;
128  if (perm != NULL)
129    strcpy(perm,file);
130}
131
132
133void setuptree(a)
134tree *a;
135{
136  short i;
137  node *p;
138
139  for (i = 0; i < spp; i++) {
140    a->nodep[i]->number = i + 1;
141    a->nodep[i]->tip = true;
142    a->nodep[i]->initialized = false;
143    a->nodep[i]->tyme = 0.0;
144    a->nodep[i]->next = NULL;
145    a->nodep[i]->back = NULL;
146  }
147  for (i = spp + 1; i <= nonodes; i++) {
148    p = a->nodep[i - 1];
149    for (j = 1; j <= 3; j++) {
150      p->tip = false;
151      p->initialized = false;
152      p->number = i;
153      p->tyme = 0.0;
154      p->back = NULL;
155      p = p->next;
156    }
157  }
158  a->likelihood = -999999.0;
159  a->root = NULL;
160}  /* setuptree */
161
162
163boolean letter(ch)
164Char ch;
165{
166  /* tests whether ch is a letter
167   -- works for both ASCII and EBCDIC codes */
168  return ((ch >= 'A' && ch <= 'I') || (ch >= 'J' && ch <= 'R') ||
169          (ch >= 'S' && ch <= 'Z') || (ch >= 'a' && ch <= 'i') ||
170          (ch >= 'j' && ch <= 'r') || (ch >= 's' && ch <= 'z'));
171}  /* letter */
172
173
174void uppercase(ch)
175Char *ch;
176{
177  (*ch) = isupper(*ch) ? (*ch) : toupper(*ch);
178}  /* uppercase */
179
180
181void getoptions()
182{
183  /* interactively set options */
184  short i, j, inseed0,scanned;
185  Char ch;
186  char line[128];
187   char rest[128];
188  boolean done, done1, done2;
189  double probsum;
190
191  fprintf(outfile, "\nNucleic acid sequence\n");
192  fprintf(outfile, "   Maximum Likelihood method with molecular ");
193  fprintf(outfile, "clock, version %s\n\n",VERSION);
194  putchar('\n');
195  auto_ = false;
196  ctgry = false;
197  categs = 1;
198  rate[0] = 1.0;
199  freqsfrom = true;
200  globle = false;
201  jumble = false;
202  njumble = 1;
203  lambda = 1.0;
204  lambda1 = 0.0;
205  lengths = false;
206  trout = true;
207  ttratio = 2.0;
208  ttr = false;
209  usertree = false;
210  weights = false;
211  printdata = false;
212  progress = true;
213  treeprint = true;
214  interleaved = true;
215  probcat = (double *)Malloc(sizeof(double));
216  probcat[0] = 1.0;
217  do {
218    if (ansi)
219      printf("\033[2J\033[H");
220    else if (vt52)
221      printf("\033E\033H");
222    else
223      putchar('\n');
224    printf("\nNucleic acid sequence\n");
225    printf("   Maximum Likelihood method with molecular clock, version %s\n\n",
226           VERSION);
227    printf("Settings for this run:\n");
228    printf("  U                 Search for best tree?");
229    if (usertree)
230      printf("  No, use user trees in input file\n");
231    else
232      printf("  Yes\n");
233    if (usertree) {
234      printf("  L           Use lengths from user tree?");
235      if (lengths)
236        printf("  Yes\n");
237      else
238        printf("  No\n");
239    }
240    printf("  T        Transition/transversion ratio:");
241    if (!ttr)
242      printf("  2.0\n");
243    else
244      printf("  %8.4f\n", ttratio);
245    printf("  F       Use empirical base frequencies?");
246    if (freqsfrom)
247      printf("  Yes\n");
248    else
249      printf("  No\n");
250    printf("  C   One category of substitution rates?");
251    if (!ctgry)
252      printf("  Yes\n");
253    else {
254      printf("  %hd categories\n", categs);
255      printf("  R   Rates at adjacent sites correlated?");
256      if (!auto_)
257        printf("  No, they are independent\n");
258      else
259        printf("  Yes, mean block length =%6.1f\n", 1.0 / lambda);
260    }
261    if (!usertree) {
262      printf("  G                Global rearrangements?");
263      if (globle)
264        printf("  Yes\n");
265      else
266        printf("  No\n");
267      printf("  J   Randomize input order of sequences?");
268      if (jumble)
269        printf("  Yes (seed =%8hd,%3hd times)\n", inseed0, njumble);
270      else
271        printf("  No. Use input order\n");
272    }
273    printf("  M           Analyze multiple data sets?");
274    if (mulsets)
275      printf("  Yes, %2hd sets\n", datasets);
276    else
277      printf("  No\n");
278    printf("  I          Input sequences interleaved?");
279    if (interleaved)
280      printf("  Yes\n");
281    else
282      printf("  No, sequential\n");
283    printf("  0   Terminal type (IBM PC, VT52, ANSI)?");
284    if (ibmpc)
285      printf("  IBM PC\n");
286    if (ansi)
287      printf("  ANSI\n");
288    if (vt52)
289      printf("  VT52\n");
290    if (!(ibmpc || vt52 || ansi))
291      printf("  (none)\n");
292    printf("  1    Print out the data at start of run");
293    if (printdata)
294      printf("  Yes\n");
295    else
296      printf("  No\n");
297    printf("  2  Print indications of progress of run");
298    if (progress)
299      printf("  Yes\n");
300    else
301      printf("  No\n");
302    printf("  3                        Print out tree");
303    if (treeprint)
304      printf("  Yes\n");
305    else
306      printf("  No\n");
307    printf("  4       Write out trees onto tree file?");
308    if (trout)
309      printf("  Yes\n");
310    else
311      printf("  No\n");
312    printf("\nAre these settings correct? (type Y or the letter for one to change)\n");
313    scanf("%c%*[^\n]", &ch);
314    getchar();
315    if (ch == '\n')
316      ch = ' ';
317    uppercase(&ch);
318    done = (ch == 'Y');
319    if (!done) {
320      uppercase(&ch);
321      if (ch == 'J' || ch == 'U' || ch == 'C' || ch == 'R' || ch == 'M' ||
322          ch == 'F' || ch == 'G' || ch == 'L' || ch == 'T' || ch == 'I' ||
323          ch == '1' || ch == '2' || ch == '3' || ch == '4' || ch == '0') {
324        switch (ch) {
325
326        case 'C':
327          ctgry = !ctgry;
328          if (ctgry) {
329            do {
330              printf("Number of categories (1-%ld)?\n",(long)maxcategs);
331              gets(line);
332              categs = (short)atoi(line);
333            } while (categs < 1 || categs > maxcategs);
334            if (probcat){
335              free(probcat);
336              free(rate);
337            }
338            probcat = (double *)Malloc(categs * sizeof(double));
339            for (;;){
340              printf("Rate for each category? (use a space to separate)\n");
341              gets(line);
342              done1 = true;
343              for (i = 0; i < categs; i++){
344                scanned = sscanf(line,"%lf %[^\n]", &rate[i],rest);
345                if ((scanned != 2 && i < (categs - 1)) ||
346                    (scanned != 1 && i == (categs - 1))){
347                  printf("Please enter exactly %hd values.\n",categs);
348                  done1 = false;
349                  break;
350                }
351                strcpy(line,rest);
352              }
353              if (done1)
354                break;
355            }
356            probsum = 0.0;
357            for (;;){
358              printf("Probability for each category? (use a space to separate)\n");
359              gets(line);
360              done1 = true;
361              probsum = 0.0;
362              for (i = 0; i < categs; i++){
363                scanned = sscanf(line,"%lf %[^\n]", &probcat[i],rest);
364                if ((scanned != 2 && i < (categs - 1)) ||
365                    (scanned != 1 && i == (categs - 1))){
366                  printf("Please enter exactly %hd values.\n",categs);
367                  done1 = false;
368                  break;
369                }
370                strcpy(line,rest);
371                probsum += probcat[i];
372              }
373              if (!done1)
374                continue;
375              if (fabs(1.0 - probsum) > 0.001) {
376                done1  = false;
377                printf("Probabilities must add up to 1.0, plus or minus 0.001.\n");
378              }
379              else
380                done1 = true;
381              if (done1)
382                break;
383            }
384          }
385          if (!ctgry)
386            auto_ = false;
387          break;
388         
389        case 'R':
390          auto_ = !auto_;
391          if (auto_) {
392            do {
393              printf(
394                "Mean block length of sites having the same rate (greater than 1)?\n");
395              scanf("%lf%*[^\n]", &lambda);
396              getchar();
397            } while (lambda < 1.0);
398            lambda = 1.0 / lambda;
399            lambda1 = 1.0 - lambda;
400          }
401          break;
402
403        case 'F':
404          freqsfrom = !freqsfrom;
405          if (!freqsfrom) {
406            printf("Base frequencies for A, C, G, T/U (use blanks to separate)?\n");
407            scanf("%lf%lf%lf%lf%*[^\n]", &freqa, &freqc, &freqg, &freqt);
408            getchar();
409          }
410          break;
411
412        case 'G':
413          globle = !globle;
414          break;
415
416        case 'J':
417          jumble = !jumble;
418          if (jumble) {
419            printf("Random number seed (must be odd)?\n");
420            scanf("%hd%*[^\n]", &inseed);
421            getchar();
422            inseed0 = inseed;
423            for (i = 0; i <= 5; i++)
424              seed[i] = 0;
425            i = 0;
426            do {
427              seed[i] = inseed & 63;
428              inseed /= 64;
429              i++;
430            } while (inseed != 0);
431            printf("Number of times to jumble?\n");
432            scanf("%hd%*[^\n]", &njumble);
433            getchar();
434          }
435          break;
436
437        case 'L':
438          lengths = !lengths;
439          break;
440
441        case 'T':
442          ttr = !ttr;
443          if (ttr) {
444            do {
445              printf("Transition/transversion ratio?\n");
446              scanf("%lf%*[^\n]", &ttratio);
447              getchar();
448            } while (ttratio < 0.0);
449          }
450          break;
451
452        case 'U':
453          usertree = !usertree;
454          break;
455
456        case 'M':
457          mulsets = !mulsets;
458          if (mulsets) {
459            done1 = false;
460            do {
461              printf("How many data sets?\n");
462              scanf("%hd%*[^\n]", &datasets);
463              getchar();
464              done1 = (datasets >= 1);
465              if (!done1)
466                printf("BAD DATA SETS NUMBER:  it must be greater than 1\n");
467            } while (done1 != true);
468          }
469          break;
470
471        case 'I':
472          interleaved = !interleaved;
473          break;
474
475        case '0':
476          if (ibmpc) {
477            ibmpc = false;
478            vt52 = true;
479          } else {
480            if (vt52) {
481              vt52 = false;
482              ansi = true;
483            } else if (ansi)
484              ansi = false;
485            else
486              ibmpc = true;
487          }
488          break;
489
490        case '1':
491          printdata = !printdata;
492          break;
493
494        case '2':
495          progress = !progress;
496          break;
497
498        case '3':
499          treeprint = !treeprint;
500          break;
501
502        case '4':
503          trout = !trout;
504          break;
505        }
506      } else
507        printf("Not a possible option!\n");
508    }
509  } while (!done);
510}  /* getoptions */
511
512void getnums()
513{
514  /* input number of species, number of sites */
515  fprintf(outfile, "\n\n");
516  fscanf(infile, "%hd%hd", &spp, &sites);
517  fprintf(outfile, "%4hd Species, %4hd Sites\n", spp, sites);
518  nonodes = spp * 2 - 1;
519}  /* getnums */
520
521
522void doinit()
523{
524  /* initializes variables */
525  short i;
526  node *p, *q;
527
528  getnums();
529  getoptions();
530  y     = (Char **)Malloc(spp*sizeof(Char *));
531  naym  = (char **)Malloc(spp*sizeof(char *));
532  curtree.nodep = (node **)Malloc(nonodes * sizeof(node *));
533  for (i=0;i<spp;i++)
534    naym[i] = (char *)Malloc(nmlngth+1);
535  for (i = 0; i < spp; i++)
536    y[i] = (char *)Malloc(sites * sizeof(char));
537  for (i = 0; i < spp; i++)
538    curtree.nodep[i] = (node *)Malloc(sizeof(node));
539  for (i = spp; i < nonodes; i++) {
540    q = NULL;
541    for (j = 1; j <= 3; j++) {
542      p = (node *)Malloc(sizeof(node));
543      p->next = q;
544      q = p;
545    }
546    p->next->next->next = p;
547    curtree.nodep[i] = p;
548  }
549  if (usertree)
550    return;
551  bestree.nodep = (node **)Malloc(nonodes * sizeof(node *));
552  for (i = 0; i < spp; i++)
553    bestree.nodep[i] = (node *)Malloc(sizeof(node));
554  for (i = spp; i < nonodes; i++) {
555    q = NULL;
556    for (j = 1; j <= 3; j++) {
557      p = (node *)Malloc(sizeof(node));
558      p->next = q;
559      q = p;
560    }
561    p->next->next->next = p;
562    bestree.nodep[i] = p;
563  }
564  if (njumble <= 1)
565    return;
566  bestree2.nodep = (node **)Malloc(nonodes*sizeof(node *));
567  for (i = 0; i < (spp); i++)
568    bestree2.nodep[i] = (node *)Malloc(sizeof(node));
569  for (i = spp; i < (nonodes); i++) {
570    q = NULL;
571    for (j = 1; j <= 3; j++) {
572      p = (node *)Malloc(sizeof(node));
573      p->next = q;
574      q = p;
575    }
576    p->next->next->next = p;
577    bestree2.nodep[i] = p;
578  }
579}  /* doinit */
580
581void inputweights()
582{
583  /* input the character weights, 0 or 1 */
584  Char ch;
585  short i;
586
587  for (i = 1; i < nmlngth; i++) {
588    ch = getc(infile);
589    if (ch == '\n')
590      ch = ' ';
591  }
592  weightsum = 0;
593  for (i = 0; i < sites; i++) {
594    do {
595      if (eoln(infile)) {
596        fscanf(infile, "%*[^\n]");
597        getc(infile);
598      }
599      ch = getc(infile);
600      if (ch == '\n')
601        ch = ' ';
602    } while (ch == ' ');
603    weight[i] = 1;
604    if (ch == '0' || ch == '1')
605      weight[i] = ch - '0';
606    else {
607      printf("BAD WEIGHT CHARACTER: %c -- WEIGHTS IN DNAMLK MUST BE 0 OR 1\n",
608             ch);
609      exit(-1);
610    }
611    weightsum += weight[i];
612  }
613  fscanf(infile, "%*[^\n]");
614  getc(infile);
615  weights = true;
616}  /* inputweights */
617
618void printweights()
619{
620  /* print out the weights of sites */
621  short i, j;
622
623  fprintf(outfile, "\nSites are weighted as follows:\n");
624  for (i = 1; i <= sites; i++) {
625    if ((i - 1) % 60 == 0) {
626      putc('\n', outfile);
627      for (j = 1; j <= nmlngth + 3; j++)
628        putc(' ', outfile);
629    }
630    fprintf(outfile, "%hd", weight[i - 1]);
631    if (i % 10 == 0 && i % 60 != 0)
632      putc(' ', outfile);
633  }
634  fprintf(outfile, "\n\n");
635}  /* printweights */
636
637void inputoptions()
638{
639  Char ch;
640  short i, extranum, cursp, curst;
641
642  if (!firstset) {
643    if (eoln(infile)) {
644      fscanf(infile, "%*[^\n]");
645      getc(infile);
646    }
647    fscanf(infile, "%hd%hd", &cursp, &curst);
648    if (cursp != spp) {
649      printf("\nERROR: INCONSISTENT NUMBER OF SPECIES IN DATA SET %4hd\n",
650             ith);
651      exit(-1);
652    }
653    sites = curst;
654  }
655  for (i = 0; i < sites; i++)
656    weight[i] = 1;
657  weightsum = sites;
658  extranum = 0;
659  while (!(eoln(infile))) {
660    ch = getc(infile);
661    if (ch == '\n')
662      ch = ' ';
663    uppercase(&ch);
664    if (ch == 'W')
665      extranum++;
666    else if (ch != ' ') {
667      printf("BAD OPTION CHARACTER: %c\n", ch);
668      exit(-1);
669    }
670  }
671  fscanf(infile, "%*[^\n]");
672  getc(infile);
673  for (i = 1; i <= extranum; i++) {
674    ch = getc(infile);
675    if (ch == '\n')
676      ch = ' ';
677    uppercase(&ch);
678    if (ch == 'W')
679      inputweights();
680    else{
681      printf("ERROR: INCORRECT AUXILIARY OPTIONS LINE WHICH STARTS WITH %c\n",
682             ch);
683      exit(-1);}
684  }
685  if (categs > 1) {
686    fprintf(outfile, "\nSite category   Rate of change\n\n");
687    for (i = 1; i <= categs; i++)
688      fprintf(outfile, "%11hd%13.3f\n", i, rate[i - 1]);
689    putc('\n', outfile);
690  }
691  if (weights && printdata)
692    printweights();
693}  /* inputoptions */
694
695void getbasefreqs()
696{
697  double aa, bb;
698
699  putc('\n', outfile);
700  if (freqsfrom)
701    fprintf(outfile, "Empirical ");
702  fprintf(outfile, "Base Frequencies:\n\n");
703  fprintf(outfile, "   A    %10.5f\n", freqa);
704  fprintf(outfile, "   C    %10.5f\n", freqc);
705  fprintf(outfile, "   G    %10.5f\n", freqg);
706  fprintf(outfile, "  T(U)  %10.5f\n", freqt);
707  freqr = freqa + freqg;
708  freqy = freqc + freqt;
709  freqar = freqa / freqr;
710  freqcy = freqc / freqy;
711  freqgr = freqg / freqr;
712  freqty = freqt / freqy;
713  fprintf(outfile, "\nTransition/transversion ratio = %10.6f\n\n", ttratio);
714  aa = ttratio * freqr * freqy - freqa * freqg - freqc * freqt;
715  bb = freqa * freqgr + freqc * freqty;
716  xi = aa / (aa + bb);
717  xv = 1.0 - xi;
718  ttratio = xi / xv;
719  if (xi <= 0.0) {
720    printf("WARNING: This transition/transversion ratio\n");
721    printf("is impossible with these base frequencies!\n");
722    xi = 3.0 / 5;
723    xv = 2.0 / 5;
724    fprintf(outfile, " Transition/transversion parameter reset\n\n");
725  }
726  fprintf(outfile, "(Transition/transversion parameter = %10.6f)\n\n",
727          xi / xv);
728  fracchange = xi * (2.0 * freqa * freqgr + 2.0 * freqc * freqty) +
729      xv*(1.0 - freqa * freqa - freqc * freqc - freqg * freqg - freqt * freqt);
730}  /* getbasefreqs */
731
732void getdata()
733{
734  short i, j, k, l, basesread, basesnew;
735  Char ch;
736  boolean allread, done;
737
738  if (printdata)
739    putc('\n', outfile);
740  j = nmlngth + (sites + (sites - 1) / 10) / 2 - 5;
741  if (j < nmlngth - 1)
742    j = nmlngth - 1;
743  if (j > 37)
744    j = 37;
745  if (printdata) {
746    fprintf(outfile, "Name");
747    for (i = 1; i <= j; i++)
748      putc(' ', outfile);
749    fprintf(outfile, "Sequences\n");
750    fprintf(outfile, "----");
751    for (i = 1; i <= j; i++)
752      putc(' ', outfile);
753    fprintf(outfile, "---------\n\n");
754  }
755  setuptree(&curtree);
756  basesread = 0;
757  allread = false;
758  while (!(allread)) {
759    allread = true;
760    if (eoln(infile)) {
761      fscanf(infile, "%*[^\n]");
762      getc(infile);
763    }
764    i = 1;
765    while (i <= spp) {
766      if ((interleaved && basesread == 0) || !interleaved) {
767        for (j = 0; j < nmlngth; j++) {
768          if (eof(infile) || eoln(infile)){
769            printf("ERROR: END-OF-LINE OR END-OF-FILE");
770            printf(" IN THE MIDDLE OF A SPECIES NAME\n");
771            exit(-1);
772          }
773          naym[i - 1][j] = getc(infile);
774        }
775      }
776      if (interleaved)
777        j = basesread;
778      else
779        j = 0;
780      done = false;
781      while (!done & !eof(infile)) {
782        if (interleaved)
783          done = true;
784        while (((j < sites) & (!(eoln(infile) | eof(infile))))) {
785          ch = getc(infile);
786          if (ch == '\n')
787            ch = ' ';
788          if (ch == ' ' || (ch >= '0' && ch <= '9'))
789            continue;
790          uppercase(&ch);
791          if (ch != 'A' && ch != 'B' && ch != 'C' && ch != 'D' && ch != 'G' &&
792              ch != 'H' && ch != 'K' && ch != 'M' && ch != 'N' && ch != 'R' &&
793              ch != 'S' && ch != 'T' && ch != 'U' && ch != 'V' && ch != 'W' &&
794              ch != 'X' && ch != 'Y' && ch != '?' && ch != 'O' && ch != '-' &&
795              ch != '.') {
796            printf("ERROR: BAD BASE:%c AT POSITION%5ld OF SPECIES %3ld\n",
797                   ch, j, i);
798            exit(-1);
799          }
800          j++;
801          if (ch == '.')
802            ch = y[0][j - 1];
803          y[i - 1][j - 1] = ch;
804        }
805        if (interleaved)
806          continue;
807        if (j < sites) {
808          fscanf(infile, "%*[^\n]");
809          getc(infile);
810        } else if (j == sites)
811          done = true;
812      }
813      if (interleaved && i == 1)
814        basesnew = j;
815      fscanf(infile, "%*[^\n]");
816      getc(infile);
817      if ((interleaved && j != basesnew) || ((!interleaved) && j != sites)){
818        printf("ERROR: SEQUENCES OUT OF ALIGNMENT\n");
819        exit(-1);}
820      i++;
821    }
822    if (interleaved) {
823      basesread = basesnew;
824      allread = (basesread == sites);
825    } else
826      allread = (i > spp);
827  }
828  if (printdata) {
829    for (i = 1; i <= ( (sites - 1) / 60 + 1);++i){
830      for (j = 1; j <= spp; j++) {
831        for (k = 0; k < nmlngth; k++)
832          putc(naym[j - 1][k], outfile);
833        fprintf(outfile, "   ");
834        l = i * 60;
835        if (l > sites)
836          l = sites;
837        for (k = (i - 1) * 60 + 1; k <= l; k++) {
838          if (j > 1 && y[j - 1][k - 1] == y[0][k - 1])
839            ch = '.';
840          else
841            ch = y[j - 1][k - 1];
842          putc(ch, outfile);
843          if (k % 10 == 0 && k % 60 != 0)
844            putc(' ', outfile);
845        }
846        putc('\n', outfile);
847      }
848      putc('\n', outfile);
849    }
850  }
851  if (!usertree) {
852    setuptree(&bestree);
853    if (njumble > 1)
854      setuptree(&bestree2);
855  }
856}  /* getdata */
857
858void sitesort()
859{
860  /* Shell sort keeping sites, weights in same order */
861  short gap, i, j, jj, jg, k, itemp;
862  boolean flip, tied;
863
864  gap = sites / 2;
865  while (gap > 0) {
866    for (i = gap + 1; i <= sites; i++) {
867      j = i - gap;
868      flip = true;
869      while (j > 0 && flip) {
870        jj = alias[j - 1];
871        jg = alias[j + gap - 1];       
872        tied = (weight[jj - 1] == weight[jg - 1]);
873        flip = (weight[jj - 1] < weight[jg - 1]);
874        k = 1;
875        while (k <= spp && tied) {
876          flip = (y[k - 1][jj - 1] > y[k - 1][jg - 1]);
877          tied = (tied && y[k - 1][jj - 1] == y[k - 1][jg - 1]);
878          k++;
879        }
880        if (!flip)
881          break;
882        itemp = alias[j - 1];
883        alias[j - 1] = alias[j + gap - 1];
884        alias[j + gap - 1] = itemp;
885        itemp = aliasweight[j - 1];
886        aliasweight[j - 1] = aliasweight[j + gap - 1];
887        aliasweight[j + gap - 1] = itemp;
888        j -= gap;
889      }
890    }
891    gap /= 2;
892  }
893}  /* sitesort */
894
895void sitecombine()
896{
897  /* combine sites that have identical patterns */
898  short i, j, k;
899  boolean tied;
900
901  i = 1;
902  while (i < sites) {
903    j = i + 1;
904    tied = true;
905    while (j <= sites && tied) {
906      tied = (weight[alias[i - 1] - 1] == weight[alias[j - 1] - 1]);
907      k = 1;
908      while (k <= spp && tied) {
909        tied = (tied &&
910            y[k - 1][alias[i - 1] - 1] == y[k - 1][alias[j - 1] - 1]);
911        k++;
912      }
913      if (!tied)
914        break;
915      aliasweight[i - 1] += aliasweight[j - 1];
916      aliasweight[j - 1] = 0;
917      ally[alias[j - 1] - 1] = alias[i - 1];
918      j++;
919    }
920    i = j;
921  }
922}  /* sitecombine */
923
924void sitescrunch()
925{
926  /* move so positively weighted sites come first */
927  short i, j, itemp;
928  boolean done, found;
929
930  done = false;
931  i = 1;
932  j = 2;
933  while (!done) {
934    found = false;
935    if (aliasweight[i - 1] > 0)
936      i++;
937    else {
938      if (j <= i)
939        j = i + 1;
940      if (j <= sites) {
941        found = false;
942        do {
943          found = (aliasweight[j - 1] > 0);
944          j++;
945        } while (!(found || j > sites));
946        if (found) {
947          j--;
948          itemp = alias[i - 1];
949          alias[i - 1] = alias[j - 1];
950          alias[j - 1] = itemp;
951          itemp = aliasweight[i - 1];
952          aliasweight[i - 1] = aliasweight[j - 1];
953          aliasweight[j - 1] = itemp;
954        } else
955          done = true;
956      } else
957        done = true;
958    }
959    done = (done || i >= sites);
960  }
961}  /* sitescrunch */
962
963void makeweights()
964{
965  /* make up weights vector to avoid duplicate computations */
966  short i,j,k;
967  node *p1,*p2,*p3;
968
969   for (i = 1; i <= sites; i++) {
970    alias[i - 1] = i;
971    ally[i - 1] = 0;
972    aliasweight[i - 1] = weight[i - 1];
973    location[i - 1] = 0;
974  }
975  sitesort();
976  sitecombine();
977  sitescrunch();
978  for (i = 1; i <= sites; i++) {
979    weight[i - 1] = aliasweight[i - 1];
980    if (weight[i - 1] > 0)
981      endsite = i;
982  }
983  for (i = 1; i <= endsite; i++) {
984    ally[alias[i - 1] - 1] = alias[i - 1];
985    location[alias[i - 1] - 1] = i;
986  }
987  sumrates = 0.0;
988  for (i = 0; i < categs; i++)
989    sumrates += probcat[i] * rate[i];
990  for (i = 0; i < categs; i++)
991    rate[i] /= sumrates;
992
993  for (i=0; i<spp;i++){
994    curtree.nodep[i]->x=(phenotype)Malloc(endsite * sizeof(sitelike *));
995    if (!usertree) {
996      bestree.nodep[i]->x=(phenotype)Malloc(endsite * sizeof(sitelike *));
997      if (njumble > 1)
998        bestree2.nodep[i]->x=(phenotype)Malloc(endsite * sizeof(sitelike *));
999    }
1000    for (j=0;j<endsite;++j){
1001      curtree.nodep[i]->x[j] = (sitelike *)Malloc(categs * sizeof(sitelike));
1002      if (!usertree) {
1003        bestree.nodep[i]->x[j] = (sitelike *)Malloc(categs*sizeof(sitelike));
1004        if (njumble > 1)
1005          bestree2.nodep[i]->x[j] = (sitelike *)Malloc(categs*sizeof(sitelike));
1006      }
1007    }
1008  }
1009
1010  for (i=spp; i < nonodes ; i++){
1011    p1 = curtree.nodep[i];
1012    if (!usertree) {
1013      p2 = bestree.nodep[i];
1014      if (njumble > 1)
1015        p3 = bestree2.nodep[i];
1016    }
1017    for (k=0;k<3;++k) {
1018      p1->x = (phenotype)Malloc(endsite * sizeof(sitelike *));
1019      if (!usertree) {
1020        p2->x = (phenotype)Malloc(endsite * sizeof(sitelike *));
1021        if (njumble > 1)
1022          p3->x = (phenotype)Malloc(endsite * sizeof(sitelike *));
1023      }
1024      for (j=0;j<endsite;++j){
1025        p1->x[j] = (sitelike *)Malloc(categs * sizeof(sitelike));
1026        if (!usertree) {
1027          p2->x[j] = (sitelike *)Malloc(categs * sizeof(sitelike));
1028          if (njumble > 1)
1029            p3->x[j] = (sitelike *)Malloc(categs * sizeof(sitelike));
1030        }
1031      }
1032      p1=p1->next;
1033      if (!usertree) {
1034        p2=p2->next;
1035        if (njumble > 1)
1036          p3=p3->next;
1037      }
1038    }
1039  }
1040}  /* makeweights */
1041
1042void makevalues()
1043{
1044  /* set up fractional likelihoods at tips */
1045  short i, j, k, l;
1046  base b;
1047
1048  for (k = 0; k < endsite; k++) {
1049    j = alias[k];
1050    for (i = 0; i < spp; i++) {
1051      for (l = 0; l < categs; l++) {
1052        for (b = A; (short)b <= (short)T; b = (base)((short)b + 1))
1053          curtree.nodep[i]->x[k][l][(short)b - (short)A] = 0.0;
1054        switch (y[i][j - 1]) {
1055
1056        case 'A':
1057          curtree.nodep[i]->x[k][l][0] = 1.0;
1058          break;
1059
1060        case 'C':
1061          curtree.nodep[i]->x[k][l][(short)C - (short)A] = 1.0;
1062          break;
1063
1064        case 'G':
1065          curtree.nodep[i]->x[k][l][(short)G - (short)A] = 1.0;
1066          break;
1067
1068        case 'T':
1069          curtree.nodep[i]->x[k][l][(short)T - (short)A] = 1.0;
1070          break;
1071
1072        case 'U':
1073          curtree.nodep[i]->x[k][l][(short)T - (short)A] = 1.0;
1074          break;
1075
1076        case 'M':
1077          curtree.nodep[i]->x[k][l][0] = 1.0;
1078          curtree.nodep[i]->x[k][l][(short)C - (short)A] = 1.0;
1079          break;
1080
1081        case 'R':
1082          curtree.nodep[i]->x[k][l][0] = 1.0;
1083          curtree.nodep[i]->x[k][l][(short)G - (short)A] = 1.0;
1084          break;
1085
1086        case 'W':
1087          curtree.nodep[i]->x[k][l][0] = 1.0;
1088          curtree.nodep[i]->x[k][l][(short)T - (short)A] = 1.0;
1089          break;
1090
1091        case 'S':
1092          curtree.nodep[i]->x[k][l][(short)C - (short)A] = 1.0;
1093          curtree.nodep[i]->x[k][l][(short)G - (short)A] = 1.0;
1094          break;
1095
1096        case 'Y':
1097          curtree.nodep[i]->x[k][l][(short)C - (short)A] = 1.0;
1098          curtree.nodep[i]->x[k][l][(short)T - (short)A] = 1.0;
1099          break;
1100
1101        case 'K':
1102          curtree.nodep[i]->x[k][l][(short)G - (short)A] = 1.0;
1103          curtree.nodep[i]->x[k][l][(short)T - (short)A] = 1.0;
1104          break;
1105
1106        case 'B':
1107          curtree.nodep[i]->x[k][l][(short)C - (short)A] = 1.0;
1108          curtree.nodep[i]->x[k][l][(short)G - (short)A] = 1.0;
1109          curtree.nodep[i]->x[k][l][(short)T - (short)A] = 1.0;
1110          break;
1111
1112        case 'D':
1113          curtree.nodep[i]->x[k][l][0] = 1.0;
1114          curtree.nodep[i]->x[k][l][(short)G - (short)A] = 1.0;
1115          curtree.nodep[i]->x[k][l][(short)T - (short)A] = 1.0;
1116          break;
1117
1118        case 'H':
1119          curtree.nodep[i]->x[k][l][0] = 1.0;
1120          curtree.nodep[i]->x[k][l][(short)C - (short)A] = 1.0;
1121          curtree.nodep[i]->x[k][l][(short)T - (short)A] = 1.0;
1122          break;
1123
1124        case 'V':
1125          curtree.nodep[i]->x[k][l][0] = 1.0;
1126          curtree.nodep[i]->x[k][l][(short)C - (short)A] = 1.0;
1127          curtree.nodep[i]->x[k][l][(short)G - (short)A] = 1.0;
1128          break;
1129
1130        case 'N':
1131          for (b = A; (short)b <= (short)T; b = (base)((short)b + 1))
1132            curtree.nodep[i]->x[k][l][(short)b - (short)A] = 1.0;
1133          break;
1134
1135        case 'X':
1136          for (b = A; (short)b <= (short)T; b = (base)((short)b + 1))
1137            curtree.nodep[i]->x[k][l][(short)b - (short)A] = 1.0;
1138          break;
1139
1140        case '?':
1141          for (b = A; (short)b <= (short)T; b = (base)((short)b + 1))
1142            curtree.nodep[i]->x[k][l][(short)b - (short)A] = 1.0;
1143          break;
1144
1145        case 'O':
1146          for (b = A; (short)b <= (short)T; b = (base)((short)b + 1))
1147            curtree.nodep[i]->x[k][l][(short)b - (short)A] = 1.0;
1148          break;
1149
1150        case '-':
1151          for (b = A; (short)b <= (short)T; b = (base)((short)b + 1))
1152            curtree.nodep[i]->x[k][l][(short)b - (short)A] = 1.0;
1153          break;
1154        }
1155      }
1156    }
1157  }
1158}  /* makevalues */
1159
1160Local Void empiricalfreqs()
1161{
1162  /* Get empirical base frequencies from the data */
1163  short i, j, k;
1164  double sum, suma, sumc, sumg, sumt, w;
1165
1166  freqa = 0.25;
1167  freqc = 0.25;
1168  freqg = 0.25;
1169  freqt = 0.25;
1170  for (k = 1; k <= 8; k++) {
1171    suma = 0.0;
1172    sumc = 0.0;
1173    sumg = 0.0;
1174    sumt = 0.0;
1175    for (i = 0; i < spp; i++) {
1176      for (j = 0; j < endsite; j++) {
1177        w = aliasweight[j];
1178        sum = freqa * curtree.nodep[i]->x[j][0][0];
1179        sum += freqc * curtree.nodep[i]->x[j][0][(short)C - (short)A];
1180        sum += freqg * curtree.nodep[i]->x[j][0][(short)G - (short)A];
1181        sum += freqt * curtree.nodep[i]->x[j][0][(short)T - (short)A];
1182        suma += w * freqa * curtree.nodep[i]->x[j][0][0] / sum;
1183        sumc += w * freqc * curtree.nodep[i]->x[j][0][(short)C - (short)A] / sum;
1184        sumg += w * freqg * curtree.nodep[i]->x[j][0][(short)G - (short)A] / sum;
1185        sumt += w * freqt * curtree.nodep[i]->x[j][0][(short)T - (short)A] / sum;
1186      }
1187    }
1188    sum = suma + sumc + sumg + sumt;
1189    freqa = suma / sum;
1190    freqc = sumc / sum;
1191    freqg = sumg / sum;
1192    freqt = sumt / sum;
1193  }
1194}  /* empiricalfreqs */
1195
1196
1197void getinput()
1198{
1199  /* reads the input data */
1200  inputoptions();
1201  if (!freqsfrom)
1202    getbasefreqs();
1203  getdata();
1204  makeweights();
1205  makevalues();
1206  if (freqsfrom) {
1207    empiricalfreqs();
1208    getbasefreqs();
1209  }
1210}  /* getinput */
1211
1212
1213main(argc, argv)
1214int argc;
1215Char *argv[];
1216{  /* DNA Maximum Likelihood with molecular clock */
1217char infilename[100],outfilename[100],trfilename[100];
1218int i;
1219#ifdef MAC
1220  macsetup("Dnamlk","Dnamlk");
1221#endif
1222
1223  openfile(&infile,INFILE,"r",argv[0],infilename);
1224  openfile(&outfile,OUTFILE,"w",argv[0],outfilename);
1225
1226  ibmpc = ibmpc0;
1227  ansi = ansi0;
1228  vt52 = vt520;
1229  datasets = 1;
1230  mulsets = false;
1231  firstset = true;
1232  doinit();
1233
1234  ttratio0    = ttratio;
1235  enterorder  = (short *)Malloc(spp*sizeof(short));
1236  weight      = (short *)Malloc(sites*sizeof(short));
1237  alias       = (short *)Malloc(sites*sizeof(short));
1238  aliasweight = (short *)Malloc(sites*sizeof(short));
1239  ally        = (short *)Malloc(sites*sizeof(short));
1240  location    = (short *)Malloc(sites*sizeof(short));
1241
1242  if (trout)
1243    openfile(&treefile,TREEFILE,"w",argv[0],trfilename);
1244  for (ith = 1; ith <= datasets; ith++) {
1245    ttratio = ttratio0;
1246    if (datasets > 1) {
1247      fprintf(outfile, "Data set # %hd:\n\n",ith);
1248      if (progress)
1249        printf("\nData set # %hd:\n",ith);
1250    }
1251    getinput();
1252    contribution = (double **)Malloc(endsite * sizeof(double *));
1253    for (i=0;i<endsite;++i)
1254        contribution[i] = (double *)Malloc(categs * sizeof(double));
1255    if (ith == 1)
1256      firstset = false;
1257    for (jumb = 1; jumb <= njumble; jumb++)
1258      maketree();
1259    for (i=0;i<endsite;++i)
1260        free (contribution[i]);
1261    free(contribution);
1262  }
1263  FClose(infile);
1264  FClose(outfile);
1265  FClose(treefile);
1266#ifdef MAC
1267  fixmacfile(outfilename);
1268  fixmacfile(trfilename);
1269#endif
1270  exit(0);
1271}  /* DNA Maximum Likelihood with molecular clock */
1272
1273int eof(f)
1274FILE *f;
1275{
1276    register int ch;
1277
1278    if (feof(f))
1279        return 1;
1280    ch = getc(f);
1281    if (ch == EOF)
1282        return 1;
1283    ungetc(ch, f);
1284    return 0;
1285  }
1286
1287
1288int eoln(f)
1289FILE *f;
1290{
1291    register int ch;
1292
1293    ch = getc(f);
1294    if (ch == EOF)
1295        return 1;
1296    ungetc(ch, f);
1297    return (ch == '\n');
1298  }
1299
1300
1301void memerror()
1302{
1303printf("Error allocating memory\n");
1304exit(-1);
1305}
1306
1307MALLOCRETURN *mymalloc(x)
1308long x;
1309{
1310MALLOCRETURN *mem;
1311mem = (MALLOCRETURN *)calloc((size_t)1,x);
1312if (!mem)
1313  memerror();
1314else
1315  return (MALLOCRETURN *)mem;
1316}
1317
Note: See TracBrowser for help on using the repository browser.