Debian Codegraph @

src/dr/evomodelxml/tree/TMRCAStatisticParser.java

raw symbols SCIP
1 /*
2 * TMRCAStatisticParser.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.TreeUtils;
30 import dr.evolution.util.Taxa;
31 import dr.evolution.util.TaxonList;
32 import dr.evomodel.tree.TMRCAStatistic;
33 import dr.inference.model.Statistic;
34 import dr.xml.*;
35
36 /**
37 *
38 * To get the age of the root in absolute time:
39 * <tmrcaStatistic id="age" absolute="true">
40 * <tree idref="tree"/>
41 * </tmrcaStatistic>
42 *
43 * @author Alexei Drummond
44 * @author Andrew Rambaut
45 */
46 public class TMRCAStatisticParser extends AbstractXMLObjectParser {
47
48 public static final String TMRCA_STATISTIC = "tmrcaStatistic";
49 public static final String MRCA = "mrca";
50 public static final String ABSOLUTE = "absolute";
51
52 // The tmrcaStatistic will represent that age of the parent node of the MRCA, rather than the MRCA itself
53 public static final String PARENT = "forParent";
54 public static final String STEM = "includeStem";
55
56
57 public String getParserName() {
58 return TMRCA_STATISTIC;
59 }
60
61 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
62
63 String name = xo.getAttribute(Statistic.NAME, xo.getId());
64 Tree tree = (Tree) xo.getChild(Tree.class);
65 TaxonList taxa = null;
66
67 if (xo.hasChildNamed(MRCA)) {
68 taxa = (TaxonList) xo.getElementFirstChild(MRCA);
69 }
70 boolean isAbsolute = xo.getAttribute(ABSOLUTE, false);
71 boolean includeStem = false;
72 if (xo.hasAttribute(PARENT) && xo.hasAttribute(STEM)) {
73 throw new XMLParseException("Please use either " + PARENT + " or " + STEM + "!");
74 } else if (xo.hasAttribute(PARENT)) {
75 includeStem = xo.getBooleanAttribute(PARENT);
76 } else if (xo.hasAttribute(STEM)) {
77 includeStem = xo.getBooleanAttribute(STEM);
78 }
79
80 try {
81 return new TMRCAStatistic(name, tree, taxa, isAbsolute, includeStem);
82 } catch (TreeUtils.MissingTaxonException mte) {
83 throw new XMLParseException(
84 "Taxon, " + mte + ", in " + getParserName() + "was not found in the tree.");
85 }
86 }
87
88 //************************************************************************
89 // AbstractXMLObjectParser implementation
90 //************************************************************************
91
92 public String getParserDescription() {
93 return "A statistic that has as its value the height of the most recent common ancestor " +
94 "of a set of taxa in a given tree. ";
95 }
96
97 public Class getReturnType() {
98 return TMRCAStatistic.class;
99 }
100
101 public XMLSyntaxRule[] getSyntaxRules() {
102 return rules;
103 }
104
105 private final XMLSyntaxRule[] rules = {
106 new ElementRule(Tree.class),
107 new StringAttributeRule("name",
108 "A name for this statistic primarily for the purposes of logging", true),
109 AttributeRule.newBooleanRule(ABSOLUTE, true),
110 new ElementRule(MRCA,
111 new XMLSyntaxRule[]{new ElementRule(Taxa.class)}, true),
112 new OrRule(
113 new XMLSyntaxRule[]{
114 AttributeRule.newBooleanRule(PARENT, true),
115 AttributeRule.newBooleanRule(STEM, true)
116 }
117 )
118 };
119
120 }