Debian Codegraph @

src/dr/evolution/tree/CladeSet.java

raw symbols SCIP
1 /*
2 * CladeSet.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.evolution.tree;
27
28 import dr.evolution.util.TaxonList;
29 import dr.util.FrequencySet;
30
31 import java.util.*;
32
33 /**
34 * Stores a set of unique clades (and their node heights) for a tree
35 *
36 * @version $Id: CladeSet.java,v 1.8 2005/05/24 20:25:56 rambaut Exp $
37 *
38 * @author Andrew Rambaut
39 * @author Alexei Drummond
40 */
41 public class CladeSet extends FrequencySet<BitSet> {
42 //
43 // Public stuff
44 //
45
46 public CladeSet() {}
47
48 /**
49 * @param tree
50 */
51 public CladeSet(Tree tree)
52 {
53 this(tree, tree);
54 }
55
56 /**
57 * @param taxonList a set of taxa used to label the tips
58 */
59 public CladeSet(Tree tree, TaxonList taxonList)
60 {
61 this.taxonList = taxonList;
62 add(tree);
63 }
64
65 /** get number of unique clades */
66 public int getCladeCount()
67 {
68 return size();
69 }
70
71 /** get clade bit set */
72 public String getClade(int index)
73 {
74 BitSet bits = get(index);
75
76 StringBuffer buffer = new StringBuffer("{");
77 boolean first = true;
78 for (String taxonId : getTaxaSet(bits)) {
79 if (!first) {
80 buffer.append(", ");
81 } else {
82 first = false;
83 }
84 buffer.append(taxonId);
85 }
86 buffer.append("}");
87 return buffer.toString();
88 }
89
90 private SortedSet<String> getTaxaSet(BitSet bits) {
91
92 SortedSet<String> taxaSet = new TreeSet<String>();
93
94 for (int i = 0; i < bits.length(); i++) {
95 if (bits.get(i)) {
96 taxaSet.add(taxonList.getTaxonId(i));
97 }
98 }
99 return taxaSet;
100 }
101
102
103 /** get clade frequency */
104 int getCladeFrequency(int index)
105 {
106 return getFrequency(index);
107 }
108
109 /** adds all the clades in the tree */
110 public void add(Tree tree)
111 {
112 if (taxonList == null) {
113 taxonList = tree;
114 }
115
116 totalTrees += 1;
117
118 // Recurse over the tree and add all the clades (or increment their
119 // frequency if already present). The root clade is not added.
120 addClades(tree, tree.getRoot(), null);
121 }
122
123 private void addClades(Tree tree, NodeRef node, BitSet bits) {
124
125 if (tree.isExternal(node)) {
126
127 if (taxonList != null) {
128 int index = taxonList.getTaxonIndex(tree.getNodeTaxon(node).getId());
129 bits.set(index);
130 } else {
131 bits.set(node.getNumber());
132 }
133 } else {
134
135 BitSet bits2 = new BitSet();
136 for (int i = 0; i < tree.getChildCount(node); i++) {
137
138 NodeRef node1 = tree.getChild(node, i);
139
140 addClades(tree, node1, bits2);
141 }
142
143 add(bits2, 1);
144 addNodeHeight(bits2, tree.getNodeHeight(node));
145
146 if (bits != null) {
147 bits.or(bits2);
148 }
149 }
150 }
151
152 public double getMeanNodeHeight(int i) {
153 BitSet bits = get(i);
154
155 return getTotalNodeHeight(bits) / getFrequency(i);
156 }
157
158 private double getTotalNodeHeight(BitSet bits) {
159 Double tnh = totalNodeHeight.get(bits);
160 if (tnh == null) return 0.0;
161 return tnh;
162 }
163
164 private void addNodeHeight(BitSet bits, double height) {
165 totalNodeHeight.put(bits, (getTotalNodeHeight(bits) + height));
166 }
167
168 // Generifying found that this code was buggy. Kuckily it is not used anymore.
169
170 // /** adds all the clades in the CladeSet */
171 // public void add(CladeSet cladeSet)
172 // {
173 // for (int i = 0, n = cladeSet.getCladeCount(); i < n; i++) {
174 // add(cladeSet.getClade(i), cladeSet.getCladeFrequency(i));
175 // }
176 // }
177
178 private BitSet annotate(MutableTree tree, NodeRef node, String freqAttrName) {
179 BitSet b = null;
180 if (tree.isExternal(node)) {
181 int index;
182 if (taxonList != null) {
183 index = taxonList.getTaxonIndex(tree.getNodeTaxon(node).getId());
184 } else {
185 index = node.getNumber();
186 }
187 b = new BitSet(tree.getExternalNodeCount());
188 b.set(index);
189
190 } else {
191
192 for (int i = 0; i < tree.getChildCount(node); i++) {
193 NodeRef child = tree.getChild(node, i);
194 BitSet b1 = annotate(tree, child, freqAttrName);
195 if( i == 0 ) {
196 b = b1;
197 } else {
198 b.or(b1);
199 }
200 }
201 final int total = getFrequency(b);
202 if( total >= 0 ) {
203 tree.setNodeAttribute(node, freqAttrName, total / (double)totalTrees );
204 }
205 }
206 return b;
207 }
208
209 /**
210 * Annotate clades of tree with posterior probability
211 * @param tree
212 * @param freqAttrName name of attribute to set per node
213 * @return sum(log(all clades probability))
214 */
215 public double annotate(MutableTree tree, String freqAttrName) {
216 annotate(tree, tree.getRoot(), freqAttrName);
217
218 double logClade = 0.0;
219 for(int n = 0; n < tree.getInternalNodeCount(); ++n) {
220 final double f = (Double)tree.getNodeAttribute(tree.getInternalNode(n), freqAttrName);
221 logClade += Math.log(f);
222 }
223 return logClade;
224 }
225
226 public boolean hasClade(int index, Tree tree) {
227 BitSet bits = get(index);
228
229 NodeRef[] mrca = new NodeRef[1];
230 findClade(bits, tree, tree.getRoot(), mrca);
231
232 return (mrca[0] != null);
233 }
234
235 private int findClade(BitSet bitSet, Tree tree, NodeRef node, NodeRef[] cladeMRCA) {
236
237 if (tree.isExternal(node)) {
238
239 if (taxonList != null) {
240 int index = taxonList.getTaxonIndex(tree.getNodeTaxon(node).getId());
241 if (bitSet.get(index)) return 1;
242 } else {
243 if (bitSet.get(node.getNumber())) return 1;
244 }
245 return -1;
246 } else {
247 int count = 0;
248 for (int i = 0; i < tree.getChildCount(node); i++) {
249
250 NodeRef node1 = tree.getChild(node, i);
251
252 int childCount = findClade(bitSet, tree, node1, cladeMRCA);
253
254 if (childCount != -1 && count != -1) {
255 count += childCount;
256 } else count = -1;
257 }
258
259 if (count == bitSet.cardinality()) cladeMRCA[0] = node;
260
261 return count;
262 }
263 }
264
265 //
266 // Private stuff
267 //
268 private TaxonList taxonList = null;
269 private final Map<BitSet, Double> totalNodeHeight = new HashMap<BitSet, Double>();
270 private int totalTrees = 0;
271 }