Debian Codegraph @

src/dr/evomodel/tree/AbstractCladeImportanceDistribution.java

raw symbols SCIP
1 /*
2 * AbstractCladeImportanceDistribution.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 /**
27 *
28 */
29 package dr.evomodel.tree;
30
31 import dr.evolution.tree.Clade;
32 import dr.evolution.tree.ImportanceDistribution;
33 import dr.evolution.tree.NodeRef;
34 import dr.evolution.tree.Tree;
35 import dr.inference.model.Likelihood;
36
37 import java.util.BitSet;
38 import java.util.HashMap;
39 import java.util.List;
40
41 /**
42 * @author shhn001
43 */
44 public abstract class AbstractCladeImportanceDistribution implements
45 ImportanceDistribution {
46
47 /**
48 *
49 */
50 public AbstractCladeImportanceDistribution() {
51 // TODO Auto-generated constructor stub
52 }
53
54 /**
55 * creates a list with all clades but just the non-complementary ones.
56 * ((A,B),(C,D)) just {A,B,C,D} and {A,B} are inserted. {A,B} is
57 * complementary to {C,D}
58 *
59 * @param tree - the tree from which the clades are extracted
60 * @param node - the starting node. All clades below starting at this branch
61 * are added
62 */
63 protected Clade getNonComplementaryClades(Tree tree, NodeRef node,
64 List<Clade> parentClades, List<Clade> childClade,
65 HashMap<String, Integer> taxonMap) {
66
67 // create a new bit set for this clade
68 BitSet bits = new BitSet();
69 Clade c = null;
70
71 // check if the node is external
72 if (tree.isExternal(node)) {
73
74 // if so, the only taxon in the clade is I
75 // int index = node.getNumber();
76 String taxon = tree.getTaxon(node.getNumber()).getId();
77 int index = taxonMap.get(taxon);
78 bits.set(index);
79
80 c = new Clade(bits, tree.getNodeHeight(node));
81
82 } else {
83 // otherwise, call all children and add its taxon together to one
84 // clade
85
86 NodeRef childNode = tree.getChild(node, 0);
87 // add just my first child to the list
88 // the second child is complementary to the first
89 Clade leftChild = getNonComplementaryClades(tree, childNode,
90 parentClades, childClade, taxonMap);
91 bits.or(leftChild.getBits());
92
93 childNode = tree.getChild(node, 1);
94 // add just my first child to the list
95 // the second child is complementary to the first
96 Clade rightChild = getNonComplementaryClades(tree, childNode,
97 parentClades, childClade, taxonMap);
98 bits.or(rightChild.getBits());
99
100 c = new Clade(bits, tree.getNodeHeight(node));
101
102 if (leftChild.getSize() >= 2) {
103 parentClades.add(c);
104 childClade.add(leftChild);
105 } else if (rightChild.getSize() >= 2) {
106 parentClades.add(c);
107 childClade.add(rightChild);
108 }
109 }
110
111 return c;
112 }
113
114 /**
115 * creates a list with all clades but just the non-complementary ones.
116 * ((A,B),(C,D)) just {A,B,C,D} and {A,B} are inserted. {A,B} is
117 * complementary to {C,D}
118 *
119 * @param tree - the tree from which the clades are extracted
120 * @param node - the starting node. All clades below starting at this branch
121 * are added
122 */
123 protected Clade getNonComplementaryClades(Tree tree, NodeRef node,
124 List<Clade> parentClades, List<Clade> childClade) {
125
126 // create a new bit set for this clade
127 BitSet bits = new BitSet();
128 Clade c = null;
129
130 // check if the node is external
131 if (tree.isExternal(node)) {
132
133 // if so, the only taxon in the clade is I
134 int index = node.getNumber();
135 bits.set(index);
136
137 c = new Clade(bits, tree.getNodeHeight(node));
138
139 } else {
140 // otherwise, call all children and add its taxon together to one
141 // clade
142
143 NodeRef childNode = tree.getChild(node, 0);
144 // add just my first child to the list
145 // the second child is complementary to the first
146 Clade leftChild = getNonComplementaryClades(tree, childNode,
147 parentClades, childClade);
148 bits.or(leftChild.getBits());
149
150 childNode = tree.getChild(node, 1);
151 // add just my first child to the list
152 // the second child is complementary to the first
153 Clade rightChild = getNonComplementaryClades(tree, childNode,
154 parentClades, childClade);
155 bits.or(rightChild.getBits());
156
157 c = new Clade(bits, tree.getNodeHeight(node));
158
159 if (leftChild.getSize() >= 2) {
160 parentClades.add(c);
161 childClade.add(leftChild);
162 } else if (rightChild.getSize() >= 2) {
163 parentClades.add(c);
164 childClade.add(rightChild);
165 }
166 }
167
168 return c;
169 }
170
171 /**
172 * Creates a list with all clades of the tree
173 *
174 * @param tree - the tree from which the clades are extracted
175 * @param node - the starting node. All clades below starting at this branch
176 * are added
177 */
178 protected Clade getClades(Tree tree, NodeRef node,
179 List<Clade> parentClades, List<Clade> childClade) {
180
181 // create a new bit set for this clade
182 BitSet bits = new BitSet();
183 Clade c = null;
184
185 // check if the node is external
186 if (tree.isExternal(node)) {
187
188 // if so, the only taxon in the clade is I
189 int index = node.getNumber();
190 bits.set(index);
191
192 c = new Clade(bits, tree.getNodeHeight(node));
193
194 } else {
195 // otherwise, call all children and add its taxon together to one
196 // clade
197
198 NodeRef childNode = tree.getChild(node, 0);
199 // add just my first child to the list
200 // the second child is complementary to the first
201 Clade leftChild = getClades(tree, childNode, parentClades,
202 childClade);
203 bits.or(leftChild.getBits());
204
205 childNode = tree.getChild(node, 1);
206 // add just my first child to the list
207 // the second child is complementary to the first
208 Clade rightChild = getClades(tree, childNode, parentClades,
209 childClade);
210 bits.or(rightChild.getBits());
211
212 c = new Clade(bits, tree.getNodeHeight(node));
213
214 if (leftChild.getSize() >= 2) {
215 parentClades.add(c);
216 childClade.add(leftChild);
217 }
218 if (rightChild.getSize() >= 2) {
219 parentClades.add(c);
220 childClade.add(rightChild);
221 }
222 }
223
224 return c;
225 }
226
227 /**
228 * Creates a list with all clades of the tree
229 *
230 * @param taxonMap - the lookup map for the taxon representing an index
231 * @param tree - the tree from which the clades are extracted
232 * @param node - the starting node. All clades below starting at this branch
233 * are added
234 */
235 protected Clade getClades(Tree tree, NodeRef node,
236 List<Clade> parentClades, List<Clade> childClade,
237 HashMap<String, Integer> taxonMap) {
238
239 // create a new bit set for this clade
240 BitSet bits = new BitSet();
241 Clade c = null;
242
243 // check if the node is external
244 if (tree.isExternal(node)) {
245
246 // if so, the only taxon in the clade is I
247 // int index = node.getNumber();
248 String taxon = tree.getTaxon(node.getNumber()).getId();
249 int index = taxonMap.get(taxon);
250 bits.set(index);
251
252 c = new Clade(bits, tree.getNodeHeight(node));
253
254 } else {
255 // otherwise, call all children and add its taxon together to one
256 // clade
257
258 NodeRef childNode = tree.getChild(node, 0);
259 // add just my first child to the list
260 // the second child is complementary to the first
261 Clade leftChild = getClades(tree, childNode, parentClades,
262 childClade, taxonMap);
263 bits.or(leftChild.getBits());
264
265 childNode = tree.getChild(node, 1);
266 // add just my first child to the list
267 // the second child is complementary to the first
268 Clade rightChild = getClades(tree, childNode, parentClades,
269 childClade, taxonMap);
270 bits.or(rightChild.getBits());
271
272 c = new Clade(bits, tree.getNodeHeight(node));
273
274 if (leftChild.getSize() >= 2) {
275 parentClades.add(c);
276 childClade.add(leftChild);
277 }
278 if (rightChild.getSize() >= 2) {
279 parentClades.add(c);
280 childClade.add(rightChild);
281 }
282 }
283
284 return c;
285 }
286
287 /**
288 * Creates a list with all clades of the tree
289 *
290 * @param tree - the tree from which the clades are extracted
291 * @param node - the starting node. All clades below starting at this branch
292 * are added
293 * @param clades - the list in which the clades are stored
294 * @param bits - a bit set to which the current bits of the clades are added
295 */
296 public void getClades(Tree tree, NodeRef node, List<Clade> clades,
297 BitSet bits) {
298
299 // create a new bit set for this clade
300 BitSet bits2 = new BitSet();
301
302 // check if the node is external
303 if (tree.isExternal(node)) {
304
305 // if so, the only taxon in the clade is I
306 int index = node.getNumber();
307 bits2.set(index);
308
309 } else {
310
311 // otherwise, call all children and add its taxon together to one
312 // clade
313 for (int i = 0; i < tree.getChildCount(node); i++) {
314 NodeRef child = tree.getChild(node, i);
315 getClades(tree, child, clades, bits2);
316 }
317 // add my bit set to the list
318 clades.add(new Clade(bits2, tree.getNodeHeight(node)));
319 }
320
321 // add my bit set to the bit set I was given
322 // this is needed for adding all children clades together
323 if (bits != null) {
324 bits.or(bits2);
325 }
326 }
327
328 /**
329 * Creates a list with all clades of the tree
330 *
331 * @param tree - the tree from which the clades are extracted
332 * @param node - the starting node. All clades heights below starting at this
333 * branch are added
334 * @param heights - the list in which the heights are stored
335 */
336 public void getCladesHeights(Tree tree, NodeRef node, List<Double> heights) {
337
338 // check if the node is external
339 if (tree.isExternal(node)) {
340
341 // if so, do nothing
342
343 } else {
344
345 // otherwise, call all children and add its taxon together to one
346 // clade
347 for (int i = 0; i < tree.getChildCount(node); i++) {
348 NodeRef child = tree.getChild(node, i);
349 getCladesHeights(tree, child, heights);
350 }
351 // add my bit set to the list
352 heights.add(tree.getNodeHeight(node));
353 }
354 }
355
356 /**
357 * Creates a list with all clades of the tree
358 *
359 * @param tree - the tree from which the clades are extracted
360 * @param node - the starting node. All clades heights below starting at this
361 * branch are added
362 * @param heights - the list in which the heights are stored
363 */
364 public void getRelativeCladesHeights(Tree tree, NodeRef node,
365 List<Double> heights) {
366
367 // check if the node is external
368 if (tree.isExternal(node)) {
369
370 // if so, do nothing
371
372 } else {
373
374 // otherwise, call all children and add its taxon together to one
375 // clade
376 for (int i = 0; i < tree.getChildCount(node); i++) {
377 NodeRef child = tree.getChild(node, i);
378 getRelativeCladesHeights(tree, child, heights);
379 }
380 // add my bit set to the list
381 if (node != tree.getRoot()) {
382 NodeRef parent = tree.getParent(node);
383 heights.add(tree.getNodeHeight(node)
384 / tree.getNodeHeight(parent));
385 } else {
386 heights.add(1.0);
387 }
388 }
389 }
390
391 /**
392 * Creates a list with all clades of the tree
393 *
394 * @param tree - the tree from which the clades are extracted
395 * @param node - the starting node. All clades below starting at this branch
396 * are added
397 */
398 protected Clade getClade(Tree tree, NodeRef node) {
399
400 // create a new bit set for this clade
401 BitSet bits = new BitSet();
402
403 // check if the node is external
404 if (tree.isExternal(node)) {
405
406 // if so, the only taxon in the clade am I
407 int index = node.getNumber();
408 bits.set(index);
409
410 } else {
411
412 // otherwise, call all children and add its taxon together to one
413 // clade
414 for (int i = 0; i < tree.getChildCount(node); i++) {
415 NodeRef child = tree.getChild(node, i);
416 Clade c = getClade(tree, child);
417 bits.or(c.getBits());
418 }
419 }
420
421 Clade c = new Clade(bits, tree.getNodeHeight(node));
422
423 return c;
424 }
425
426 /**
427 * Finds the parent of a given clade in a list of clades. The parent is the
428 * direct parent and not the grandparent or so.
429 *
430 * @param clades - list of clades in which we are searching the parent
431 * @param child - the child of whom we are searching the parent
432 * @return the parent clade if found, otherwise itself
433 */
434 protected Clade getParentClade(List<Clade> clades, Clade child) {
435 Clade parent = null;
436 BitSet childBits = child.getBits();
437 int parentSize = Integer.MAX_VALUE;
438
439 // look in all clades of the list which contains the child and has the
440 // minimum cardinality (least taxa) -> that's the parent :-)
441 for (int i = 0; i < clades.size(); i++) {
442 Clade tmp = clades.get(i);
443 if (!child.equals(tmp) && containsClade(tmp.getBits(), childBits)) {
444 if (parent == null || parentSize > tmp.getSize()) {
445 parent = tmp;
446 parentSize = parent.getSize();
447 }
448 }
449 }
450 // if there isn't a parent, then you probably asked for the whole tree
451 if (parent == null) {
452 parent = child;
453 }
454
455 return parent;
456 }
457
458 /**
459 * Checks if clade i contains clade j.
460 *
461 * @param i - the parent clade
462 * @param j - the child clade
463 * @return true, if i contains j
464 */
465 protected boolean containsClade(Clade i, Clade j) {
466 return containsClade(i.getBits(), j.getBits());
467 }
468
469 /**
470 * Checks if clade i contains clade j.
471 *
472 * @param i - the parent clade
473 * @param j - the child clade
474 * @return true, if i contains j
475 */
476 protected boolean containsClade(BitSet i, BitSet j) {
477 BitSet tmpI = (BitSet) i.clone();
478
479 // just set the bits which are either in j but not in i or in i but not
480 // in j
481 tmpI.xor(j);
482 int numberOfBitsInEither = tmpI.cardinality();
483 // which bits are just in i
484 tmpI.and(i);
485 int numberIfBitJustInContaining = tmpI.cardinality();
486
487 // if the number of bits just in i is equal to the number of bits just
488 // in one of i or j
489 // then i contains j
490 return numberOfBitsInEither == numberIfBitJustInContaining;
491 }
492
493 /*
494 * (non-Javadoc)
495 *
496 * @see
497 * dr.evolution.tree.ImportanceDistribution#addTree(dr.evolution.tree.Tree)
498 */
499 public abstract void addTree(Tree tree);
500
501 /*
502 * (non-Javadoc)
503 *
504 * @see
505 * dr.evolution.tree.ImportanceDistribution#getTreeProbability(dr.evolution
506 * .tree.Tree)
507 */
508 public abstract double getTreeProbability(Tree tree);
509
510 /*
511 * (non-Javadoc)
512 *
513 * @see
514 * dr.evolution.tree.ImportanceDistribution#splitClade(dr.evolution.tree
515 * .Clade, dr.evolution.tree.Clade[])
516 */
517 public abstract double splitClade(Clade parent, Clade[] children);
518
519 public abstract double setNodeHeights(TreeModel tree, Likelihood likelihood);
520
521 public abstract double getChanceForNodeHeights(TreeModel tree, Likelihood likelihood);
522
523 }