Debian Codegraph @

src/dr/evoxml/UPGMATreeParser.java

raw symbols SCIP
1 /*
2 * UPGMATreeParser.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.distance.DistanceMatrix;
29 import dr.evolution.tree.MutableTree;
30 import dr.evolution.tree.NodeRef;
31 import dr.evolution.tree.UPGMATree;
32 import dr.evolution.util.TimeScale;
33 import dr.evomodelxml.tree.TreeModelParser;
34 import dr.math.MathUtils;
35 import dr.xml.*;
36
37 import java.util.logging.Logger;
38
39 /**
40 * @author Alexei Drummond
41 * @author Andrew Rambaut
42 * @author Marc A. Suchard
43 * @version $Id: UPGMATreeParser.java,v 1.6 2006/07/28 11:27:32 rambaut Exp $
44 */
45 public class UPGMATreeParser extends AbstractXMLObjectParser {
46
47 //
48 // Public stuff
49 //
50
51 public static final String UPGMA_TREE = "upgmaTree";
52 //public static final String DISTANCES = "distances";
53 public static final String ROOT_HEIGHT = TreeModelParser.ROOT_HEIGHT;
54 public static final String RANDOMIZE = "nonzeroBranchLengths";
55
56 public String getParserName() {
57 return UPGMA_TREE;
58 }
59
60 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
61
62 boolean usingDatesSpecified = false;
63 boolean usingDates = true;
64 double rootHeight = xo.getAttribute(ROOT_HEIGHT, -1.0);
65
66 if (xo.hasAttribute(SimpleTreeParser.USING_DATES)) {
67 usingDatesSpecified = true;
68 usingDates = xo.getBooleanAttribute(SimpleTreeParser.USING_DATES);
69 }
70
71 DistanceMatrix distances = (DistanceMatrix) xo.getChild(DistanceMatrix.class);
72
73 UPGMATree tree = new UPGMATree(distances);
74
75 if (rootHeight > 0) {
76 double scaleFactor = rootHeight / tree.getNodeHeight(tree.getRoot());
77
78 for (int i = 0; i < tree.getInternalNodeCount(); i++) {
79 NodeRef node = tree.getInternalNode(i);
80 double height = tree.getNodeHeight(node);
81 tree.setNodeHeight(node, height * scaleFactor);
82 }
83 }
84
85 if (usingDates) {
86
87 dr.evolution.util.Date mostRecent = null;
88 for (int i = 0; i < tree.getTaxonCount(); i++) {
89
90 dr.evolution.util.Date date = (dr.evolution.util.Date) tree.getTaxonAttribute(i, dr.evolution.util.Date.DATE);
91
92 if (date == null) {
93 date = (dr.evolution.util.Date) tree.getNodeAttribute(tree.getExternalNode(i), dr.evolution.util.Date.DATE);
94 }
95
96 if (date != null && ((mostRecent == null) || date.after(mostRecent))) {
97 mostRecent = date;
98 }
99 }
100
101 for (int i = 0; i < tree.getInternalNodeCount(); i++) {
102 dr.evolution.util.Date date = (dr.evolution.util.Date) tree.getNodeAttribute(tree.getInternalNode(i), dr.evolution.util.Date.DATE);
103
104 if (date != null && ((mostRecent == null) || date.after(mostRecent))) {
105 mostRecent = date;
106 }
107 }
108
109 if (mostRecent == null) {
110 if (usingDatesSpecified) {
111 throw new XMLParseException("no date elements in tree (and usingDates attribute set)");
112 }
113 } else {
114 TimeScale timeScale = new TimeScale(mostRecent.getUnits(), true, mostRecent.getAbsoluteTimeValue());
115
116 for (int i = 0; i < tree.getTaxonCount(); i++) {
117 dr.evolution.util.Date date = (dr.evolution.util.Date) tree.getTaxonAttribute(i, dr.evolution.util.Date.DATE);
118
119 if (date == null) {
120 date = (dr.evolution.util.Date) tree.getNodeAttribute(tree.getExternalNode(i), dr.evolution.util.Date.DATE);
121 }
122
123 if (date != null) {
124 double height = timeScale.convertTime(date.getTimeValue(), date);
125 tree.setNodeHeight(tree.getExternalNode(i), height);
126 }
127 }
128
129 for (int i = 0; i < tree.getInternalNodeCount(); i++) {
130 dr.evolution.util.Date date = (dr.evolution.util.Date) tree.getNodeAttribute(tree.getInternalNode(i), dr.evolution.util.Date.DATE);
131
132 if (date != null) {
133 double height = timeScale.convertTime(date.getTimeValue(), date);
134 tree.setNodeHeight(tree.getInternalNode(i), height);
135 }
136 }
137
138 MutableTree.Utils.correctHeightsForTips(tree);
139 }
140
141 }
142
143 if (rootHeight > 0) {
144 double scaleFactor = rootHeight / tree.getNodeHeight(tree.getRoot());
145
146 for (int i = 0; i < tree.getInternalNodeCount(); i++) {
147 NodeRef node = tree.getInternalNode(i);
148 double height = tree.getNodeHeight(node);
149 tree.setNodeHeight(node, height * scaleFactor);
150 }
151 }
152
153 if (xo.getAttribute(RANDOMIZE, false)) {
154 shakeTree(tree);
155 }
156
157
158 return tree;
159 }
160
161 private boolean shakeNode(UPGMATree tree, NodeRef node) {
162 if (tree.isRoot(node) || tree.isExternal(node)) {
163 return false;
164 }
165
166 boolean shake = false;
167 if (tree.getBranchLength(node) <= tolerance) {
168 shake = true;
169 }
170 double maxHeight = tree.getNodeHeight(tree.getParent(node));
171 double minHeight = Double.NEGATIVE_INFINITY;
172 for (int i = 0; i < tree.getChildCount(node); i++) {
173 NodeRef child = tree.getChild(node, i);
174 if (tree.getBranchLength(child) <= tolerance) {
175 shake = true;
176 }
177 double thisHeight = tree.getNodeHeight(child);
178 if (thisHeight > minHeight) {
179 minHeight = thisHeight;
180 }
181 }
182 if (shake) {
183 double draw = minHeight + (maxHeight - minHeight) * MathUtils.nextDouble();
184 tree.setNodeHeight(node, draw);
185 }
186 return shake;
187 }
188
189 private void shakeTree(UPGMATree tree) {
190 boolean shake = true;
191 int[] permutation = new int[tree.getNodeCount()];
192 for (int i = 0; i < tree.getNodeCount(); i++) {
193 permutation[i] = i;
194 }
195 while (shake) {
196 Logger.getLogger("dr.evomodelxml").info("Adjusting heights in UPGMA tree");
197 MathUtils.permute(permutation);
198 shake = false;
199 for (int i = 0; i < tree.getNodeCount(); i++) {
200 NodeRef node = tree.getNode(permutation[i]);
201 if (shakeNode(tree, node)) {
202 shake = true;
203 }
204 }
205 }
206 }
207
208 private static double tolerance = 0.0;
209
210 //************************************************************************
211 // AbstractXMLObjectParser implementation
212 //************************************************************************
213
214 public XMLSyntaxRule[] getSyntaxRules() {
215 return rules;
216 }
217
218 private final XMLSyntaxRule[] rules = {
219 AttributeRule.newBooleanRule(SimpleTreeParser.USING_DATES, true),
220 AttributeRule.newDoubleRule(ROOT_HEIGHT, true),
221 AttributeRule.newBooleanRule(RANDOMIZE, true),
222 new ElementRule(DistanceMatrix.class)
223 };
224
225 public String getParserDescription() {
226 return "This element returns a UPGMA tree generated from the given distances.";
227 }
228
229 public Class getReturnType() {
230 return UPGMATree.class;
231 }
232 }