Debian Codegraph @

src/dr/evolution/coalescent/Skyline.java

raw symbols SCIP
1 /*
2 * Skyline.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.evolution.coalescent;
27
28 import dr.evolution.tree.Tree;
29 import dr.evolution.util.Units;
30 import dr.math.Binomial;
31
32 /**
33 * Skyline plot derived from a strictly bifurcating tree
34 * or a coalescent interval.
35 *
36 * This class provides the "classic" skyline plot method by
37 * Pybus, Rambaut and Harvey 2000. Genetics 155:1429-1437, as well
38 * as the "generalized" skyline plot method described in
39 * Strimmer and Pybus. 2001. MBE submitted.
40 *
41 * @version $Id: Skyline.java,v 1.15 2005/05/24 20:25:56 rambaut Exp $
42 *
43 * @author Korbinian Strimmer
44 */
45 public class Skyline implements Units {
46 //
47 // Public stuff
48 //
49
50 Skyline() {}
51
52 /**
53 * Construct skyline plot from tree
54 *
55 * @param epsilon smoothing parameter (if set < 0 then epsilon will be optimized)
56 */
57 public Skyline(Tree tree, double epsilon)
58 {
59 this(new TreeIntervals(tree), 1.0, epsilon);
60 }
61
62 /**
63 * Construct skyline plot from tree
64 *
65 * @param epsilon smoothing parameter (if set < 0 then epsilon will be optimized)
66 */
67 public Skyline(Tree tree, double mutationRate, double epsilon)
68 {
69 this(new TreeIntervals(tree), mutationRate, epsilon);
70 }
71
72
73 /**
74 * Construct skyline plot from given coalescent intervals
75 *
76 * @param epsilon smoothing parameter (if set < 0 then epsilon will be optimized)
77 */
78 public Skyline(IntervalList intervals, double mutationRate, double epsilon)
79 {
80 init(intervals, mutationRate, epsilon);
81 }
82
83 void init(IntervalList intervals, double mutationRate, double epsilon) {
84 if (!intervals.isBinaryCoalescent()) {
85 throw new IllegalArgumentException("All intervals must contain only a single coalescent");
86 }
87
88 mu = mutationRate;
89
90 size = intervals.getIntervalCount();
91
92 this.intervals = intervals;
93
94 // population size in each coalescent interval
95 populationSize = new double[size];
96
97 // cumulative interval sizes
98 cis = new double[size];
99
100 maxTime = 0.0;
101 for (int i = 0; i < size; i++) {
102 cis[i] = maxTime;
103 maxTime += intervals.getInterval(i) / mu;
104 }
105
106 if (epsilon == 0.0) {
107 /* init with classic skyline plot */
108 computeClassic();
109 } else if (epsilon > 0.0) {
110 /* init with generalized skyline plot */
111 computeGeneralized(epsilon);
112 } else {
113 // find optimal generalized skyline plot
114 optimize();
115 }
116 }
117
118 public String toString()
119 {
120 StringBuffer buffer = new StringBuffer("Skyline Plot ");
121 if (eps == 0.0) {
122 buffer.append("(classic): ");
123 } else {
124 buffer.append("(generalized): ");
125 buffer.append("epsilon = " + eps + ", ");
126 }
127
128 buffer.append("log L = " + getLogLikelihood());
129 if (params > size-2) {
130 buffer.append("\tlog L(AICC) not available");
131 } else {
132 buffer.append("\tlog L(AICC) = " + getAICC());
133 }
134
135 return buffer.toString();
136 }
137
138 /**
139 * Compute classic skyline plot
140 */
141 public void computeClassic()
142 {
143 int i = 0;
144 int k = 0;
145 do {
146
147 double popSize = 0.0;
148 boolean done = false;
149
150 k = i;
151
152 do {
153 double w = intervals.getInterval(i) / mu;
154 int n = intervals.getLineageCount(i);
155 if (n < 0) n = 0;
156 done = intervals.getIntervalType(i) == IntervalType.COALESCENT;
157
158 popSize += w * Binomial.choose2(n);
159 i++;
160 } while (i < size && !done);
161
162 for (int j = k; j < i; j++) {
163 populationSize[j] = popSize;
164 }
165 } while (i < size);
166
167 params = k;
168 eps = 0.0;
169 }
170
171 /**
172 * Compute generalized skyline plot
173 */
174 public void computeGeneralized(double epsilon)
175 {
176 params = 0;
177 double cw = 0; //cumulative w
178
179 for (int i = 0; i < size; i++) {
180
181 int n = intervals.getLineageCount(i);
182 if (n < 0) n = 0;
183
184 double w = intervals.getInterval(i) / mu;
185
186 int start = i;
187 int k = 1;
188 while ((w < epsilon && i < size-1) ||
189 intervals.getIntervalType(i) != IntervalType.COALESCENT) {
190 i++;
191 k++;
192 w += intervals.getInterval(i) / mu;
193
194 //System.out.println(ci.getInterval(i));
195 }
196
197 //System.out.println("w=" + w + " k=" + k + " i=" + i);
198
199 // if remainder is smaller than epsilon
200 // continue pooling until the end
201 if (maxTime - cw - w < epsilon) {
202 for (int j = i+1; j < size; j++)
203 {
204 i++;
205 k++;
206 w += intervals.getInterval(i) / mu;
207 }
208
209 }
210
211 double m = w * Binomial.choose2(n) / k;
212
213 // assign the same pop.size to all sub intervals
214 for (int j = start; j < start+k; j++) {
215 populationSize[j] = m;
216 }
217 params++;
218 cw += w;
219 }
220 eps = epsilon;
221 }
222
223 /**
224 * Optimize generalized skyline plot
225 */
226 public void optimize()
227 {
228 // this is the naive way of doing this ...
229
230 double besteps = getMaxTime();
231 computeGeneralized(besteps);
232 double bestaicc = getAICC();
233
234 int GRID = 1000;
235 double delta = besteps/GRID;
236
237 double MINEPS = 1e-6;
238 // Why MINEPS?
239 // Because most "clock-like" trees are not properly
240 // clock-like for a variety of reasons, i.e. the heights
241 // of the tips are not exactly zero.
242
243
244
245 eps = eps - delta;
246 while(eps > MINEPS)
247 {
248 computeGeneralized(eps);
249 double aicc = getAICC();
250
251 if (aicc > bestaicc && params < size-1)
252 {
253 besteps = eps;
254 bestaicc = aicc;
255 }
256 eps = eps - delta;
257 }
258
259 computeGeneralized(besteps);
260 }
261
262 /**
263 * Compute log-likelihood
264 */
265 public double getLogLikelihood()
266 {
267 double logL = 0.0;
268
269 for (int i = 0; i < size; i++) {
270
271 double w = intervals.getInterval(i);
272 double m = populationSize[i];
273
274 double n = intervals.getIntervalCount();
275 double nc2 = n*(n-1.0)/2.0;
276
277 if (intervals.getIntervalType(i) == IntervalType.COALESCENT) {
278 logL += Math.log(nc2/m) - w*nc2/m;
279 } else {
280 logL -= w*nc2/m;
281 }
282 }
283
284 return logL;
285 }
286
287 /**
288 * Compute AICC-corrected log-likelihood
289 */
290 public double getAICC()
291 {
292 double logL = getLogLikelihood();
293
294 return AICC(logL, params, size);
295 }
296
297 /**
298 * Second-order Akaike (AICC) correction (Hurvich and Tsai 1989)
299 *
300 * @param l log-likelihood
301 * @param k number of inferred parameters
302 * @param n sample size
303 *
304 * @return l - k - (k(k+1))/(n - k - 1)
305 */
306 public static double AICC(double l, int k, int n)
307 {
308 if (k > n-2) throw new IllegalArgumentException("k must be smaller than n-1");
309
310 return l - k - (double) (k*(k+1.0))/ (double) (n - k - 1.0) ;
311 }
312
313 /**
314 * Find interval corresponding to a specific time
315 */
316 public double findInterval(double time)
317 {
318 if (time < 0) throw new IllegalArgumentException("Negative values for time are not allowed");
319
320 for (int i = 0; i < size-1; i++)
321 {
322 if (time >= cis[i] && time < cis[i+1]) return i;
323 }
324
325 return size-1;
326 }
327
328 /**
329 * Returns the largest value of time defined in this plot
330 * (= maximum value for epsilon)
331 */
332 public double getMaxTime()
333 {
334 return maxTime;
335 }
336
337
338 /**
339 * Returns the largest estimate of population size.
340 */
341 public double getMaxPopulationSize() {
342 double max = 0.0;
343 for (int i = 0; i < size; i++) {
344 if (populationSize[i] > max) {
345 max = populationSize[i];
346 }
347 }
348
349 return max;
350 }
351
352 /**
353 * Returns the coalescent intervals in this skyline plot.
354 */
355 public IntervalList getIntervals() {
356 return intervals;
357 }
358
359 /**
360 * Returns the number of intervals in this skyline plot.
361 */
362 public int getSize() {
363 return size;
364 }
365
366 /**
367 * Returns the number of composite intervals (=number of parameters).
368 */
369 public int getParameterCount() {
370 return params;
371 }
372
373 /**
374 * Returns epsilon
375 */
376 public double getEpsilon() {
377 return eps;
378 }
379
380 /**
381 * Returns the population size in interval i.
382 */
383 public double getPopulationSize(int i) {
384 return populationSize[i];
385 }
386
387 /**
388 * Return the units
389 */
390 public final Type getUnits() {
391 return intervals.getUnits();
392 }
393
394 /**
395 * Sets the units
396 */
397 public final void setUnits(Type units) {
398 throw new IllegalArgumentException("Can't set skyline's units");
399 }
400
401
402 // private
403
404 private IntervalList intervals;
405 private int size;
406 private double maxTime;
407 private double eps;
408 private double mu;
409 private int params;
410
411 /** cummulative interval sizes */
412 private double[] cis;
413
414 /** estimated population size in a coalescent interval */
415 private double[] populationSize;
416
417
418 }