| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
package dr.evolution.tree; |
| 27 |
|
| 28 |
import dr.evolution.util.TaxonList; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public class SplitUtils |
| 38 |
{ |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 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 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public static SplitSystem getSplits(Tree tree) |
| 78 |
{ |
| 79 |
return getSplits(tree, tree); |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 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 |
|
| 100 |
for (int i = 0; i < split.length; i++) |
| 101 |
{ |
| 102 |
split[i] = false; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
for (int i = 0; i < tree.getChildCount(internalNode); i++) |
| 108 |
{ |
| 109 |
markNode(taxonList, tree, internalNode, split); |
| 110 |
} |
| 111 |
|
| 112 |
|
| 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 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 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 |
|
| 143 |
if (s1[i] == s2[i]) return false; |
| 144 |
} |
| 145 |
else |
| 146 |
{ |
| 147 |
|
| 148 |
if (s1[i] != s2[i]) return false; |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
return true; |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 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 |
} |