Debian Codegraph @

src/dr/evomodel/tree/NodeHeightsStatistic.java

raw symbols SCIP
1 /*
2 * NodeHeightsStatistic.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.evomodel.tree;
27
28 import dr.evolution.tree.Tree;
29 import dr.inference.model.Parameter;
30 import dr.inference.model.Statistic;
31
32 import java.util.Arrays;
33
34 /**
35 * A statistic that reports the heights of all the nodes in sorted order with the option
36 * of grouping (for skyline-type plots).
37 *
38 * @author Andrew Rambaut
39 * @version $Id:$
40 */
41 public class NodeHeightsStatistic extends TreeStatistic {
42
43 public NodeHeightsStatistic(String name, Tree tree) {
44 this(name, tree, null);
45 }
46
47 public NodeHeightsStatistic(String name, Tree tree, Parameter groupSizes) {
48 super(name);
49 this.tree = tree;
50 this.groupSizes = groupSizes;
51 }
52
53 public void setTree(Tree tree) {
54 this.tree = tree;
55 }
56
57 public Tree getTree() {
58 return tree;
59 }
60
61 public int getDimension() {
62 if (groupSizes != null) {
63 return groupSizes.getDimension();
64 }
65 return tree.getInternalNodeCount();
66 }
67
68 /**
69 * @return the total length of all the branches in the tree
70 */
71 public double getStatisticValue(int dim) {
72 if (dim == 0) {
73 // This assumes that each dimension will be called in turn, so
74 // the call for dim 0 updates the array.
75 calculateHeights();
76 }
77
78 return heights[dim];
79 }
80
81 private void calculateHeights() {
82 heights = new double[tree.getInternalNodeCount()];
83
84 for (int i = 0; i < heights.length; i++) {
85 heights[i] = tree.getNodeHeight(tree.getInternalNode(i));
86 }
87 Arrays.sort(heights);
88
89 if (groupSizes != null) {
90 double[] allHeights = heights;
91 heights = new double[groupSizes.getDimension()];
92 int k = 0;
93 for (int i = 0; i < groupSizes.getDimension(); i++) {
94 k += groupSizes.getValue(i);
95 heights[i] = allHeights[k - 1];
96 }
97 }
98 }
99
100 private Tree tree = null;
101 private Parameter groupSizes = null;
102 private double[] heights = null;
103 }