| 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 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
package dr.evomodel.arg.operators; |
| 35 |
|
| 36 |
import dr.evolution.tree.MutableTree; |
| 37 |
import dr.evolution.tree.NodeRef; |
| 38 |
import dr.evolution.tree.Tree; |
| 39 |
import dr.evomodel.arg.ARGModel; |
| 40 |
import dr.evomodel.arg.ARGModel.Node; |
| 41 |
import dr.evomodel.operators.SubtreeSlideOperator; |
| 42 |
import dr.inference.operators.*; |
| 43 |
import dr.math.MathUtils; |
| 44 |
import dr.xml.*; |
| 45 |
|
| 46 |
import java.util.ArrayList; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
public class ARGSubtreeSlideOperator extends AbstractAdaptableOperator { |
| 55 |
|
| 56 |
|
| 57 |
public static final String SUBTREE_SLIDE = "argSubtreeSlide"; |
| 58 |
|
| 59 |
public static final String SWAP_RATES = "swapRates"; |
| 60 |
public static final String SWAP_TRAITS = "swapTraits"; |
| 61 |
public static final String DIRICHLET_BRANCHES = "branchesAreScaledDirichlet"; |
| 62 |
|
| 63 |
private ARGModel tree = null; |
| 64 |
private double size = 1.0; |
| 65 |
private boolean gaussian = false; |
| 66 |
private boolean swapRates; |
| 67 |
private boolean swapTraits; |
| 68 |
private boolean scaledDirichletBranches; |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
public ARGSubtreeSlideOperator(ARGModel tree, int weight, double size, boolean gaussian, boolean swapRates, |
| 73 |
boolean swapTraits, boolean scaledDirichletBranches, AdaptationMode mode) { |
| 74 |
super(mode); |
| 75 |
this.tree = tree; |
| 76 |
setWeight(weight); |
| 77 |
|
| 78 |
this.size = size; |
| 79 |
this.gaussian = gaussian; |
| 80 |
this.swapRates = swapRates; |
| 81 |
this.swapTraits = swapTraits; |
| 82 |
this.scaledDirichletBranches = scaledDirichletBranches; |
| 83 |
|
| 84 |
|
| 85 |
} |
| 86 |
|
| 87 |
public void sanityCheck() { |
| 88 |
int len = tree.getNodeCount(); |
| 89 |
for (int i = 0; i < len; i++) { |
| 90 |
Node node = (Node) tree.getNode(i); |
| 91 |
if (node.bifurcation) { |
| 92 |
boolean equalChild = (node.leftChild == node.rightChild); |
| 93 |
if ((equalChild && node.leftChild != null)) { |
| 94 |
if (!node.leftChild.bifurcation && ((node.leftChild).leftParent == node)) |
| 95 |
; |
| 96 |
else { |
| 97 |
System.err.println("Node " + (i + 1) + " is insane."); |
| 98 |
System.err.println(tree.toGraphString()); |
| 99 |
System.exit(-1); |
| 100 |
} |
| 101 |
} |
| 102 |
} else { |
| 103 |
if ((node.leftChild != node.rightChild)) { |
| 104 |
System.err.println("Node " + (i + 1) + " is insane."); |
| 105 |
System.err.println(tree.toGraphString()); |
| 106 |
System.exit(-1); |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
public double doOperation() { |
| 118 |
|
| 119 |
|
| 120 |
double logq = 0; |
| 121 |
|
| 122 |
double oldTreeHeight = tree.getNodeHeight(tree.getRoot()); |
| 123 |
|
| 124 |
NodeRef i, newParent, newChild; |
| 125 |
|
| 126 |
|
| 127 |
ArrayList<NodeRef> potentialSubtrees = new ArrayList<NodeRef>(); |
| 128 |
int numPotentialSubtrees = this.getSlideableSubtrees(tree, potentialSubtrees); |
| 129 |
|
| 130 |
i = potentialSubtrees.get(MathUtils.nextInt(numPotentialSubtrees)); |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
NodeRef iP = tree.getParent(i); |
| 135 |
|
| 136 |
|
| 137 |
NodeRef CiP = getOtherChild(tree, iP, i); |
| 138 |
NodeRef PiP; |
| 139 |
if (tree.isBifurcation(iP)) |
| 140 |
PiP = tree.getParent(iP); |
| 141 |
else { |
| 142 |
PiP = tree.getParent(iP, MathUtils.nextInt(2)); |
| 143 |
logq -= Math.log(2); |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
double delta = getDelta(); |
| 148 |
double oldHeight = tree.getNodeHeight(iP); |
| 149 |
double newHeight = oldHeight + delta; |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
if (delta > 0) { |
| 155 |
|
| 156 |
|
| 157 |
if (PiP != null && tree.getNodeHeight(PiP) < newHeight) { |
| 158 |
|
| 159 |
|
| 160 |
newParent = PiP; |
| 161 |
newChild = iP; |
| 162 |
while (tree.getNodeHeight(newParent) < newHeight) { |
| 163 |
newChild = newParent; |
| 164 |
if (tree.isBifurcation(newParent)) |
| 165 |
newParent = tree.getParent(newParent); |
| 166 |
else { |
| 167 |
newParent = tree.getParent(newParent, MathUtils.nextInt(2)); |
| 168 |
logq -= Math.log(2); |
| 169 |
} |
| 170 |
if (newParent == null) break; |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
tree.beginTreeEdit(); |
| 178 |
|
| 179 |
|
| 180 |
if (tree.isRoot(newChild)) { |
| 181 |
|
| 182 |
if (true) { |
| 183 |
tree.endTreeEdit(); |
| 184 |
try { |
| 185 |
tree.checkTreeIsValid(); |
| 186 |
} catch (MutableTree.InvalidTreeException e) { |
| 187 |
e.printStackTrace(); |
| 188 |
} |
| 189 |
throw new RuntimeException("Temporarily disable re-rooting"); |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
tree.doubleRemoveChild(iP, CiP); |
| 196 |
tree.doubleRemoveChild(PiP, iP); |
| 197 |
tree.doubleAddChild(iP, newChild); |
| 198 |
if (tree.isBifurcation(PiP)) |
| 199 |
tree.addChild(PiP, CiP); |
| 200 |
else |
| 201 |
tree.addChild(PiP, CiP); |
| 202 |
tree.setRoot(iP); |
| 203 |
|
| 204 |
} |
| 205 |
|
| 206 |
else { |
| 207 |
boolean doubly = tree.isBifurcationDoublyLinked(newParent); |
| 208 |
|
| 209 |
tree.doubleRemoveChild(iP, CiP); |
| 210 |
tree.doubleRemoveChild(PiP, iP); |
| 211 |
tree.doubleRemoveChild(newParent, newChild); |
| 212 |
|
| 213 |
|
| 214 |
tree.doubleAddChild(PiP, CiP); |
| 215 |
if (!doubly) { |
| 216 |
tree.doubleAddChild(iP, newChild); |
| 217 |
tree.doubleAddChild(newParent, iP); |
| 218 |
} else { |
| 219 |
tree.singleAddChild(newParent, iP); |
| 220 |
tree.singleAddChildWithOneParent(iP, newChild); |
| 221 |
tree.singleAddChild(newParent, newChild); |
| 222 |
} |
| 223 |
|
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
tree.setNodeHeight(iP, newHeight); |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
tree.endTreeEdit(); |
| 241 |
try { |
| 242 |
tree.checkTreeIsValid(); |
| 243 |
} catch (MutableTree.InvalidTreeException ite) { |
| 244 |
throw new RuntimeException(ite.toString()); |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
int possibleSources = intersectingEdges(tree, newChild, iP, oldHeight, null); |
| 249 |
|
| 250 |
|
| 251 |
logq -= Math.log(possibleSources); |
| 252 |
} else { |
| 253 |
|
| 254 |
|
| 255 |
tree.setNodeHeight(iP, newHeight); |
| 256 |
logq = 0.0; |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
} |
| 262 |
|
| 263 |
else { |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
if (tree.getNodeHeight(i) > newHeight) { |
| 268 |
return Double.NEGATIVE_INFINITY; |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
if (tree.getNodeHeight(CiP) > newHeight) { |
| 273 |
|
| 274 |
ArrayList<NodeRef[]> newChildren = new ArrayList<NodeRef[]>(); |
| 275 |
int possibleDestinations = intersectingEdges(tree, CiP, iP, newHeight, newChildren); |
| 276 |
|
| 277 |
|
| 278 |
if (newChildren.size() == 0) |
| 279 |
throw new RuntimeException("no valid destinations"); |
| 280 |
|
| 281 |
|
| 282 |
int childIndex = MathUtils.nextInt(newChildren.size()); |
| 283 |
NodeRef[] draw = (NodeRef[]) newChildren.get(childIndex); |
| 284 |
newChild = draw[1]; |
| 285 |
newParent = draw[0]; |
| 286 |
|
| 287 |
|
| 288 |
NodeRef oops = null; |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
tree.beginTreeEdit(); |
| 306 |
|
| 307 |
|
| 308 |
if (tree.isRoot(iP)) { |
| 309 |
|
| 310 |
if (!tree.isBifurcation(CiP)) |
| 311 |
throw new RuntimeException("root cannot be a reassortment"); |
| 312 |
boolean doubly = tree.isBifurcationDoublyLinked(newParent); |
| 313 |
tree.doubleRemoveChild(iP, CiP); |
| 314 |
tree.doubleRemoveChild(newParent, newChild); |
| 315 |
|
| 316 |
|
| 317 |
if (tree.isBifurcation(newChild)) |
| 318 |
tree.doubleAddChild(iP, newChild); |
| 319 |
else |
| 320 |
tree.singleAddChildWithOneParent(iP, newChild); |
| 321 |
if (!doubly) |
| 322 |
tree.doubleAddChild(newParent, iP); |
| 323 |
else { |
| 324 |
tree.singleAddChild(newParent, iP); |
| 325 |
tree.singleAddChildWithOneParent(newParent, newChild); |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
tree.setRoot(CiP); |
| 330 |
|
| 331 |
} else { |
| 332 |
boolean doubly = tree.isBifurcationDoublyLinked(newParent); |
| 333 |
tree.doubleRemoveChild(iP, CiP); |
| 334 |
tree.doubleRemoveChild(PiP, iP); |
| 335 |
tree.doubleRemoveChild(newParent, newChild); |
| 336 |
|
| 337 |
if (tree.isBifurcation(newChild)) |
| 338 |
tree.doubleAddChild(iP, newChild); |
| 339 |
else |
| 340 |
tree.singleAddChildWithOneParent(iP, newChild); |
| 341 |
tree.doubleAddChild(PiP, CiP); |
| 342 |
if (!doubly) |
| 343 |
tree.doubleAddChild(newParent, iP); |
| 344 |
else { |
| 345 |
tree.singleAddChild(newParent, iP); |
| 346 |
tree.singleAddChildWithOneParent(newParent, newChild); |
| 347 |
} |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
} |
| 359 |
|
| 360 |
tree.setNodeHeight(iP, newHeight); |
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
tree.endTreeEdit(); |
| 375 |
try { |
| 376 |
tree.checkTreeIsValid(); |
| 377 |
} catch (MutableTree.InvalidTreeException ite) { |
| 378 |
throw new RuntimeException(ite.toString()); |
| 379 |
} |
| 380 |
|
| 381 |
|
| 382 |
logq += Math.log((double) possibleDestinations); |
| 383 |
|
| 384 |
|
| 385 |
} else { |
| 386 |
try { |
| 387 |
tree.setNodeHeight(iP, newHeight); |
| 388 |
} catch (Exception e) { |
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
} |
| 393 |
logq = 0.0; |
| 394 |
} |
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
} |
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
if (tree.isBifurcationDoublyLinked(tree.getRoot())) |
| 424 |
throw new RuntimeException("invalid slide"); |
| 425 |
|
| 426 |
|
| 427 |
if (!tree.validRoot()) |
| 428 |
throw new RuntimeException("Roots are invalid"); |
| 429 |
|
| 430 |
if (logq == Double.NEGATIVE_INFINITY) |
| 431 |
throw new RuntimeException("invalid slide"); |
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
if (scaledDirichletBranches) { |
| 438 |
if (oldTreeHeight != tree.getNodeHeight(tree.getRoot())) |
| 439 |
throw new RuntimeException("Temporarily disabled."); |
| 440 |
} |
| 441 |
|
| 442 |
|
| 443 |
return logq; |
| 444 |
} |
| 445 |
|
| 446 |
private double getDelta() { |
| 447 |
if (!gaussian) { |
| 448 |
return (MathUtils.nextDouble() * size) - (size / 2.0); |
| 449 |
} else { |
| 450 |
return MathUtils.nextGaussian() * size; |
| 451 |
} |
| 452 |
} |
| 453 |
|
| 454 |
private int getSlideableSubtrees(ARGModel tree, ArrayList<NodeRef> potentials) { |
| 455 |
int count = 0; |
| 456 |
for (int i = 0, n = tree.getNodeCount(); i < n; i++) { |
| 457 |
NodeRef node = tree.getNode(i); |
| 458 |
if (!tree.isRoot(node) && tree.isBifurcation(node) |
| 459 |
&& tree.isBifurcation(tree.getParent(node))) { |
| 460 |
if (potentials != null) |
| 461 |
potentials.add(node); |
| 462 |
count++; |
| 463 |
} |
| 464 |
} |
| 465 |
return count; |
| 466 |
} |
| 467 |
|
| 468 |
private int intersectingEdges(ARGModel tree, NodeRef node, NodeRef parent, double height, ArrayList<NodeRef[]> directChildren) { |
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
if (tree.getNodeHeight(parent) < height) return 0; |
| 474 |
|
| 475 |
if (tree.getNodeHeight(node) < height) { |
| 476 |
if (directChildren != null) { |
| 477 |
NodeRef[] addition = new NodeRef[2]; |
| 478 |
addition[0] = parent; |
| 479 |
addition[1] = node; |
| 480 |
directChildren.add(addition); |
| 481 |
} |
| 482 |
|
| 483 |
return 1; |
| 484 |
} |
| 485 |
|
| 486 |
int count = 0; |
| 487 |
|
| 488 |
count += intersectingEdges(tree, tree.getChild(node, 0), node, height, directChildren); |
| 489 |
if (tree.isBifurcation(node)) |
| 490 |
count += intersectingEdges(tree, tree.getChild(node, 1), node, height, directChildren); |
| 491 |
|
| 492 |
return count; |
| 493 |
} |
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
private NodeRef getOtherChild(Tree tree, NodeRef parent, NodeRef child) { |
| 534 |
|
| 535 |
if (tree.getChild(parent, 0) == child) { |
| 536 |
return tree.getChild(parent, 1); |
| 537 |
} else { |
| 538 |
return tree.getChild(parent, 0); |
| 539 |
} |
| 540 |
} |
| 541 |
|
| 542 |
public double getSize() { |
| 543 |
return size; |
| 544 |
} |
| 545 |
|
| 546 |
public void setSize(double size) { |
| 547 |
this.size = size; |
| 548 |
} |
| 549 |
|
| 550 |
public double getAdaptableParameter() { |
| 551 |
return Math.log(getSize()); |
| 552 |
} |
| 553 |
|
| 554 |
public void setAdaptableParameter(double value) { |
| 555 |
setSize(Math.exp(value)); |
| 556 |
} |
| 557 |
|
| 558 |
public double getRawParameter() { |
| 559 |
return getSize(); |
| 560 |
} |
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
public String getAdaptableParameterName() { |
| 567 |
return "size"; |
| 568 |
} |
| 569 |
|
| 570 |
public String getOperatorName() { |
| 571 |
return SUBTREE_SLIDE; |
| 572 |
} |
| 573 |
|
| 574 |
public static dr.xml.XMLObjectParser PARSER = new dr.xml.AbstractXMLObjectParser() { |
| 575 |
|
| 576 |
public String getParserName() { |
| 577 |
return SUBTREE_SLIDE; |
| 578 |
} |
| 579 |
|
| 580 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 581 |
|
| 582 |
boolean swapRates = false; |
| 583 |
boolean swapTraits = false; |
| 584 |
boolean scaledDirichletBranches = false; |
| 585 |
|
| 586 |
AdaptationMode mode = AdaptationMode.parseMode(xo); |
| 587 |
|
| 588 |
if (xo.hasAttribute(SWAP_RATES)) { |
| 589 |
swapRates = xo.getBooleanAttribute(SWAP_RATES); |
| 590 |
} |
| 591 |
if (xo.hasAttribute(SWAP_TRAITS)) { |
| 592 |
swapTraits = xo.getBooleanAttribute(SWAP_TRAITS); |
| 593 |
} |
| 594 |
|
| 595 |
if (xo.hasAttribute(DIRICHLET_BRANCHES)) { |
| 596 |
scaledDirichletBranches = xo.getBooleanAttribute(DIRICHLET_BRANCHES); |
| 597 |
} |
| 598 |
|
| 599 |
ARGModel treeModel = (ARGModel) xo.getChild(ARGModel.class); |
| 600 |
int weight = xo.getIntegerAttribute("weight"); |
| 601 |
double size = xo.getDoubleAttribute("size"); |
| 602 |
boolean gaussian = xo.getBooleanAttribute("gaussian"); |
| 603 |
return new ARGSubtreeSlideOperator(treeModel, weight, size, gaussian, swapRates, |
| 604 |
swapTraits, scaledDirichletBranches, mode); |
| 605 |
} |
| 606 |
|
| 607 |
public String getParserDescription() { |
| 608 |
return "An operator that slides a subtree."; |
| 609 |
} |
| 610 |
|
| 611 |
public Class getReturnType() { |
| 612 |
return SubtreeSlideOperator.class; |
| 613 |
} |
| 614 |
|
| 615 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 616 |
return rules; |
| 617 |
} |
| 618 |
|
| 619 |
private final XMLSyntaxRule[] rules = { |
| 620 |
AttributeRule.newIntegerRule("weight"), |
| 621 |
AttributeRule.newDoubleRule("size"), |
| 622 |
AttributeRule.newBooleanRule("gaussian"), |
| 623 |
AttributeRule.newBooleanRule(SWAP_RATES, true), |
| 624 |
AttributeRule.newBooleanRule(SWAP_TRAITS, true), |
| 625 |
AttributeRule.newBooleanRule(AUTO_OPTIMIZE, true), |
| 626 |
new ElementRule(ARGModel.class) |
| 627 |
}; |
| 628 |
}; |
| 629 |
|
| 630 |
} |