| 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.evolution.tree; |
| 27 |
|
| 28 |
import dr.evolution.util.TaxonList; |
| 29 |
import dr.util.FrequencySet; |
| 30 |
|
| 31 |
import java.util.*; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public class CladeSet extends FrequencySet<BitSet> { |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public CladeSet() {} |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public CladeSet(Tree tree) |
| 52 |
{ |
| 53 |
this(tree, tree); |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
public CladeSet(Tree tree, TaxonList taxonList) |
| 60 |
{ |
| 61 |
this.taxonList = taxonList; |
| 62 |
add(tree); |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
public int getCladeCount() |
| 67 |
{ |
| 68 |
return size(); |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
public String getClade(int index) |
| 73 |
{ |
| 74 |
BitSet bits = get(index); |
| 75 |
|
| 76 |
StringBuffer buffer = new StringBuffer("{"); |
| 77 |
boolean first = true; |
| 78 |
for (String taxonId : getTaxaSet(bits)) { |
| 79 |
if (!first) { |
| 80 |
buffer.append(", "); |
| 81 |
} else { |
| 82 |
first = false; |
| 83 |
} |
| 84 |
buffer.append(taxonId); |
| 85 |
} |
| 86 |
buffer.append("}"); |
| 87 |
return buffer.toString(); |
| 88 |
} |
| 89 |
|
| 90 |
private SortedSet<String> getTaxaSet(BitSet bits) { |
| 91 |
|
| 92 |
SortedSet<String> taxaSet = new TreeSet<String>(); |
| 93 |
|
| 94 |
for (int i = 0; i < bits.length(); i++) { |
| 95 |
if (bits.get(i)) { |
| 96 |
taxaSet.add(taxonList.getTaxonId(i)); |
| 97 |
} |
| 98 |
} |
| 99 |
return taxaSet; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
int getCladeFrequency(int index) |
| 105 |
{ |
| 106 |
return getFrequency(index); |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
public void add(Tree tree) |
| 111 |
{ |
| 112 |
if (taxonList == null) { |
| 113 |
taxonList = tree; |
| 114 |
} |
| 115 |
|
| 116 |
totalTrees += 1; |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
addClades(tree, tree.getRoot(), null); |
| 121 |
} |
| 122 |
|
| 123 |
private void addClades(Tree tree, NodeRef node, BitSet bits) { |
| 124 |
|
| 125 |
if (tree.isExternal(node)) { |
| 126 |
|
| 127 |
if (taxonList != null) { |
| 128 |
int index = taxonList.getTaxonIndex(tree.getNodeTaxon(node).getId()); |
| 129 |
bits.set(index); |
| 130 |
} else { |
| 131 |
bits.set(node.getNumber()); |
| 132 |
} |
| 133 |
} else { |
| 134 |
|
| 135 |
BitSet bits2 = new BitSet(); |
| 136 |
for (int i = 0; i < tree.getChildCount(node); i++) { |
| 137 |
|
| 138 |
NodeRef node1 = tree.getChild(node, i); |
| 139 |
|
| 140 |
addClades(tree, node1, bits2); |
| 141 |
} |
| 142 |
|
| 143 |
add(bits2, 1); |
| 144 |
addNodeHeight(bits2, tree.getNodeHeight(node)); |
| 145 |
|
| 146 |
if (bits != null) { |
| 147 |
bits.or(bits2); |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
public double getMeanNodeHeight(int i) { |
| 153 |
BitSet bits = get(i); |
| 154 |
|
| 155 |
return getTotalNodeHeight(bits) / getFrequency(i); |
| 156 |
} |
| 157 |
|
| 158 |
private double getTotalNodeHeight(BitSet bits) { |
| 159 |
Double tnh = totalNodeHeight.get(bits); |
| 160 |
if (tnh == null) return 0.0; |
| 161 |
return tnh; |
| 162 |
} |
| 163 |
|
| 164 |
private void addNodeHeight(BitSet bits, double height) { |
| 165 |
totalNodeHeight.put(bits, (getTotalNodeHeight(bits) + height)); |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
private BitSet annotate(MutableTree tree, NodeRef node, String freqAttrName) { |
| 179 |
BitSet b = null; |
| 180 |
if (tree.isExternal(node)) { |
| 181 |
int index; |
| 182 |
if (taxonList != null) { |
| 183 |
index = taxonList.getTaxonIndex(tree.getNodeTaxon(node).getId()); |
| 184 |
} else { |
| 185 |
index = node.getNumber(); |
| 186 |
} |
| 187 |
b = new BitSet(tree.getExternalNodeCount()); |
| 188 |
b.set(index); |
| 189 |
|
| 190 |
} else { |
| 191 |
|
| 192 |
for (int i = 0; i < tree.getChildCount(node); i++) { |
| 193 |
NodeRef child = tree.getChild(node, i); |
| 194 |
BitSet b1 = annotate(tree, child, freqAttrName); |
| 195 |
if( i == 0 ) { |
| 196 |
b = b1; |
| 197 |
} else { |
| 198 |
b.or(b1); |
| 199 |
} |
| 200 |
} |
| 201 |
final int total = getFrequency(b); |
| 202 |
if( total >= 0 ) { |
| 203 |
tree.setNodeAttribute(node, freqAttrName, total / (double)totalTrees ); |
| 204 |
} |
| 205 |
} |
| 206 |
return b; |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
public double annotate(MutableTree tree, String freqAttrName) { |
| 216 |
annotate(tree, tree.getRoot(), freqAttrName); |
| 217 |
|
| 218 |
double logClade = 0.0; |
| 219 |
for(int n = 0; n < tree.getInternalNodeCount(); ++n) { |
| 220 |
final double f = (Double)tree.getNodeAttribute(tree.getInternalNode(n), freqAttrName); |
| 221 |
logClade += Math.log(f); |
| 222 |
} |
| 223 |
return logClade; |
| 224 |
} |
| 225 |
|
| 226 |
public boolean hasClade(int index, Tree tree) { |
| 227 |
BitSet bits = get(index); |
| 228 |
|
| 229 |
NodeRef[] mrca = new NodeRef[1]; |
| 230 |
findClade(bits, tree, tree.getRoot(), mrca); |
| 231 |
|
| 232 |
return (mrca[0] != null); |
| 233 |
} |
| 234 |
|
| 235 |
private int findClade(BitSet bitSet, Tree tree, NodeRef node, NodeRef[] cladeMRCA) { |
| 236 |
|
| 237 |
if (tree.isExternal(node)) { |
| 238 |
|
| 239 |
if (taxonList != null) { |
| 240 |
int index = taxonList.getTaxonIndex(tree.getNodeTaxon(node).getId()); |
| 241 |
if (bitSet.get(index)) return 1; |
| 242 |
} else { |
| 243 |
if (bitSet.get(node.getNumber())) return 1; |
| 244 |
} |
| 245 |
return -1; |
| 246 |
} else { |
| 247 |
int count = 0; |
| 248 |
for (int i = 0; i < tree.getChildCount(node); i++) { |
| 249 |
|
| 250 |
NodeRef node1 = tree.getChild(node, i); |
| 251 |
|
| 252 |
int childCount = findClade(bitSet, tree, node1, cladeMRCA); |
| 253 |
|
| 254 |
if (childCount != -1 && count != -1) { |
| 255 |
count += childCount; |
| 256 |
} else count = -1; |
| 257 |
} |
| 258 |
|
| 259 |
if (count == bitSet.cardinality()) cladeMRCA[0] = node; |
| 260 |
|
| 261 |
return count; |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
private TaxonList taxonList = null; |
| 269 |
private final Map<BitSet, Double> totalNodeHeight = new HashMap<BitSet, Double>(); |
| 270 |
private int totalTrees = 0; |
| 271 |
} |