| 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 jebl.evolution.graphs.Node; |
| 29 |
import jebl.evolution.taxa.Taxon; |
| 30 |
import jebl.evolution.treemetrics.RootedTreeMetric; |
| 31 |
import jebl.evolution.trees.RootedTree; |
| 32 |
|
| 33 |
import java.util.*; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public class CladeMetric implements RootedTreeMetric { |
| 42 |
public CladeMetric() { |
| 43 |
taxonMap = null; |
| 44 |
} |
| 45 |
|
| 46 |
public CladeMetric(List<Taxon> taxa) { |
| 47 |
taxonMap = new HashMap<Taxon, Integer>(); |
| 48 |
for (int i = 0; i < taxa.size(); i++) { |
| 49 |
taxonMap.put(taxa.get(i), i); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
public double getMetric(RootedTree tree1, RootedTree tree2) { |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
Map<Taxon, Integer> tm = taxonMap; |
| 58 |
|
| 59 |
if (tm == null) { |
| 60 |
List<Taxon> taxa = new ArrayList<Taxon>(tree2.getTaxa()); |
| 61 |
|
| 62 |
if (!tree2.getTaxa().equals(taxa)) |
| 63 |
tm = new HashMap<Taxon, Integer>(); |
| 64 |
for (int i = 0; i < taxa.size(); i++) { |
| 65 |
tm.put(taxa.get(i), i); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
Clade reference = getClade(tm, tree1, tree1.getRootNode(), null); |
| 70 |
|
| 71 |
List<Clade> clades = new ArrayList<Clade>(); |
| 72 |
getClades(tm, tree2, tree2.getRootNode(), clades, null); |
| 73 |
|
| 74 |
if (contains(clades, reference)){ |
| 75 |
return 1.0; |
| 76 |
} |
| 77 |
else { |
| 78 |
return 0.0; |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
private void getClades(Map<Taxon, Integer> taxonMap, RootedTree tree, |
| 83 |
Node node, List<Clade> clades, BitSet bits) { |
| 84 |
|
| 85 |
BitSet bits2 = new BitSet(); |
| 86 |
|
| 87 |
if (tree.isExternal(node)) { |
| 88 |
|
| 89 |
int index = taxonMap.get(tree.getTaxon(node)); |
| 90 |
bits2.set(index); |
| 91 |
|
| 92 |
} else { |
| 93 |
|
| 94 |
for (Node child : tree.getChildren(node)) { |
| 95 |
getClades(taxonMap, tree, child, clades, bits2); |
| 96 |
} |
| 97 |
clades.add(new Clade(bits2, tree.getHeight(node))); |
| 98 |
} |
| 99 |
|
| 100 |
if (bits != null) { |
| 101 |
bits.or(bits2); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
private Clade getClade(Map<Taxon, Integer> taxonMap, RootedTree tree, |
| 106 |
Node node, BitSet bits) { |
| 107 |
|
| 108 |
BitSet bits2 = new BitSet(); |
| 109 |
Clade c = null; |
| 110 |
|
| 111 |
if (tree.isExternal(node)) { |
| 112 |
|
| 113 |
int index = taxonMap.get(tree.getTaxon(node)); |
| 114 |
bits2.set(index); |
| 115 |
|
| 116 |
} else { |
| 117 |
|
| 118 |
for (Node child : tree.getChildren(node)) { |
| 119 |
getClade(taxonMap, tree, child, bits2); |
| 120 |
} |
| 121 |
c = new Clade(bits2, tree.getHeight(node)); |
| 122 |
} |
| 123 |
|
| 124 |
if (bits != null) { |
| 125 |
bits.or(bits2); |
| 126 |
} |
| 127 |
|
| 128 |
return c; |
| 129 |
|
| 130 |
} |
| 131 |
|
| 132 |
private boolean contains (List<Clade> list, Clade c){ |
| 133 |
for (Clade cl : list){ |
| 134 |
if (c.equals(cl)){ |
| 135 |
return true; |
| 136 |
} |
| 137 |
} |
| 138 |
return false; |
| 139 |
} |
| 140 |
|
| 141 |
BitSet tmpBits = new BitSet(); |
| 142 |
|
| 143 |
private final Map<Taxon, Integer> taxonMap; |
| 144 |
} |