Debian Codegraph @

src/dr/evomodel/tree/CompatibilityStatistic.java

raw symbols SCIP
1 /*
2 * CompatibilityStatistic.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.TreeUtils;
31 import dr.inference.model.BooleanStatistic;
32
33 import java.util.BitSet;
34 import java.util.HashSet;
35 import java.util.Set;
36
37 /**
38 * Tests whether 2 (possibly unresolved) trees are compatible
39 * *
40 *
41 * @author Andrew Rambaut
42 */
43 public class CompatibilityStatistic extends TreeStatistic implements BooleanStatistic {
44
45 public CompatibilityStatistic(String name, Tree tree1, Tree tree2) throws TreeUtils.MissingTaxonException {
46
47 super(name);
48 this.tree = tree1;
49
50 intersection = new BitSet(tree1.getExternalNodeCount());
51 clades = new HashSet<BitSet>();
52 getClades(tree1, tree2, tree2.getRoot(), null, clades);
53
54 for (int i = 0; i < tree1.getTaxonCount(); i++) {
55 String id = tree1.getTaxonId(i);
56 if (tree2.getTaxonIndex(id) == -1) {
57 throw new TreeUtils.MissingTaxonException(tree1.getTaxon(i));
58 }
59 }
60 }
61
62 public void setTree(Tree tree) {
63 this.tree = tree;
64 }
65
66 public Tree getTree() {
67 return tree;
68 }
69
70 public int getDimension() {
71 return 1;
72 }
73
74 /**
75 * @return boolean result of test.
76 */
77 public double getStatisticValue(int dim) {
78 return getBoolean(dim) ? 1.0 : 0.0;
79 }
80
81 /**
82 * @return boolean result of test.
83 */
84 public boolean getBoolean(int dim) {
85 return isCompatible(tree, tree.getRoot(), null);
86 }
87
88 private boolean isCompatible(Tree tree, NodeRef node, BitSet leaves) {
89 if (tree.isExternal(node)) {
90 leaves.set(node.getNumber());
91 return true;
92 } else {
93
94 BitSet ls = new BitSet(tree.getExternalNodeCount());
95
96 for (int i = 0; i < tree.getChildCount(node); i++) {
97
98 NodeRef node1 = tree.getChild(node, i);
99
100 if (!isCompatible(tree, node1, ls)) {
101 // as soon as we have an incompatibility break out...
102 return false;
103 }
104 }
105
106 if (leaves != null) {
107 if (!clades.contains(ls)) {
108 // except for the root clade...
109 for (BitSet clade : clades) {
110 intersection.clear();
111 intersection.or(clade);
112 intersection.and(ls);
113
114 int card = intersection.cardinality();
115 if (card != 0 &&
116 card != ls.cardinality() &&
117 card != clade.cardinality()) {
118 return false;
119 }
120 }
121 }
122
123 leaves.or(ls);
124 }
125 }
126 return true;
127 }
128
129 private void getClades(Tree referenceTree, Tree tree, NodeRef node, BitSet leaves, Set<BitSet> clades) {
130
131 if (tree.isExternal(node)) {
132 String taxonId = tree.getNodeTaxon(node).getId();
133 for (int i = 0; i < referenceTree.getExternalNodeCount(); i++) {
134 NodeRef n = referenceTree.getExternalNode(i);
135
136 if (taxonId.equals(referenceTree.getNodeTaxon(n).getId())) {
137 leaves.set(n.getNumber());
138 }
139 }
140 } else {
141
142 BitSet ls = new BitSet(tree.getExternalNodeCount());
143
144 for (int i = 0; i < tree.getChildCount(node); i++) {
145
146 NodeRef node1 = tree.getChild(node, i);
147
148 getClades(referenceTree, tree, node1, ls, clades);
149 }
150
151 if (leaves != null) {
152 // except for the root clade...
153 leaves.or(ls);
154 clades.add(ls);
155 }
156
157 }
158 }
159
160 private Tree tree;
161 private final Set<BitSet> clades;
162 private final BitSet intersection;
163
164 }