Debian Codegraph @

src/dr/evolution/tree/SplitUtils.java

raw symbols SCIP
1 /*
2 * SplitUtils.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
30 /**
31 * utilities for split systems
32 *
33 * @version $Id: SplitUtils.java,v 1.6 2005/05/24 20:25:57 rambaut Exp $
34 *
35 * @author Korbinian Strimmer
36 */
37 public class SplitUtils
38 {
39 //
40 // Public stuff
41 //
42
43 /**
44 * creates a split system from a tree
45 * (using a pre-specified order of sequences)
46 *
47 * @param taxonList the list of taxa (order is important)
48 * @param tree
49 */
50 public static SplitSystem getSplits(TaxonList taxonList, Tree tree)
51 {
52 int size = tree.getInternalNodeCount()-1;
53 SplitSystem splitSystem = new SplitSystem(taxonList, size);
54
55 boolean[][] splits = splitSystem.getSplitVector();
56
57 int j = 0;
58 for (int i = 0; i < tree.getInternalNodeCount(); i++) {
59 NodeRef node = tree.getInternalNode(i);
60 if (node != tree.getRoot()) {
61 getSplit(taxonList, tree, node, splits[j]);
62 j++;
63 }
64 }
65
66 return splitSystem;
67 }
68
69
70
71 /**
72 * creates a split system from a tree
73 * (using tree-induced order of sequences)
74 *
75 * @param tree
76 */
77 public static SplitSystem getSplits(Tree tree)
78 {
79 return getSplits(tree, tree);
80 }
81
82
83
84 /**
85 * get split for branch associated with internal node
86 *
87 * @param taxonList
88 * @param tree
89 * @param internalNode Node
90 * @param split
91 */
92 public static void getSplit(TaxonList taxonList, Tree tree, NodeRef internalNode, boolean[] split)
93 {
94 if (tree.isExternal(internalNode) || tree.isRoot(internalNode))
95 {
96 throw new IllegalArgumentException("Only internal nodes (and no root) nodes allowed");
97 }
98
99 // make sure split is reset
100 for (int i = 0; i < split.length; i++)
101 {
102 split[i] = false;
103 }
104
105 // mark all leafs downstream of the node
106
107 for (int i = 0; i < tree.getChildCount(internalNode); i++)
108 {
109 markNode(taxonList, tree, internalNode, split);
110 }
111
112 // standardize split (i.e. first index is alway true)
113 if ( !split[0] )
114 {
115 for (int i = 0; i < split.length; i++)
116 {
117 split[i] = !split[i];
118 }
119 }
120 }
121
122 /**
123 * checks whether two splits are identical
124 * (assuming they are of the same length
125 * and use the same leaf order)
126 *
127 * @param s1 split 1
128 * @param s2 split 2
129 */
130 public static boolean isSame(boolean[] s1, boolean[] s2)
131 {
132 boolean reverse;
133 reverse = s1[0] != s2[0];
134
135 if (s1.length != s2.length)
136 throw new IllegalArgumentException("Splits must be of the same length!");
137
138 for (int i = 0; i < s1.length; i++)
139 {
140 if (reverse)
141 {
142 // splits not identical
143 if (s1[i] == s2[i]) return false;
144 }
145 else
146 {
147 // splits not identical
148 if (s1[i] != s2[i]) return false;
149 }
150 }
151
152 return true;
153 }
154
155 //
156 // Package stuff
157 //
158
159 static void markNode(TaxonList taxonList, Tree tree, NodeRef node, boolean[] split)
160 {
161 if (tree.isExternal(node))
162 {
163 String name = tree.getTaxonId(node.getNumber());
164 int index = taxonList.getTaxonIndex(name);
165
166 if (index < 0)
167 {
168 throw new IllegalArgumentException("INCOMPATIBLE IDENTIFIER (" + name + ")");
169 }
170
171 split[index] = true;
172 }
173 else
174 {
175 for (int i = 0; i < tree.getChildCount(node); i++)
176 {
177 markNode(taxonList, tree, tree.getChild(node, i), split);
178 }
179 }
180 }
181
182 }