Debian Codegraph @

src/dr/evomodelxml/tree/AncestralTraitParser.java

raw symbols SCIP
1 /*
2 * AncestralTraitParser.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.Tree;
29 import dr.evolution.tree.TreeTrait;
30 import dr.evolution.tree.TreeTraitProvider;
31 import dr.evolution.tree.TreeUtils;
32 import dr.evolution.util.Taxa;
33 import dr.evolution.util.TaxonList;
34 import dr.evomodel.tree.AncestralTrait;
35 import dr.evomodel.tree.TreeModel;
36 import dr.xml.*;
37
38 /**
39 */
40 public class AncestralTraitParser extends AbstractXMLObjectParser {
41
42 public static final String ANCESTRAL_TRAIT = "ancestralTrait";
43 public static final String ANCESTRAL_STATE = "ancestralState";
44 public static final String NAME = "name";
45 public static final String MRCA = "mrca";
46 public static final String TRAIT_NAME = "traitName";
47 public static final String STATES = "states";
48
49 public String getParserName() {
50 return ANCESTRAL_TRAIT;
51 }
52
53 public String[] getParserNames() {
54 // provide a synonym to maintain backwards compatibility
55 return new String[]{
56 getParserName(), ANCESTRAL_STATE
57 };
58 }
59
60 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
61
62 String traitName = xo.getAttribute(TRAIT_NAME, STATES);
63 String name = xo.getAttribute(NAME, traitName);
64 Tree tree = (Tree) xo.getChild(Tree.class);
65 TreeTraitProvider treeTraitProvider = (TreeTraitProvider) xo.getChild(TreeTraitProvider.class);
66
67 TaxonList taxa = null;
68 if (xo.hasChildNamed(MRCA)) {
69 taxa = (TaxonList) xo.getElementFirstChild(MRCA);
70 }
71
72 TreeTrait trait = treeTraitProvider.getTreeTrait(traitName);
73 if (trait == null) {
74 throw new XMLParseException("A trait called, " + traitName + ", was not available from the TreeTraitProvider supplied to " + getParserName() + (xo.hasId() ? ", with ID " + xo.getId() : ""));
75 }
76 try {
77 return new AncestralTrait(name, trait, tree, taxa);
78 } catch (TreeUtils.MissingTaxonException mte) {
79 throw new XMLParseException("Taxon, " + mte + ", in " + getParserName() + "was not found in the tree.");
80 }
81 }
82
83 //************************************************************************
84 // AbstractXMLObjectParser implementation
85 //************************************************************************
86
87 public String getParserDescription() {
88 return "A statistic that has as its value the height of the most recent common ancestor of a set of taxa in a given tree";
89 }
90
91 public Class getReturnType() {
92 return AncestralTrait.class;
93 }
94
95 public XMLSyntaxRule[] getSyntaxRules() {
96 return rules;
97 }
98
99 private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{
100 new StringAttributeRule(NAME, "A name for this statistic primarily for the purposes of logging", true),
101 new StringAttributeRule(TRAIT_NAME, "The name of the trait to log", true),
102 new ElementRule(TreeModel.class),
103 new ElementRule(TreeTraitProvider.class),
104 new ElementRule(MRCA, new XMLSyntaxRule[]{new ElementRule(Taxa.class)}, "The MRCA to reconstruct the trait at (default root node)", true)
105 };
106 }