| 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.evomodel.arg.operators; |
| 27 |
|
| 28 |
import dr.evolution.tree.MutableTree; |
| 29 |
import dr.evolution.tree.NodeRef; |
| 30 |
import dr.evomodel.arg.ARGModel; |
| 31 |
import dr.evomodel.arg.ARGModel.Node; |
| 32 |
import dr.inference.operators.SimpleMCMCOperator; |
| 33 |
import dr.math.MathUtils; |
| 34 |
import dr.xml.*; |
| 35 |
|
| 36 |
import java.util.ArrayList; |
| 37 |
import java.util.Collections; |
| 38 |
import java.util.Comparator; |
| 39 |
import java.util.logging.Logger; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
public class ARGSwapOperator extends SimpleMCMCOperator { |
| 50 |
|
| 51 |
public static final String ARG_SWAP_OPERATOR = "argSwapOperator"; |
| 52 |
public static final String SWAP_TYPE = "type"; |
| 53 |
public static final String BIFURCATION_SWAP = "bifurcationSwap"; |
| 54 |
public static final String REASSORTMENT_SWAP = "reassortmentSwap"; |
| 55 |
public static final String DUAL_SWAP = "dualSwap"; |
| 56 |
public static final String FULL_SWAP = "fullSwap"; |
| 57 |
public static final String NARROW_SWAP = "narrowSwap"; |
| 58 |
|
| 59 |
private ARGModel arg; |
| 60 |
private String mode; |
| 61 |
|
| 62 |
public ARGSwapOperator(ARGModel arg, String mode, int weight) { |
| 63 |
this.arg = arg; |
| 64 |
this.mode = mode; |
| 65 |
|
| 66 |
setWeight(weight); |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
public double doOperation() { |
| 71 |
|
| 72 |
if (mode.equals(NARROW_SWAP)) { |
| 73 |
return narrowSwap(); |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
if ((mode.equals(REASSORTMENT_SWAP) || mode.equals(DUAL_SWAP)) && |
| 78 |
arg.getReassortmentNodeCount() == 0) { |
| 79 |
return 0.0; |
| 80 |
} |
| 81 |
|
| 82 |
ArrayList<NodeRef> bifurcationNodes = new ArrayList<NodeRef>(arg.getNodeCount()); |
| 83 |
ArrayList<NodeRef> reassortmentNodes = new ArrayList<NodeRef>(arg.getNodeCount()); |
| 84 |
|
| 85 |
setupBifurcationNodes(bifurcationNodes); |
| 86 |
setupReassortmentNodes(reassortmentNodes); |
| 87 |
|
| 88 |
if (mode.equals(BIFURCATION_SWAP)) { |
| 89 |
return bifurcationSwap(bifurcationNodes.get(MathUtils.nextInt(bifurcationNodes.size()))); |
| 90 |
} else if (mode.equals(REASSORTMENT_SWAP)) { |
| 91 |
return reassortmentSwap(reassortmentNodes.get(MathUtils.nextInt(reassortmentNodes.size()))); |
| 92 |
} else if (mode.equals(DUAL_SWAP)) { |
| 93 |
reassortmentSwap(reassortmentNodes.get(MathUtils.nextInt(reassortmentNodes.size()))); |
| 94 |
return bifurcationSwap(bifurcationNodes.get(MathUtils.nextInt(bifurcationNodes.size()))); |
| 95 |
} |
| 96 |
|
| 97 |
bifurcationNodes.addAll(reassortmentNodes); |
| 98 |
|
| 99 |
Collections.sort(bifurcationNodes, NodeSorter); |
| 100 |
|
| 101 |
for (NodeRef x : bifurcationNodes) { |
| 102 |
if (arg.isBifurcation(x)) |
| 103 |
bifurcationSwap(x); |
| 104 |
else |
| 105 |
reassortmentSwap(x); |
| 106 |
} |
| 107 |
|
| 108 |
return 0; |
| 109 |
} |
| 110 |
|
| 111 |
private double narrowSwap() { |
| 112 |
ArrayList<NarrowSwap> possibleSwaps = new ArrayList<NarrowSwap>(arg.getNodeCount()); |
| 113 |
findAllNarrowSwaps(possibleSwaps); |
| 114 |
int possibleSwapsBefore = possibleSwaps.size(); |
| 115 |
|
| 116 |
if (possibleSwapsBefore == 0) |
| 117 |
return 0; |
| 118 |
|
| 119 |
doNarrowSwap(possibleSwaps.get(MathUtils.nextInt(possibleSwaps.size()))); |
| 120 |
|
| 121 |
possibleSwaps.clear(); |
| 122 |
findAllNarrowSwaps(possibleSwaps); |
| 123 |
|
| 124 |
return Math.log((double) possibleSwapsBefore / possibleSwaps.size()); |
| 125 |
} |
| 126 |
|
| 127 |
public int findAllNarrowSwaps(ArrayList<NarrowSwap> moves) { |
| 128 |
|
| 129 |
for (int i = 0, n = arg.getInternalNodeCount(); i < n; i++) { |
| 130 |
Node x = (Node) arg.getInternalNode(i); |
| 131 |
if (x.bifurcation && !x.isRoot() && x.leftParent.bifurcation) { |
| 132 |
NarrowSwap a = new NarrowSwap(x.leftChild, x, x.leftParent); |
| 133 |
NarrowSwap b = new NarrowSwap(x.rightChild, x, x.leftParent); |
| 134 |
|
| 135 |
if (a.isValid()) |
| 136 |
moves.add(a); |
| 137 |
if (b.isValid()) |
| 138 |
moves.add(b); |
| 139 |
} |
| 140 |
} |
| 141 |
return moves.size(); |
| 142 |
} |
| 143 |
|
| 144 |
private void doNarrowSwap(NarrowSwap swap) { |
| 145 |
arg.beginTreeEdit(); |
| 146 |
|
| 147 |
String before = arg.toARGSummary(); |
| 148 |
|
| 149 |
if (swap.c == swap.pb) { |
| 150 |
Node c = (Node) swap.c; |
| 151 |
Node p = (Node) swap.p; |
| 152 |
Node gp = (Node) swap.gp; |
| 153 |
|
| 154 |
if (c.leftParent == p) { |
| 155 |
c.leftParent = gp; |
| 156 |
c.rightParent = p; |
| 157 |
} else { |
| 158 |
c.leftParent = p; |
| 159 |
c.rightParent = gp; |
| 160 |
} |
| 161 |
} else if (arg.getChild(swap.p, 0) == arg.getChild(swap.p, 1)) { |
| 162 |
Node p = (Node) swap.p; |
| 163 |
Node c = (Node) swap.c; |
| 164 |
|
| 165 |
if (MathUtils.nextBoolean()) |
| 166 |
p.leftChild = c.leftParent = null; |
| 167 |
else |
| 168 |
p.rightChild = c.rightParent = null; |
| 169 |
arg.removeChild(swap.gp, swap.pb); |
| 170 |
|
| 171 |
arg.singleAddChild(swap.gp, swap.c); |
| 172 |
arg.singleAddChild(swap.p, swap.pb); |
| 173 |
} else { |
| 174 |
arg.removeChild(swap.gp, swap.pb); |
| 175 |
arg.removeChild(swap.p, swap.c); |
| 176 |
arg.singleAddChild(swap.gp, swap.c); |
| 177 |
arg.singleAddChild(swap.p, swap.pb); |
| 178 |
} |
| 179 |
|
| 180 |
assert nodeCheck() : swap + " " + before + " " + arg.toARGSummary(); |
| 181 |
|
| 182 |
arg.pushTreeChangedEvent(swap.gp); |
| 183 |
arg.pushTreeChangedEvent(swap.p); |
| 184 |
|
| 185 |
arg.endTreeEdit(); |
| 186 |
try { |
| 187 |
arg.checkTreeIsValid(); |
| 188 |
} catch (MutableTree.InvalidTreeException ite) { |
| 189 |
System.out.println(swap); |
| 190 |
System.out.println(before); |
| 191 |
System.err.println(ite.getMessage()); |
| 192 |
System.exit(-1); |
| 193 |
} catch (NullPointerException e) { |
| 194 |
System.out.println(swap); |
| 195 |
System.out.println(before); |
| 196 |
System.err.println(e.getMessage()); |
| 197 |
System.exit(-1); |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
private class NarrowSwap { |
| 202 |
public NodeRef c; |
| 203 |
public NodeRef p; |
| 204 |
public NodeRef gp; |
| 205 |
public NodeRef pb; |
| 206 |
|
| 207 |
public NarrowSwap(NodeRef child, NodeRef parent, NodeRef gParent) { |
| 208 |
c = child; |
| 209 |
p = parent; |
| 210 |
gp = gParent; |
| 211 |
pb = arg.getOtherChild(gParent, parent); |
| 212 |
} |
| 213 |
|
| 214 |
public boolean isValid() { |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
return (arg.getNodeHeight(pb) < arg.getNodeHeight(p)); |
| 219 |
} |
| 220 |
|
| 221 |
public String toString() { |
| 222 |
return "Child: " + c.toString() + |
| 223 |
", Parent: " + p.toString() + |
| 224 |
", G-parent: " + gp.toString() + |
| 225 |
", P-brother: " + pb.toString(); |
| 226 |
} |
| 227 |
|
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
private double bifurcationSwap(NodeRef x) { |
| 232 |
Node startNode = (Node) x; |
| 233 |
|
| 234 |
|
| 235 |
Node moveChild = startNode.rightChild; |
| 236 |
|
| 237 |
if (MathUtils.nextBoolean()) { |
| 238 |
|
| 239 |
moveChild = startNode.leftChild; |
| 240 |
} |
| 241 |
|
| 242 |
ArrayList<NodeRef> possibleNodes = new ArrayList<NodeRef>(arg.getNodeCount()); |
| 243 |
|
| 244 |
findNodesAtHeight(possibleNodes, startNode.getHeight()); |
| 245 |
|
| 246 |
assert !possibleNodes.contains(startNode); |
| 247 |
assert possibleNodes.size() > 0; |
| 248 |
|
| 249 |
|
| 250 |
Node swapNode = (Node) possibleNodes.get(MathUtils.nextInt(possibleNodes.size())); |
| 251 |
Node swapNodeParent = swapNode.leftParent; |
| 252 |
|
| 253 |
|
| 254 |
arg.beginTreeEdit(); |
| 255 |
|
| 256 |
String before = arg.toARGSummary(); |
| 257 |
|
| 258 |
|
| 259 |
if (swapNode.bifurcation) { |
| 260 |
swapNodeParent = swapNode.leftParent; |
| 261 |
|
| 262 |
arg.singleRemoveChild(startNode, moveChild); |
| 263 |
|
| 264 |
if (swapNodeParent.bifurcation) { |
| 265 |
arg.singleRemoveChild(swapNodeParent, swapNode); |
| 266 |
arg.singleAddChild(swapNodeParent, moveChild); |
| 267 |
} else { |
| 268 |
arg.doubleRemoveChild(swapNodeParent, swapNode); |
| 269 |
arg.doubleAddChild(swapNodeParent, moveChild); |
| 270 |
} |
| 271 |
|
| 272 |
arg.singleAddChild(startNode, swapNode); |
| 273 |
|
| 274 |
} else { |
| 275 |
boolean leftSide = true; |
| 276 |
boolean[] sideOk = {swapNode.leftParent.getHeight() > startNode.getHeight(), |
| 277 |
swapNode.rightParent.getHeight() > startNode.getHeight()}; |
| 278 |
|
| 279 |
if (sideOk[0] && sideOk[1]) { |
| 280 |
if (MathUtils.nextBoolean()) { |
| 281 |
swapNodeParent = swapNode.rightParent; |
| 282 |
leftSide = false; |
| 283 |
} |
| 284 |
} else if (sideOk[1]) { |
| 285 |
swapNodeParent = swapNode.rightParent; |
| 286 |
leftSide = false; |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
if (swapNode.leftParent == swapNode.rightParent) { |
| 291 |
arg.singleRemoveChild(startNode, moveChild); |
| 292 |
|
| 293 |
if (leftSide) { |
| 294 |
swapNode.leftParent = null; |
| 295 |
swapNodeParent.leftChild = null; |
| 296 |
} else { |
| 297 |
swapNode.rightParent = null; |
| 298 |
swapNodeParent.rightChild = null; |
| 299 |
} |
| 300 |
|
| 301 |
arg.singleAddChild(startNode, swapNode); |
| 302 |
arg.singleAddChild(swapNodeParent, moveChild); |
| 303 |
} else if (swapNode.leftParent == startNode || swapNode.rightParent == startNode) { |
| 304 |
arg.singleRemoveChild(startNode, moveChild); |
| 305 |
|
| 306 |
if (swapNodeParent.bifurcation) { |
| 307 |
arg.singleRemoveChild(swapNodeParent, swapNode); |
| 308 |
arg.singleAddChild(swapNodeParent, moveChild); |
| 309 |
} else { |
| 310 |
arg.doubleRemoveChild(swapNodeParent, swapNode); |
| 311 |
arg.doubleAddChild(swapNodeParent, moveChild); |
| 312 |
} |
| 313 |
|
| 314 |
if (startNode.leftChild == null) |
| 315 |
startNode.leftChild = swapNode; |
| 316 |
else |
| 317 |
startNode.rightChild = swapNode; |
| 318 |
|
| 319 |
if (swapNode.leftParent == null) |
| 320 |
swapNode.leftParent = startNode; |
| 321 |
else |
| 322 |
swapNode.rightParent = startNode; |
| 323 |
|
| 324 |
} else { |
| 325 |
arg.singleRemoveChild(startNode, moveChild); |
| 326 |
|
| 327 |
if (swapNodeParent.bifurcation) { |
| 328 |
arg.singleRemoveChild(swapNodeParent, swapNode); |
| 329 |
arg.singleAddChild(swapNodeParent, moveChild); |
| 330 |
} else { |
| 331 |
arg.doubleRemoveChild(swapNodeParent, swapNode); |
| 332 |
arg.doubleAddChild(swapNodeParent, moveChild); |
| 333 |
} |
| 334 |
arg.singleAddChild(startNode, swapNode); |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
arg.pushTreeChangedEvent(); |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
assert nodeCheck(); |
| 343 |
|
| 344 |
arg.endTreeEdit(); |
| 345 |
try { |
| 346 |
arg.checkTreeIsValid(); |
| 347 |
} catch (MutableTree.InvalidTreeException ite) { |
| 348 |
System.out.println(before); |
| 349 |
System.err.println(ite.getMessage()); |
| 350 |
System.exit(-1); |
| 351 |
} |
| 352 |
|
| 353 |
return 0; |
| 354 |
} |
| 355 |
|
| 356 |
private double reassortmentSwap(NodeRef x) { |
| 357 |
Node startNode = (Node) x; |
| 358 |
Node startChild = startNode.leftChild; |
| 359 |
|
| 360 |
ArrayList<NodeRef> possibleNodes = new ArrayList<NodeRef>(arg.getNodeCount()); |
| 361 |
|
| 362 |
findNodesAtHeight(possibleNodes, startNode.getHeight()); |
| 363 |
|
| 364 |
assert !possibleNodes.contains(startNode); |
| 365 |
assert possibleNodes.size() > 0; |
| 366 |
|
| 367 |
Node swapNode = (Node) possibleNodes.get(MathUtils.nextInt(possibleNodes.size())); |
| 368 |
|
| 369 |
Node swapParent; |
| 370 |
|
| 371 |
arg.beginTreeEdit(); |
| 372 |
|
| 373 |
if (swapNode.bifurcation) { |
| 374 |
swapParent = swapNode.leftParent; |
| 375 |
|
| 376 |
arg.doubleRemoveChild(startNode, startChild); |
| 377 |
|
| 378 |
if (swapParent.bifurcation) |
| 379 |
arg.singleRemoveChild(swapParent, swapNode); |
| 380 |
else |
| 381 |
arg.doubleRemoveChild(swapParent, swapNode); |
| 382 |
|
| 383 |
arg.doubleAddChild(startNode, swapNode); |
| 384 |
|
| 385 |
|
| 386 |
if (startChild.bifurcation) { |
| 387 |
startChild.leftParent = swapParent; |
| 388 |
startChild.rightParent = swapParent; |
| 389 |
} else { |
| 390 |
if (startChild.leftParent == null) { |
| 391 |
startChild.leftParent = swapParent; |
| 392 |
} else { |
| 393 |
startChild.rightParent = swapParent; |
| 394 |
} |
| 395 |
} |
| 396 |
if (!swapParent.bifurcation) { |
| 397 |
swapParent.leftChild = startChild; |
| 398 |
swapParent.rightChild = startChild; |
| 399 |
} else { |
| 400 |
if (swapParent.leftChild == null) { |
| 401 |
swapParent.leftChild = startChild; |
| 402 |
} else { |
| 403 |
swapParent.rightChild = startChild; |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
} else { |
| 408 |
|
| 409 |
boolean leftSide = true; |
| 410 |
boolean[] sideOk = {swapNode.leftParent.getHeight() > startNode.getHeight(), |
| 411 |
swapNode.rightParent.getHeight() > startNode.getHeight()}; |
| 412 |
|
| 413 |
swapParent = swapNode.leftParent; |
| 414 |
|
| 415 |
if (sideOk[0] && sideOk[1]) { |
| 416 |
if (MathUtils.nextBoolean()) { |
| 417 |
leftSide = false; |
| 418 |
swapParent = swapNode.rightParent; |
| 419 |
} |
| 420 |
} else if (sideOk[1]) { |
| 421 |
leftSide = false; |
| 422 |
swapParent = swapNode.rightParent; |
| 423 |
} |
| 424 |
|
| 425 |
if (swapNode.leftParent == swapNode.rightParent) { |
| 426 |
arg.doubleRemoveChild(startNode, startChild); |
| 427 |
|
| 428 |
if (leftSide) { |
| 429 |
swapParent.leftChild = swapNode.leftParent = null; |
| 430 |
swapParent.leftChild = startChild; |
| 431 |
swapNode.leftParent = startNode; |
| 432 |
} else { |
| 433 |
swapParent.rightChild = swapNode.rightParent = null; |
| 434 |
swapParent.rightChild = startChild; |
| 435 |
swapNode.rightParent = startNode; |
| 436 |
} |
| 437 |
|
| 438 |
startNode.leftChild = startNode.rightChild = swapNode; |
| 439 |
|
| 440 |
if (startChild.bifurcation) { |
| 441 |
startChild.leftParent = startChild.rightParent = swapParent; |
| 442 |
} else { |
| 443 |
if (startChild.leftParent == null) |
| 444 |
startChild.leftParent = swapParent; |
| 445 |
else |
| 446 |
startChild.rightParent = swapParent; |
| 447 |
} |
| 448 |
} else { |
| 449 |
arg.doubleRemoveChild(startNode, startChild); |
| 450 |
|
| 451 |
if (swapParent.bifurcation) |
| 452 |
arg.singleRemoveChild(swapParent, swapNode); |
| 453 |
else |
| 454 |
arg.doubleRemoveChild(swapParent, swapNode); |
| 455 |
|
| 456 |
startNode.leftChild = startNode.rightChild = swapNode; |
| 457 |
|
| 458 |
if (leftSide) |
| 459 |
swapNode.leftParent = startNode; |
| 460 |
else |
| 461 |
swapNode.rightParent = startNode; |
| 462 |
|
| 463 |
if (swapParent.bifurcation) { |
| 464 |
if (swapParent.leftChild == null) |
| 465 |
swapParent.leftChild = startChild; |
| 466 |
else |
| 467 |
swapParent.rightChild = startChild; |
| 468 |
} else { |
| 469 |
swapParent.leftChild = swapParent.rightChild = startChild; |
| 470 |
} |
| 471 |
|
| 472 |
if (startChild.bifurcation) { |
| 473 |
startChild.leftParent = startChild.rightParent = swapParent; |
| 474 |
} else { |
| 475 |
if (startChild.leftParent == null) |
| 476 |
startChild.leftParent = swapParent; |
| 477 |
else |
| 478 |
startChild.rightParent = swapParent; |
| 479 |
} |
| 480 |
|
| 481 |
} |
| 482 |
|
| 483 |
} |
| 484 |
|
| 485 |
arg.pushTreeChangedEvent(); |
| 486 |
|
| 487 |
arg.endTreeEdit(); |
| 488 |
try { |
| 489 |
arg.checkTreeIsValid(); |
| 490 |
} catch (MutableTree.InvalidTreeException ite) { |
| 491 |
System.err.println(ite.getMessage()); |
| 492 |
System.exit(-1); |
| 493 |
} |
| 494 |
|
| 495 |
|
| 496 |
return 0; |
| 497 |
} |
| 498 |
|
| 499 |
private void setupBifurcationNodes(ArrayList<NodeRef> list) { |
| 500 |
for (int i = 0, n = arg.getNodeCount(); i < n; i++) { |
| 501 |
NodeRef x = arg.getNode(i); |
| 502 |
if (arg.isInternal(x) && arg.isBifurcation(x) && !arg.isRoot(x)) { |
| 503 |
list.add(x); |
| 504 |
} |
| 505 |
} |
| 506 |
} |
| 507 |
|
| 508 |
private void setupReassortmentNodes(ArrayList<NodeRef> list) { |
| 509 |
for (int i = 0, n = arg.getNodeCount(); i < n; i++) { |
| 510 |
NodeRef x = arg.getNode(i); |
| 511 |
if (arg.isReassortment(x)) { |
| 512 |
list.add(x); |
| 513 |
} |
| 514 |
} |
| 515 |
} |
| 516 |
|
| 517 |
|
| 518 |
private void findNodesAtHeight(ArrayList<NodeRef> x, double height) { |
| 519 |
for (int i = 0, n = arg.getNodeCount(); i < n; i++) { |
| 520 |
Node test = (Node) arg.getNode(i); |
| 521 |
if (test.getHeight() < height) { |
| 522 |
if (test.bifurcation) { |
| 523 |
if (test.leftParent.getHeight() > height) { |
| 524 |
x.add(test); |
| 525 |
} |
| 526 |
} else { |
| 527 |
if (test.leftParent.getHeight() > height) { |
| 528 |
x.add(test); |
| 529 |
} |
| 530 |
if (test.rightParent.getHeight() > height) { |
| 531 |
x.add(test); |
| 532 |
} |
| 533 |
} |
| 534 |
} |
| 535 |
} |
| 536 |
} |
| 537 |
|
| 538 |
|
| 539 |
public String getOperatorName() { |
| 540 |
return mode; |
| 541 |
} |
| 542 |
|
| 543 |
public String getPerformanceSuggestion() { |
| 544 |
return ""; |
| 545 |
} |
| 546 |
|
| 547 |
private Comparator<NodeRef> NodeSorter = new Comparator<NodeRef>() { |
| 548 |
|
| 549 |
public int compare(NodeRef o1, NodeRef o2) { |
| 550 |
double[] heights = {arg.getNodeHeight(o1), arg.getNodeHeight(o2)}; |
| 551 |
|
| 552 |
if (heights[0] < heights[1]) { |
| 553 |
return -1; |
| 554 |
} else if (heights[0] > heights[1]) { |
| 555 |
return 1; |
| 556 |
} |
| 557 |
|
| 558 |
return 0; |
| 559 |
} |
| 560 |
}; |
| 561 |
|
| 562 |
public static XMLObjectParser PARSER = new AbstractXMLObjectParser() { |
| 563 |
|
| 564 |
public String getParserDescription() { |
| 565 |
return "Swaps nodes on a tree"; |
| 566 |
} |
| 567 |
|
| 568 |
public Class getReturnType() { |
| 569 |
return ARGSwapOperator.class; |
| 570 |
} |
| 571 |
|
| 572 |
private String[] validFormats = {BIFURCATION_SWAP, REASSORTMENT_SWAP, |
| 573 |
DUAL_SWAP, FULL_SWAP, NARROW_SWAP}; |
| 574 |
|
| 575 |
private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{ |
| 576 |
AttributeRule.newIntegerRule(WEIGHT), |
| 577 |
new StringAttributeRule(SWAP_TYPE, "The mode of the operator", |
| 578 |
validFormats, false), |
| 579 |
new ElementRule(ARGModel.class), |
| 580 |
|
| 581 |
}; |
| 582 |
|
| 583 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 584 |
return rules; |
| 585 |
} |
| 586 |
|
| 587 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 588 |
int weight = xo.getIntegerAttribute(WEIGHT); |
| 589 |
|
| 590 |
String mode = xo.getStringAttribute(SWAP_TYPE); |
| 591 |
|
| 592 |
Logger.getLogger("dr.evomodel").info("Creating ARGSwapOperator: " + mode); |
| 593 |
|
| 594 |
ARGModel arg = (ARGModel) xo.getChild(ARGModel.class); |
| 595 |
return new ARGSwapOperator(arg, mode, weight); |
| 596 |
} |
| 597 |
|
| 598 |
public String getParserName() { |
| 599 |
return ARG_SWAP_OPERATOR; |
| 600 |
} |
| 601 |
|
| 602 |
}; |
| 603 |
|
| 604 |
public boolean nodeCheck() { |
| 605 |
for (int i = 0, n = arg.getNodeCount(); i < n; i++) { |
| 606 |
Node x = (Node) arg.getNode(i); |
| 607 |
|
| 608 |
if (x.leftParent != x.rightParent && |
| 609 |
x.leftChild != x.rightChild) { |
| 610 |
return false; |
| 611 |
} |
| 612 |
if (x.leftParent != null) { |
| 613 |
if (x.leftParent.leftChild.getNumber() != i && |
| 614 |
x.leftParent.rightChild.getNumber() != i) |
| 615 |
return false; |
| 616 |
} |
| 617 |
if (x.rightParent != null) { |
| 618 |
if (x.rightParent.leftChild.getNumber() != i && |
| 619 |
x.rightParent.rightChild.getNumber() != i) |
| 620 |
return false; |
| 621 |
} |
| 622 |
if (x.leftChild != null) { |
| 623 |
if (x.leftChild.leftParent.getNumber() != i && |
| 624 |
x.leftChild.rightParent.getNumber() != i) |
| 625 |
return false; |
| 626 |
} |
| 627 |
if (x.rightChild != null) { |
| 628 |
if (x.rightChild.leftParent.getNumber() != i && |
| 629 |
x.rightChild.rightParent.getNumber() != i) |
| 630 |
return false; |
| 631 |
} |
| 632 |
} |
| 633 |
|
| 634 |
return true; |
| 635 |
} |
| 636 |
|
| 637 |
} |