Debian Codegraph @

src/dr/evomodelxml/tree/StarTreeModelParser.java

raw symbols SCIP
1 /*
2 * StarTreeModelParser.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.evomodel.tree.StarTreeModel;
32 import dr.evomodel.tree.TreeModel;
33 import dr.inference.model.Parameter;
34 import dr.inference.model.ParameterParser;
35 import dr.xml.*;
36
37 import java.util.logging.Logger;
38
39 /**
40 * @author Marc Suchard
41 */
42 public class StarTreeModelParser extends AbstractXMLObjectParser {
43
44 public static final String STAR_TREE_MODEL = "starTreeModel";
45
46 public static final String SHARE_ROOT = "sharedRootHeight";
47
48 public static final String ROOT_HEIGHT = "rootHeight";
49 public static final String LEAF_HEIGHT = "leafHeight";
50 public static final String LEAF_TRAIT = "leafTrait";
51
52 // public static final String NODE_HEIGHTS = "nodeHeights";
53 public static final String NODE_RATES = "nodeRates";
54 public static final String NODE_TRAITS = "nodeTraits";
55 public static final String MULTIVARIATE_TRAIT = "traitDimension";
56 public static final String INITIAL_VALUE = "initialValue";
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 public static final String FIRE_TREE_EVENTS = "fireTreeEvents";
62
63 public static final String TAXON = "taxon";
64 public static final String NAME = "name";
65
66 public StarTreeModelParser() {
67 rules = new XMLSyntaxRule[]{
68 new ElementRule(Tree.class),
69 new XORRule(
70 new ElementRule(ROOT_HEIGHT, Parameter.class, "A parameter definition with id only (cannot be a reference!)", false),
71 new ElementRule(SHARE_ROOT, TreeModel.class)
72 ),
73 new ElementRule(LEAF_HEIGHT,
74 new XMLSyntaxRule[]{
75 AttributeRule.newStringRule(TAXON, false, "The name of the taxon for the leaf"),
76 new ElementRule(Parameter.class, "A parameter definition with id only (cannot be a reference!)")
77 }, 0, Integer.MAX_VALUE),
78 new ElementRule(LEAF_TRAIT,
79 new XMLSyntaxRule[]{
80 AttributeRule.newStringRule(TAXON, false, "The name of the taxon for the leaf"),
81 AttributeRule.newStringRule(NAME, false, "The name of the trait attribute in the taxa"),
82 new ElementRule(Parameter.class, "A parameter definition with id only (cannot be a reference!)")
83 }, 0, Integer.MAX_VALUE)
84 };
85 }
86
87 public String getParserName() {
88 return STAR_TREE_MODEL;
89 }
90
91 /**
92 * @return a tree object based on the XML element it was passed.
93 */
94 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
95
96 Tree tree = (Tree) xo.getChild(Tree.class);
97 StarTreeModel treeModel = new StarTreeModel(xo.getId(), tree);
98
99 Logger.getLogger("dr.evomodel").info("Creating a star tree model, '" + xo.getId() + "'");
100
101 for (int i = 0; i < xo.getChildCount(); i++) {
102 if (xo.getChild(i) instanceof XMLObject) {
103
104 XMLObject cxo = (XMLObject) xo.getChild(i);
105
106 if (cxo.getName().equals(ROOT_HEIGHT)) {
107
108 if (cxo.getRawChild(0) instanceof Reference) {
109 // Co-opt existing parameter
110 // treeModel.setRootHeightParameter((Parameter) cxo.getChild(Parameter.class));
111 throw new XMLParseException("Can not provide idref to a new root height parameter");
112 } else {
113 ParameterParser.replaceParameter(cxo, treeModel.getRootHeightParameter());
114 }
115
116 } else if (cxo.getName().equals(SHARE_ROOT)) {
117 TreeModel sharedRoot = (TreeModel) cxo.getChild(TreeModel.class);
118 treeModel.setSharedRootHeightParameter(sharedRoot);
119 } else if (cxo.getName().equals(LEAF_HEIGHT)) {
120
121 String taxonName;
122 if (cxo.hasAttribute(TAXON)) {
123 taxonName = cxo.getStringAttribute(TAXON);
124 } else {
125 throw new XMLParseException("taxa element missing from leafHeight element in treeModel element");
126 }
127
128 int index = treeModel.getTaxonIndex(taxonName);
129 if (index == -1) {
130 throw new XMLParseException("taxon " + taxonName + " not found for leafHeight element in treeModel element");
131 }
132 NodeRef node = treeModel.getExternalNode(index);
133 ParameterParser.replaceParameter(cxo, treeModel.getLeafHeightParameter(node));
134
135 // } else if (cxo.getName().equals(NODE_HEIGHTS)) {
136 //
137 // boolean rootNode = cxo.getAttribute(ROOT_NODE, false);
138 // boolean internalNodes = cxo.getAttribute(INTERNAL_NODES, false);
139 // boolean leafNodes = cxo.getAttribute(LEAF_NODES, false);
140 //
141 // if (!rootNode && !internalNodes && !leafNodes) {
142 // throw new XMLParseException("one or more of root, internal or leaf nodes must be selected for the nodeHeights element");
143 // }
144 //
145 // ParameterParser.replaceParameter(cxo, treeModel.createNodeHeightsParameter(rootNode, internalNodes, leafNodes));
146 //
147 // } else if (cxo.getName().equals(NODE_RATES)) {
148 //
149 // boolean rootNode = cxo.getAttribute(ROOT_NODE, false);
150 // boolean internalNodes = cxo.getAttribute(INTERNAL_NODES, false);
151 // boolean leafNodes = cxo.getAttribute(LEAF_NODES, false);
152 // double[] initialValues = null;
153 //
154 // if (cxo.hasAttribute(INITIAL_VALUE)) {
155 // initialValues = cxo.getDoubleArrayAttribute(INITIAL_VALUE);
156 // }
157 //
158 // if (!rootNode && !internalNodes && !leafNodes) {
159 // throw new XMLParseException("one or more of root, internal or leaf nodes must be selected for the nodeRates element");
160 // }
161 //
162 // ParameterParser.replaceParameter(cxo, treeModel.createNodeRatesParameter(initialValues, rootNode, internalNodes, leafNodes));
163 //
164 // } else if (cxo.getName().equals(NODE_TRAITS)) {
165 //
166 // boolean rootNode = cxo.getAttribute(ROOT_NODE, false);
167 // boolean internalNodes = cxo.getAttribute(INTERNAL_NODES, false);
168 // boolean leafNodes = cxo.getAttribute(LEAF_NODES, false);
169 // boolean fireTreeEvents = cxo.getAttribute(FIRE_TREE_EVENTS, false);
170 // String name = cxo.getAttribute(NAME, "trait");
171 // int dim = cxo.getAttribute(MULTIVARIATE_TRAIT, 1);
172 //
173 // double[] initialValues = null;
174 // if (cxo.hasAttribute(INITIAL_VALUE)) {
175 // initialValues = cxo.getDoubleArrayAttribute(INITIAL_VALUE);
176 // }
177 //
178 // if (!rootNode && !internalNodes && !leafNodes) {
179 // throw new XMLParseException("one or more of root, internal or leaf nodes must be selected for the nodeTraits element");
180 // }
181 //
182 // ParameterParser.replaceParameter(cxo, treeModel.createNodeTraitsParameter(name, dim, initialValues, rootNode, internalNodes, leafNodes, fireTreeEvents));
183
184 } else if (cxo.getName().equals(LEAF_TRAIT)) {
185
186 String name = cxo.getAttribute(NAME, "trait");
187
188 String taxonName;
189 if (cxo.hasAttribute(TAXON)) {
190 taxonName = cxo.getStringAttribute(TAXON);
191 } else {
192 throw new XMLParseException("taxa element missing from leafTrait element in treeModel element");
193 }
194
195 int index = treeModel.getTaxonIndex(taxonName);
196 if (index == -1) {
197 throw new XMLParseException("taxon '" + taxonName + "' not found for leafTrait element in treeModel element");
198 }
199 NodeRef node = treeModel.getExternalNode(index);
200
201 Parameter parameter = treeModel.getNodeTraitParameter(node, name);
202
203 if (parameter == null)
204 throw new XMLParseException("trait '" + name + "' not found for leafTrait (taxon, " + taxonName + ") element in treeModel element");
205
206 ParameterParser.replaceParameter(cxo, parameter);
207
208
209 } else {
210 throw new XMLParseException("illegal child element in " + getParserName() + ": " + cxo.getName());
211 }
212
213 } else if (xo.getChild(i) instanceof Tree) {
214 // do nothing - already handled
215 } else {
216 throw new XMLParseException("illegal child element in " + getParserName() + ": " + xo.getChildName(i) + " " + xo.getChild(i));
217 }
218 }
219
220 Logger.getLogger("dr.evomodel").info(" initial tree topology = " + TreeUtils.uniqueNewick(treeModel, treeModel.getRoot()));
221 Logger.getLogger("dr.evomodel").info(" tree height = " + treeModel.getNodeHeight(treeModel.getRoot()));
222 return treeModel;
223 }
224
225 //************************************************************************
226 // AbstractXMLObjectParser implementation
227 //************************************************************************
228
229 public String getParserDescription() {
230 return "This element represents a model of the tree. The tree model includes and attributes of the nodes " +
231 "including the age (or <i>height</i>) and the rate of evolution at each node in the tree.";
232 }
233
234 public String getExample() {
235 return
236 "<!-- the tree model as special sockets for attaching parameters to various aspects of the tree -->\n" +
237 "<!-- The treeModel below shows the standard setup with a parameter associated with the root height -->\n" +
238 "<!-- a parameter associated with the internal node heights (minus the root height) and -->\n" +
239 "<!-- a parameter associates with all the internal node heights -->\n" +
240 "<!-- Notice that these parameters are overlapping -->\n" +
241 "<!-- The parameters are subsequently used in operators to propose changes to the tree node heights -->\n" +
242 "<treeModel id=\"treeModel1\">\n" +
243 " <tree idref=\"startingTree\"/>\n" +
244 " <rootHeight>\n" +
245 " <parameter id=\"treeModel1.rootHeight\"/>\n" +
246 " </rootHeight>\n" +
247 " <nodeHeights internalNodes=\"true\" rootNode=\"false\">\n" +
248 " <parameter id=\"treeModel1.internalNodeHeights\"/>\n" +
249 " </nodeHeights>\n" +
250 " <nodeHeights internalNodes=\"true\" rootNode=\"true\">\n" +
251 " <parameter id=\"treeModel1.allInternalNodeHeights\"/>\n" +
252 " </nodeHeights>\n" +
253 "</treeModel>";
254
255 }
256
257 public Class getReturnType() {
258 return StarTreeModel.class;
259 }
260
261 public XMLSyntaxRule[] getSyntaxRules() {
262 return rules;
263 }
264
265 private final XMLSyntaxRule[] rules;
266 }