| 1 |
package dr.evomodel.operators; |
| 2 |
|
| 3 |
|
| 4 |
import dr.evolution.tree.NodeRef; |
| 5 |
import dr.evolution.tree.Tree; |
| 6 |
import dr.evolution.util.Taxa; |
| 7 |
import dr.evolution.util.Taxon; |
| 8 |
import dr.evomodel.tree.TreeModel; |
| 9 |
import dr.evomodelxml.operators.TipLeapOperatorParser; |
| 10 |
import dr.inference.operators.*; |
| 11 |
import dr.math.MathUtils; |
| 12 |
|
| 13 |
import java.util.ArrayList; |
| 14 |
import java.util.HashMap; |
| 15 |
import java.util.List; |
| 16 |
import java.util.Map; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
public class TipLeapOperator extends AbstractTreeOperator implements AdaptableMCMCOperator { |
| 35 |
private double size; |
| 36 |
|
| 37 |
private final TreeModel tree; |
| 38 |
private final AdaptationMode mode; |
| 39 |
private final double targetAcceptance; |
| 40 |
|
| 41 |
private Taxa taxa; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
public TipLeapOperator(TreeModel tree, Taxa taxa, double weight, double size, double targetAcceptance, AdaptationMode mode) { |
| 53 |
this.tree = tree; |
| 54 |
setWeight(weight); |
| 55 |
this.size = size; |
| 56 |
this.targetAcceptance = targetAcceptance; |
| 57 |
this.mode = mode; |
| 58 |
this.taxa = taxa; |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
public double doOperation() { |
| 69 |
double logq; |
| 70 |
|
| 71 |
final double delta = getDelta(); |
| 72 |
|
| 73 |
|
| 74 |
Taxon taxon = taxa.getTaxon(MathUtils.nextInt(taxa.getTaxonCount())); |
| 75 |
NodeRef node = null; |
| 76 |
int external = tree.getExternalNodeCount(); |
| 77 |
for (int i = 0; i < external; i++) { |
| 78 |
if (tree.getNodeTaxon(tree.getExternalNode(i)).getId().equals(taxon.getId())) { |
| 79 |
node = tree.getExternalNode(i); |
| 80 |
break; |
| 81 |
} |
| 82 |
} |
| 83 |
if (node == null) { |
| 84 |
System.err.println("Taxon not found in tree: " + taxon.getId()); |
| 85 |
System.exit(1); |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
final NodeRef parent = tree.getParent(node); |
| 90 |
|
| 91 |
|
| 92 |
final NodeRef sibling = getOtherChild(tree, parent, node); |
| 93 |
|
| 94 |
|
| 95 |
final NodeRef grandParent = tree.getParent(parent); |
| 96 |
|
| 97 |
final Map<NodeRef, Double> destinations = getDestinations(node, parent, sibling, delta); |
| 98 |
final List<NodeRef> destinationNodes = new ArrayList<NodeRef>(destinations.keySet()); |
| 99 |
|
| 100 |
|
| 101 |
int r = MathUtils.nextInt(destinations.size()); |
| 102 |
|
| 103 |
double forwardProbability = 1.0 / destinations.size(); |
| 104 |
|
| 105 |
final NodeRef j = destinationNodes.get(r); |
| 106 |
final double newHeight = destinations.get(j); |
| 107 |
|
| 108 |
final NodeRef jParent = tree.getParent(j); |
| 109 |
|
| 110 |
if (jParent != null && newHeight > tree.getNodeHeight(jParent)) { |
| 111 |
throw new IllegalArgumentException("height error"); |
| 112 |
} |
| 113 |
|
| 114 |
if (newHeight < tree.getNodeHeight(j)) { |
| 115 |
throw new IllegalArgumentException("height error"); |
| 116 |
} |
| 117 |
|
| 118 |
tree.beginTreeEdit(); |
| 119 |
|
| 120 |
if (j == parent || jParent == parent) { |
| 121 |
|
| 122 |
} else { |
| 123 |
if (grandParent == null) { |
| 124 |
|
| 125 |
|
| 126 |
tree.removeChild(parent, sibling); |
| 127 |
tree.setRoot(sibling); |
| 128 |
|
| 129 |
} else { |
| 130 |
|
| 131 |
tree.removeChild(parent, sibling); |
| 132 |
tree.removeChild(grandParent, parent); |
| 133 |
tree.addChild(grandParent, sibling); |
| 134 |
} |
| 135 |
|
| 136 |
if (jParent == null) { |
| 137 |
|
| 138 |
tree.addChild(parent, j); |
| 139 |
tree.setRoot(parent); |
| 140 |
} else { |
| 141 |
|
| 142 |
tree.removeChild(jParent, j); |
| 143 |
|
| 144 |
|
| 145 |
tree.addChild(parent, j); |
| 146 |
|
| 147 |
|
| 148 |
tree.addChild(jParent, parent); |
| 149 |
} |
| 150 |
} |
| 151 |
tree.endTreeEdit(); |
| 152 |
|
| 153 |
tree.setNodeHeight(parent, newHeight); |
| 154 |
|
| 155 |
if (tree.getParent(parent) != null && newHeight > tree.getNodeHeight(tree.getParent(parent))) { |
| 156 |
throw new IllegalArgumentException("height error"); |
| 157 |
} |
| 158 |
|
| 159 |
if (newHeight < tree.getNodeHeight(node)) { |
| 160 |
throw new IllegalArgumentException("height error"); |
| 161 |
} |
| 162 |
|
| 163 |
if (newHeight < tree.getNodeHeight(getOtherChild(tree, parent, node))) { |
| 164 |
throw new IllegalArgumentException("height error"); |
| 165 |
} |
| 166 |
|
| 167 |
final Map<NodeRef, Double> reverseDestinations = getDestinations(node, parent, getOtherChild(tree, parent, node), delta); |
| 168 |
double reverseProbability = 1.0 / reverseDestinations.size(); |
| 169 |
|
| 170 |
|
| 171 |
logq = Math.log(reverseProbability) - Math.log(forwardProbability); |
| 172 |
return logq; |
| 173 |
} |
| 174 |
|
| 175 |
private Map<NodeRef, Double> getDestinations(NodeRef node, NodeRef parent, NodeRef sibling, double delta) { |
| 176 |
|
| 177 |
final Map<NodeRef, Double> destinations = new HashMap<NodeRef, Double>(); |
| 178 |
|
| 179 |
|
| 180 |
final double height = tree.getNodeHeight(parent); |
| 181 |
|
| 182 |
final double heightBelow = height - delta; |
| 183 |
|
| 184 |
if (heightBelow > tree.getNodeHeight(node)) { |
| 185 |
|
| 186 |
|
| 187 |
final List<NodeRef> edges = new ArrayList<NodeRef>(); |
| 188 |
|
| 189 |
getIntersectingEdges(tree, sibling, heightBelow, edges); |
| 190 |
|
| 191 |
|
| 192 |
for (NodeRef n : edges) { |
| 193 |
destinations.put(n, heightBelow); |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
final double heightAbove = height + delta; |
| 198 |
|
| 199 |
NodeRef node1 = parent; |
| 200 |
|
| 201 |
|
| 202 |
boolean done = false; |
| 203 |
while (!done) { |
| 204 |
NodeRef parent1 = tree.getParent(node1); |
| 205 |
|
| 206 |
if (parent1 != null) { |
| 207 |
final double height1 = tree.getNodeHeight(parent1); |
| 208 |
if (height1 < heightAbove) { |
| 209 |
|
| 210 |
|
| 211 |
NodeRef sibling1 = getOtherChild(tree, parent1, node1); |
| 212 |
|
| 213 |
double heightBelow1 = height1 - (heightAbove - height1); |
| 214 |
|
| 215 |
if (heightBelow1 > tree.getNodeHeight(node)) { |
| 216 |
|
| 217 |
final List<NodeRef> edges = new ArrayList<NodeRef>(); |
| 218 |
|
| 219 |
getIntersectingEdges(tree, sibling1, heightBelow1, edges); |
| 220 |
|
| 221 |
|
| 222 |
for (NodeRef n : edges) { |
| 223 |
destinations.put(n, heightBelow1); |
| 224 |
} |
| 225 |
} |
| 226 |
} else { |
| 227 |
|
| 228 |
destinations.put(node1, heightAbove); |
| 229 |
done = true; |
| 230 |
} |
| 231 |
|
| 232 |
node1 = parent1; |
| 233 |
} else { |
| 234 |
|
| 235 |
destinations.put(node1, heightAbove); |
| 236 |
done = true; |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
return destinations; |
| 241 |
} |
| 242 |
|
| 243 |
private double getDelta() { |
| 244 |
return Math.abs(MathUtils.nextGaussian() * size); |
| 245 |
} |
| 246 |
|
| 247 |
private int getIntersectingEdges(Tree tree, NodeRef node, double height, List<NodeRef> edges) { |
| 248 |
|
| 249 |
final NodeRef parent = tree.getParent(node); |
| 250 |
|
| 251 |
if (tree.getNodeHeight(parent) < height) return 0; |
| 252 |
|
| 253 |
if (tree.getNodeHeight(node) < height) { |
| 254 |
edges.add(node); |
| 255 |
return 1; |
| 256 |
} |
| 257 |
|
| 258 |
int count = 0; |
| 259 |
for (int i = 0; i < tree.getChildCount(node); i++) { |
| 260 |
count += getIntersectingEdges(tree, tree.getChild(node, i), height, edges); |
| 261 |
} |
| 262 |
return count; |
| 263 |
} |
| 264 |
|
| 265 |
public double getSize() { |
| 266 |
return size; |
| 267 |
} |
| 268 |
|
| 269 |
public void setSize(double size) { |
| 270 |
this.size = size; |
| 271 |
} |
| 272 |
|
| 273 |
public double getAdaptableParameter() { |
| 274 |
return Math.log(getSize()); |
| 275 |
} |
| 276 |
|
| 277 |
public void setAdaptableParameter(double value) { |
| 278 |
setSize(Math.exp(value)); |
| 279 |
} |
| 280 |
|
| 281 |
public double getRawParameter() { |
| 282 |
return getSize(); |
| 283 |
} |
| 284 |
|
| 285 |
public AdaptationMode getMode() { |
| 286 |
return mode; |
| 287 |
} |
| 288 |
|
| 289 |
public double getMinimumAcceptanceLevel() { |
| 290 |
return 0.1; |
| 291 |
} |
| 292 |
|
| 293 |
public double getMaximumAcceptanceLevel() { |
| 294 |
return 0.4; |
| 295 |
} |
| 296 |
|
| 297 |
public double getMinimumGoodAcceptanceLevel() { |
| 298 |
return 0.20; |
| 299 |
} |
| 300 |
|
| 301 |
public double getMaximumGoodAcceptanceLevel() { |
| 302 |
return 0.30; |
| 303 |
} |
| 304 |
|
| 305 |
public final String getPerformanceSuggestion() { |
| 306 |
return null; |
| 307 |
} |
| 308 |
|
| 309 |
@Override |
| 310 |
public double getTargetAcceptanceProbability() { |
| 311 |
return targetAcceptance; |
| 312 |
} |
| 313 |
|
| 314 |
public String getAdaptableParameterName() { |
| 315 |
return "size"; |
| 316 |
} |
| 317 |
|
| 318 |
public String getOperatorName() { |
| 319 |
return TipLeapOperatorParser.TIP_LEAP + "(" + tree.getId() + ")"; |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
} |