Debian Codegraph @

src/dr/evomodel/transmission/TransmissionDemographicModel.java

raw symbols SCIP
1 /*
2 * TransmissionDemographicModel.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.transmission;
27
28 import dr.evolution.coalescent.ConstantPopulation;
29 import dr.evolution.coalescent.DemographicFunction;
30 import dr.evomodel.coalescent.DemographicModel;
31 import dr.evoxml.util.XMLUnits;
32 import dr.inference.model.Parameter;
33 import dr.xml.*;
34
35 /**
36 * A demographic model for within patient evolution in a transmission history.
37 *
38 * @author Alexei Drummond
39 * @author Andrew Rambaut
40 * @version $Id: TransmissionDemographicModel.java,v 1.7 2005/05/24 20:25:58 rambaut Exp $
41 */
42 public class TransmissionDemographicModel extends DemographicModel {
43
44 //
45 // Public stuff
46 //
47
48 public static String TRANSMISSION_MODEL = "transmissionModel";
49
50 public static String CONSTANT = "constant";
51 public static String EXPONENTIAL = "exponential";
52 public static String LOGISTIC = "logistic";
53
54 public static String POPULATION_SIZE = "populationSize";
55 public static String ANCESTRAL_PROPORTION = "ancestralProportion";
56 public static String GROWTH_RATE = "growthRate";
57 public static String DOUBLING_TIME = "doublingTime";
58
59 /**
60 * Construct demographic model with default settings
61 */
62 public TransmissionDemographicModel(int model,
63 Parameter N0Parameter, Parameter N1Parameter,
64 Parameter growthRateParameter, Parameter doublingTimeParameter, Type units) {
65
66 this(TRANSMISSION_MODEL, model, N0Parameter, N1Parameter, growthRateParameter, doublingTimeParameter, units);
67 }
68
69 /**
70 * Construct demographic model with default settings
71 */
72 public TransmissionDemographicModel(String name, int model,
73 Parameter N0Parameter, Parameter N1Parameter,
74 Parameter growthRateParameter, Parameter doublingTimeParameter, Type units) {
75
76 super(name);
77
78 this.model = model;
79 if (model == 0) {
80 hostDemographic = new ConstantPopulation(units);
81 } else if (model == 1) {
82 hostDemographic = new TransmissionExponentialGrowth(units);
83 } else if (model == 2) {
84 hostDemographic = new TransmissionLogisticGrowth(units);
85 }
86
87 if (N0Parameter != null) {
88 this.N0Parameter = N0Parameter;
89 addVariable(N0Parameter);
90 N0Parameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1));
91 }
92
93 if (N1Parameter != null) {
94 this.N1Parameter = N1Parameter;
95 addVariable(N1Parameter);
96 N1Parameter.addBounds(new Parameter.DefaultBounds(1.0, 0.0, 1));
97 }
98
99 if (growthRateParameter != null) {
100 this.growthRateParameter = growthRateParameter;
101 addVariable(growthRateParameter);
102 growthRateParameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1));
103 }
104
105 if (doublingTimeParameter != null) {
106 this.doublingTimeParameter = doublingTimeParameter;
107 addVariable(doublingTimeParameter);
108 doublingTimeParameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1));
109 }
110
111 setUnits(units);
112 }
113
114 public DemographicFunction getDemographicFunction() {
115 throw new RuntimeException("getDemographicFunction not used for TransmissionDemographicModel");
116 }
117
118 protected int getIndexFromHost(int host) {
119 return 0;
120 }
121
122 // Attempting to get the transmission model to be host-specific
123 public DemographicFunction getDemographicFunction(double transmissionTime, double donorSize, int host) {
124
125 final int index = getIndexFromHost(host);
126
127 if (model == 0) {
128 // constant
129 double N0 = N0Parameter.getParameterValue(index);
130 ((ConstantPopulation) hostDemographic).setN0(N0);
131
132 } else if (model == 1) {
133 // exponential
134 double N1 = N1Parameter.getParameterValue(index);
135 ((TransmissionDemographicFunction) hostDemographic).setTransmissionTime(transmissionTime);
136 ((TransmissionDemographicFunction) hostDemographic).setDonorSize(donorSize);
137 ((TransmissionDemographicFunction) hostDemographic).setBottleNeckProportion(N1);
138 if (growthRateParameter != null) {
139 double r = growthRateParameter.getParameterValue(index);
140 ((TransmissionExponentialGrowth) hostDemographic).setGrowthRate(r);
141 } else {
142 double d = doublingTimeParameter.getParameterValue(index);
143 ((TransmissionExponentialGrowth) hostDemographic).setDoublingTime(d);
144 }
145
146 } else if (model == 2) {
147 // logistic
148 ((TransmissionDemographicFunction) hostDemographic).setTransmissionTime(transmissionTime);
149 ((TransmissionDemographicFunction) hostDemographic).setDonorSize(donorSize);
150
151 double N0 = N0Parameter.getParameterValue(index);
152 ((TransmissionLogisticGrowth) hostDemographic).setN0(N0);
153 double N1 = N1Parameter.getParameterValue(index);
154 ((TransmissionDemographicFunction) hostDemographic).setBottleNeckProportion(N1);
155 if (growthRateParameter != null) {
156 double r = growthRateParameter.getParameterValue(index);
157 ((TransmissionLogisticGrowth) hostDemographic).setGrowthRate(r);
158 } else {
159 double d = doublingTimeParameter.getParameterValue(index);
160 ((TransmissionLogisticGrowth) hostDemographic).setDoublingTime(d);
161 }
162
163 }
164
165 return hostDemographic;
166 }
167
168 /**
169 * Parses an element from an DOM document into a ExponentialGrowth.
170 */
171 public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
172
173 public String getParserName() {
174 return TRANSMISSION_MODEL;
175 }
176
177 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
178
179 Type units = XMLUnits.Utils.getUnitsAttr(xo);
180
181 int model = 0;
182
183 Parameter N0Param = null;
184 Parameter N1Param = null;
185 Parameter rParam = null;
186 Parameter dParam = null;
187
188 if (xo.hasChildNamed(CONSTANT)) {
189
190 XMLObject cxo = (XMLObject) xo.getChild(CONSTANT);
191
192 N0Param = (Parameter) cxo.getElementFirstChild(POPULATION_SIZE);
193 model = 0;
194
195 } else if (xo.hasChildNamed(EXPONENTIAL)) {
196
197 XMLObject cxo = (XMLObject) xo.getChild(EXPONENTIAL);
198
199 N1Param = (Parameter) cxo.getElementFirstChild(ANCESTRAL_PROPORTION);
200 if (cxo.hasChildNamed(GROWTH_RATE)) {
201 rParam = (Parameter) cxo.getElementFirstChild(GROWTH_RATE);
202 } else {
203 dParam = (Parameter) cxo.getElementFirstChild(DOUBLING_TIME);
204 }
205 model = 1;
206
207 } else if (xo.hasChildNamed(LOGISTIC)) {
208
209 XMLObject cxo = (XMLObject) xo.getChild(LOGISTIC);
210
211 N0Param = (Parameter) cxo.getElementFirstChild(POPULATION_SIZE);
212 N1Param = (Parameter) cxo.getElementFirstChild(ANCESTRAL_PROPORTION);
213
214 if (cxo.hasChildNamed(GROWTH_RATE)) {
215 rParam = (Parameter) cxo.getElementFirstChild(GROWTH_RATE);
216 } else {
217 dParam = (Parameter) cxo.getElementFirstChild(DOUBLING_TIME);
218 }
219 model = 2;
220 }
221
222 return new TransmissionDemographicModel(model, N0Param, N1Param, rParam, dParam, units);
223 }
224
225 //************************************************************************
226 // AbstractXMLObjectParser implementation
227 //************************************************************************
228
229 public String getParserDescription() {
230 return "A SiteModel that has a gamma distributed rates across sites";
231 }
232
233 public Class getReturnType() {
234 return TransmissionDemographicModel.class;
235 }
236
237 public XMLSyntaxRule[] getSyntaxRules() {
238 return rules;
239 }
240
241 private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{
242 XMLUnits.UNITS_RULE,
243 new XORRule(
244 new ElementRule(CONSTANT,
245 new XMLSyntaxRule[]{
246 new ElementRule(POPULATION_SIZE,
247 new XMLSyntaxRule[]{new ElementRule(Parameter.class)},
248 "This parameter represents the carrying capacity (maximum population size). " +
249 "If the shape is very large then the current day population size will be very close to the carrying capacity."),
250 }
251 ),
252 new XORRule(
253 new ElementRule(EXPONENTIAL,
254 new XMLSyntaxRule[]{
255 new XORRule(
256 new ElementRule(GROWTH_RATE,
257 new XMLSyntaxRule[]{new ElementRule(Parameter.class)},
258 "This parameter determines the rate of growth during the exponential phase. See exponentialGrowth for details."),
259 new ElementRule(DOUBLING_TIME,
260 new XMLSyntaxRule[]{new ElementRule(Parameter.class)},
261 "This parameter determines the doubling time at peak growth rate.")),
262 new ElementRule(ANCESTRAL_PROPORTION,
263 new XMLSyntaxRule[]{new ElementRule(Parameter.class)},
264 "This parameter determines the populaation size at transmission.")
265 }
266 ),
267 new ElementRule(LOGISTIC,
268 new XMLSyntaxRule[]{
269 new ElementRule(POPULATION_SIZE,
270 new XMLSyntaxRule[]{new ElementRule(Parameter.class)},
271 "This parameter represents the carrying capacity (maximum population size). " +
272 "If the shape is very large then the current day population size will be very close to the carrying capacity."),
273 new XORRule(
274 new ElementRule(GROWTH_RATE,
275 new XMLSyntaxRule[]{new ElementRule(Parameter.class)},
276 "This parameter determines the rate of growth during the exponential phase. See exponentialGrowth for details."),
277 new ElementRule(DOUBLING_TIME,
278 new XMLSyntaxRule[]{new ElementRule(Parameter.class)},
279 "This parameter determines the doubling time at peak growth rate.")),
280 new ElementRule(ANCESTRAL_PROPORTION,
281 new XMLSyntaxRule[]{new ElementRule(Parameter.class)},
282 "This parameter determines the populaation size at transmission.")
283 }
284 )
285 )
286 )
287 };
288
289 };
290
291 private abstract class TransmissionDemographicFunction extends DemographicFunction.Abstract {
292 public TransmissionDemographicFunction(Type units) {
293 super(units);
294 }
295
296 public void setDonorSize(double donorSize) {
297 this.donorSize = donorSize;
298 }
299
300 public void setTransmissionTime(double transmissionTime) {
301 this.transmissionTime = transmissionTime;
302 }
303
304 public abstract void setBottleNeckProportion(double prop);
305
306 protected double transmissionTime;
307 protected double donorSize;
308 }
309
310 private class TransmissionExponentialGrowth extends TransmissionDemographicFunction {
311
312 public TransmissionExponentialGrowth(Type units) {
313 super(units);
314 }
315
316 public void setGrowthRate(double r) {
317 this.r = r;
318 }
319
320 public void setDoublingTime(double doublingTime) {
321 setGrowthRate(Math.log(2) / doublingTime);
322 }
323
324 public void setBottleNeckProportion(double prop) {
325 this.N1 = prop * donorSize;
326 }
327
328 // Implementation of abstract methods
329
330 public double getDemographic(double t) {
331
332 return N1 * Math.exp(-r * (t - transmissionTime));
333 }
334
335 public double getIntegral(double start, double finish) {
336
337 double t1 = start - transmissionTime;
338 double t2 = finish - transmissionTime;
339
340 double integral = ((Math.exp(t2 * r) - 1.0) / N1 / r) - ((Math.exp(t1 * r) - 1.0) / N1 / r);
341
342 return integral;
343 }
344
345 public double getIntensity(double t) {
346 throw new RuntimeException("Function not used");
347 }
348
349 public double getInverseIntensity(double x) {
350 throw new RuntimeException("Function not used");
351 }
352
353 public int getNumArguments() {
354 throw new RuntimeException("Function not used");
355 }
356
357 public String getArgumentName(int n) {
358 throw new RuntimeException("Function not used");
359 }
360
361 public double getArgument(int n) {
362 throw new RuntimeException("Function not used");
363 }
364
365 public void setArgument(int n, double value) {
366 throw new RuntimeException("Function not used");
367 }
368
369 public double getLowerBound(int n) {
370 throw new RuntimeException("Function not used");
371 }
372
373 public double getUpperBound(int n) {
374 throw new RuntimeException("Function not used");
375 }
376
377 public DemographicFunction getCopy() {
378 TransmissionExponentialGrowth t = new TransmissionExponentialGrowth(getUnits());
379 t.r = r;
380 t.N1 = N1;
381 t.transmissionTime = transmissionTime;
382 t.donorSize = donorSize;
383
384 return t;
385 }
386
387 private double r;
388 private double N1;
389 }
390
391 private class TransmissionLogisticGrowth extends TransmissionDemographicFunction {
392
393 public TransmissionLogisticGrowth(Type units) {
394 super(units);
395 }
396
397 public void setGrowthRate(double r) {
398 this.r = r;
399 }
400
401 public void setDoublingTime(double doublingTime) {
402 setGrowthRate(Math.log(2) / doublingTime);
403 }
404
405 public void setBottleNeckProportion(double prop) {
406 this.N1 = prop * donorSize;
407 }
408
409 public void setN0(double N0) {
410 this.N0 = N0;
411 }
412
413 public double getN0() {
414 return N0;
415 }
416
417 // Implementation of abstract methods
418
419 public double getDemographic(double t) {
420
421 double common = Math.exp(-r * (t - transmissionTime - (Math.log(1.0 / ((N0 / N1) - 1.0)) / r)));
422 return (N0 * common) / (1.0 + common);
423 }
424
425 public double getIntegral(double start, double finish) {
426
427 double g = finish - start;
428 double t = start - transmissionTime - (Math.log(1.0 / ((N0 / N1) - 1.0)) / r);
429
430 double eMinusRT = Math.exp(-r * t);
431 double eMinusRG = Math.exp(-r * g);
432
433 double integral = (g / N0) + ((1 - eMinusRG) / (N0 * eMinusRT * r * eMinusRG));
434
435 // double integral2 = getNumericalIntegral(start, finish);
436 // if (Math.abs(integral - integral2) > 1E-8) {
437 // System.err.println("Numerical integration failed: " + integral + " (truth=" + integral2 + ")");
438 // //throw new RuntimeException("Numerical integration failed: " + integral + " (truth=" + integral2 + ")");
439 // }
440
441 return integral;
442 }
443
444 public double getIntensity(double t) {
445 throw new RuntimeException("Function not used");
446 }
447
448 public double getInverseIntensity(double x) {
449 throw new RuntimeException("Function not used");
450 }
451
452 public int getNumArguments() {
453 throw new RuntimeException("Function not used");
454 }
455
456 public String getArgumentName(int n) {
457 throw new RuntimeException("Function not used");
458 }
459
460 public double getArgument(int n) {
461 throw new RuntimeException("Function not used");
462 }
463
464 public void setArgument(int n, double value) {
465 throw new RuntimeException("Function not used");
466 }
467
468 public double getLowerBound(int n) {
469 throw new RuntimeException("Function not used");
470 }
471
472 public double getUpperBound(int n) {
473 throw new RuntimeException("Function not used");
474 }
475
476 public DemographicFunction getCopy() {
477 TransmissionLogisticGrowth t = new TransmissionLogisticGrowth(getUnits());
478 t.N0 = N0;
479 t.r = r;
480 t.N1 = N1;
481 t.transmissionTime = transmissionTime;
482 t.donorSize = donorSize;
483
484 return t;
485 }
486
487 private double N0;
488 private double r;
489 private double N1;
490 }
491
492 //
493 // protected stuff
494 //
495
496 Parameter N0Parameter = null;
497 Parameter N1Parameter = null;
498 Parameter growthRateParameter = null;
499 Parameter doublingTimeParameter = null;
500 DemographicFunction hostDemographic = null;
501
502 int model = 0;
503 }