Debian Codegraph @

src/dr/evolution/tree/treemetrics/SteelPennyPathDifferenceMetric.java

raw symbols SCIP
1 /*
2 * TopologyTracer.java
3 *
4 * Copyright (c) 2002-2017 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.treemetrics;
27
28 import dr.evolution.io.Importer;
29 import dr.evolution.io.NewickImporter;
30 import dr.evolution.tree.NodeRef;
31 import dr.evolution.tree.Tree;
32
33 import java.io.IOException;
34 import java.util.Collections;
35 import java.util.HashSet;
36 import java.util.Set;
37
38 import static dr.evolution.tree.treemetrics.TreeMetric.Utils.checkTreeTaxa;
39
40 /**
41 * @author Guy Baele
42 * @author Andrew Rambaut
43 * Path difference metric according to Steel & Penny (1993)
44 */
45 public class SteelPennyPathDifferenceMetric implements TreeMetric {
46
47 public static Type TYPE = Type.STEEL_PENNY;
48
49 private Tree focalTree;
50 private int dim;
51 private double[] focalPath;
52 private final boolean fixedFocalTree;
53
54 public SteelPennyPathDifferenceMetric() {
55 this.fixedFocalTree = false;
56 }
57
58 public SteelPennyPathDifferenceMetric(Tree focalTree) {
59 this.focalTree = focalTree;
60 this.fixedFocalTree = true;
61 this.dim = focalTree.getExternalNodeCount() * focalTree.getExternalNodeCount();
62 this.focalPath = new double[dim];
63
64 traverse(this.focalTree, this.focalTree.getRoot(), focalPath);
65 }
66
67 /**
68 * Compute the metric between two trees. If tree1 is not the focal tree provided to the
69 * constructor then it will store this as the new focal tree. If the focal tree is constant
70 * from call to call then a cached set of precomputation will be used, increasing efficiency.
71 * @param tree1
72 * @param tree2
73 * @return
74 */
75 public double getMetric(Tree tree1, Tree tree2) {
76
77 checkTreeTaxa(tree1, tree2);
78
79 if (tree1 != focalTree) {
80 if (fixedFocalTree) {
81 // If we set a focal tree in the constructor then it makes sense to check it is the same
82 // as the one set here.
83 throw new RuntimeException("Focal tree is different from that set in the constructor.");
84 }
85
86 // cache tree1 and the pre-computed path for future calls
87 focalTree = tree1;
88 if (focalPath == null) {
89 this.dim = focalTree.getExternalNodeCount() * focalTree.getExternalNodeCount();
90 this.focalPath = new double[dim];
91 }
92 traverse(focalTree, focalTree.getRoot(), focalPath);
93 }
94
95 double[] pathTwo = new double[dim];
96 traverse(tree2, tree2.getRoot(), pathTwo);
97
98 int n = tree1.getExternalNodeCount();
99
100 double metric = 0.0;
101 for (int i = 0; i < n; i++) {
102 for (int j = i + 1; j < n; j++) {
103 int index = (i * n) + j;
104 metric += Math.pow(focalPath[index] - pathTwo[index], 2);
105 }
106 }
107 metric = Math.sqrt(metric);
108
109 return metric;
110 }
111
112 private Set<NodeRef> traverse(Tree tree, NodeRef node, double[] lengths) {
113 NodeRef left = tree.getChild(node, 0);
114 NodeRef right = tree.getChild(node, 1);
115
116
117 Set<NodeRef> leftSet = null;
118 Set<NodeRef> rightSet = null;
119
120 if (!tree.isExternal(left)) {
121 leftSet = traverse(tree, left, lengths);
122 } else {
123 leftSet = Collections.singleton(left);
124 }
125 if (!tree.isExternal(right)) {
126 rightSet = traverse(tree, right, lengths);
127 } else {
128 rightSet = Collections.singleton(right);
129 }
130
131 for (NodeRef tip1 : leftSet) {
132 for (NodeRef tip2 : rightSet) {
133 int index;
134 if (tip1.getNumber() < tip2.getNumber()) {
135 index = (tip1.getNumber() * tree.getExternalNodeCount()) + tip2.getNumber();
136 } else {
137 index = (tip2.getNumber() * tree.getExternalNodeCount()) + tip1.getNumber();
138 }
139 lengths[index] = tree.getNodeHeight(node) * 2
140 - tree.getNodeHeight(tip1)
141 - tree.getNodeHeight(tip2);
142
143 }
144 }
145
146 Set<NodeRef> tips = new HashSet<NodeRef>();
147 tips.addAll(leftSet);
148 tips.addAll(rightSet);
149
150 return tips;
151 }
152
153 @Override
154 public Type getType() {
155 return TYPE;
156 }
157
158 @Override
159 public String toString() {
160 return getType().getShortName();
161 }
162
163 }