Debian Codegraph @

src/dr/app/treespace/TreeLineages.java

raw symbols SCIP
1 /*
2 * TreeLineages.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
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 * @author Andrew Rambaut
37 * @version $Id$
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 // the offset is the distance to the right most tip - used to align the tips of the tree
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 // set the tip count to zero for this traversal
123 // currentY = 0.0;
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 // lineage.ax = (Double)ag[0];
189 // lineage.ay = (Double)ag[1];
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 // lineage.ay = ag2;
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 // the y of this node is the average of the two children
218 lineage.dy /= 2.0;
219
220 // now change the children to relative y positions.
221 // lineage.child1.dy = lineage.child1.dy - lineage.dy;
222 // lineage.child2.dy = lineage.child2.dy - lineage.dy;
223
224 Double location = nodeLocations.get(lineage.bits);
225 if (location == null) {
226 BitSet bestBits = lineage.bits;
227
228 if (!majorityNodes.contains(bestBits)) {
229 // otherwise find a bigger node...
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 // lineage.dy = LATITUDES[lineage.state];
260 lineage.dy = lineage.tipNumber;
261 // lineage.dy = currentY;
262 // currentY += 1.0;
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 }