| 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.app.treespace; |
| 27 |
|
| 28 |
import dr.xml.Reference; |
| 29 |
import jebl.evolution.graphs.Node; |
| 30 |
import jebl.evolution.trees.RootedTree; |
| 31 |
|
| 32 |
import java.awt.geom.Rectangle2D; |
| 33 |
import java.util.*; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public class TreeLineages { |
| 40 |
|
| 41 |
private final static String[] LOCATIONS = { |
| 42 |
"Africa", |
| 43 |
"USA", |
| 44 |
"Taiwan", |
| 45 |
"China", |
| 46 |
"Russia", |
| 47 |
"Oceania", |
| 48 |
"Asia", |
| 49 |
"Japan", |
| 50 |
"Mexico", |
| 51 |
"South America", |
| 52 |
"Canada", |
| 53 |
"Europe", |
| 54 |
"Southeast Asia", |
| 55 |
"South Korea" |
| 56 |
}; |
| 57 |
|
| 58 |
private final static String[] AIR_COMMUNITIES = { |
| 59 |
"AC1", |
| 60 |
"AC2", |
| 61 |
"AC3", |
| 62 |
"AC4", |
| 63 |
"AC5", |
| 64 |
"AC6", |
| 65 |
"AC7", |
| 66 |
"AC8", |
| 67 |
"AC9", |
| 68 |
"AC10", |
| 69 |
"AC11", |
| 70 |
"AC12", |
| 71 |
"AC13", |
| 72 |
"AC14" |
| 73 |
}; |
| 74 |
|
| 75 |
Map<String, Integer> locationMap = new HashMap<String, Integer>(); |
| 76 |
private static final double THRESHOLD = 0.01; |
| 77 |
|
| 78 |
public TreeLineages() { |
| 79 |
for (int i = 0; i < AIR_COMMUNITIES.length; i++) { |
| 80 |
locationMap.put(AIR_COMMUNITIES[i], i); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
public List<Lineage> getRootLineages() { |
| 85 |
return rootLineages; |
| 86 |
} |
| 87 |
|
| 88 |
public Rectangle2D getTreeBounds() { |
| 89 |
return treeBounds; |
| 90 |
} |
| 91 |
|
| 92 |
public Rectangle2D getAntigenicBounds() { |
| 93 |
return antigenicBounds; |
| 94 |
} |
| 95 |
|
| 96 |
public void addTree(RootedTree tree) { |
| 97 |
|
| 98 |
|
| 99 |
offsetX = 0.0; |
| 100 |
Lineage lineage = new Lineage(); |
| 101 |
addNode(tree, tree.getRootNode(), lineage, 0.0); |
| 102 |
|
| 103 |
lineage.dx = offsetX; |
| 104 |
|
| 105 |
rootLineages.add(lineage); |
| 106 |
} |
| 107 |
|
| 108 |
public void setupTrees() { |
| 109 |
for (BitSet key : nodeCounts.keySet()) { |
| 110 |
int count = nodeCounts.get(key); |
| 111 |
if ((((double)count) / rootLineages.size()) > THRESHOLD) { |
| 112 |
majorityNodes.add(key); |
| 113 |
} |
| 114 |
} |
| 115 |
Collections.sort(majorityNodes, new Comparator<BitSet>() { |
| 116 |
public int compare(BitSet bitSet, BitSet bitSet1) { |
| 117 |
return bitSet.cardinality() - bitSet1.cardinality(); |
| 118 |
} |
| 119 |
}); |
| 120 |
|
| 121 |
for (Lineage rootLineage : rootLineages) { |
| 122 |
|
| 123 |
|
| 124 |
positionNode(rootLineage); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
public BitSet addNode(RootedTree tree, Node node, Lineage lineage, double cumulativeX) { |
| 129 |
lineage.bits = new BitSet(); |
| 130 |
|
| 131 |
cumulativeX += tree.getLength(node); |
| 132 |
lineage.dx = cumulativeX; |
| 133 |
|
| 134 |
String location = (String)node.getAttribute("states"); |
| 135 |
if (location != null) { |
| 136 |
lineage.state = locationMap.get(location); |
| 137 |
} |
| 138 |
|
| 139 |
if (!tree.isExternal(node)) { |
| 140 |
List<Node> children = tree.getChildren(node); |
| 141 |
if (children.size() != 2) { |
| 142 |
throw new RuntimeException("Tree is not binary"); |
| 143 |
} |
| 144 |
|
| 145 |
lineage.child1 = new Lineage(); |
| 146 |
lineage.child2 = new Lineage(); |
| 147 |
|
| 148 |
lineage.bits.or(addNode(tree, children.get(0), lineage.child1, cumulativeX)); |
| 149 |
lineage.bits.or(addNode(tree, children.get(1), lineage.child2, cumulativeX)); |
| 150 |
|
| 151 |
lineage.tipCount = lineage.child1.tipCount + lineage.child2.tipCount; |
| 152 |
|
| 153 |
if (lineage.child1.tipCount > lineage.child2.tipCount || |
| 154 |
(lineage.child1.tipCount == lineage.child2.tipCount && |
| 155 |
lineage.child1.tipNumber > lineage.child2.tipNumber)) { |
| 156 |
Lineage tmp = lineage.child1; |
| 157 |
lineage.child1 = lineage.child2; |
| 158 |
lineage.child2 = tmp; |
| 159 |
} |
| 160 |
|
| 161 |
Integer count = nodeCounts.get(lineage.bits); |
| 162 |
if (count == null) { |
| 163 |
count = 1; |
| 164 |
} else { |
| 165 |
count ++; |
| 166 |
} |
| 167 |
nodeCounts.put(lineage.bits, count); |
| 168 |
|
| 169 |
} else { |
| 170 |
Integer tipNumber = taxonNumbers.get(tree.getTaxon(node).getName()); |
| 171 |
if (tipNumber == null) { |
| 172 |
tipNumber = taxonNumbers.size(); |
| 173 |
taxonNumbers.put(tree.getTaxon(node).getName(), tipNumber); |
| 174 |
} |
| 175 |
|
| 176 |
lineage.tipNumber = tipNumber; |
| 177 |
lineage.tipCount = 1; |
| 178 |
|
| 179 |
lineage.bits.set(lineage.tipNumber); |
| 180 |
|
| 181 |
if (cumulativeX > offsetX) { |
| 182 |
offsetX = cumulativeX; |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
Object[] ag = (Object[])node.getAttribute("antigenic"); |
| 187 |
if (ag != null) { |
| 188 |
|
| 189 |
|
| 190 |
lineage.ax = lineage.dx; |
| 191 |
lineage.ay = (Double)ag[0]; |
| 192 |
|
| 193 |
antigenicBounds.add(lineage.ax, lineage.ay); |
| 194 |
} else { |
| 195 |
Double ag1 = (Double)node.getAttribute("antigenic1"); |
| 196 |
Double ag2 = (Double)node.getAttribute("antigenic2"); |
| 197 |
if (ag1 != null) { |
| 198 |
double ad = |
| 199 |
|
| 200 |
lineage.ay = ag1; |
| 201 |
|
| 202 |
lineage.ax = lineage.dx; |
| 203 |
|
| 204 |
antigenicBounds.add(lineage.ax, lineage.ay); |
| 205 |
} |
| 206 |
|
| 207 |
} |
| 208 |
|
| 209 |
return lineage.bits; |
| 210 |
} |
| 211 |
|
| 212 |
public double positionNode(Lineage lineage) { |
| 213 |
if (lineage.child1 != null) { |
| 214 |
lineage.dy = positionNode(lineage.child1); |
| 215 |
lineage.dy += positionNode(lineage.child2); |
| 216 |
|
| 217 |
|
| 218 |
lineage.dy /= 2.0; |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
Double location = nodeLocations.get(lineage.bits); |
| 225 |
if (location == null) { |
| 226 |
BitSet bestBits = lineage.bits; |
| 227 |
|
| 228 |
if (!majorityNodes.contains(bestBits)) { |
| 229 |
|
| 230 |
for (BitSet bits : majorityNodes) { |
| 231 |
BitSet bits1 = (BitSet)bits.clone(); |
| 232 |
bits1.and(lineage.bits); |
| 233 |
if (bits1.cardinality() == lineage.bits.cardinality()) { |
| 234 |
bestBits = bits; |
| 235 |
break; |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
location = nodeLocations.get(bestBits); |
| 240 |
if (location != null) { |
| 241 |
lineage.dy = location; |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
nodeLocations.put(bestBits, lineage.dy); |
| 246 |
|
| 247 |
} else { |
| 248 |
lineage.dy = location; |
| 249 |
} |
| 250 |
|
| 251 |
} else { |
| 252 |
Integer orderedTipNumber = orderedTipNumbers.get(lineage.tipNumber); |
| 253 |
if (orderedTipNumber == null) { |
| 254 |
orderedTipNumber = orderedTipNumbers.size(); |
| 255 |
orderedTipNumbers.put(lineage.tipNumber, orderedTipNumber); |
| 256 |
} |
| 257 |
|
| 258 |
lineage.tipNumber = orderedTipNumber; |
| 259 |
|
| 260 |
lineage.dy = lineage.tipNumber; |
| 261 |
|
| 262 |
|
| 263 |
} |
| 264 |
|
| 265 |
treeBounds.add(lineage.dx, lineage.dy); |
| 266 |
|
| 267 |
return lineage.dy; |
| 268 |
} |
| 269 |
|
| 270 |
class Lineage { |
| 271 |
double dx = 0; |
| 272 |
double dy = 0; |
| 273 |
double ax = 0; |
| 274 |
double ay = 0; |
| 275 |
Lineage child1 = null; |
| 276 |
Lineage child2 = null; |
| 277 |
int tipNumber = 0; |
| 278 |
int tipCount = 0; |
| 279 |
int state; |
| 280 |
BitSet bits; |
| 281 |
} |
| 282 |
|
| 283 |
private List<Lineage> rootLineages = new ArrayList<Lineage>(); |
| 284 |
private Map<String, Integer> taxonNumbers = new HashMap<String, Integer>(); |
| 285 |
private Map<Integer, Integer> orderedTipNumbers = new HashMap<Integer, Integer>(); |
| 286 |
private Map<BitSet, Integer> nodeCounts = new HashMap<BitSet, Integer>(); |
| 287 |
private Map<BitSet, Double> nodeLocations = new HashMap<BitSet, Double>(); |
| 288 |
private List<BitSet> majorityNodes = new ArrayList<BitSet>(); |
| 289 |
|
| 290 |
private double offsetX; |
| 291 |
|
| 292 |
private Rectangle2D treeBounds = new Rectangle2D.Double(); |
| 293 |
private Rectangle2D antigenicBounds = new Rectangle2D.Double(); |
| 294 |
} |