Debian Codegraph @

src/dr/evomodel/speciation/BirthDeathSerialSamplingModel.java

raw symbols SCIP
1 /*
2 * BirthDeathSerialSamplingModel.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.evomodel.speciation;
27
28 import dr.evolution.tree.NodeRef;
29 import dr.evolution.tree.Tree;
30 import dr.evolution.util.Taxon;
31 import dr.inference.model.Parameter;
32 import dr.inference.model.Variable;
33 import dr.util.Author;
34 import dr.util.Citable;
35 import dr.util.Citation;
36
37 import java.util.Collections;
38 import java.util.List;
39 import java.util.Set;
40
41 /**
42 * Beginning of tree prior for birth-death + serial sampling + extant sample proportion. More Tanja magic...
43 *
44 * @author Alexei Drummond
45 */
46 public class BirthDeathSerialSamplingModel extends MaskableSpeciationModel implements Citable {
47
48 // R0
49 Variable<Double> R0;
50
51 // recovery rate
52 Variable<Double> recoveryRate;
53
54 // sampling probability
55 Variable<Double> samplingProbability;
56
57
58 // birth rate
59 Variable<Double> lambda;
60
61 // death rate
62 Variable<Double> mu;
63
64 // serial sampling rate
65 Variable<Double> psi;
66
67 // extant sampling proportion
68 Variable<Double> p;
69
70 //boolean death rate is relative?
71 boolean relativeDeath = false;
72
73 // boolean stating whether sampled individuals remain infectious, or become non-infectious
74 // boolean sampledIndividualsRemainInfectious = false; // replaced by r
75
76 // the additional parameter 0 <= r <= 1 has to be estimated.
77 // for r=1, this is sampledRemainInfectiousProb=0
78 // for r=0, this is sampledRemainInfectiousProb=1
79 Variable<Double> r;
80
81 //Variable<Double> finalTimeInterval;
82
83 boolean hasFinalSample = false;
84
85 // the origin of the infection, x0 > tree.getRoot();
86 Variable<Double> origin;
87
88 public BirthDeathSerialSamplingModel(
89 Variable<Double> lambda,
90 Variable<Double> mu,
91 Variable<Double> psi,
92 Variable<Double> p,
93 boolean relativeDeath,
94 Variable<Double> r,
95 boolean hasFinalSample,
96 Variable<Double> origin,
97 Type units) {
98
99 this("birthDeathSerialSamplingModel", lambda, mu, psi, p, relativeDeath, r, hasFinalSample, origin, units);
100 }
101
102 public BirthDeathSerialSamplingModel(
103 String modelName,
104 Variable<Double> lambda,
105 Variable<Double> mu,
106 Variable<Double> psi,
107 Variable<Double> p,
108 boolean relativeDeath,
109 Variable<Double> r,
110 boolean hasFinalSample,
111 Variable<Double> origin,
112 Type units) {
113
114 super(modelName, units);
115
116 this.relativeDeath = relativeDeath;
117
118 this.lambda = lambda;
119 addVariable(lambda);
120 lambda.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1));
121
122 this.mu = mu;
123 addVariable(mu);
124 mu.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1));
125
126 this.psi = psi;
127 addVariable(psi);
128 psi.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1));
129
130 this.p = p;
131 addVariable(p);
132 p.addBounds(new Parameter.DefaultBounds(1.0, 0.0, 1));
133
134 this.hasFinalSample = hasFinalSample;
135
136 this.r = r;
137 addVariable(r);
138 r.addBounds(new Parameter.DefaultBounds(1.0, 0.0, 1));
139
140 this.origin = origin;
141 if (origin != null) {
142 addVariable(origin);
143 origin.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1));
144 }
145 }
146
147 public BirthDeathSerialSamplingModel(
148 String modelName,
149 Variable<Double> R0,
150 Variable<Double> recoveryRate,
151 Variable<Double> samplingProbability,
152 Variable<Double> origin,
153 Type units) {
154
155 super(modelName, units);
156
157 this.relativeDeath = false;
158 this.hasFinalSample = false;
159
160 this.R0 = R0;
161 addVariable(R0);
162 R0.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1));
163
164 this.recoveryRate = recoveryRate;
165 addVariable(recoveryRate);
166 recoveryRate.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1));
167
168 this.samplingProbability = samplingProbability;
169 addVariable(samplingProbability);
170 samplingProbability.addBounds(new Parameter.DefaultBounds(1.0, 0.0, 1));
171
172 this.origin = origin;
173 if (origin != null) {
174 addVariable(origin);
175 origin.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1));
176 }
177 }
178
179 /**
180 * @param b birth rate
181 * @param d death rate
182 * @param p proportion sampled at final time point
183 * @param psi rate of sampling per lineage per unit time
184 * @param t time
185 * @return the probability of no sampled descendants after time, t
186 */
187 public static double p0(double b, double d, double p, double psi, double t) {
188 double c1 = c1(b, d, psi);
189 double c2 = c2(b, d, p, psi);
190
191 double expc1trc2 = Math.exp(-c1 * t) * (1.0 - c2);
192
193 return (b + d + psi + c1 * ((expc1trc2 - (1.0 + c2)) / (expc1trc2 + (1.0 + c2)))) / (2.0 * b);
194 }
195
196 public static double q(double b, double d, double p, double psi, double t) {
197 double c1 = c1(b, d, psi);
198 double c2 = c2(b, d, p, psi);
199 // double res = 2.0 * (1.0 - c2 * c2) + Math.exp(-c1 * t) * (1.0 - c2) * (1.0 - c2) + Math.exp(c1 * t) * (1.0 + c2) * (1.0 + c2);
200 double res = c1 * t + 2.0 * Math.log( Math.exp(-c1 * t) * (1.0 - c2) + (1.0 + c2) ); // operate directly in logspace, c1 * t too big
201 return res;
202 }
203
204 private static double c1(double b, double d, double psi) {
205 return Math.abs(Math.sqrt(Math.pow(b - d - psi, 2.0) + 4.0 * b * psi));
206 }
207
208 private static double c2(double b, double d, double p, double psi) {
209 return -(b - d - 2.0 * b * p - psi) / c1(b, d, psi);
210 }
211
212
213 public double p0(double t) {
214 return p0(birth(), death(), p(), psi(), t);
215 }
216
217 public double q(double t) {
218 return q(birth(), death(), p(), psi(), t);
219 }
220
221 private double c1() {
222 return c1(birth(), death(), psi());
223 }
224
225 private double c2() {
226 return c2(birth(), death(), p(), psi());
227 }
228
229 public double birth() {
230 if (mask != null) return mask.birth();
231 if (lambda != null) {
232 return lambda.getValue(0);
233 } else {
234 double r0 = R0.getValue(0);
235 double rr = recoveryRate.getValue(0);
236 return r0 * rr;
237 }
238 }
239
240 public double death() {
241 if (mask != null) return mask.death();
242 if (mu != null) {
243 return relativeDeath ? mu.getValue(0) * birth() : mu.getValue(0);
244 } else {
245 double rr = recoveryRate.getValue(0);
246 double sp = samplingProbability.getValue(0);
247
248 return rr * (1.0 - sp);
249 }
250 }
251
252 public double psi() {
253 if (mask != null) return mask.psi();
254
255 if (psi != null) {
256 return psi.getValue(0);
257 } else {
258 double rr = recoveryRate.getValue(0);
259 double sp = samplingProbability.getValue(0);
260
261 return rr * sp;
262 }
263 }
264
265 /**
266 * @return the proportion of population sampled at final sample, or zero if there is no final sample
267 */
268 public double p() {
269
270 if (mask != null) return mask.p.getValue(0);
271 return hasFinalSample ? p.getValue(0) : 0;
272 }
273
274 // The mask does not affect the following three methods
275
276 public boolean isSamplingOrigin() {
277 return origin != null;
278 }
279
280 public double x0() {
281 return origin.getValue(0);
282 }
283
284 /**
285 * Generic likelihood calculation
286 *
287 * @param tree the tree to calculate likelihood of
288 * @return log-likelihood of density
289 */
290 public final double calculateTreeLogLikelihood(Tree tree) {
291
292 if (isSamplingOrigin() && x0() < tree.getNodeHeight(tree.getRoot())) {
293 return Double.NEGATIVE_INFINITY;
294 // throw new RuntimeException("Orign value (" + x0() + ") cannot < tree root height (" + tree.getNodeHeight(tree.getRoot()) + ")");
295 }
296
297 //System.out.println("calculating tree log likelihood");
298 //double time = finalTimeInterval();
299
300 // extant leaves
301 int n = 0;
302 // extinct leaves
303 int m = 0;
304
305 for (int i = 0; i < tree.getExternalNodeCount(); i++) {
306 NodeRef node = tree.getExternalNode(i);
307 if (tree.getNodeHeight(node) == 0.0) {
308 n += 1;
309 } else {
310 m += 1;
311 }
312 }
313
314 if (!hasFinalSample && n < 1) {
315 throw new RuntimeException(
316 "For sampling-through-time model there must be at least one tip at time zero.");
317 }
318
319 double b = birth();
320 double p = p();
321
322 double logL;
323 if (isSamplingOrigin()) {
324 // logL = Math.log(1.0 / q(x0()));
325 logL = - q(x0());
326 //System.out.println("originLogL=" + logL + " x0");
327 } else {
328 throw new RuntimeException(
329 "The origin must be sampled, as integrating it out is not implemented!");
330 // integrating out the time between the origin and the root of the tree
331 //double bottom = c1 * (c2 + 1) * (1 - c2 + (1 + c2) * Math.exp(c1 * x1));
332 //logL = Math.log(1 / bottom);
333 }
334 if (hasFinalSample) {
335 logL += n * Math.log(4.0 * p);
336 }
337 for (int i = 0; i < tree.getInternalNodeCount(); i++) {
338 double x = tree.getNodeHeight(tree.getInternalNode(i));
339 logL += Math.log(b) - q(x);
340
341 //System.out.println("internalNodeLogL=" + Math.log(b / q(x)));
342
343 }
344 for (int i = 0; i < tree.getExternalNodeCount(); i++) {
345 double y = tree.getNodeHeight(tree.getExternalNode(i));
346
347 if (y > 0.0) {
348 logL += Math.log(psi()) + q(y);
349
350 //System.out.println("externalNodeLogL=" + Math.log(psi() * (r() + (1.0 - r()) * p0(y)) * q(y)));
351
352 } else if (!hasFinalSample) {
353 //handle condition ending on final tip in sampling-through-time-only situation
354 logL += Math.log(psi()) + q(y);
355 // System.out.println("externalNodeLogL=" + Math.log(psi() * q(y)));
356
357 }
358 }
359
360 return logL;
361 }
362
363 public double calculateTreeLogLikelihood(Tree tree, Set<Taxon> exclude) {
364 if (exclude.size() == 0) return calculateTreeLogLikelihood(tree);
365 throw new RuntimeException("Not implemented!");
366 }
367
368 public void mask(SpeciationModel mask) {
369 if (mask instanceof BirthDeathSerialSamplingModel) {
370 this.mask = (BirthDeathSerialSamplingModel) mask;
371 } else {
372 throw new IllegalArgumentException();
373 }
374 }
375
376 public void unmask() {
377 mask = null;
378 }
379
380 // if a mask exists then use the mask's parameters instead (except for origin and finalTimeInterval)
381 BirthDeathSerialSamplingModel mask = null;
382
383 @Override
384 public Citation.Category getCategory() {
385 return Citation.Category.TREE_PRIORS;
386 }
387
388 @Override
389 public String getDescription() {
390 return "Gernhard 2008 Birth Death Tree Model";
391 }
392
393 @Override
394 public List<Citation> getCitations() {
395 return Collections.singletonList(new Citation(
396 new Author[]{
397 new Author("T", "Gernhard"),
398 },
399 "The conditioned reconstructed process",
400 2008,
401 "Journal of Theoretical Biology",
402 253,
403 769, 778,
404 "10.1016/j.jtbi.2008.04.005"
405 ));
406 }
407 }