| 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 |
|
| 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 |
|
| 40 |
|
| 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 |
|
| 69 |
|
| 70 |
throw new RuntimeException("Focal tree is different from that set in the constructor."); |
| 71 |
} |
| 72 |
|
| 73 |
|
| 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 |
|
| 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 |
} |