Debian Codegraph @

src/dr/evomodelxml/tree/TreeMetricStatisticParser.java

raw symbols SCIP
1 /*
2 * TreeMetricStatisticParser.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.treemetrics.*;
30 import dr.evomodel.tree.TreeMetricStatistic;
31 import dr.inference.model.Statistic;
32 import dr.xml.*;
33
34 /**
35 */
36 public class TreeMetricStatisticParser extends AbstractXMLObjectParser {
37
38 public static final String TREE_METRIC_STATISTIC = "treeMetricStatistic";
39 public static final String TARGET = "target";
40 public static final String REFERENCE = "reference";
41 public static final String TYPE = "type";
42
43 public String getParserName() {
44 return TREE_METRIC_STATISTIC;
45 }
46
47 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
48
49 TreeMetric.Type type = null;
50 if (xo.hasAttribute(TYPE)) {
51 final String s = xo.getStringAttribute(TYPE);
52 for (TreeMetric.Type t : TreeMetric.Type.values()) {
53 if (t.toString().toLowerCase().equals(s.toLowerCase())) {
54 type = t;
55 break;
56 }
57 }
58
59 if (type == null) {
60 throw new XMLParseException("Tree metric type, " + s + ", is not recognised");
61 }
62 }
63
64 final String name = xo.getAttribute(Statistic.NAME, xo.hasId() ? xo.getId() :
65 (type == null ? "topology" : type.toString()));
66 final Tree target = (Tree) xo.getElementFirstChild(TARGET);
67 final Tree reference = (Tree) xo.getElementFirstChild(REFERENCE);
68
69 TreeMetric treeMetric = null;
70 if (type != null) {
71 switch (type) {
72 case ROBINSON_FOULDS:
73 treeMetric = new RobinsonFouldsMetric();
74 break;
75 case BRANCH_SCORE:
76 treeMetric = new BranchScoreMetric();
77 break;
78 case ROOTED_BRANCH_SCORE:
79 treeMetric = new RootedBranchScoreMetric();
80 break;
81 case CLADE_HEIGHT:
82 treeMetric = new CladeHeightMetric();
83 break;
84 case KENDALL_COLIJN:
85 treeMetric = new KendallColijnPathDifferenceMetric(0.5);
86 break;
87 case STEEL_PENNY:
88 treeMetric = new SteelPennyPathDifferenceMetric();
89 break;
90 default:
91 throw new IllegalArgumentException("Unknown tree metric type");
92 }
93 }
94
95 return new TreeMetricStatistic(name, reference, target, treeMetric);
96 }
97
98 // ************************************************************************
99 // AbstractXMLObjectParser
100 // implementation
101 // ************************************************************************
102
103 public String getParserDescription() {
104 return "A statistic that returns the distance between two trees. "
105 + " with method=\"topology\", return a 0 for identity and a 1 for difference. "
106 + "With other methods return the distance metric associated with that method.";
107 }
108
109 public Class getReturnType() {
110 return TreeMetricStatistic.class;
111 }
112
113 public XMLSyntaxRule[] getSyntaxRules() {
114 return rules;
115 }
116
117 private final XMLSyntaxRule[] rules = {
118 new StringAttributeRule(Statistic.NAME,
119 "A name for this statistic primarily for the purposes of logging",
120 true),
121 new StringAttributeRule(TYPE, "tree metric name", true),
122 new ElementRule(TARGET, new XMLSyntaxRule[]{new ElementRule(
123 Tree.class)}),
124 new ElementRule(REFERENCE, new XMLSyntaxRule[]{new ElementRule(
125 Tree.class)}),
126 };
127 }