Debian Codegraph @

src/dr/evomodelxml/tree/TreeModelParser.java

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