Debian Codegraph @

src/dr/evoxml/SimpleTreeParser.java

raw symbols SCIP
1 /*
2 * SimpleTreeParser.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.evoxml;
27
28 import dr.evolution.tree.MutableTree;
29 import dr.evolution.tree.SimpleNode;
30 import dr.evolution.tree.SimpleTree;
31 import dr.evolution.tree.Tree;
32 import dr.evolution.util.TimeScale;
33 import dr.evolution.util.Units;
34 import dr.evoxml.util.XMLUnits;
35 import dr.xml.*;
36
37 /**
38 * @author Alexei Drummond
39 * @author Andrew Rambaut
40 * @version $Id: SimpleTreeParser.java,v 1.3 2005/05/24 20:25:59 rambaut Exp $
41 */
42 public class SimpleTreeParser extends AbstractXMLObjectParser {
43
44 //
45 // Public stuff
46 //
47
48 public static final String SIMPLE_TREE = "tree";
49 public static final String USING_DATES = "usingDates";
50 public static final String DATE = "date";
51
52 public String getParserName() {
53 return SIMPLE_TREE;
54 }
55
56 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
57
58 Units.Type units = XMLUnits.Utils.getUnitsAttr(xo);
59 boolean usingDates = xo.getAttribute(USING_DATES, false);
60
61 SimpleNode root = (SimpleNode) xo.getChild(SimpleNode.class);
62
63 if (root == null) {
64 throw new XMLParseException("node element missing from tree");
65 }
66
67 SimpleTree tree = new SimpleTree(root);
68
69 if (usingDates) {
70
71 dr.evolution.util.Date mostRecent = null;
72 for (int i = 0; i < tree.getTaxonCount(); i++) {
73
74 dr.evolution.util.Date date = (dr.evolution.util.Date) tree.getTaxonAttribute(i, DATE);
75
76 if (date == null) {
77 date = (dr.evolution.util.Date) tree.getNodeAttribute(tree.getExternalNode(i), DATE);
78 }
79
80 if (date != null && ((mostRecent == null) || date.after(mostRecent))) {
81 mostRecent = date;
82 }
83 }
84
85 for (int i = 0; i < tree.getInternalNodeCount(); i++) {
86 dr.evolution.util.Date date = (dr.evolution.util.Date) tree.getNodeAttribute(tree.getInternalNode(i), DATE);
87
88 if (date != null && ((mostRecent == null) || date.after(mostRecent))) {
89 mostRecent = date;
90 }
91 }
92
93 if (mostRecent == null) {
94 throw new XMLParseException("no date elements in tree (and usingDates attribute set)");
95 }
96
97 TimeScale timeScale = new TimeScale(mostRecent.getUnits(), true, mostRecent.getAbsoluteTimeValue());
98
99 for (int i = 0; i < tree.getTaxonCount(); i++) {
100 dr.evolution.util.Date date = (dr.evolution.util.Date) tree.getTaxonAttribute(i, DATE);
101
102 if (date == null) {
103 date = (dr.evolution.util.Date) tree.getNodeAttribute(tree.getExternalNode(i), DATE);
104 }
105
106 if (date != null) {
107 double height = timeScale.convertTime(date.getTimeValue(), date);
108 tree.setNodeHeight(tree.getExternalNode(i), height);
109 }
110 }
111
112 for (int i = 0; i < tree.getInternalNodeCount(); i++) {
113 dr.evolution.util.Date date = (dr.evolution.util.Date) tree.getNodeAttribute(tree.getInternalNode(i), DATE);
114
115 if (date != null) {
116 double height = timeScale.convertTime(date.getTimeValue(), date);
117 tree.setNodeHeight(tree.getInternalNode(i), height);
118 }
119 }
120
121 MutableTree.Utils.correctHeightsForTips(tree);
122 }
123
124 tree.setUnits(units);
125
126 return tree;
127 }
128
129 //************************************************************************
130 // AbstractXMLObjectParser implementation
131 //************************************************************************
132
133 public String getParserDescription() {
134 return "This element represents a rooted binary tree and associated attributes.";
135 }
136
137 public Class getReturnType() {
138 return Tree.class;
139 }
140
141 public XMLSyntaxRule[] getSyntaxRules() {
142 return rules;
143 }
144
145 private final XMLSyntaxRule[] rules = {
146 XMLUnits.SYNTAX_RULES[0],
147 new ElementRule(SimpleNode.class)
148 };
149 }