Debian Codegraph @

src/dr/evoxml/RescaledTreeParser.java

raw symbols SCIP
1 /*
2 * RescaledTreeParser.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.*;
29 import dr.evolution.util.Taxon;
30 import dr.evolution.util.TaxonList;
31 import dr.xml.*;
32
33 import java.util.HashSet;
34 import java.util.Set;
35
36 /**
37 * Parser takes a tree and rescales the node heights to match a set of clade heights defined by taxon sets.
38 * @author Andrew Rambaut
39 *
40 * @version $Id$
41 */
42 public class RescaledTreeParser extends AbstractXMLObjectParser {
43
44 //
45 // Public stuff
46 //
47 public final static String RESCALED_TREE = "rescaledTree";
48 public final static String CLADE = "clade";
49 public final static String HEIGHT = "height";
50
51 public String getParserName() { return RESCALED_TREE; }
52
53 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
54
55 Tree tree = (Tree)xo.getChild(Tree.class);
56
57 // make a mutable copy
58 SimpleTree rescaledTree = new SimpleTree(tree);
59
60 // First flag all internal nodes as unset....
61 for (int i = 0; i < rescaledTree.getInternalNodeCount(); i++) {
62 rescaledTree.setNodeHeight(rescaledTree.getInternalNode(i), Double.NEGATIVE_INFINITY);
63 }
64
65 for (int i = 0; i < xo.getChildCount(); i++) {
66 if (xo.getChild(i) instanceof XMLObject) {
67 XMLObject cxo = (XMLObject)xo.getChild(i);
68 if (cxo.getName().equals(CLADE)) {
69 TaxonList taxa = (TaxonList)cxo.getChild(TaxonList.class);
70
71 Set<String> leafSet = new HashSet<String>();
72 for (Taxon taxon : taxa) {
73 leafSet.add(taxon.getId());
74 }
75
76 NodeRef mrca = TreeUtils.getCommonAncestorNode(rescaledTree, leafSet);
77 if (mrca == null || TreeUtils.getLeafCount(rescaledTree, mrca) != leafSet.size()) {
78 throw new XMLParseException("Clade defined by taxon Set, " + taxa.getId() + ", is not found in the guide tree");
79 }
80
81 if (cxo.hasAttribute(HEIGHT)) {
82 double height = cxo.getDoubleAttribute(HEIGHT);
83 rescaledTree.setNodeHeight(mrca, height);
84 }
85 }
86 }
87 }
88
89 if (xo.hasAttribute(HEIGHT)) {
90 rescaledTree.setNodeHeight(rescaledTree.getRoot(), xo.getDoubleAttribute(HEIGHT));
91 }
92
93 interpolateHeights(rescaledTree, rescaledTree.getRoot());
94
95 return rescaledTree;
96 }
97
98 private double interpolateHeights(MutableTree tree, NodeRef node) {
99 if (!tree.isExternal(node)) {
100
101 double maxHeight = Double.NEGATIVE_INFINITY;
102
103 for (int i = 0; i < tree.getChildCount(node); i++) {
104 NodeRef child = tree.getChild(node, i);
105
106 double h = interpolateHeights(tree, child);
107
108 if (h > maxHeight) {
109 maxHeight = h;
110 }
111 }
112
113 double height = tree.getNodeHeight(node);
114 if (Double.isInfinite(height)) {
115 int count = 1;
116 NodeRef parent = tree.getParent(node);
117 while (parent != null && Double.isInfinite(tree.getNodeHeight(parent))) {
118 parent = tree.getParent(parent);
119 count ++;
120 }
121
122 if (parent == null) {
123 height = maxHeight + count;
124 } else {
125 height = (tree.getNodeHeight(parent) + maxHeight) / 2.0;
126 }
127 }
128
129 tree.setNodeHeight(node, height);
130
131 return height;
132 } else {
133 return tree.getNodeHeight(node);
134 }
135 }
136
137 public String getParserDescription() {
138 return "This element rescales a given tree with a set of clade heights.";
139 }
140
141 public Class getReturnType() { return Tree.class; }
142
143 public XMLSyntaxRule[] getSyntaxRules() { return rules; }
144
145 private XMLSyntaxRule[] rules = new XMLSyntaxRule[] {
146 AttributeRule.newDoubleRule(HEIGHT, true),
147 new ElementRule(Tree.class),
148 new ElementRule(CLADE, new XMLSyntaxRule[] {
149 AttributeRule.newDoubleRule(HEIGHT, true),
150 new ElementRule(TaxonList.class)
151 }, 0, Integer.MAX_VALUE)
152 };
153 }