Debian Codegraph @

src/dr/evomodel/epidemiology/casetocase/PartitionedTreeModelParser.java

raw symbols SCIP
1 /*
2 * PartitionedTreeModelParser.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.epidemiology.casetocase;
27
28 import dr.evolution.tree.NodeRef;
29 import dr.evolution.tree.Tree;
30 import dr.evolution.tree.TreeUtils;
31 import dr.evolution.util.Date;
32 import dr.evolution.util.Taxon;
33 import dr.evolution.util.TaxonList;
34 import dr.inference.model.CompoundParameter;
35 import dr.inference.model.Parameter;
36 import dr.inference.model.ParameterParser;
37 import dr.xml.*;
38
39 import java.util.logging.Logger;
40
41 /**
42 * For now, this is basically a copy of TreeModelParser. Later, initialisation options for the partitioning should be
43 * moved here
44 *
45 * @author Alexei Drummond
46 * @author Matthew Hall
47 */
48
49 public class PartitionedTreeModelParser extends AbstractXMLObjectParser {
50
51 public static final String ROOT_HEIGHT = "rootHeight";
52 public static final String LEAF_HEIGHT = "leafHeight";
53 public static final String LEAF_TRAIT = "leafTrait";
54
55 public static final String NODE_HEIGHTS = "nodeHeights";
56 public static final String NODE_RATES = "nodeRates";
57 public static final String NODE_TRAITS = "nodeTraits";
58 public static final String MULTIVARIATE_TRAIT = "traitDimension";
59 public static final String INITIAL_VALUE = "initialValue";
60
61 public static final String ROOT_NODE = "rootNode";
62 public static final String INTERNAL_NODES = "internalNodes";
63 public static final String LEAF_NODES = "leafNodes";
64
65 public static final String LEAF_HEIGHTS = "leafHeights";
66
67 public static final String FIRE_TREE_EVENTS = "fireTreeEvents";
68
69 public static final String TAXON = "taxon";
70 public static final String NAME = "name";
71
72 public static final String OUTBREAK = "outbreak";
73 public static final String STARTING_TT_FILE = "startingTransmissionTreeFile";
74
75 public PartitionedTreeModelParser() {
76 rules = new XMLSyntaxRule[]{
77 new ElementRule(Tree.class),
78 new ElementRule(ROOT_HEIGHT, Parameter.class, "A parameter definition with id only (cannot be a reference!)", false),
79 new ElementRule(NODE_HEIGHTS,
80 new XMLSyntaxRule[]{
81 AttributeRule.newBooleanRule(ROOT_NODE, true, "If true the root height is included in the parameter"),
82 AttributeRule.newBooleanRule(INTERNAL_NODES, true, "If true the internal node heights (minus the root) are included in the parameter"),
83 new ElementRule(Parameter.class, "A parameter definition with id only (cannot be a reference!)")
84 }, 1, Integer.MAX_VALUE),
85 new ElementRule(LEAF_HEIGHT,
86 new XMLSyntaxRule[]{
87 AttributeRule.newStringRule(TAXON, false, "The name of the taxon for the leaf"),
88 new ElementRule(Parameter.class, "A parameter definition with id only (cannot be a reference!)")
89 }, 0, Integer.MAX_VALUE),
90 new ElementRule(NODE_TRAITS,
91 new XMLSyntaxRule[]{
92 AttributeRule.newStringRule(NAME, false, "The name of the trait attribute in the taxa"),
93 AttributeRule.newBooleanRule(ROOT_NODE, true, "If true the root trait is included in the parameter"),
94 AttributeRule.newBooleanRule(INTERNAL_NODES, true, "If true the internal node traits (minus the root) are included in the parameter"),
95 AttributeRule.newBooleanRule(LEAF_NODES, true, "If true the leaf node traits are included in the parameter"),
96 AttributeRule.newIntegerRule(MULTIVARIATE_TRAIT, true, "The number of dimensions (if multivariate)"),
97 AttributeRule.newDoubleRule(INITIAL_VALUE, true, "The initial value(s)"),
98 AttributeRule.newBooleanRule(FIRE_TREE_EVENTS, true, "Whether to fire tree events if the traits change"),
99 new ElementRule(Parameter.class, "A parameter definition with id only (cannot be a reference!)")
100 }, 0, Integer.MAX_VALUE),
101 new ElementRule(NODE_RATES,
102 new XMLSyntaxRule[]{
103 AttributeRule.newBooleanRule(ROOT_NODE, true, "If true the root rate is included in the parameter"),
104 AttributeRule.newBooleanRule(INTERNAL_NODES, true, "If true the internal node rate (minus the root) are included in the parameter"),
105 AttributeRule.newBooleanRule(LEAF_NODES, true, "If true the leaf node rate are included in the parameter"),
106 AttributeRule.newDoubleRule(INITIAL_VALUE, true, "The initial value(s)"),
107 new ElementRule(Parameter.class, "A parameter definition with id only (cannot be a reference!)")
108 }, 0, Integer.MAX_VALUE),
109 new ElementRule(LEAF_TRAIT,
110 new XMLSyntaxRule[]{
111 AttributeRule.newStringRule(TAXON, false, "The name of the taxon for the leaf"),
112 AttributeRule.newStringRule(NAME, false, "The name of the trait attribute in the taxa"),
113 new ElementRule(Parameter.class, "A parameter definition with id only (cannot be a reference!)")
114 }, 0, Integer.MAX_VALUE),
115 new ElementRule(LEAF_HEIGHTS,
116 new XMLSyntaxRule[]{
117 new ElementRule(TaxonList.class, "A set of taxa for which leaf heights are required"),
118 new ElementRule(Parameter.class, "A compound parameter containing the leaf heights")
119 }, true),
120 new ElementRule(OUTBREAK, AbstractOutbreak.class, "The case data"),
121 AttributeRule.newStringRule(STARTING_TT_FILE, true)
122 };
123 }
124
125 public String getParserName() {
126 return PartitionedTreeModel.PARTITIONED_TREE_MODEL;
127 }
128
129 /**
130 * @return a tree object based on the XML element it was passed.
131 */
132 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
133
134 Tree tree = (Tree) xo.getChild(Tree.class);
135
136 AbstractOutbreak outbreak = (AbstractOutbreak)xo.getElementFirstChild(OUTBREAK);
137 PartitionedTreeModel treeModel;
138
139 if(xo.hasAttribute(STARTING_TT_FILE)){
140 treeModel = new PartitionedTreeModel(xo.getId(), tree, outbreak, xo.getStringAttribute(STARTING_TT_FILE));
141 } else {
142 treeModel = new PartitionedTreeModel(xo.getId(), tree, outbreak);
143 }
144
145 Logger.getLogger("dr.evomodel").info("Creating the partitioned tree model, '" + xo.getId() + "'");
146
147 for (int i = 0; i < xo.getChildCount(); i++) {
148 if (xo.getChild(i) instanceof XMLObject) {
149
150 XMLObject cxo = (XMLObject) xo.getChild(i);
151
152 if (cxo.getName().equals(ROOT_HEIGHT)) {
153
154 ParameterParser.replaceParameter(cxo, treeModel.getRootHeightParameter());
155
156 } else if (cxo.getName().equals(LEAF_HEIGHT)) {
157
158 String taxonName;
159 if (cxo.hasAttribute(TAXON)) {
160 taxonName = cxo.getStringAttribute(TAXON);
161 } else {
162 throw new XMLParseException("taxa element missing from leafHeight element in treeModel element");
163 }
164
165 int index = treeModel.getTaxonIndex(taxonName);
166 if (index == -1) {
167 throw new XMLParseException("taxon " + taxonName + " not found for leafHeight element in treeModel element");
168 }
169 NodeRef node = treeModel.getExternalNode(index);
170
171 Parameter newParameter = treeModel.getLeafHeightParameter(node);
172
173 ParameterParser.replaceParameter(cxo, newParameter);
174
175 Taxon taxon = treeModel.getTaxon(index);
176
177 setPrecisionBounds(newParameter, taxon);
178
179 } else if (cxo.getName().equals(LEAF_HEIGHTS)) {
180 // get a set of leaf height parameters out as a compound parameter...
181
182 TaxonList taxa = (TaxonList)cxo.getChild(TaxonList.class);
183 Parameter offsetParameter = (Parameter)cxo.getChild(Parameter.class);
184
185 CompoundParameter leafHeights = new CompoundParameter("leafHeights");
186 for (Taxon taxon : taxa) {
187 int index = treeModel.getTaxonIndex(taxon);
188 if (index == -1) {
189 throw new XMLParseException("taxon " + taxon.getId() + " not found for leafHeight element in treeModel element");
190 }
191 NodeRef node = treeModel.getExternalNode(index);
192
193 Parameter newParameter = treeModel.getLeafHeightParameter(node);
194
195 leafHeights.addParameter(newParameter);
196
197 setPrecisionBounds(newParameter, taxon);
198 }
199
200 ParameterParser.replaceParameter(cxo, leafHeights);
201
202 } else if (cxo.getName().equals(NODE_HEIGHTS)) {
203
204 boolean rootNode = cxo.getAttribute(ROOT_NODE, false);
205 boolean internalNodes = cxo.getAttribute(INTERNAL_NODES, false);
206 boolean leafNodes = cxo.getAttribute(LEAF_NODES, false);
207
208 if (!rootNode && !internalNodes && !leafNodes) {
209 throw new XMLParseException("one or more of root, internal or leaf nodes must be selected for the nodeHeights element");
210 }
211
212 ParameterParser.replaceParameter(cxo, treeModel.createNodeHeightsParameter(rootNode, internalNodes, leafNodes));
213
214 } else if (cxo.getName().equals(NODE_RATES)) {
215
216 boolean rootNode = cxo.getAttribute(ROOT_NODE, false);
217 boolean internalNodes = cxo.getAttribute(INTERNAL_NODES, false);
218 boolean leafNodes = cxo.getAttribute(LEAF_NODES, false);
219 double[] initialValues = null;
220
221 if (cxo.hasAttribute(INITIAL_VALUE)) {
222 initialValues = cxo.getDoubleArrayAttribute(INITIAL_VALUE);
223 }
224
225 if (!rootNode && !internalNodes && !leafNodes) {
226 throw new XMLParseException("one or more of root, internal or leaf nodes must be selected for the nodeRates element");
227 }
228
229 ParameterParser.replaceParameter(cxo, treeModel.createNodeRatesParameter(initialValues, rootNode, internalNodes, leafNodes));
230
231 } else if (cxo.getName().equals(NODE_TRAITS)) {
232
233 boolean rootNode = cxo.getAttribute(ROOT_NODE, false);
234 boolean internalNodes = cxo.getAttribute(INTERNAL_NODES, false);
235 boolean leafNodes = cxo.getAttribute(LEAF_NODES, false);
236 boolean fireTreeEvents = cxo.getAttribute(FIRE_TREE_EVENTS, false);
237 String name = cxo.getAttribute(NAME, "trait");
238 int dim = cxo.getAttribute(MULTIVARIATE_TRAIT, 1);
239
240 double[] initialValues = null;
241 if (cxo.hasAttribute(INITIAL_VALUE)) {
242 initialValues = cxo.getDoubleArrayAttribute(INITIAL_VALUE);
243 }
244
245 if (!rootNode && !internalNodes && !leafNodes) {
246 throw new XMLParseException("one or more of root, internal or leaf nodes must be selected for the nodeTraits element");
247 }
248
249 ParameterParser.replaceParameter(cxo, treeModel.createNodeTraitsParameter(name, dim, initialValues, rootNode, internalNodes, leafNodes, fireTreeEvents));
250
251 } else if (cxo.getName().equals(LEAF_TRAIT)) {
252
253 String name = cxo.getAttribute(NAME, "trait");
254
255 String taxonName;
256 if (cxo.hasAttribute(TAXON)) {
257 taxonName = cxo.getStringAttribute(TAXON);
258 } else {
259 throw new XMLParseException("taxa element missing from leafTrait element in treeModel element");
260 }
261
262 int index = treeModel.getTaxonIndex(taxonName);
263 if (index == -1) {
264 throw new XMLParseException("taxon '" + taxonName + "' not found for leafTrait element in treeModel element");
265 }
266 NodeRef node = treeModel.getExternalNode(index);
267
268 Parameter parameter = treeModel.getNodeTraitParameter(node, name);
269
270 if (parameter == null)
271 throw new XMLParseException("trait '" + name + "' not found for leafTrait (taxon, " + taxonName + ") element in treeModel element");
272
273 ParameterParser.replaceParameter(cxo, parameter);
274
275 } else {
276 if(!cxo.getName().equals(OUTBREAK)) {
277 throw new XMLParseException("illegal child element in " + getParserName() + ": " + cxo.getName());
278 }
279 }
280
281 } else if (xo.getChild(i) instanceof Tree) {
282 // do nothing - already handled
283 } else {
284 throw new XMLParseException("illegal child element in " + getParserName() + ": " + xo.getChildName(i) + " " + xo.getChild(i));
285 }
286 }
287
288 // AR this is doubling up the number of bounds on each node.
289 // treeModel.setupHeightBounds();
290 //System.err.println("done constructing treeModel");
291
292 Logger.getLogger("dr.evomodel").info(" initial tree topology = " + TreeUtils.uniqueNewick(treeModel, treeModel.getRoot()));
293 Logger.getLogger("dr.evomodel").info(" tree height = " + treeModel.getNodeHeight(treeModel.getRoot()));
294 return treeModel;
295 }
296
297 private void setPrecisionBounds(Parameter newParameter, Taxon taxon) {
298 Date date = taxon.getDate();
299 if (date != null) {
300 double precision = date.getUncertainty();
301 if (precision > 0.0) {
302 // taxon date not specified to exact value so add appropriate bounds
303 double upper = Taxon.getHeightFromDate(date);
304 double lower = Taxon.getHeightFromDate(date);
305 if (date.isBackwards()) {
306 upper += precision;
307 } else {
308 lower -= precision;
309 }
310
311 // set the bounds for the given precision
312 newParameter.addBounds(new Parameter.DefaultBounds(upper, lower, 1));
313
314 // set the initial value to be mid-point
315 newParameter.setParameterValue(0, (upper + lower) / 2);
316 }
317 }
318 }
319
320 //************************************************************************
321 // AbstractXMLObjectParser implementation
322 //************************************************************************
323
324 public String getParserDescription() {
325 return "This element represents a model of a phylogenetic tree together with the partitioning of its nodes " +
326 "into connected subgraphs to represent the transmission tree.";
327 }
328
329 public Class getReturnType() {
330 return PartitionedTreeModel.class;
331 }
332
333 public XMLSyntaxRule[] getSyntaxRules() {
334 return rules;
335 }
336
337 private final XMLSyntaxRule[] rules;
338 }