Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * BranchScoreMetric.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 /**
27 *
28 */
29 package dr.evolution.tree.treemetrics;
30
31 import java.util.*;
32
33 import dr.evolution.tree.Clade;
34 import dr.evolution.tree.Tree;
35
36 import static dr.evolution.tree.treemetrics.TreeMetric.Utils.checkTreeTaxa;
37
38 /**
39 * @author Andrew Rambaut
40 * @author Sebastian Hoehna
41 *
42 */
43 public class BranchScoreMetric implements TreeMetric {
44 public static Type TYPE = Type.BRANCH_SCORE;
45
46 private Tree focalTree;
47 private List<Clade> focalClades;
48 private final boolean fixedFocalTree;
49
50 public BranchScoreMetric() {
51 this.fixedFocalTree = false;
52 }
53
54 public BranchScoreMetric(Tree focalTree) {
55 this.focalTree = focalTree;
56 this.fixedFocalTree = true;
57 focalClades = Clade.getCladeList(focalTree);
58 }
59
60
61 @Override
62 public double getMetric(Tree tree1, Tree tree2) {
63
64 checkTreeTaxa(tree1, tree2);
65
66 if (tree1 != focalTree) {
67 if (fixedFocalTree) {
68 // If we set a focal tree in the constructor then it makes sense to check it is the same
69 // as the one set here.
70 throw new RuntimeException("Focal tree is different from that set in the constructor.");
71 }
72
73 // cache tree1 and the pre-computed path for future calls
74 focalTree = tree1;
75 focalClades = Clade.getCladeList(focalTree);
76 }
77
78 List<Clade> clades2 = Clade.getCladeList(tree2);
79
80 return getDistance(focalClades, clades2);
81 }
82
83 protected double getDistance(List<Clade> clades1, List<Clade> clades2) {
84
85
86 Collections.sort(clades1);
87 Collections.sort(clades2);
88 double distance = 0.0;
89 int indexClade2 = 0;
90 Clade clade2 = null;
91 Clade parent1, parent2 = null;
92 double height1, height2;
93
94 for (Clade clade1 : clades1) {
95
96 parent1 = findParent(clade1, clades1);
97 height1 = parent1.getHeight() - clade1.getHeight();
98
99 if (indexClade2 < clades2.size()) {
100 clade2 = clades2.get(indexClade2);
101 parent2 = findParent(clade2, clades2);
102 }
103 while (clade1.compareTo(clade2) > 0 && indexClade2 < clades2.size()) {
104 height2 = parent2.getHeight() - clade2.getHeight();
105 distance += height2 * height2;
106 indexClade2++;
107 if (indexClade2 < clades2.size()) {
108 clade2 = clades2.get(indexClade2);
109 parent2 = findParent(clade2, clades2);
110 }
111 }
112 if (clade1.compareTo(clade2) == 0) {
113 height2 = parent2.getHeight() - clade2.getHeight();
114 distance += (height1 - height2) * (height1 - height2);
115 indexClade2++;
116 } else {
117 distance += height1 * height1;
118 }
119 }
120
121 return Math.sqrt(distance);
122 }
123
124 private Clade findParent(Clade clade1, List<Clade> clades) {
125 Clade parent = null;
126 for (Clade clade2 : clades) {
127 if (isParent(clade2, clade1)) {
128 if (parent == null || parent.getSize() > clade2.getSize())
129 parent = clade2;
130 }
131 }
132
133 if (parent == null){
134 //the case that this clade is the whole tree
135 return clade1;
136 }
137
138 return parent;
139 }
140
141 private boolean isParent(Clade parent, Clade child) {
142 if (parent.getSize() <= child.getSize()) {
143 return false;
144 }
145
146 tmpBits.clear();
147 tmpBits.or(parent.getBits());
148 tmpBits.xor(child.getBits());
149
150 return tmpBits.cardinality() < parent.getSize();
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 BitSet tmpBits = new BitSet();
164 }