Debian Codegraph @

src/dr/app/tempest/TreeUtils.java

raw symbols SCIP
1 /*
2 * TreeUtils.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.app.tempest;
27
28 import dr.evolution.tree.MutableTree;
29 import dr.evolution.tree.NodeRef;
30 import dr.evolution.tree.Tree;
31 import dr.evolution.util.Date;
32 import dr.evolution.util.Taxon;
33 import dr.evolution.util.TimeScale;
34 import dr.stats.Variate;
35
36 /**
37 * Class for getting information from trees.
38 *
39 * @author Andrew Rambaut
40 * @author Alexei Drummond
41 * @version $Id: TreeUtils.java,v 1.7 2005/07/11 14:07:25 rambaut Exp $
42 */
43 public class TreeUtils {
44
45 /**
46 * Gets the root to tip distances from a tree.
47 */
48 public static void getRootToTipDistances(Tree tree, Variate distances) {
49
50 double rootHeight = tree.getNodeHeight(tree.getRoot());
51 double height;
52
53 for (int i = 0; i < tree.getExternalNodeCount(); i++) {
54
55 height = tree.getNodeHeight(tree.getExternalNode(i));
56 distances.add(rootHeight - height);
57 }
58 }
59
60 /**
61 * Gets the tip dates from a tree.
62 */
63 public static void getTipDates(Tree tree, Variate dates) {
64
65 for (int i = 0; i < tree.getExternalNodeCount(); i++) {
66
67 Taxon taxon = tree.getNodeTaxon(tree.getExternalNode(i));
68 Object date = taxon.getAttribute("date");
69 if (date != null) {
70 if (date instanceof Date) {
71 dates.add(((Date) date).getTimeValue());
72 } else {
73 try {
74 dates.add(Double.parseDouble(date.toString()));
75 } catch (NumberFormatException nfe) {
76 dates.add(0.0);
77 }
78 }
79 } else {
80 dates.add(0.0);
81 }
82 }
83 }
84
85 /**
86 * Gets the root to tip distances from a node.
87 */
88 public static void getRootToTipDistances(Tree tree, NodeRef node, double rootHeight,
89 Variate distances) {
90
91 if (tree.isExternal(node)) {
92 double height = tree.getNodeHeight(node);
93 distances.add(rootHeight - height);
94 } else {
95
96 getRootToTipDistances(tree, tree.getChild(node, 0), rootHeight, distances);
97 getRootToTipDistances(tree, tree.getChild(node, 1), rootHeight, distances);
98 }
99 }
100
101 /**
102 * Gets the tip dates from a node.
103 */
104 public static void getTipDates(Tree tree, NodeRef node, Variate dates) {
105
106 if (tree.isExternal(node)) {
107
108 String date = (String) tree.getNodeTaxon(node).getAttribute("date");
109 if (date != null) {
110 dates.add(Double.parseDouble(date));
111 } else {
112 dates.add(0.0);
113 }
114 } else {
115
116 getTipDates(tree, tree.getChild(node, 0), dates);
117 getTipDates(tree, tree.getChild(node, 1), dates);
118 }
119 }
120
121 /**
122 * Returns -1 if there is no number suffix
123 */
124 public static double guessDate(String s) {
125
126 int i = s.length();
127 char c;
128 do {
129 i--;
130 c = s.charAt(i);
131 } while (i >= 0 && (Character.isDigit(c) || c == '.'));
132
133 if (i == s.length()) {
134 return 0.0;
135 }
136
137 return Double.parseDouble(s.substring(i + 1));
138 }
139
140 /**
141 * Sets the tip heights from the tip dates
142 */
143 public static void setHeightsFromDates(MutableTree tree) {
144
145 Date mostRecent = null;
146
147 for (int i = 0; i < tree.getExternalNodeCount(); i++) {
148
149 Taxon taxon = tree.getNodeTaxon(tree.getExternalNode(i));
150
151 Date date = (Date) taxon.getAttribute("date");
152
153 if (date != null) {
154
155 if ((mostRecent == null) || date.after(mostRecent)) {
156 mostRecent = date;
157 }
158 }
159 }
160
161 TimeScale timeScale = new TimeScale(mostRecent.getUnits(), true, mostRecent.getAbsoluteTimeValue());
162
163 for (int i = 0; i < tree.getExternalNodeCount(); i++) {
164 NodeRef node = tree.getExternalNode(i);
165 Taxon taxon = tree.getNodeTaxon(node);
166 Date date = (Date) taxon.getAttribute("date");
167
168 if (date != null) {
169 double height = timeScale.convertTime(date.getTimeValue(), date);
170 tree.setNodeHeight(node, height);
171 } else {
172 tree.setNodeHeight(node, 0.0);
173 }
174 }
175
176 adjustInternalHeights(tree, tree.getRoot());
177
178 if (mostRecent != null) {
179 tree.setUnits(mostRecent.getUnits());
180 }
181 }
182
183 // **************************************************************
184 // Private static methods
185 // **************************************************************
186
187 private static void adjustInternalHeights(MutableTree tree, NodeRef node) {
188
189 if (!tree.isExternal(node)) {
190 // pre-order recursion
191 for (int i = 0; i < tree.getChildCount(node); i++) {
192 adjustInternalHeights(tree, tree.getChild(node, i));
193 }
194 }
195
196 NodeRef parent = tree.getParent(node);
197
198 if (parent != null) {
199
200 if (tree.getNodeHeight(parent) < tree.getNodeHeight(node)) {
201 tree.setNodeHeight(parent, tree.getNodeHeight(node));
202 }
203 }
204 }
205
206
207 }