Debian Codegraph @

src/dr/evomodel/transmission/TransmissionSimulator.java

raw symbols SCIP
1 /*
2 * TransmissionSimulator.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.Intervals;
29 import dr.evolution.tree.NodeRef;
30 import dr.evolution.tree.Tree;
31 import dr.evolution.util.Taxa;
32 import dr.evolution.util.TaxonList;
33 import dr.evomodel.coalescent.DemographicModel;
34 import dr.xml.*;
35
36
37 /**
38 * A likelihood function for a complete transmission history. Takes a viruses tree
39 * and a demographic model. The transmission history consists of a number of
40 * hosts with known history of transmission. The viruses tree should have tip
41 * attributes specifying which host they are from (host="").
42 *
43 * @version $Id: TransmissionSimulator.java,v 1.5 2005/06/15 17:20:55 rambaut Exp $
44 *
45 * @author Andrew Rambaut
46 * @author Alexei Drummond
47 */
48 public class TransmissionSimulator {
49
50 // PUBLIC STUFF
51
52 public static final String TRANSMISSION_SIMULATOR = "transmissionSimulator";
53 public static final String SOURCE_PATIENT = "sourcePatient";
54
55 public TransmissionSimulator(Taxa taxa, Tree hostTree, DemographicModel demographic)
56 throws TaxonList.MissingTaxonException {
57 this(TRANSMISSION_SIMULATOR, taxa, hostTree, demographic);
58 }
59
60 public TransmissionSimulator(String name, Taxa taxa, Tree hostTree, DemographicModel demographic)
61 throws TaxonList.MissingTaxonException {
62
63 this.hostTree = hostTree;
64
65 setupHosts();
66 }
67
68 private void setupHosts() {
69
70 hostCount = hostTree.getTaxonCount();
71
72 intervals = new Intervals[hostCount];
73 for (int i = 0; i < hostCount; i++) {
74 // 3 times virusTree tip count will be enough events...
75 intervals[i] = new Intervals(virusTree.getExternalNodeCount() * 3);
76 }
77 donorHost = new int[hostCount];
78 donorHost[0] = -1;
79 transmissionTime = new double[hostCount];
80 transmissionTime[0] = Double.POSITIVE_INFINITY;
81
82 setupHosts(hostTree.getRoot());
83 }
84
85 private int setupHosts(NodeRef node) {
86
87 int host;
88
89 if (hostTree.isExternal(node)) {
90 host = node.getNumber();
91 } else {
92
93 // This traversal assumes that the first child is the donor
94 // and the second is the recipient
95
96 int host1 = setupHosts(hostTree.getChild(node, 0));
97 int host2 = setupHosts(hostTree.getChild(node, 1));
98
99 donorHost[host2] = host1;
100 transmissionTime[host2] = hostTree.getNodeHeight(node);
101
102 host = host1;
103 }
104
105 return host;
106 }
107
108 // ****************************************************************
109 // Private and protected stuff
110 // ****************************************************************
111
112 public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
113
114 public String getParserName() { return TRANSMISSION_SIMULATOR; }
115
116 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
117
118 DemographicModel demoModel0 = (DemographicModel)xo.getElementFirstChild(SOURCE_PATIENT);
119 TransmissionDemographicModel demoModel1 = (TransmissionDemographicModel)xo.getChild(TransmissionDemographicModel.class);
120 Tree hostTree = (Tree)xo.getElementFirstChild("hostTree");
121 Tree virusTree = (Tree)xo.getElementFirstChild("parasiteTree");
122
123 TransmissionSimulator simulator = null;
124 // try {
125 // simulator = new TransmissionSimulator(hostTree, virusTree, demoModel0, demoModel1);
126 // } catch (TaxonList.MissingTaxonException e) {
127 // throw new XMLParseException(e.toString());
128 // }
129 return simulator;
130 }
131
132 //************************************************************************
133 // AbstractXMLObjectParser implementation
134 //************************************************************************
135
136 public String getParserDescription() {
137 return "This element represents a simulator for a transmission history.";
138 }
139
140 public Class getReturnType() { return TransmissionSimulator.class; }
141
142 public XMLSyntaxRule[] getSyntaxRules() { return rules; }
143
144 private XMLSyntaxRule[] rules = new XMLSyntaxRule[] {
145 new ElementRule(SOURCE_PATIENT, DemographicModel.class,
146 "This describes the demographic process for the source donor patient."),
147 new ElementRule(TransmissionDemographicModel.class,
148 "This describes the demographic process for the recipient patients."),
149 new ElementRule("hostTree",
150 new XMLSyntaxRule[] { new ElementRule(Tree.class) }),
151 new ElementRule("parasiteTree",
152 new XMLSyntaxRule[] { new ElementRule(Tree.class) })
153 };
154 };
155
156 /** The host tree. */
157 private Tree hostTree = null;
158
159 /** The viruses tree. */
160 private Tree virusTree = null;
161
162 /** The number of hosts. */
163 private int hostCount;
164
165 /** The intervals for each host. */
166 private Intervals[] intervals;
167
168 /** The donor host for each recipient host (-1 for initial host). */
169 private int[] donorHost;
170
171 /** The time of transmission into this host (POSITIVE_INFINITY for initial host). */
172 private double[] transmissionTime;
173
174 }