Debian Codegraph @

src/dr/evomodelxml/tree/AncestralTraitTreeModelParser.java

raw symbols SCIP
1 /*
2 * TransformedTreeModelParser.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.MutableTreeModel;
29 import dr.evolution.tree.NodeRef;
30 import dr.evolution.tree.Tree;
31 import dr.evolution.tree.TreeUtils;
32 import dr.evolution.util.Taxa;
33 import dr.evolution.util.Taxon;
34 import dr.evolution.util.TaxonList;
35 import dr.evomodel.continuous.AncestralTaxonInTree;
36 import dr.evomodel.tree.*;
37 import dr.inference.model.FastMatrixParameter;
38 import dr.inference.model.Parameter;
39 import dr.inference.model.ParameterParser;
40 import dr.xml.*;
41
42 import java.util.ArrayList;
43 import java.util.List;
44
45 import static dr.evomodelxml.tree.TreeModelParser.*;
46
47 /**
48 * @author Marc Suchard
49 */
50 public class AncestralTraitTreeModelParser extends AbstractXMLObjectParser {
51
52 public static final String ANCESTRAL_TRAIT_TREE_MODEL = "ancestralTraitTreeModel";
53 // public static final String PSEUDO_BRANCH_LENGTH_NAME = "pseudoBranchLengthName";
54 public static final String ANCESTOR = "ancestor";
55
56 public String getParserName() {
57 return ANCESTRAL_TRAIT_TREE_MODEL;
58 }
59
60 /**
61 * @return a tree object based on the XML element it was passed.
62 */
63 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
64
65 MutableTreeModel tree = (MutableTreeModel) xo.getChild(MutableTreeModel.class);
66 List<AncestralTaxonInTree> ancestors = parseAllAncestors(tree, xo);
67
68 int index = tree.getExternalNodeCount();
69 for (AncestralTaxonInTree ancestor : ancestors) {
70 ancestor.setIndex(index);
71 ancestor.setNode(new NodeRef() {
72 @Override
73 public int getNumber() {
74 return 0;
75 }
76
77 @Override
78 public void setNumber(int n) {
79
80 }
81 });
82 }
83
84 // if (xo.hasChildNamed())
85
86 if (xo.hasChildNamed(NODE_TRAITS)) {
87 for (XMLObject cxo : xo.getAllChildren(NODE_TRAITS)) {
88 parseNodeTraits(cxo, tree, ancestors);
89 }
90 }
91
92 return new AncestralTraitTreeModel(xo.getId(), tree, ancestors);
93 }
94
95 private static void parseNodeTraits(XMLObject cxo, Tree tree, List<AncestralTaxonInTree> ancestors)
96 throws XMLParseException {
97
98 // boolean rootNode = cxo.getAttribute(ROOT_NODE, false);
99 // boolean internalNodes = cxo.getAttribute(INTERNAL_NODES, false);
100 // boolean leafNodes = cxo.getAttribute(LEAF_NODES, false);
101 // boolean fireTreeEvents = cxo.getAttribute(FIRE_TREE_EVENTS, false);
102 // boolean asMatrix = cxo.getAttribute(AS_MATRIX, false);
103 String name = cxo.getAttribute(NAME, "trait");
104 int dim = cxo.getAttribute(MULTIVARIATE_TRAIT, 1);
105
106 double[] initialValues = null;
107 if (cxo.hasAttribute(INITIAL_VALUE)) {
108 initialValues = cxo.getDoubleArrayAttribute(INITIAL_VALUE);
109 }
110
111 // if (!rootNode && !internalNodes && !leafNodes) {
112 // throw new XMLParseException("one or more of root, internal or leaf nodes must be selected for the nodeTraits element");
113 // }
114
115 final int rowDim = dim;
116 final int colDim = tree.getExternalNodeCount() + ancestors.size();
117
118 FastMatrixParameter parameter = new FastMatrixParameter(name, rowDim, colDim, 0.0);
119 parameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY,
120 rowDim * colDim));
121
122 int parameterIndex = 0;
123 for (int i = 0; i < tree.getExternalNodeCount(); i++) {
124 // if (leafNodes) {
125 // nodes[i].addTraitParameter(name, parameter.getParameter(parameterIndex), initialValues, firesTreeEvents);
126 ++parameterIndex;
127 // }
128 }
129
130 for (AncestralTaxonInTree ancestor : ancestors) {
131 ++parameterIndex;
132 }
133
134
135 ParameterParser.replaceParameter(cxo, parameter);
136 }
137
138 private static List<AncestralTaxonInTree> parseAllAncestors(MutableTreeModel tree, XMLObject xo) throws XMLParseException {
139 int index = tree.getExternalNodeCount();
140 List<AncestralTaxonInTree> ancestors = new ArrayList<AncestralTaxonInTree>();
141 for (XMLObject cxo : xo.getAllChildren(ANCESTOR)) {
142 ancestors.add(parseAncestor(tree, cxo, index));
143 ++index;
144 }
145 return ancestors;
146 }
147
148 private static AncestralTaxonInTree parseAncestor(MutableTreeModel tree, XMLObject xo, final int index) throws XMLParseException {
149
150 Taxon ancestor = (Taxon) xo.getChild(Taxon.class);
151
152 TaxonList descendants = MonophylyStatisticParser.parseTaxonListOrTaxa(
153 xo.getChild(MonophylyStatisticParser.MRCA));
154
155 Parameter pseudoBranchLength = (Parameter) xo.getChild(Parameter.class);
156
157 AncestralTaxonInTree ancestorInTree;
158 try {
159
160 NodeRef node = new NodeRef() {
161 @Override
162 public int getNumber() {
163 return index;
164 }
165
166 @Override
167 public void setNumber(int n) {
168 throw new RuntimeException("Do not set");
169 }
170 };
171
172 ancestorInTree = new AncestralTaxonInTree(ancestor, tree, descendants, pseudoBranchLength,
173 node, index);
174 } catch (TreeUtils.MissingTaxonException e) {
175 throw new XMLParseException("Unable to find taxa for " + ancestor.getId());
176 }
177
178 return ancestorInTree;
179 }
180
181 //************************************************************************
182 // AbstractXMLObjectParser implementation
183 //************************************************************************
184
185 public String getParserDescription() {
186 return "This element represents a transformed model of the tree.";
187 }
188
189 public Class getReturnType() {
190 return TransformedTreeModel.class;
191 }
192
193 public XMLSyntaxRule[] getSyntaxRules() {
194 return rules;
195 }
196
197 private final XMLSyntaxRule[] rules =
198 new XMLSyntaxRule[]{
199 new ElementRule(MutableTreeModel.class),
200 // AttributeRule.newStringRule(PSEUDO_BRANCH_LENGTH_NAME),
201 new ElementRule(ANCESTOR, new XMLSyntaxRule[] {
202 new ElementRule(Taxon.class),
203 new ElementRule(Parameter.class),
204 new ElementRule(MonophylyStatisticParser.MRCA, new XMLSyntaxRule[]{
205 new XORRule(
206 new ElementRule(Taxon.class, 1, Integer.MAX_VALUE),
207 new ElementRule(Taxa.class)
208 )
209 }),
210 }, 0, Integer.MAX_VALUE),
211 nodeTraitsRule,
212 };
213 }