Debian Codegraph @

src/dr/evolution/tree/TreeShape.java

raw symbols SCIP
1 /*
2 * TreeShape.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.evolution.tree;
27
28 import dr.evolution.io.NexusImporter;
29
30 import java.io.FileReader;
31 import java.util.ArrayList;
32 import java.util.Collections;
33
34 /**
35 * A collection of tree shape summary statistics.
36 *
37 * @version $Id: TreeShape.java,v 1.7 2005/05/24 20:25:57 rambaut Exp $
38 *
39 * @author Alexei Drummond
40 */
41 public class TreeShape {
42
43 /**
44 * The NBar statistic is the average number of nodes above a terminal node.
45 * see Kirkpatrick & Slatkin (1992)
46 */
47 public static double getNBarStatistic(Tree tree) {
48
49 double NBar = 0.0;
50 for (int i =0; i < tree.getExternalNodeCount(); i++) {
51 NodeRef node = tree.getExternalNode(i);
52 while (!tree.isRoot(node)) {
53 node = tree.getParent(node);
54 NBar += 1.0;
55 }
56 }
57 return NBar / tree.getExternalNodeCount();
58 }
59
60 /**
61 * The varNBar statistic is the average number of nodes above a terminal node.
62 * see Kirkpatrick & Slatkin (1992)
63 */
64 public static double getVarNBarStatistic(Tree tree) {
65
66 double[] N = new double[tree.getExternalNodeCount()];
67 for (int i =0; i < N.length; i++) {
68
69 NodeRef node = tree.getExternalNode(i);
70 while (!tree.isRoot(node)) {
71 node = tree.getParent(node);
72 N[i] += 1.0;
73 }
74 }
75 return dr.stats.DiscreteStatistics.variance(N);
76 }
77
78 /**
79 * The C statistic is the normalized sum of differences of
80 * number of children in left and right subtrees over all
81 * internal nodes.
82 * see Kirkpatrick & Slatkin (1992)
83 */
84 public static double getCStatistic(Tree tree) {
85
86 double C = 0.0;
87 int n = tree.getExternalNodeCount();
88 for (int i =0; i < n-1; i++) {
89
90 NodeRef node = tree.getInternalNode(i);
91
92 int r = TreeUtils.getLeafCount(tree, tree.getChild(node, 0));
93 int s = TreeUtils.getLeafCount(tree, tree.getChild(node, 1));
94
95 C += Math.abs(r-s);
96 }
97 C *= 2.0 / (n * (n - 3) + 2);
98 return C;
99 }
100
101 /**
102 * The B1 statistic measures the maximum number of nodes between
103 * each interior node and a tip (Mi) for all internal nodes except the
104 * root. THe statistic is the sum of the reciprocals of this Mi
105 * statistic.
106 * see Kirkpatrick & Slatkin (1992)
107 */
108 public static double getB1Statistic(Tree tree) {
109
110 double B1 = 0.0;
111 int n = tree.getInternalNodeCount();
112 for (int i =0; i < n; i++) {
113
114 NodeRef node = tree.getInternalNode(i);
115 if (!tree.isRoot(node)) {
116 B1 += 1.0/getMi(tree, node);
117 }
118 }
119 return B1;
120 }
121
122 /**
123 * Assumes strictly bifurcating tree
124 */
125 private static int getMi(Tree tree, NodeRef node) {
126 int childCount = tree.getChildCount(node);
127 if (childCount == 0) return 0;
128 int Mi = 0;
129 for (int i =0; i < childCount; i++) {
130 int mi = getMi(tree, tree.getChild(node, i));
131 if (mi > Mi) Mi = mi;
132 }
133 Mi += 1;
134 return Mi;
135 }
136
137 public static double getTreeness(Tree tree) {
138 double noise = 0.0;
139 double signal = 0.0;
140 for (int i = 0; i < tree.getExternalNodeCount(); i++) {
141 NodeRef node = tree.getExternalNode(i);
142 NodeRef parent = tree.getParent(node);
143 noise += tree.getNodeHeight(parent) - tree.getNodeHeight(node);
144 }
145
146 for (int i = 0; i < tree.getInternalNodeCount(); i++) {
147 NodeRef node = tree.getInternalNode(i);
148 if (!tree.isRoot(node)) {
149 NodeRef parent = tree.getParent(node);
150 signal += tree.getNodeHeight(parent) - tree.getNodeHeight(node);
151 }
152 }
153 return signal/noise;
154 }
155
156 /**
157 * @return the gamma statistic of tree shape (see Pybus & Harvey, 2000).
158 * Assumes a strictly bifurcating tree and contemporaneous sequences.
159 */
160 public static double getGammaStatistic(Tree tree) {
161
162 int n = tree.getExternalNodeCount();
163 double[] g = getIntervals(tree);
164
165 double T = 0; // total branch length
166 for (int j = 2; j <= n; j++) {
167 T += j * g[j-2];
168 }
169
170 double gamma = 0.0;
171 for (int i = 2; i < n; i++) {
172 for (int k = 2; k <= i; k++) {
173 gamma += k * g[k-2];
174 }
175 }
176
177 gamma *= (1.0 / n-2.0);
178 gamma -= T/2.0;
179 gamma /= T * Math.sqrt(1.0/(12.0*(n-2.0)));
180
181 return gamma;
182 }
183
184 /**
185 * @return the intervals in an ultrametric tree in order from root to tips.
186 */
187 private static double[] getIntervals(Tree tree) {
188
189 ArrayList<Double> heights = new ArrayList<Double>();
190 if (TreeUtils.isUltrametric(tree)) {
191 for (int i = 0; i < tree.getInternalNodeCount(); i++) {
192 heights.add(tree.getNodeHeight(tree.getInternalNode(i)));
193 }
194 Collections.sort(heights);
195 // for (int i = 0; i < heights.size(); i++) {
196 // System.out.print(heights.get(i)+" ");
197 // }
198 // System.out.println();
199
200 double[] intervals = new double[heights.size()];
201 for (int i = 0; i < intervals.length-1; i++) {
202 double height1 = heights.get(i);
203 double height2 = heights.get(i + 1);
204
205 intervals[i] = height1 - height2;
206 }
207 intervals[intervals.length - 1] = heights.get(intervals.length - 1);
208
209 return intervals;
210 } else
211 throw new IllegalArgumentException("Expecting ultrametric tree.");
212
213 }
214
215 public static void main(String[] args) throws Exception {
216 NexusImporter importer = new NexusImporter(new FileReader(args[0]));
217
218
219 Tree[] trees = importer.importTrees(null);
220
221 System.out.println("File = " + args[0]);
222 double[] treeness = new double[trees.length];
223 for (int i = 0; i < treeness.length; i++) {
224 treeness[i] = getTreeness(trees[i]);
225 }
226 System.out.println("Mean treeness = " + dr.stats.DiscreteStatistics.mean(treeness));
227 System.out.println("Lower (95%) treeness = " + dr.stats.DiscreteStatistics.quantile(0.025, treeness));
228 System.out.println("Upper (95%) treeness = " + dr.stats.DiscreteStatistics.quantile(0.975, treeness));
229
230 }
231
232 }