| 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; |
| 30 |
|
| 31 |
import java.io.BufferedWriter; |
| 32 |
import java.io.File; |
| 33 |
import java.io.FileWriter; |
| 34 |
import java.io.IOException; |
| 35 |
import java.util.*; |
| 36 |
|
| 37 |
import dr.math.MathUtils; |
| 38 |
import dr.math.distributions.Distribution; |
| 39 |
import dr.math.distributions.NormalDistribution; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public class Clade implements Comparable<Clade> { |
| 46 |
|
| 47 |
private final BitSet bits; |
| 48 |
private double height; |
| 49 |
private List<Double> heights; |
| 50 |
private final int size; |
| 51 |
|
| 52 |
public Clade(final BitSet bits, final double height) { |
| 53 |
this.bits = bits; |
| 54 |
this.height = height; |
| 55 |
size = bits.cardinality(); |
| 56 |
heights = new ArrayList<Double>(); |
| 57 |
} |
| 58 |
|
| 59 |
public BitSet getBits() { |
| 60 |
return bits; |
| 61 |
} |
| 62 |
|
| 63 |
public double getHeight() { |
| 64 |
return height; |
| 65 |
} |
| 66 |
|
| 67 |
public double getHeight(int index) { |
| 68 |
return heights.get(index); |
| 69 |
} |
| 70 |
|
| 71 |
public int getSampleCount(){ |
| 72 |
return heights.size(); |
| 73 |
} |
| 74 |
|
| 75 |
public int getSize() { |
| 76 |
return size; |
| 77 |
} |
| 78 |
|
| 79 |
public int compareTo(Clade clade) { |
| 80 |
int setBitIndexI = -1; |
| 81 |
int setBitIndexJ = -1; |
| 82 |
BitSet otherBits = clade.getBits(); |
| 83 |
do { |
| 84 |
setBitIndexI = bits.nextSetBit(setBitIndexI + 1); |
| 85 |
setBitIndexJ = otherBits.nextSetBit(setBitIndexJ + 1); |
| 86 |
} while (setBitIndexI == setBitIndexJ && setBitIndexI != -1); |
| 87 |
|
| 88 |
return (setBitIndexI < setBitIndexJ ? -1 |
| 89 |
: (setBitIndexI > setBitIndexJ ? 1 : 0)); |
| 90 |
} |
| 91 |
|
| 92 |
public boolean equals(Object o){ |
| 93 |
if (o instanceof Clade){ |
| 94 |
return equals((Clade)o); |
| 95 |
} |
| 96 |
return false; |
| 97 |
} |
| 98 |
|
| 99 |
public boolean equals(Clade c){ |
| 100 |
if (size != c.getSize()){ |
| 101 |
return false; |
| 102 |
} |
| 103 |
return compareTo(c) == 0; |
| 104 |
} |
| 105 |
|
| 106 |
public String toString(){ |
| 107 |
return bits.toString(); |
| 108 |
} |
| 109 |
|
| 110 |
public int hashCode(){ |
| 111 |
return bits.hashCode(); |
| 112 |
} |
| 113 |
|
| 114 |
public void addHeight(double height){ |
| 115 |
heights.add(height); |
| 116 |
setHeight(height); |
| 117 |
} |
| 118 |
|
| 119 |
public void setHeight(double height){ |
| 120 |
this.height = height; |
| 121 |
} |
| 122 |
|
| 123 |
public static Set<Clade> getCladeSet(Tree tree) { |
| 124 |
return new HashSet<Clade>(getCladeList(tree)); |
| 125 |
} |
| 126 |
|
| 127 |
public static List<Clade> getCladeList(Tree tree) { |
| 128 |
List<Clade> clades = new ArrayList<Clade>(); |
| 129 |
getClades(clades, tree, tree.getRoot()); |
| 130 |
return clades; |
| 131 |
} |
| 132 |
|
| 133 |
private static BitSet getClades(List<Clade> clades, Tree tree, NodeRef node) { |
| 134 |
|
| 135 |
BitSet bits = new BitSet(); |
| 136 |
|
| 137 |
if (tree.isExternal(node)) { |
| 138 |
|
| 139 |
bits.set(node.getNumber()); |
| 140 |
|
| 141 |
} else { |
| 142 |
bits.or(getClades(clades, tree, tree.getChild(node, 0))); |
| 143 |
bits.or(getClades(clades, tree, tree.getChild(node, 1))); |
| 144 |
|
| 145 |
clades.add(new Clade(bits, tree.getNodeHeight(node))); |
| 146 |
} |
| 147 |
|
| 148 |
return bits; |
| 149 |
} |
| 150 |
|
| 151 |
} |