Debian Codegraph @

src/dr/evomodel/transmission/TransmissionStatistic.java

raw symbols SCIP
1 /*
2 * TransmissionStatistic.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.tree.NodeRef;
29 import dr.evolution.tree.Tree;
30 import dr.evolution.util.Taxon;
31 import dr.evomodel.tree.TreeStatistic;
32 import dr.inference.model.BooleanStatistic;
33 import dr.inference.model.Statistic;
34 import dr.xml.*;
35
36 import java.util.HashSet;
37 import java.util.Set;
38
39
40 /**
41 * A statistic for the compatibility of a viruses tree with a transmission
42 * history. The transmission history consists of a number of
43 * hosts with known history of transmission. The viruses tree should have tip
44 * attributes specifying which host they are from (host="").
45 *
46 * @author Andrew Rambaut
47 * @author Alexei Drummond
48 * @version $Id: TransmissionStatistic.java,v 1.11 2005/06/27 21:19:15 rambaut Exp $
49 */
50 public class TransmissionStatistic extends TreeStatistic implements BooleanStatistic {
51
52 // PUBLIC STUFF
53
54 public static final String TRANSMISSION_STATISTIC = "transmissionStatistic";
55
56 public TransmissionStatistic(String name, TransmissionHistoryModel transmissionHistoryModel, Tree virusTree) {
57
58 super(name);
59
60 this.transmissionHistoryModel = transmissionHistoryModel;
61 this.virusTree = virusTree;
62
63 setupHosts();
64 }
65
66 public TransmissionStatistic(String name, Tree hostTree, Tree virusTree) {
67
68 super(name);
69
70 this.hostTree = hostTree;
71 this.virusTree = virusTree;
72
73 setupHosts();
74 }
75
76 private void setupHosts() {
77
78 if (transmissionHistoryModel != null) {
79 hostCount = transmissionHistoryModel.getHostCount();
80
81 } else {
82 hostCount = hostTree.getTaxonCount();
83 }
84
85 donorHost = new int[hostCount];
86 donorHost[0] = -1;
87 transmissionTime = new double[hostCount];
88 transmissionTime[0] = java.lang.Double.POSITIVE_INFINITY;
89
90 if (transmissionHistoryModel != null) {
91 for (int i = 0; i < transmissionHistoryModel.getTransmissionEventCount(); i++)
92 {
93 TransmissionHistoryModel.TransmissionEvent event = transmissionHistoryModel.getTransmissionEvent(i);
94
95 int host1 = transmissionHistoryModel.getHostIndex(event.getDonor());
96 int host2 = transmissionHistoryModel.getHostIndex(event.getRecipient());
97
98 donorHost[host2] = host1;
99 transmissionTime[host2] = event.getTransmissionTime();
100 }
101 } else {
102 setupHostsTree(hostTree.getRoot());
103 }
104 }
105
106 private int setupHostsTree(NodeRef node) {
107
108 int host;
109
110 if (hostTree.isExternal(node)) {
111 host = node.getNumber();
112 } else {
113
114 // This traversal assumes that the first child is the donor
115 // and the second is the recipient
116
117 int host1 = setupHostsTree(hostTree.getChild(node, 0));
118 int host2 = setupHostsTree(hostTree.getChild(node, 1));
119
120 donorHost[host2] = host1;
121 transmissionTime[host2] = hostTree.getNodeHeight(node);
122
123 host = host1;
124 }
125
126 return host;
127 }
128
129 public void setTree(Tree tree) {
130 this.virusTree = tree;
131 }
132
133 public Tree getTree() {
134 return virusTree;
135 }
136
137 public String getDimensionName(int dim) {
138 String recipient = transmissionHistoryModel.getHost(dim).getId();
139
140 String donor = (donorHost[dim] == -1 ? "" : transmissionHistoryModel.getHost(donorHost[dim]).getId() + "->");
141 return "transmission(" + donor + recipient + ")";
142 }
143
144 public int getDimension() {
145 return hostCount;
146 }
147
148 /**
149 * @return boolean result of test.
150 */
151 public double getStatisticValue(int dim) {
152 return getBoolean(dim) ? 1.0 : 0.0;
153 }
154
155 /** /**
156 * @return true if the population tree is compatible with the species tree
157 */
158 public boolean getBoolean(int dim) {
159 Set<Integer> incompatibleSet = new HashSet<Integer>();
160 setupHosts();
161 isCompatible(virusTree.getRoot(), incompatibleSet);
162
163 return !incompatibleSet.contains(dim);
164 }
165
166 private int isCompatible(NodeRef node, Set<Integer> incompatibleSet) {
167
168 double height = virusTree.getNodeHeight(node);
169 int host;
170
171 if (virusTree.isExternal(node)) {
172 Taxon hostTaxon = (Taxon) virusTree.getTaxonAttribute(node.getNumber(), "host");
173 if (transmissionHistoryModel != null) {
174 host = transmissionHistoryModel.getHostIndex(hostTaxon);
175 } else {
176 host = hostTree.getTaxonIndex(hostTaxon);
177 }
178
179 if (host != -1 && height > transmissionTime[host]) {
180 // This means that the sequence was sampled
181 // before the host was infected so we should probably flag
182 // this as an error before we get to this point...
183 throw new RuntimeException("Sequence " + virusTree.getNodeTaxon(node) + ", was sampled ("+height+") before host, " + hostTaxon + ", was infected ("+transmissionTime[host]+")");
184 }
185
186 } else {
187
188 // Tree should be bifurcating...
189 int host1 = isCompatible(virusTree.getChild(node, 0), incompatibleSet);
190
191 int host2 = isCompatible(virusTree.getChild(node, 1), incompatibleSet);
192
193 if (host1 == host2) {
194
195 host = host1;
196 while (height > transmissionTime[host]) {
197 host = donorHost[host];
198 }
199
200 } else {
201 while (height > transmissionTime[host1]) {
202 host1 = donorHost[host1];
203 }
204
205 while (height > transmissionTime[host2]) {
206 host2 = donorHost[host2];
207 }
208
209 if (host1 != host2) {
210 if (transmissionTime[host1] < transmissionTime[host2]) {
211 incompatibleSet.add(host1);
212 host = host2;
213 } else {
214 incompatibleSet.add(host2);
215 host = host1;
216 }
217 } else {
218 host = host1;
219 }
220 }
221 }
222
223 return host;
224 }
225
226 // ****************************************************************
227 // Private and protected stuff
228 // ****************************************************************
229
230 public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
231
232 public String getParserName() {
233 return TRANSMISSION_STATISTIC;
234 }
235
236 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
237
238 String name = xo.getStringAttribute("name");
239 Tree virusTree = (Tree) xo.getElementFirstChild("parasiteTree");
240
241 if (xo.getChild(TransmissionHistoryModel.class) != null) {
242 TransmissionHistoryModel history = (TransmissionHistoryModel) xo.getChild(TransmissionHistoryModel.class);
243 return new TransmissionStatistic(name, history, virusTree);
244 } else {
245 Tree hostTree = (Tree) xo.getElementFirstChild("hostTree");
246 return new TransmissionStatistic(name, hostTree, virusTree);
247 }
248
249 }
250
251 public String getParserDescription() {
252 return "A statistic that returns true if the given parasite tree is compatible with the host tree.";
253 }
254
255 public Class getReturnType() {
256 return Statistic.class;
257 }
258
259 public XMLSyntaxRule[] getSyntaxRules() {
260 return rules;
261 }
262
263 private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{
264 new StringAttributeRule("name", "A name for this statistic for the purpose of logging"),
265 new XORRule(
266 new ElementRule("hostTree",
267 new XMLSyntaxRule[]{new ElementRule(Tree.class)}),
268 new ElementRule(TransmissionHistoryModel.class,
269 "This describes the transmission history of the patients.")
270 ),
271 new ElementRule("parasiteTree",
272 new XMLSyntaxRule[]{new ElementRule(Tree.class)})
273 };
274 };
275
276 /**
277 * The host tree.
278 */
279 private Tree hostTree = null;
280
281 private TransmissionHistoryModel transmissionHistoryModel = null;
282
283 /**
284 * The viruses tree.
285 */
286 private Tree virusTree = null;
287
288 /**
289 * The number of hosts.
290 */
291 private int hostCount;
292
293 /**
294 * The donor host for each recipient host (-1 for initial host).
295 */
296 private int[] donorHost;
297
298 /**
299 * The time of transmission into this host (POSITIVE_INFINITY for initial host).
300 */
301 private double[] transmissionTime;
302 }