Debian Codegraph @

src/dr/evomodel/tree/BackboneNodeFilter.java

raw symbols SCIP
1 /*
2 * BackboneNodeFilter.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.evomodel.tree;
27
28 import dr.evolution.tree.NodeRef;
29 import dr.evolution.tree.Tree;
30 import dr.evolution.tree.TreeNodeFilter;
31 import dr.evolution.tree.TreeUtils;
32 import dr.evolution.util.TaxonList;
33 import dr.inference.model.AbstractModel;
34 import dr.inference.model.Model;
35 import dr.inference.model.Variable;
36
37 import java.util.BitSet;
38 import java.util.HashSet;
39 import java.util.Set;
40
41 /**
42 * @author Marc A. Suchard
43 * @author Andrew Rambaut
44 */
45 public class BackboneNodeFilter extends AbstractModel implements TreeNodeFilter {
46
47 public BackboneNodeFilter(String name, Tree tree, TaxonList taxonList, boolean excludeClade, boolean includeStem) {
48 super(name);
49
50 this.tree = tree;
51 this.excludeClade = excludeClade;
52 this.includeStem = includeStem;
53
54 backboneSet = new HashSet<NodeRef>();
55
56 try {
57 tipSet = TreeUtils.getTipsBitSetForTaxa(this.tree, taxonList);
58 } catch (TreeUtils.MissingTaxonException e) {
59 e.printStackTrace();
60 }
61
62 if (tree instanceof TreeModel) {
63 addModel((TreeModel) tree);
64 }
65
66 backboneSetKnown = false;
67 }
68
69 public boolean includeNode(Tree tree, NodeRef node) {
70
71 if (!backboneSetKnown) {
72 computeBackBoneMap();
73 backboneSetKnown = true;
74 }
75 return backboneSet.contains(node);
76 }
77
78 public void computeBackBoneMap() {
79 backboneSet.clear();
80 recursivelyComputeBackBoneMap(tree, tree.getRoot());
81 }
82
83 private boolean recursivelyComputeBackBoneMap(Tree tree, NodeRef node) {
84 boolean onBackBone = false;
85
86 if (tree.isExternal(node)) {
87 if (tipSet.get(node.getNumber())) {
88 onBackBone = true;
89 }
90 } else {
91 for (int i = 0; i < tree.getChildCount(node); i++) {
92 NodeRef child = tree.getChild(node, i);
93 if (recursivelyComputeBackBoneMap(tree, child)) {
94 // if any of the desendents are back bone then this node is too
95 onBackBone = true;
96 }
97 }
98 }
99
100 if (onBackBone) {
101 recursivelyPruneStemAndClade(tree, node, includeStem, excludeClade);
102 return true;
103 }
104 return false;
105 }
106
107 private void recursivelyPruneStemAndClade(Tree tree, NodeRef node,
108 boolean includeStem, boolean excludeClade) {
109
110 if (!tree.isExternal(node) && !excludeClade) {
111 for (int i = 0; i < tree.getChildCount(node); i++) {
112 NodeRef child = tree.getChild(node, i);
113 recursivelyPruneStemAndClade(tree, child, true, true);
114 }
115 }
116
117 if (includeStem && !backboneSet.contains(node)) {
118 backboneSet.add(node);
119 }
120 }
121
122 @Override
123 protected void handleModelChangedEvent(Model model, Object object, int index) {
124
125 if (model == tree) {
126 backboneSetKnown = false;
127 }
128
129 // if (model == tree) {
130 // if (object instanceof TreeModel.TreeChangedEvent) {
131 // TreeModel.TreeChangedEvent event = (TreeModel.TreeChangedEvent) object;
132 // if (event.isHeightChanged()) {
133 // // Do nothing
134 // return;
135 // }
136 // if (event.isNodeParameterChanged()) {
137 // // Do nothing
138 // return;
139 // } // else a topology change (?)
140 // backboneSetKnown = false;
141 // return;
142 // }
143 // }
144 }
145
146 @Override
147 protected void handleVariableChangedEvent(Variable variable, int index, Variable.ChangeType type) {
148 }
149
150 @Override
151 protected void storeState() {
152 }
153
154 @Override
155 protected void restoreState() {
156 }
157
158 @Override
159 protected void acceptState() {
160 }
161
162 private boolean backboneSetKnown;
163 private final Set<NodeRef> backboneSet;
164 private BitSet tipSet;
165
166 private final Tree tree;
167 private final boolean includeStem;
168 private final boolean excludeClade;
169 }