Debian Codegraph @

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

raw symbols SCIP
1 package dr.evolution.tree.treemetrics;
2
3 import dr.evolution.tree.Clade;
4 import dr.evolution.tree.CladeSet;
5 import dr.evolution.tree.NodeRef;
6 import dr.evolution.tree.Tree;
7
8 import java.util.*;
9
10 import static dr.evolution.tree.treemetrics.TreeMetric.Utils.checkTreeTaxa;
11
12 /**
13 * For each clade_j in treeB, find the MRCA_j of the taxa in clade_j in treeA.
14 *
15 * obviously, if clade_i exists in treeB then clade_i == MRCA_i.
16 *
17 * Then find the sum of squares of the differences in height:
18 *
19 * d = sqrt( sum across i[ (height(clade_i) - height(MRCA_i))^2 ] +
20 * sum across j[ (height(clade_j) - height(MRCA_j))^2 ] )
21 *
22 * The rationale is that if a clade moves then this includes the size of movement
23 * across the MRCA node and down again. I.e., a clade that moves from one side
24 * of the tree to the other scores the difference between the height of that node
25 * and the root and back down to the height of the node in the other tree.
26 *
27 * +---------A +---------A
28 * +-+ +-+
29 * | +---------B | |+--------B
30 * + ====> + ++
31 * | +---------C | +--------C
32 * +-+ |
33 * +---------D +-----------D
34 *
35 * height(tree1) tmrca(tree2) diff
36 * AB 10 10 0
37 * CD 10 12 2
38 * ABCD 12 12 0
39 *
40 * height(tree1) tmrca(tree2) diff
41 * BC 9 12 3
42 * ABC 10 10 0
43 * ABCD 12 12 0
44 *
45 * So the score is sqrt(2^2 + 3^2) = sqrt(13)
46 *
47 * Scores much less than this:
48 *
49 * +----------A +----------A
50 * + + +-+
51 * | +----------B | | +-B
52 * + ====> + +--------+
53 * | +---C | +-C
54 * +--------+ |
55 * +---D +------------D
56 *
57 * height(tree1) tmrca(tree2) diff
58 * AB 10 10 0
59 * CD 4 12 8
60 * ABCD 12 12 0
61 *
62 * height(tree1) tmrca(tree2) diff
63 * BC 2 12 10
64 * ABC 10 10 0
65 * ABCD 12 12 0
66 *
67 * So the score is sqrt(8^2 + 10^2) = sqrt(164)
68 *
69 * @author Andrew Rambaut
70 * @version $Id$
71 */
72 public class CladeHeightMetric implements TreeMetric {
73 public static Type TYPE = Type.CLADE_HEIGHT;
74
75 public CladeHeightMetric() {
76
77 }
78
79 @Override
80 public double getMetric(Tree tree1, Tree tree2) {
81
82 checkTreeTaxa(tree1, tree2);
83
84 Set<Clade> clades1 = Clade.getCladeSet(tree1);
85 Set<Clade> clades2 = Clade.getCladeSet(tree2);
86
87 return getDistance(clades1, clades2);
88 }
89
90 private double getDistance(Set<Clade> clades1, Set<Clade> clades2) {
91
92 double distance = 0.0;
93
94 for (Clade clade1 : clades1) {
95 double height1 = clade1.getHeight();
96
97 Clade clade2 = findMRCA(clade1, clades2);
98 double height2 = clade2.getHeight();
99
100 distance += (height1 - height2) * (height1 - height2);
101 }
102
103 for (Clade clade2 : clades2) {
104 double height2 = clade2.getHeight();
105
106 Clade clade1 = findMRCA(clade2, clades1);
107 double height1 = clade1.getHeight();
108
109 distance += (height1 - height2) * (height1 - height2);
110 }
111
112 return Math.sqrt(distance);
113 }
114
115 private Clade findMRCA(Clade clade1, Set<Clade> clades) {
116
117 for (Clade clade2 : clades) {
118 if (isMRCA(clade1, clade2)) {
119 return clade2;
120 }
121 }
122
123 return null;
124 }
125
126 private boolean isMRCA(Clade clade1, Clade clade2) {
127 if (clade1.getSize() > clade2.getSize()) {
128 return false;
129 }
130
131 tmpBits.clear();
132 tmpBits.or(clade1.getBits());
133 tmpBits.and(clade2.getBits());
134
135 return tmpBits.cardinality() == clade1.getSize();
136 }
137
138 @Override
139 public Type getType() {
140 return TYPE;
141 }
142
143 @Override
144 public String toString() {
145 return getType().getShortName();
146 }
147
148 BitSet tmpBits = new BitSet();
149
150 }