Debian Codegraph @

src/dr/app/treespace/CladeSystem.java

raw symbols SCIP
1 /*
2 * CladeSystem.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.evolution.tree.*;
29 import dr.evolution.util.TaxonList;
30 import java.util.*;
31
32 /**
33 * @author Andrew Rambaut
34 * @version $Id$
35 */
36 public class CladeSystem {
37 /**
38 */
39 public CladeSystem() {
40 }
41
42 /**
43 * adds all the clades in the tree
44 */
45 public void add(Tree tree, boolean includeTips) {
46 if (taxonList == null) {
47 taxonList = tree;
48 }
49
50 // Recurse over the tree and add all the clades (or increment their
51 // frequency if already present). The root clade is added too (for
52 // annotation purposes).
53 addClades(tree, tree.getRoot(), includeTips);
54 }
55 //
56 // public Clade getClade(NodeRef node) {
57 // return null;
58 // }
59
60 private BitSet addClades(Tree tree, NodeRef node, boolean includeTips) {
61
62 BitSet bits = new BitSet();
63
64 if (tree.isExternal(node)) {
65
66 int index = taxonList.getTaxonIndex(tree.getNodeTaxon(node).getId());
67 bits.set(index);
68
69 if (includeTips) {
70 addClade(bits, taxonList.getTaxon(index).getId());
71 }
72
73 } else {
74
75 for (int i = 0; i < tree.getChildCount(node); i++) {
76
77 NodeRef node1 = tree.getChild(node, i);
78
79 bits.or(addClades(tree, node1, includeTips));
80 }
81
82 addClade(bits);
83 }
84
85 return bits;
86 }
87
88 private void addClade(BitSet bits) {
89 addClade(bits, null);
90 }
91
92 private void addClade(BitSet bits, String label) {
93 Clade clade = cladeMap.get(bits);
94 if (clade == null) {
95 clade = new Clade(bits);
96 cladeMap.put(bits, clade);
97 }
98 clade.setCount(clade.getCount() + 1);
99 clade.label = label;
100 }
101
102 /**
103 * adds all the clades in the tree
104 */
105 public void addCooccurances(Tree tree) {
106 addCladeCooccurances(tree, tree.getRoot(), null);
107 }
108
109 private BitSet addCladeCooccurances(Tree tree, NodeRef node, Clade parent) {
110
111 BitSet bits = getCladeBitset(tree, node);
112 Clade clade = cladeMap.get(bits);
113
114 if (!tree.isExternal(node)) {
115 if (clade == null) {
116 throw new IllegalArgumentException("Clade missing for cooccurance network");
117 }
118
119 for (int i = 0; i < tree.getChildCount(node); i++) {
120 bits.or(addCladeCooccurances(tree, tree.getChild(node, i), clade));
121 }
122 }
123
124 if (clade != null && parent != null) {
125 if (clade.parents == null) {
126 clade.parents = new HashMap<Clade, Integer>();
127 }
128
129 Integer frequency = clade.parents.get(parent);
130 if (frequency == null) {
131 frequency = 1;
132 } else {
133 frequency += 1;
134 }
135 clade.parents.put(parent, frequency);
136
137 if (parent.children == null) {
138 parent.children = new HashMap<Clade, Integer>();
139 }
140 frequency = parent.children.get(clade);
141 if (frequency == null) {
142 frequency = 1;
143 } else {
144 frequency += 1;
145 }
146 parent.children.put(clade, frequency);
147 }
148
149 return bits;
150 }
151
152 private BitSet getCladeBitset(Tree tree, NodeRef node) {
153
154 BitSet bits = new BitSet();
155
156 if (tree.isExternal(node)) {
157
158 int index = taxonList.getTaxonIndex(tree.getNodeTaxon(node).getId());
159 bits.set(index);
160
161 } else {
162
163 for (int i = 0; i < tree.getChildCount(node); i++) {
164 bits.or(getCladeBitset(tree, tree.getChild(node, i)));
165 }
166 }
167
168 return bits;
169 }
170
171 public Map<BitSet, Clade> getCladeMap() {
172 return cladeMap;
173 }
174
175 public void normalizeClades(int totalTreesUsed) {
176 int i = 0;
177 for (Clade clade : cladeMap.values()) {
178
179 if (clade.getCount() > totalTreesUsed) {
180
181 throw new AssertionError("clade.getCount=(" + clade.getCount() +
182 ") should be <= totalTreesUsed = (" + totalTreesUsed + ")");
183 }
184
185 clade.setCredibility(((double) clade.getCount()) / (double) totalTreesUsed);
186 clade.index = i;
187 i++;
188 }
189 }
190
191
192 public double getLogCladeCredibility(Tree tree, NodeRef node, BitSet bits) {
193
194 double logCladeCredibility = 0.0;
195
196 if (tree.isExternal(node)) {
197
198 int index = taxonList.getTaxonIndex(tree.getNodeTaxon(node).getId());
199 bits.set(index);
200 } else {
201
202 BitSet bits2 = new BitSet();
203 for (int i = 0; i < tree.getChildCount(node); i++) {
204
205 NodeRef node1 = tree.getChild(node, i);
206
207 logCladeCredibility += getLogCladeCredibility(tree, node1, bits2);
208 }
209
210 logCladeCredibility += Math.log(getCladeCredibility(bits2));
211
212 if (bits != null) {
213 bits.or(bits2);
214 }
215 }
216
217 return logCladeCredibility;
218 }
219
220 private double getCladeCredibility(BitSet bits) {
221 Clade clade = cladeMap.get(bits);
222 if (clade == null) {
223 return 0.0;
224 }
225 return clade.getCredibility();
226 }
227
228 public BitSet removeClades(Tree tree, NodeRef node, boolean includeTips) {
229
230 BitSet bits = new BitSet();
231
232 if (tree.isExternal(node)) {
233
234 int index = taxonList.getTaxonIndex(tree.getNodeTaxon(node).getId());
235 bits.set(index);
236
237 if (includeTips) {
238 removeClade(bits);
239 }
240
241 } else {
242
243 for (int i = 0; i < tree.getChildCount(node); i++) {
244
245 NodeRef node1 = tree.getChild(node, i);
246
247 bits.or(removeClades(tree, node1, includeTips));
248 }
249
250 removeClade(bits);
251 }
252
253 return bits;
254 }
255
256 private void removeClade(BitSet bits) {
257 Clade clade = cladeMap.get(bits);
258 if (clade != null) {
259 clade.setCount(clade.getCount() - 1);
260 }
261
262 }
263
264 public List<Clade> getClades() {
265 return new ArrayList<Clade>(cladeMap.values());
266 }
267
268 class Clade {
269 public Clade(BitSet bits) {
270 this.bits = bits;
271 count = 0;
272 credibility = 0.0;
273 }
274
275 public int getCount() {
276 return count;
277 }
278
279 public void setCount(int count) {
280 this.count = count;
281 }
282
283 public double getCredibility() {
284 return credibility;
285 }
286
287 public void setCredibility(double credibility) {
288 this.credibility = credibility;
289 }
290
291 public boolean equals(Object o) {
292 if (this == o) return true;
293 if (o == null || getClass() != o.getClass()) return false;
294
295 final Clade clade = (Clade) o;
296
297 return !(bits != null ? !bits.equals(clade.bits) : clade.bits != null);
298
299 }
300
301 public int hashCode() {
302 return (bits != null ? bits.hashCode() : 0);
303 }
304
305 public String toString() {
306 return "clade " + bits.toString();
307 }
308
309 int count;
310 double credibility;
311 BitSet bits;
312 int index;
313 String label;
314 double height;
315 float x, y;
316 Map<Clade, Integer> children;
317 Map<Clade, Integer> parents;
318 }
319
320 //
321 // Private stuff
322 //
323 TaxonList taxonList = null;
324 Map<BitSet, Clade> cladeMap = new HashMap<BitSet, Clade>();
325
326 Tree targetTree;
327 }