Debian Codegraph @

src/dr/evomodelxml/treelikelihood/TraitLoggerParser.java

raw symbols SCIP
1 /*
2 * StratifiedTraitLoggerParser.java
3 *
4 * Copyright (c) 2002-2016 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.treelikelihood;
27
28 import dr.evolution.tree.Tree;
29 import dr.evolution.tree.TreeTrait;
30 import dr.evolution.tree.TreeTraitProvider;
31 import dr.evomodel.treelikelihood.utilities.TreeTraitLogger;
32 import dr.xml.*;
33
34 /**
35 * @author Marc A. Suchard
36 */
37
38 public class TraitLoggerParser extends AbstractXMLObjectParser {
39
40 public static final String PARSER_NAME = "traitLogger";
41 public static final String TRAIT_NAME = "traitName";
42 private static final String NODES = "nodes";
43
44 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
45
46 Tree treeModel = (Tree) xo.getChild(Tree.class);
47 TreeTrait trait = parseTreeTrait(xo, false);
48 TreeTraitLogger.NodeRestriction nodes = TreeTraitLogger.NodeRestriction.parse(
49 xo.getAttribute(NODES, "all"));
50
51 return new TreeTraitLogger(treeModel, new TreeTrait[] { trait }, nodes);
52 }
53
54 static TreeTrait parseTreeTrait(XMLObject xo, boolean wholeTreeOnly) throws XMLParseException {
55
56 TreeTraitProvider traitProvider =
57 (TreeTraitProvider) xo.getChild(TreeTraitProvider.class);
58
59 String traitName = (String) xo.getAttribute(TRAIT_NAME);
60
61 TreeTrait trait = traitProvider.getTreeTrait(traitName);
62
63 if (trait == null || (wholeTreeOnly && trait.getIntent() != TreeTrait.Intent.WHOLE_TREE)) {
64
65 StringBuilder sb = new StringBuilder("Unable to find ");
66
67 if (wholeTreeOnly) {
68 sb.append("whole ");
69 }
70
71 String id = xo.hasId() ? xo.getId() : "null";
72
73 sb.append("tree trait '").append(traitName).append("' in '").append(id).append("\n");
74
75 sb.append("\tPossible traits:");
76 for (TreeTrait existingTrait : traitProvider.getTreeTraits()) {
77 if (!wholeTreeOnly || existingTrait.getIntent() == TreeTrait.Intent.WHOLE_TREE) {
78 sb.append(" ").append(existingTrait.getTraitName());
79 }
80 }
81 sb.append("\n");
82
83 throw new XMLParseException(sb.toString());
84 }
85
86 return trait;
87 }
88
89 private static final XMLSyntaxRule[] rules = new XMLSyntaxRule[]{
90 AttributeRule.newStringRule(TRAIT_NAME),
91 AttributeRule.newStringRule(NODES, true),
92 new ElementRule(Tree.class),
93 new ElementRule(TreeTraitProvider.class),
94 };
95
96 public XMLSyntaxRule[] getSyntaxRules() {
97 return rules;
98 }
99
100 public String getParserDescription() {
101 return "A parser to log tree traits";
102 }
103
104 public Class getReturnType() {
105 return TreeTraitLogger.class;
106 }
107
108 public String getParserName() {
109 return PARSER_NAME;
110 }
111 }