source: tags/arb-6.0/GDE/PHYLIP/cont.c

Last change on this file was 2175, checked in by westram, 20 years ago

upgrade to PHYLIP 3.6

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.2 KB
Line 
1#include "phylip.h"
2#include "cont.h"
3
4/* version 3.6. (c) Copyright 1999-2000 by the University of Washington.
5   Written by Joseph Felsenstein, Akiko Fuseki, Sean Lamont, and Andrew Keeffe.
6   Permission is granted to copy and use this program provided no fee is
7   charged for it and provided that this copyright notice is not removed. */
8
9
10
11void alloctree(pointarray *treenode, long nonodes)
12{
13  /* allocate treenode dynamically */
14  /* used in contml & contrast */
15  long i, j;
16  node *p, *q;
17
18  *treenode = (pointarray)Malloc(nonodes*sizeof(node *));
19  for (i = 0; i < spp; i++)
20    (*treenode)[i] = (node *)Malloc(sizeof(node));
21  for (i = spp; i < nonodes; i++) {
22    q = NULL;
23    for (j = 1; j <= 3; j++) {
24      p = (node *)Malloc(sizeof(node));
25      p->next = q;
26      q = p;
27    }
28    p->next->next->next = p;
29    (*treenode)[i] = p;
30  }
31} /* alloctree */
32
33
34void setuptree(tree *a, long nonodes)
35{
36  /* initialize a tree */
37  /* used in contml & contrast */
38  long i, j;
39  node *p;
40
41  for (i = 1; i <= spp; i++) {
42    a->nodep[i - 1]->back = NULL;
43    a->nodep[i - 1]->tip = (i <= spp);
44    a->nodep[i - 1]->iter = true;
45    a->nodep[i - 1]->index = i;
46  }
47  for (i = spp + 1; i <= nonodes; i++) {
48    p = a->nodep[i - 1];
49    for (j = 1; j <= 3; j++) {
50      p->back = NULL;
51      p->tip = false;
52      p->iter = true;
53      p->index = i;
54      p = p->next;
55    }
56  }
57  a->likelihood = -99999.0;
58  a->start = a->nodep[0];
59}  /* setuptree */
60
61
62void allocview(tree *a, long nonodes, long totalleles)
63{
64  /* allocate view */
65  /* used in contml */
66  long i, j;
67  node *p;
68
69  for (i = 0; i < spp; i++)
70    a->nodep[i]->view = (phenotype3)Malloc(totalleles*sizeof(double));
71  for (i = spp; i < nonodes; i++) {
72    p = a->nodep[i];
73    for (j = 1; j <= 3; j++) {
74      p->view = (phenotype3)Malloc(totalleles*sizeof(double));
75      p = p->next;
76    }
77  }
78}  /* allocview */
79
80
81void freeview(tree *a, long nonodes)
82{
83  /* deallocate view */
84  /* used in contml */
85  long i, j;
86  node *p;
87
88  for (i = 0; i < spp; i++)
89    free(a->nodep[i]->view);
90  for (i = spp; i < nonodes; i++) {
91    p = a->nodep[i];
92    for (j = 1; j <= 3; j++) {
93      free(p->view);
94      p = p->next;
95    }
96  }
97}  /* freeview */
98
99
100void standev2(long numtrees, long maxwhich, long a, long b, double maxlogl,
101              double *l0gl, double **l0gf, longer seed)
102{  /* compute and write standard deviation of user trees */
103  /* used in contml */
104  double **covar, *P, *f;
105  long i, j, k;
106  double sumw, sum, sum2, sd;
107  double temp;
108
109#define SAMPLES 1000
110#define MAXSHIMOTREES 1000
111/* ????? if numtrees too big for Shimo, truncate */
112  if (numtrees == 2) {
113    fprintf(outfile, "Kishino-Hasegawa-Templeton test\n\n");
114    fprintf(outfile, "Tree    logL    Diff logL    Its S.D.");
115    fprintf(outfile, "   Significantly worse?\n\n");
116    i = 1;
117    while (i <= numtrees) {
118      fprintf(outfile, "%3ld%10.1f", i, l0gl[i - 1]);
119      if (maxwhich == i)
120        fprintf(outfile, "  <------ best\n");
121      else {
122        sumw = 0.0;
123        sum = 0.0;
124        sum2 = 0.0;
125        for (j = a; j <= b; j++) {
126          sumw += 1;
127          temp = l0gf[i - 1][j] - l0gf[maxwhich - 1][j];
128          sum += temp;
129          sum2 += temp * temp;
130        }
131        temp = sum / sumw;
132        sd = sqrt(sumw / (sumw - 1.0) * (sum2 - temp * temp));
133        fprintf(outfile, "%10.1f%12.4f", (l0gl[i - 1])-maxlogl, sd);
134        if (sum > 1.95996 * sd)
135          fprintf(outfile, "           Yes\n");
136        else
137          fprintf(outfile, "           No\n");
138      }
139      i++;
140    }
141    fprintf(outfile, "\n\n");
142  } else {           /* Shimodaira-Hasegawa test using normal approximation */
143    fprintf(outfile, "Shimodaira-Hasegawa test\n\n");
144    covar = (double **)Malloc(numtrees*sizeof(double *)); 
145    sumw = b-a+1;
146    for (i = 0; i < numtrees; i++)
147      covar[i] = (double *)Malloc(numtrees*sizeof(double)); 
148    for (i = 0; i < numtrees; i++) {        /* compute covariances of trees */
149      sum = l0gl[i]/sumw;
150      for (j = 0; j <=i; j++) {
151        sum2 = l0gl[j]/sumw;
152        temp = 0.0;
153        for (k = a; k <= b ; k++) {
154          temp = temp + (l0gf[i][k]-sum)*(l0gf[j][k]-sum2);
155        }
156        covar[i][j] = temp;
157        if (i != j)
158          covar[j][i] = temp;
159      }
160    }
161    for (i = 0; i < numtrees; i++) { /* in-place Cholesky decomposition
162                                        of trees x trees covariance matrix */
163      sum = 0.0;
164      for (j = 0; j <= i-1; j++)
165        sum = sum + covar[i][j] * covar[i][j];
166      temp = sqrt(covar[i][i] - sum);
167      covar[i][i] = temp;
168      for (j = i+1; j < numtrees; j++) {
169        sum = 0.0;
170        for (k = 0; k < i; k++)
171          sum = sum + covar[i][k] * covar[j][k];
172        if (fabs(temp) < 1.0E-12)
173          covar[j][i] = 0.0;
174        else
175          covar[j][i] = (covar[j][i] - sum)/temp;
176      }
177    }
178    f = (double *)Malloc(numtrees*sizeof(double)); /* vector of P's of trees */
179    P = (double *)Malloc(numtrees*sizeof(double)); /* vector of P's of trees */
180    for (i = 0; i < numtrees; i++)
181      P[i] = 0.0;
182    for (i = 1; i < SAMPLES; i++) {           /* loop over resampled trees */
183      for (j = 0; j < numtrees; j++) {        /* draw vectors */
184        sum = 0.0;
185        for (k = 0; k <= j; k++)
186          sum += normrand(seed)*covar[j][k];
187        f[j] = sum;
188      }
189      sum = f[1];
190      for (j = 1; j < numtrees; j++)          /* get max of vector */
191        if (f[j] > sum)
192          sum = f[j];
193      for (j = 0; j < numtrees; j++)          /* accumulate P's */
194        if (maxlogl-l0gl[j] < sum-f[j])
195          P[j] += 1.0/SAMPLES;
196    }
197    fprintf(outfile, "Tree    logL    Diff logL    P value");
198    fprintf(outfile, "   Significantly worse?\n\n");
199    for (i = 0; i < numtrees; i++) {
200      fprintf(outfile, "%3ld%10.1f", i+1, l0gl[i]);
201      if ((maxwhich-1) == i)
202        fprintf(outfile, "  <------ best\n");
203      else {
204        fprintf(outfile, " %9.1f  %10.3f", l0gl[i]-maxlogl, P[i]);
205        if (P[i] < 0.05)
206          fprintf(outfile, "           Yes\n");
207        else
208          fprintf(outfile, "           No\n");
209      }
210    }
211  fprintf(outfile, "\n");
212  free(P);             /* free the variables we Malloc'ed */
213  free(f);
214  for (i = 0; i < numtrees; i++)
215    free(covar[i]);
216  free(covar);
217  }
218}  /* standev */
Note: See TracBrowser for help on using the repository browser.