| 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.colouring; |
| 27 |
|
| 28 |
import dr.evolution.tree.NodeRef; |
| 29 |
import dr.evolution.tree.Tree; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public class DefaultTreeColouring implements TreeColouring { |
| 38 |
|
| 39 |
public DefaultTreeColouring(int colourCount, Tree tree) { |
| 40 |
this.colourCount = colourCount; |
| 41 |
this.tree = tree; |
| 42 |
branchColourings = new DefaultBranchColouring[tree.getNodeCount()]; |
| 43 |
colourChangeCount = 0; |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
public DefaultTreeColouring( DefaultTreeColouring treeColouring ) { |
| 48 |
|
| 49 |
if (!treeColouring.immutable) { |
| 50 |
throw new RuntimeException("Attempted to create a copy of a mutable colouring"); |
| 51 |
} |
| 52 |
this.colourCount = treeColouring.colourCount; |
| 53 |
this.tree = treeColouring.tree; |
| 54 |
colourChangeCount = treeColouring.colourChangeCount; |
| 55 |
branchColourings = (DefaultBranchColouring[])treeColouring.branchColourings.clone(); |
| 56 |
for (int i=0; i<branchColourings.length; i++) { |
| 57 |
branchColourings[i] = branchColourings[i].getCopy(); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
public int getColourCount() { |
| 62 |
return colourCount; |
| 63 |
} |
| 64 |
|
| 65 |
public Tree getTree() { |
| 66 |
return tree; |
| 67 |
} |
| 68 |
|
| 69 |
public void setBranchColouring(NodeRef node, DefaultBranchColouring branchColouring) { |
| 70 |
|
| 71 |
if (immutable) throw new RuntimeException("Attempted to setBranchColouring of an immutable colouring"); |
| 72 |
if (tree.isRoot(node)) throw new IllegalArgumentException("Can't set a BranchColouring for the root node"); |
| 73 |
|
| 74 |
if (branchColourings[node.getNumber()] != null) { |
| 75 |
colourChangeCount -= branchColourings[node.getNumber()].getColourChanges().size(); |
| 76 |
} |
| 77 |
|
| 78 |
branchColourings[node.getNumber()] = branchColouring; |
| 79 |
|
| 80 |
colourChangeCount += branchColouring.getColourChanges().size(); |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
public void setLogProbabilityDensity(double logP) { |
| 101 |
if (hasProbability) throw new RuntimeException("Attempted to set probability twice"); |
| 102 |
makeImmutable(); |
| 103 |
hasProbability = true; |
| 104 |
this.logP = logP; |
| 105 |
} |
| 106 |
|
| 107 |
public void makeImmutable() { |
| 108 |
immutable = true; |
| 109 |
} |
| 110 |
|
| 111 |
public boolean isImmutable() { |
| 112 |
return immutable; |
| 113 |
} |
| 114 |
|
| 115 |
public boolean hasProbability() { |
| 116 |
return hasProbability; |
| 117 |
} |
| 118 |
|
| 119 |
public void checkColouring() { |
| 120 |
checkColouringAtNode(tree.getRoot()); |
| 121 |
} |
| 122 |
|
| 123 |
private void checkColouringAtNode(NodeRef node) { |
| 124 |
int colour = getNodeColour(node); |
| 125 |
|
| 126 |
if (!tree.isExternal(node)) { |
| 127 |
for(int i = 0; i < tree.getChildCount(node); i++) { |
| 128 |
NodeRef child = tree.getChild(node, i); |
| 129 |
if (branchColourings[child.getNumber()].getParentColour() != colour) { |
| 130 |
throw new RuntimeException("Colour mismatch at node " + node.toString()); |
| 131 |
} |
| 132 |
checkColouringAtNode(child); |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
public int getNodeColour(NodeRef node) { |
| 142 |
if (tree.isRoot(node)) { |
| 143 |
NodeRef child = tree.getChild(node, 0); |
| 144 |
return branchColourings[child.getNumber()].getParentColour(); |
| 145 |
} else { |
| 146 |
return branchColourings[node.getNumber()].getChildColour(); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
public BranchColouring getBranchColouring(NodeRef node) { |
| 183 |
return branchColourings[node.getNumber()]; |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
public int getColourChangeCount() { |
| 188 |
return colourChangeCount; |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
public double getLogProbabilityDensity() { |
| 195 |
if (!hasProbability) { |
| 196 |
throw new RuntimeException("Tree colouring has no probability density; use colouringModel.getTreeColouringWithProbability()"); |
| 197 |
} |
| 198 |
return logP; |
| 199 |
} |
| 200 |
|
| 201 |
private final Tree tree; |
| 202 |
private final int colourCount; |
| 203 |
private final DefaultBranchColouring[] branchColourings; |
| 204 |
private int colourChangeCount; |
| 205 |
|
| 206 |
private double logP = Double.NEGATIVE_INFINITY; |
| 207 |
|
| 208 |
private boolean immutable = false; |
| 209 |
private boolean hasProbability = false; |
| 210 |
|
| 211 |
} |