| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 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 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 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 |
|
| 70 |
|
| 71 |
public double getStatisticValue(int dim) { |
| 72 |
if (dim == 0) { |
| 73 |
|
| 74 |
|
| 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 |
} |