| 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 |
|
| 35 |
package dr.evomodel.arg.operators; |
| 36 |
|
| 37 |
import dr.evolution.tree.MutableTree; |
| 38 |
import dr.evolution.tree.NodeRef; |
| 39 |
import dr.evolution.tree.TreeUtils; |
| 40 |
import dr.evomodel.arg.ARGModel; |
| 41 |
import dr.evomodel.arg.ARGModel.Node; |
| 42 |
import dr.evomodel.arg.ARGPartitionLikelihood; |
| 43 |
import dr.evomodel.arg.ARGRatePrior; |
| 44 |
import dr.evomodelxml.tree.TreeModelParser; |
| 45 |
import dr.inference.model.CompoundParameter; |
| 46 |
import dr.inference.model.Parameter; |
| 47 |
import dr.inference.operators.AbstractAdaptableOperator; |
| 48 |
import dr.inference.operators.AdaptationMode; |
| 49 |
import dr.inference.operators.MCMCOperator; |
| 50 |
import dr.math.MathUtils; |
| 51 |
import dr.xml.*; |
| 52 |
|
| 53 |
import java.util.ArrayList; |
| 54 |
import java.util.logging.Logger; |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public class ARGAddRemoveEventOperator extends AbstractAdaptableOperator { |
| 65 |
|
| 66 |
public static final String ADD_PROBABILITY = "addProbability"; |
| 67 |
public static final String ARG_EVENT_OPERATOR = "ARGEventOperator"; |
| 68 |
public static final String INTERNAL_NODES = "internalNodes"; |
| 69 |
public static final String INTERNAL_AND_ROOT = "internalNodesPlusRoot"; |
| 70 |
public static final String NODE_RATES = TreeModelParser.NODE_RATES; |
| 71 |
public static final String BELOW_ROOT_PROBABILITY = "belowRootProbability"; |
| 72 |
public static final String FLIP_MEAN = "flipMean"; |
| 73 |
public static final String JOINT_PARTITIONING = "jointPartitioning"; |
| 74 |
public static final String RELAXED = "relaxed"; |
| 75 |
|
| 76 |
public static final double LOG_TWO = Math.log(2.0); |
| 77 |
|
| 78 |
|
| 79 |
private boolean relaxed = false; |
| 80 |
|
| 81 |
private ARGModel arg = null; |
| 82 |
private double size = 0.0; |
| 83 |
private double probBelowRoot = 0.9; |
| 84 |
|
| 85 |
private ARGPartitionLikelihood partLike; |
| 86 |
private ARGRatePrior ratePrior; |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
private CompoundParameter internalNodeParameters; |
| 91 |
private CompoundParameter internalAndRootNodeParameters; |
| 92 |
private CompoundParameter nodeRates; |
| 93 |
|
| 94 |
private int tossSize = -1; |
| 95 |
|
| 96 |
|
| 97 |
public ARGAddRemoveEventOperator(ARGModel arg, int weight, double size, AdaptationMode mode, |
| 98 |
CompoundParameter param1, |
| 99 |
CompoundParameter param2, |
| 100 |
CompoundParameter param3, |
| 101 |
double belowRootProbability, |
| 102 |
ARGPartitionLikelihood partLike, |
| 103 |
ARGRatePrior ratePrior, |
| 104 |
int tossSize) { |
| 105 |
super(mode); |
| 106 |
this.arg = arg; |
| 107 |
this.size = size; |
| 108 |
this.internalNodeParameters = param1; |
| 109 |
this.internalAndRootNodeParameters = param2; |
| 110 |
this.nodeRates = param3; |
| 111 |
|
| 112 |
this.partLike = partLike; |
| 113 |
this.ratePrior = ratePrior; |
| 114 |
|
| 115 |
if (ratePrior != null) { |
| 116 |
relaxed = true; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
setWeight(weight); |
| 140 |
|
| 141 |
this.probBelowRoot = belowRootProbability; |
| 142 |
|
| 143 |
|
| 144 |
probBelowRoot = -Math.log(1 - Math.sqrt(probBelowRoot)); |
| 145 |
|
| 146 |
this.tossSize = tossSize; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
public double doOperation() { |
| 156 |
double logq = 0; |
| 157 |
|
| 158 |
try { |
| 159 |
if (MathUtils.nextDouble() < 1.0 / (1 + Math.exp(-size))) { |
| 160 |
logq = AddOperation() - size; |
| 161 |
} else { |
| 162 |
logq = RemoveOperation() + size; |
| 163 |
} |
| 164 |
} catch (NoReassortmentEventException nree) { |
| 165 |
throw new RuntimeException(""); |
| 166 |
} |
| 167 |
|
| 168 |
assert !Double.isInfinite(logq) && !Double.isNaN(logq); |
| 169 |
|
| 170 |
|
| 171 |
return logq; |
| 172 |
} |
| 173 |
|
| 174 |
private double AddOperation() { |
| 175 |
double logHastings = 0; |
| 176 |
|
| 177 |
|
| 178 |
double treeHeight = arg.getNodeHeight(arg.getRoot()); |
| 179 |
double newBifurcationHeight = Double.POSITIVE_INFINITY; |
| 180 |
double newReassortmentHeight = Double.POSITIVE_INFINITY; |
| 181 |
|
| 182 |
double theta = probBelowRoot / treeHeight; |
| 183 |
|
| 184 |
while (newBifurcationHeight > treeHeight && newReassortmentHeight > treeHeight) { |
| 185 |
newBifurcationHeight = MathUtils.nextExponential(theta); |
| 186 |
newReassortmentHeight = MathUtils.nextExponential(theta); |
| 187 |
} |
| 188 |
|
| 189 |
logHastings += theta * (newBifurcationHeight + newReassortmentHeight) - LOG_TWO |
| 190 |
- 2.0 * Math.log(theta) + Math.log(1 - Math.exp(-2.0 * treeHeight * theta)); |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
if (newBifurcationHeight < newReassortmentHeight) { |
| 211 |
double temp = newBifurcationHeight; |
| 212 |
newBifurcationHeight = newReassortmentHeight; |
| 213 |
newReassortmentHeight = temp; |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
ArrayList<NodeRef> potentialBifurcationChildren = new ArrayList<NodeRef>(); |
| 218 |
ArrayList<NodeRef> potentialReassortmentChildren = new ArrayList<NodeRef>(); |
| 219 |
|
| 220 |
int totalPotentialBifurcationChildren = findPotentialAttachmentPoints( |
| 221 |
newBifurcationHeight, potentialBifurcationChildren); |
| 222 |
int totalPotentialReassortmentChildren = findPotentialAttachmentPoints( |
| 223 |
newReassortmentHeight, potentialReassortmentChildren); |
| 224 |
|
| 225 |
assert totalPotentialBifurcationChildren > 0; |
| 226 |
assert totalPotentialReassortmentChildren > 0; |
| 227 |
|
| 228 |
logHastings += Math.log((double) potentialBifurcationChildren.size() * |
| 229 |
potentialReassortmentChildren.size()); |
| 230 |
|
| 231 |
|
| 232 |
Node reassortChild = (Node) potentialReassortmentChildren.get(MathUtils |
| 233 |
.nextInt(totalPotentialReassortmentChildren)); |
| 234 |
|
| 235 |
Node reassortLeftParent = reassortChild.leftParent; |
| 236 |
Node reassortRightParent = reassortChild.rightParent; |
| 237 |
Node reassortSplitParent = reassortChild.leftParent; |
| 238 |
|
| 239 |
boolean splitReassortLeftParent = true; |
| 240 |
|
| 241 |
if (!reassortChild.bifurcation) { |
| 242 |
boolean[] tester = {arg.getNodeHeight(reassortLeftParent) > newReassortmentHeight, |
| 243 |
arg.getNodeHeight(reassortRightParent) > newReassortmentHeight}; |
| 244 |
|
| 245 |
if (tester[0] && tester[1]) { |
| 246 |
if (MathUtils.nextBoolean()) { |
| 247 |
splitReassortLeftParent = false; |
| 248 |
reassortSplitParent = reassortRightParent; |
| 249 |
} |
| 250 |
|
| 251 |
} else if (tester[0]) { |
| 252 |
|
| 253 |
} else if (tester[1]) { |
| 254 |
splitReassortLeftParent = false; |
| 255 |
reassortSplitParent = reassortRightParent; |
| 256 |
} else { |
| 257 |
assert false; |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
Node bifurcationChild = (Node) potentialBifurcationChildren.get(MathUtils |
| 282 |
.nextInt(potentialBifurcationChildren.size())); |
| 283 |
|
| 284 |
|
| 285 |
Node bifurcationLeftParent = bifurcationChild.leftParent; |
| 286 |
Node bifurcationRightParent = bifurcationChild.rightParent; |
| 287 |
Node bifurcationSplitParent = bifurcationLeftParent; |
| 288 |
|
| 289 |
boolean splitBifurcationLeftParent = true; |
| 290 |
|
| 291 |
if (!bifurcationChild.bifurcation) { |
| 292 |
boolean[] tester = {arg.getNodeHeight(bifurcationLeftParent) > newBifurcationHeight, |
| 293 |
arg.getNodeHeight(bifurcationRightParent) > newBifurcationHeight}; |
| 294 |
|
| 295 |
if (tester[0] && tester[1]) { |
| 296 |
if (MathUtils.nextBoolean()) { |
| 297 |
splitBifurcationLeftParent = false; |
| 298 |
bifurcationSplitParent = bifurcationRightParent; |
| 299 |
} |
| 300 |
|
| 301 |
} else if (tester[0]) { |
| 302 |
|
| 303 |
} else if (tester[1]) { |
| 304 |
splitBifurcationLeftParent = false; |
| 305 |
bifurcationSplitParent = bifurcationRightParent; |
| 306 |
} else { |
| 307 |
assert false; |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
boolean attachNewReassortNewBifurcationThroughLeft = MathUtils.nextBoolean(); |
| 312 |
|
| 313 |
|
| 314 |
logHastings += LOG_TWO; |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
Node newReassortment = arg.new Node(); |
| 337 |
newReassortment.bifurcation = false; |
| 338 |
|
| 339 |
double[] reassortmentValues = {1.0}; |
| 340 |
|
| 341 |
if (relaxed) { |
| 342 |
reassortmentValues = ratePrior.generateValues(); |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
newReassortment.rateParameter = new Parameter.Default(reassortmentValues); |
| 348 |
|
| 349 |
newReassortment.rateParameter.addBounds(new Parameter.DefaultBounds( |
| 350 |
Double.POSITIVE_INFINITY, 0, reassortmentValues.length)); |
| 351 |
|
| 352 |
newReassortment.number = arg.getNodeCount() + 1; |
| 353 |
|
| 354 |
|
| 355 |
Node newBifurcation = arg.new Node(); |
| 356 |
|
| 357 |
double[] bifurcationValues = {1.0}; |
| 358 |
|
| 359 |
if (relaxed) { |
| 360 |
bifurcationValues = ratePrior.generateValues(); |
| 361 |
logHastings += ratePrior.getAddHastingsRatio(bifurcationValues); |
| 362 |
} |
| 363 |
|
| 364 |
newBifurcation.rateParameter = new Parameter.Default(bifurcationValues); |
| 365 |
|
| 366 |
newBifurcation.rateParameter.addBounds(new Parameter.DefaultBounds( |
| 367 |
Double.POSITIVE_INFINITY, 0, bifurcationValues.length)); |
| 368 |
|
| 369 |
newBifurcation.number = arg.getNodeCount(); |
| 370 |
|
| 371 |
|
| 372 |
arg.beginTreeEdit(); |
| 373 |
|
| 374 |
adjustRandomPartitioning(); |
| 375 |
|
| 376 |
if (newBifurcationHeight < treeHeight) { |
| 377 |
|
| 378 |
if (bifurcationChild == reassortChild) { |
| 379 |
if (bifurcationChild.bifurcation) { |
| 380 |
bifurcationChild.leftParent = bifurcationChild.rightParent = newReassortment; |
| 381 |
newReassortment.leftChild = newReassortment.rightChild = bifurcationChild; |
| 382 |
newReassortment.leftParent = newReassortment.rightParent = newBifurcation; |
| 383 |
newBifurcation.leftChild = newBifurcation.rightChild = newReassortment; |
| 384 |
newBifurcation.leftParent = newBifurcation.rightParent = bifurcationSplitParent; |
| 385 |
|
| 386 |
if (bifurcationSplitParent.bifurcation) { |
| 387 |
if (bifurcationSplitParent.leftChild == bifurcationChild) { |
| 388 |
bifurcationSplitParent.leftChild = newBifurcation; |
| 389 |
} else { |
| 390 |
bifurcationSplitParent.rightChild = newBifurcation; |
| 391 |
} |
| 392 |
} else { |
| 393 |
bifurcationSplitParent.leftChild = bifurcationSplitParent.rightChild = newBifurcation; |
| 394 |
} |
| 395 |
|
| 396 |
logHastings -= LOG_TWO; |
| 397 |
} else { |
| 398 |
if (splitBifurcationLeftParent && splitReassortLeftParent) { |
| 399 |
bifurcationChild.leftParent = newReassortment; |
| 400 |
newReassortment.leftChild = newReassortment.rightChild = bifurcationChild; |
| 401 |
newReassortment.leftParent = newReassortment.rightParent = newBifurcation; |
| 402 |
|
| 403 |
newBifurcation.leftChild = newBifurcation.rightChild = newReassortment; |
| 404 |
newBifurcation.leftParent = newBifurcation.rightParent = bifurcationSplitParent; |
| 405 |
|
| 406 |
if (bifurcationSplitParent.bifurcation) { |
| 407 |
if (bifurcationSplitParent.leftChild == bifurcationChild) { |
| 408 |
bifurcationSplitParent.leftChild = newBifurcation; |
| 409 |
} else { |
| 410 |
bifurcationSplitParent.rightChild = newBifurcation; |
| 411 |
} |
| 412 |
} else { |
| 413 |
bifurcationSplitParent.leftChild = bifurcationSplitParent.rightChild = newBifurcation; |
| 414 |
} |
| 415 |
|
| 416 |
|
| 417 |
} else if (splitBifurcationLeftParent) { |
| 418 |
|
| 419 |
|
| 420 |
bifurcationChild.leftParent = newBifurcation; |
| 421 |
bifurcationChild.rightParent = newReassortment; |
| 422 |
|
| 423 |
newBifurcation.leftChild = bifurcationChild; |
| 424 |
newBifurcation.rightChild = newReassortment; |
| 425 |
newBifurcation.leftParent = newBifurcation.rightParent = bifurcationSplitParent; |
| 426 |
|
| 427 |
if (bifurcationSplitParent.bifurcation) { |
| 428 |
if (bifurcationSplitParent.leftChild == bifurcationChild) { |
| 429 |
bifurcationSplitParent.leftChild = newBifurcation; |
| 430 |
} else { |
| 431 |
bifurcationSplitParent.rightChild = newBifurcation; |
| 432 |
} |
| 433 |
} else { |
| 434 |
bifurcationSplitParent.leftChild = bifurcationSplitParent.rightChild = newBifurcation; |
| 435 |
} |
| 436 |
|
| 437 |
newReassortment.leftChild = newReassortment.rightChild = bifurcationChild; |
| 438 |
|
| 439 |
if (attachNewReassortNewBifurcationThroughLeft) { |
| 440 |
newReassortment.leftParent = newBifurcation; |
| 441 |
newReassortment.rightParent = reassortSplitParent; |
| 442 |
} else { |
| 443 |
newReassortment.rightParent = newBifurcation; |
| 444 |
newReassortment.leftParent = reassortSplitParent; |
| 445 |
} |
| 446 |
|
| 447 |
if (reassortSplitParent.bifurcation) { |
| 448 |
if (reassortSplitParent.leftChild == reassortChild) { |
| 449 |
reassortSplitParent.leftChild = newReassortment; |
| 450 |
} else { |
| 451 |
reassortSplitParent.rightChild = newReassortment; |
| 452 |
} |
| 453 |
} else { |
| 454 |
reassortSplitParent.leftChild = reassortSplitParent.rightChild = newReassortment; |
| 455 |
} |
| 456 |
|
| 457 |
|
| 458 |
} else if (splitReassortLeftParent) { |
| 459 |
|
| 460 |
|
| 461 |
bifurcationChild.rightParent = newBifurcation; |
| 462 |
bifurcationChild.leftParent = newReassortment; |
| 463 |
|
| 464 |
newBifurcation.leftChild = bifurcationChild; |
| 465 |
newBifurcation.rightChild = newReassortment; |
| 466 |
newBifurcation.leftParent = newBifurcation.rightParent = bifurcationSplitParent; |
| 467 |
|
| 468 |
if (bifurcationSplitParent.bifurcation) { |
| 469 |
if (bifurcationSplitParent.leftChild == bifurcationChild) { |
| 470 |
bifurcationSplitParent.leftChild = newBifurcation; |
| 471 |
} else { |
| 472 |
bifurcationSplitParent.rightChild = newBifurcation; |
| 473 |
} |
| 474 |
} else { |
| 475 |
bifurcationSplitParent.leftChild = bifurcationSplitParent.rightChild = newBifurcation; |
| 476 |
} |
| 477 |
|
| 478 |
newReassortment.leftChild = newReassortment.rightChild = bifurcationChild; |
| 479 |
|
| 480 |
if (attachNewReassortNewBifurcationThroughLeft) { |
| 481 |
newReassortment.leftParent = newBifurcation; |
| 482 |
newReassortment.rightParent = reassortSplitParent; |
| 483 |
} else { |
| 484 |
newReassortment.rightParent = newBifurcation; |
| 485 |
newReassortment.leftParent = reassortSplitParent; |
| 486 |
} |
| 487 |
|
| 488 |
if (reassortSplitParent.bifurcation) { |
| 489 |
if (reassortSplitParent.leftChild == reassortChild) { |
| 490 |
reassortSplitParent.leftChild = newReassortment; |
| 491 |
} else { |
| 492 |
reassortSplitParent.rightChild = newReassortment; |
| 493 |
} |
| 494 |
} else { |
| 495 |
reassortSplitParent.leftChild = reassortSplitParent.rightChild = newReassortment; |
| 496 |
} |
| 497 |
|
| 498 |
|
| 499 |
} else { |
| 500 |
|
| 501 |
bifurcationChild.rightParent = newReassortment; |
| 502 |
newReassortment.leftChild = newReassortment.rightChild = bifurcationChild; |
| 503 |
newReassortment.leftParent = newReassortment.rightParent = newBifurcation; |
| 504 |
|
| 505 |
newBifurcation.leftChild = newBifurcation.rightChild = newReassortment; |
| 506 |
newBifurcation.leftParent = newBifurcation.rightParent = bifurcationSplitParent; |
| 507 |
|
| 508 |
if (bifurcationSplitParent.bifurcation) { |
| 509 |
if (bifurcationSplitParent.leftChild == bifurcationChild) { |
| 510 |
bifurcationSplitParent.leftChild = newBifurcation; |
| 511 |
} else { |
| 512 |
bifurcationSplitParent.rightChild = newBifurcation; |
| 513 |
} |
| 514 |
} else { |
| 515 |
bifurcationSplitParent.leftChild = bifurcationSplitParent.rightChild = newBifurcation; |
| 516 |
} |
| 517 |
|
| 518 |
logHastings -= LOG_TWO; |
| 519 |
|
| 520 |
} |
| 521 |
} |
| 522 |
|
| 523 |
} else { |
| 524 |
|
| 525 |
|
| 526 |
newReassortment.leftChild = newReassortment.rightChild = reassortChild; |
| 527 |
newBifurcation.leftParent = newBifurcation.rightParent = bifurcationSplitParent; |
| 528 |
newBifurcation.leftChild = newReassortment; |
| 529 |
newBifurcation.rightChild = bifurcationChild; |
| 530 |
|
| 531 |
if (attachNewReassortNewBifurcationThroughLeft) { |
| 532 |
newReassortment.leftParent = newBifurcation; |
| 533 |
newReassortment.rightParent = reassortSplitParent; |
| 534 |
} else { |
| 535 |
newReassortment.rightParent = newBifurcation; |
| 536 |
newReassortment.leftParent = reassortSplitParent; |
| 537 |
} |
| 538 |
|
| 539 |
if (reassortChild.bifurcation) { |
| 540 |
reassortChild.leftParent = reassortChild.rightParent = newReassortment; |
| 541 |
} else if (splitReassortLeftParent) { |
| 542 |
reassortChild.leftParent = newReassortment; |
| 543 |
} else { |
| 544 |
reassortChild.rightParent = newReassortment; |
| 545 |
} |
| 546 |
|
| 547 |
if (reassortSplitParent.bifurcation) { |
| 548 |
if (reassortSplitParent.leftChild == reassortChild) { |
| 549 |
reassortSplitParent.leftChild = newReassortment; |
| 550 |
} else { |
| 551 |
reassortSplitParent.rightChild = newReassortment; |
| 552 |
} |
| 553 |
} else { |
| 554 |
reassortSplitParent.leftChild = reassortSplitParent.rightChild = newReassortment; |
| 555 |
} |
| 556 |
|
| 557 |
if (bifurcationChild.bifurcation) { |
| 558 |
bifurcationChild.leftParent = bifurcationChild.rightParent = newBifurcation; |
| 559 |
} else if (splitBifurcationLeftParent) { |
| 560 |
bifurcationChild.leftParent = newBifurcation; |
| 561 |
} else { |
| 562 |
bifurcationChild.rightParent = newBifurcation; |
| 563 |
} |
| 564 |
|
| 565 |
if (bifurcationSplitParent.bifurcation) { |
| 566 |
if (bifurcationSplitParent.leftChild == bifurcationChild) { |
| 567 |
bifurcationSplitParent.leftChild = newBifurcation; |
| 568 |
} else { |
| 569 |
bifurcationSplitParent.rightChild = newBifurcation; |
| 570 |
} |
| 571 |
} else { |
| 572 |
bifurcationSplitParent.leftChild = bifurcationSplitParent.rightChild = newBifurcation; |
| 573 |
} |
| 574 |
|
| 575 |
|
| 576 |
} |
| 577 |
|
| 578 |
Parameter partition = new Parameter.Default(arg.getNumberOfPartitions()); |
| 579 |
drawRandomPartitioning(partition); |
| 580 |
|
| 581 |
|
| 582 |
newReassortment.partitioning = partition; |
| 583 |
|
| 584 |
newBifurcation.heightParameter = new Parameter.Default(newBifurcationHeight); |
| 585 |
newReassortment.heightParameter = new Parameter.Default(newReassortmentHeight); |
| 586 |
newBifurcation.setupHeightBounds(); |
| 587 |
newReassortment.setupHeightBounds(); |
| 588 |
|
| 589 |
|
| 590 |
arg.expandARG(newBifurcation, newReassortment, |
| 591 |
internalNodeParameters, |
| 592 |
internalAndRootNodeParameters, |
| 593 |
nodeRates); |
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
assert nodeCheck() : arg.toARGSummary(); |
| 601 |
|
| 602 |
} else { |
| 603 |
|
| 604 |
assert newReassortmentHeight < treeHeight; |
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
newReassortment.heightParameter = new Parameter.Default(newReassortmentHeight); |
| 611 |
newReassortment.setupHeightBounds(); |
| 612 |
|
| 613 |
bifurcationChild = newBifurcation; |
| 614 |
if (arg.isRoot(reassortSplitParent)) |
| 615 |
reassortSplitParent = newBifurcation; |
| 616 |
|
| 617 |
|
| 618 |
Node root = (Node) arg.getRoot(); |
| 619 |
Node rootLeftChild = root.leftChild; |
| 620 |
Node rootRightChild = root.rightChild; |
| 621 |
|
| 622 |
arg.singleRemoveChild(root, rootLeftChild); |
| 623 |
arg.singleRemoveChild(root, rootRightChild); |
| 624 |
arg.singleAddChild(newBifurcation, rootLeftChild); |
| 625 |
arg.singleAddChild(newBifurcation, rootRightChild); |
| 626 |
|
| 627 |
if (reassortSplitParent.isBifurcation()) |
| 628 |
arg.singleRemoveChild(reassortSplitParent, reassortChild); |
| 629 |
else |
| 630 |
arg.doubleRemoveChild(reassortSplitParent, reassortChild); |
| 631 |
|
| 632 |
arg.doubleAddChild(newReassortment, reassortChild); |
| 633 |
arg.singleAddChild(root, newBifurcation); |
| 634 |
|
| 635 |
Parameter partitioning = new Parameter.Default(arg.getNumberOfPartitions()); |
| 636 |
drawRandomPartitioning(partitioning); |
| 637 |
|
| 638 |
|
| 639 |
arg.addChildAsRecombinant(root, reassortSplitParent, newReassortment, partitioning); |
| 640 |
|
| 641 |
if (attachNewReassortNewBifurcationThroughLeft) { |
| 642 |
newReassortment.leftParent = root; |
| 643 |
newReassortment.rightParent = reassortSplitParent; |
| 644 |
} else { |
| 645 |
newReassortment.leftParent = reassortSplitParent; |
| 646 |
newReassortment.rightParent = root; |
| 647 |
} |
| 648 |
|
| 649 |
newBifurcation.heightParameter = new Parameter.Default(root.getHeight()); |
| 650 |
|
| 651 |
newBifurcation.setupHeightBounds(); |
| 652 |
root.heightParameter.setParameterValue(0, newBifurcationHeight); |
| 653 |
|
| 654 |
|
| 655 |
arg.expandARG(newBifurcation, newReassortment, |
| 656 |
internalNodeParameters, |
| 657 |
internalAndRootNodeParameters, |
| 658 |
nodeRates); |
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
assert nodeCheck(); |
| 667 |
|
| 668 |
|
| 669 |
} |
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
arg.pushTreeSizeIncreasedEvent(); |
| 794 |
|
| 795 |
arg.endTreeEdit(); |
| 796 |
try { |
| 797 |
arg.checkTreeIsValid(); |
| 798 |
} catch (MutableTree.InvalidTreeException ite) { |
| 799 |
throw new RuntimeException(ite.toString() + "\n" + arg.toString() |
| 800 |
+ "\n" + TreeUtils.uniqueNewick(arg, arg.getRoot())); |
| 801 |
} |
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
assert nodeCheck(); |
| 806 |
|
| 807 |
logHastings -= Math.log((double) findPotentialNodesToRemove(null)); |
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
assert nodeCheck(); |
| 817 |
|
| 818 |
assert !Double.isNaN(logHastings) && !Double.isInfinite(logHastings); |
| 819 |
|
| 820 |
if (newReassortment.leftParent.bifurcation && newReassortment.rightParent.bifurcation |
| 821 |
&& newReassortment.leftParent != newReassortment.rightParent) { |
| 822 |
logHastings -= LOG_TWO; |
| 823 |
} |
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
logHastings += getPartitionAddHastingsRatio(newReassortment.partitioning.getParameterValues()); |
| 830 |
|
| 831 |
return logHastings; |
| 832 |
} |
| 833 |
|
| 834 |
private int findPotentialAttachmentPoints(double time, ArrayList<NodeRef> list) { |
| 835 |
int count = 0; |
| 836 |
|
| 837 |
for (int i = 0, n = arg.getNodeCount(); i < n; i++) { |
| 838 |
NodeRef nr = arg.getNode(i); |
| 839 |
if (!arg.isRoot(nr)) { |
| 840 |
if (arg.getNodeHeight(nr) < time) { |
| 841 |
Node left = (Node) arg.getParent(nr, 0); |
| 842 |
Node right = (Node) arg.getParent(nr, 1); |
| 843 |
if (arg.isBifurcation(nr)) { |
| 844 |
assert left == right; |
| 845 |
if (arg.getNodeHeight(left) > time) { |
| 846 |
if (list != null) |
| 847 |
list.add(nr); |
| 848 |
count++; |
| 849 |
} |
| 850 |
} else { |
| 851 |
if (arg.getNodeHeight(left) > time) { |
| 852 |
if (list != null) |
| 853 |
list.add(nr); |
| 854 |
count++; |
| 855 |
} |
| 856 |
if (arg.getNodeHeight(right) > time) { |
| 857 |
if (list != null) |
| 858 |
list.add(nr); |
| 859 |
count++; |
| 860 |
} |
| 861 |
} |
| 862 |
} |
| 863 |
} else { |
| 864 |
if (arg.getNodeHeight(nr) < time) { |
| 865 |
if (list != null) |
| 866 |
list.add(nr); |
| 867 |
count++; |
| 868 |
} |
| 869 |
} |
| 870 |
} |
| 871 |
|
| 872 |
return count; |
| 873 |
} |
| 874 |
|
| 875 |
|
| 876 |
private int findPotentialNodesToRemove(ArrayList<NodeRef> list) { |
| 877 |
int count = 0; |
| 878 |
int n = arg.getNodeCount(); |
| 879 |
|
| 880 |
|
| 881 |
for (int i = 0; i < n; i++) { |
| 882 |
Node node = (Node) arg.getNode(i); |
| 883 |
Node lp = node.leftParent; |
| 884 |
Node rp = node.rightParent; |
| 885 |
|
| 886 |
if (node.isReassortment() && (lp.bifurcation || rp.bifurcation)) { |
| 887 |
if (list != null) |
| 888 |
list.add(node); |
| 889 |
count++; |
| 890 |
} |
| 891 |
} |
| 892 |
|
| 893 |
return count; |
| 894 |
} |
| 895 |
|
| 896 |
|
| 897 |
private double RemoveOperation() throws NoReassortmentEventException { |
| 898 |
|
| 899 |
double logHastings = 0; |
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
ArrayList<NodeRef> potentialNodes = new ArrayList<NodeRef>(); |
| 904 |
int totalPotentials = findPotentialNodesToRemove(potentialNodes); |
| 905 |
|
| 906 |
if (totalPotentials == 0) |
| 907 |
throw new NoReassortmentEventException(); |
| 908 |
|
| 909 |
|
| 910 |
logHastings += Math.log((double) totalPotentials); |
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
Node recNode = (Node) potentialNodes.get(MathUtils.nextInt(totalPotentials)); |
| 919 |
|
| 920 |
double[] removePartitioningValues = recNode.partitioning.getParameterValues(); |
| 921 |
|
| 922 |
double beforeReassortmentHeight = recNode.getHeight(); |
| 923 |
double beforeBifurcationHeight = 0; |
| 924 |
double beforeTreeHeight = arg.getNodeHeight(arg.getRoot()); |
| 925 |
|
| 926 |
arg.beginTreeEdit(); |
| 927 |
|
| 928 |
boolean doneSomething = false; |
| 929 |
Node recParent = recNode.leftParent; |
| 930 |
Node recChild = recNode.leftChild; |
| 931 |
|
| 932 |
Node beforeReassortChild = recNode.leftChild; |
| 933 |
Node beforeBifurcationChild = recNode.leftParent; |
| 934 |
|
| 935 |
|
| 936 |
if (recNode.leftParent == recNode.rightParent) { |
| 937 |
if (!arg.isRoot(recNode.leftParent)) { |
| 938 |
beforeBifurcationHeight = recParent.getHeight(); |
| 939 |
|
| 940 |
Node recGrandParent = recParent.leftParent; |
| 941 |
|
| 942 |
arg.doubleRemoveChild(recGrandParent, recParent); |
| 943 |
arg.doubleRemoveChild(recNode, recChild); |
| 944 |
if (recGrandParent.bifurcation) |
| 945 |
arg.singleAddChild(recGrandParent, recChild); |
| 946 |
else |
| 947 |
arg.doubleAddChild(recGrandParent, recChild); |
| 948 |
doneSomething = true; |
| 949 |
|
| 950 |
beforeBifurcationChild = beforeReassortChild; |
| 951 |
} else { |
| 952 |
|
| 953 |
|
| 954 |
assert recChild.bifurcation; |
| 955 |
assert false; |
| 956 |
} |
| 957 |
logHastings += LOG_TWO; |
| 958 |
|
| 959 |
} else { |
| 960 |
|
| 961 |
Node recDeleteParent = null; |
| 962 |
Node recKeepParent = null; |
| 963 |
|
| 964 |
|
| 965 |
if (recNode.leftParent.bifurcation && recNode.rightParent.bifurcation) { |
| 966 |
if (MathUtils.nextBoolean()) { |
| 967 |
recDeleteParent = recNode.rightParent; |
| 968 |
recKeepParent = recNode.leftParent; |
| 969 |
} else { |
| 970 |
recDeleteParent = recNode.leftParent; |
| 971 |
recKeepParent = recNode.rightParent; |
| 972 |
} |
| 973 |
logHastings += LOG_TWO; |
| 974 |
} else if (recNode.rightParent.bifurcation) { |
| 975 |
recDeleteParent = recNode.rightParent; |
| 976 |
recKeepParent = recNode.leftParent; |
| 977 |
} else { |
| 978 |
recDeleteParent = recNode.leftParent; |
| 979 |
recKeepParent = recNode.rightParent; |
| 980 |
} |
| 981 |
|
| 982 |
beforeBifurcationChild = recDeleteParent.leftChild; |
| 983 |
if (beforeBifurcationChild == recNode) { |
| 984 |
beforeBifurcationChild = recDeleteParent.rightChild; |
| 985 |
} |
| 986 |
|
| 987 |
beforeBifurcationHeight = recDeleteParent.getHeight(); |
| 988 |
|
| 989 |
|
| 990 |
if (arg.isRoot(recDeleteParent)) { |
| 991 |
|
| 992 |
Node oldRoot = (Node) arg.getOtherChild(recDeleteParent, recNode); |
| 993 |
Node oldRootLeft = oldRoot.leftChild; |
| 994 |
Node oldRootRight = oldRoot.rightChild; |
| 995 |
|
| 996 |
if (oldRoot == recKeepParent) { |
| 997 |
|
| 998 |
arg.singleRemoveChild(recDeleteParent, recNode); |
| 999 |
arg.singleRemoveChild(recDeleteParent, oldRoot); |
| 1000 |
arg.singleRemoveChild(oldRoot, oldRootLeft); |
| 1001 |
arg.singleRemoveChild(oldRoot, oldRootRight); |
| 1002 |
|
| 1003 |
arg.singleAddChild(recDeleteParent, oldRootLeft); |
| 1004 |
arg.singleAddChild(recDeleteParent, oldRootRight); |
| 1005 |
|
| 1006 |
arg.singleRemoveChild(recDeleteParent, recNode); |
| 1007 |
arg.doubleRemoveChild(recNode, recChild); |
| 1008 |
|
| 1009 |
arg.singleAddChild(recDeleteParent, recChild); |
| 1010 |
|
| 1011 |
recDeleteParent.setHeight(oldRoot.getHeight()); |
| 1012 |
|
| 1013 |
recDeleteParent = oldRoot; |
| 1014 |
|
| 1015 |
} else { |
| 1016 |
arg.singleRemoveChild(recDeleteParent, recNode); |
| 1017 |
arg.singleRemoveChild(recDeleteParent, oldRoot); |
| 1018 |
arg.singleRemoveChild(oldRoot, oldRootLeft); |
| 1019 |
arg.singleRemoveChild(oldRoot, oldRootRight); |
| 1020 |
|
| 1021 |
arg.singleAddChild(recDeleteParent, oldRootLeft); |
| 1022 |
arg.singleAddChild(recDeleteParent, oldRootRight); |
| 1023 |
|
| 1024 |
if (recKeepParent.bifurcation) |
| 1025 |
arg.singleRemoveChild(recKeepParent, recNode); |
| 1026 |
else |
| 1027 |
arg.doubleRemoveChild(recKeepParent, recNode); |
| 1028 |
|
| 1029 |
arg.doubleRemoveChild(recNode, recChild); |
| 1030 |
|
| 1031 |
if (recKeepParent.bifurcation) |
| 1032 |
arg.singleAddChild(recKeepParent, recChild); |
| 1033 |
else |
| 1034 |
arg.doubleAddChild(recKeepParent, recChild); |
| 1035 |
|
| 1036 |
recDeleteParent.setHeight(oldRoot.getHeight()); |
| 1037 |
recDeleteParent = oldRoot; |
| 1038 |
|
| 1039 |
} |
| 1040 |
|
| 1041 |
|
| 1042 |
} else { |
| 1043 |
Node recGrandParent = recDeleteParent.leftParent; |
| 1044 |
|
| 1045 |
Node otherChild = recDeleteParent.leftChild; |
| 1046 |
if (otherChild == recNode) |
| 1047 |
otherChild = recDeleteParent.rightChild; |
| 1048 |
|
| 1049 |
if (recGrandParent.bifurcation) |
| 1050 |
arg.singleRemoveChild(recGrandParent, recDeleteParent); |
| 1051 |
else |
| 1052 |
arg.doubleRemoveChild(recGrandParent, recDeleteParent); |
| 1053 |
|
| 1054 |
arg.singleRemoveChild(recDeleteParent, otherChild); |
| 1055 |
if (recKeepParent.bifurcation) |
| 1056 |
arg.singleRemoveChild(recKeepParent, recNode); |
| 1057 |
else |
| 1058 |
arg.doubleRemoveChild(recKeepParent, recNode); |
| 1059 |
arg.doubleRemoveChild(recNode, recChild); |
| 1060 |
if (otherChild != recChild) { |
| 1061 |
if (recGrandParent.bifurcation) |
| 1062 |
arg.singleAddChild(recGrandParent, otherChild); |
| 1063 |
else |
| 1064 |
arg.doubleAddChild(recGrandParent, otherChild); |
| 1065 |
if (recKeepParent.bifurcation) |
| 1066 |
arg.singleAddChild(recKeepParent, recChild); |
| 1067 |
else |
| 1068 |
arg.doubleAddChild(recKeepParent, recChild); |
| 1069 |
} else { |
| 1070 |
if (recGrandParent.bifurcation) |
| 1071 |
arg.singleAddChildWithOneParent(recGrandParent, otherChild); |
| 1072 |
else |
| 1073 |
arg.doubleAddChildWithOneParent(recGrandParent, otherChild); |
| 1074 |
if (recKeepParent.bifurcation) |
| 1075 |
arg.singleAddChildWithOneParent(recKeepParent, recChild); |
| 1076 |
else |
| 1077 |
arg.doubleAddChildWithOneParent(recKeepParent, recChild); |
| 1078 |
} |
| 1079 |
} |
| 1080 |
|
| 1081 |
doneSomething = true; |
| 1082 |
recParent = recDeleteParent; |
| 1083 |
} |
| 1084 |
|
| 1085 |
if (relaxed) { |
| 1086 |
|
| 1087 |
double[] rateValues = recParent.rateParameter.getParameterValues(); |
| 1088 |
logHastings -= ratePrior.getAddHastingsRatio(rateValues); |
| 1089 |
} |
| 1090 |
|
| 1091 |
|
| 1092 |
if (doneSomething) { |
| 1093 |
try { |
| 1094 |
arg.contractARG(recParent, recNode, |
| 1095 |
internalNodeParameters, internalAndRootNodeParameters, nodeRates); |
| 1096 |
|
| 1097 |
|
| 1098 |
|
| 1099 |
} catch (Exception e) { |
| 1100 |
System.err.println(e.getMessage()); |
| 1101 |
System.err.println(e); |
| 1102 |
System.exit(-1); |
| 1103 |
} |
| 1104 |
} |
| 1105 |
|
| 1106 |
|
| 1107 |
int max = Math.max(recParent.getNumber(), recNode.getNumber()); |
| 1108 |
int min = Math.min(recParent.getNumber(), recNode.getNumber()); |
| 1109 |
|
| 1110 |
for (int i = 0, n = arg.getNodeCount(); i < n; i++) { |
| 1111 |
Node x = (Node) arg.getNode(i); |
| 1112 |
if (x.getNumber() > max) { |
| 1113 |
x.number--; |
| 1114 |
} |
| 1115 |
if (x.getNumber() > min) { |
| 1116 |
x.number--; |
| 1117 |
} |
| 1118 |
} |
| 1119 |
|
| 1120 |
|
| 1121 |
adjustRandomPartitioning(); |
| 1122 |
|
| 1123 |
|
| 1124 |
arg.pushTreeSizeDecreasedEvent(); |
| 1125 |
|
| 1126 |
arg.endTreeEdit(); |
| 1127 |
try { |
| 1128 |
arg.checkTreeIsValid(); |
| 1129 |
} catch (MutableTree.InvalidTreeException ite) { |
| 1130 |
throw new RuntimeException(ite.toString() + "\n" + arg.toString() |
| 1131 |
+ "\n" + TreeUtils.uniqueNewick(arg, arg.getRoot())); |
| 1132 |
} |
| 1133 |
|
| 1134 |
assert nodeCheck() : arg.toARGSummary(); |
| 1135 |
|
| 1136 |
|
| 1137 |
|
| 1138 |
double afterTreeHeight = arg.getNodeHeight(arg.getRoot()); |
| 1139 |
|
| 1140 |
|
| 1141 |
|
| 1142 |
|
| 1143 |
|
| 1144 |
|
| 1145 |
|
| 1146 |
|
| 1147 |
|
| 1148 |
|
| 1149 |
|
| 1150 |
|
| 1151 |
|
| 1152 |
double theta = probBelowRoot / afterTreeHeight; |
| 1153 |
|
| 1154 |
logHastings -= theta * (beforeBifurcationHeight + beforeReassortmentHeight) - LOG_TWO |
| 1155 |
- 2.0 * Math.log(theta) + Math.log(1 - Math.exp(-2.0 * afterTreeHeight * theta)); |
| 1156 |
|
| 1157 |
|
| 1158 |
logHastings -= Math.log((double) findPotentialAttachmentPoints(beforeBifurcationHeight, null) |
| 1159 |
* findPotentialAttachmentPoints(beforeReassortmentHeight, null)); |
| 1160 |
|
| 1161 |
|
| 1162 |
logHastings -= getPartitionAddHastingsRatio(removePartitioningValues); |
| 1163 |
|
| 1164 |
logHastings -= LOG_TWO; |
| 1165 |
|
| 1166 |
|
| 1167 |
|
| 1168 |
|
| 1169 |
|
| 1170 |
|
| 1171 |
|
| 1172 |
|
| 1173 |
|
| 1174 |
|
| 1175 |
|
| 1176 |
|
| 1177 |
|
| 1178 |
assert nodeCheck(); |
| 1179 |
assert !Double.isNaN(logHastings) && !Double.isInfinite(logHastings); |
| 1180 |
|
| 1181 |
return logHastings; |
| 1182 |
} |
| 1183 |
|
| 1184 |
private double getPartitionAddHastingsRatio(double[] values) { |
| 1185 |
return -partLike.getLogLikelihood(values); |
| 1186 |
} |
| 1187 |
|
| 1188 |
private void adjustRandomPartitioning() { |
| 1189 |
if (tossSize < 1) { |
| 1190 |
return; |
| 1191 |
} |
| 1192 |
|
| 1193 |
if (arg.getReassortmentNodeCount() > 0) { |
| 1194 |
int total = arg.getReassortmentNodeCount(); |
| 1195 |
Parameter xyz = arg.getPartitioningParameters().getParameter(MathUtils.nextInt(total)); |
| 1196 |
|
| 1197 |
|
| 1198 |
if (arg.isRecombinationPartitionType()) { |
| 1199 |
adjustRecombinationPartition(xyz); |
| 1200 |
} else { |
| 1201 |
adjustReassortmentPartition(xyz); |
| 1202 |
} |
| 1203 |
} |
| 1204 |
|
| 1205 |
} |
| 1206 |
|
| 1207 |
private void adjustRecombinationPartition(Parameter part) { |
| 1208 |
double[] values = part.getParameterValues(); |
| 1209 |
|
| 1210 |
Logger.getLogger("dr.evomodel").severe("NOT IMPLENTED"); |
| 1211 |
} |
| 1212 |
|
| 1213 |
public static double arraySum(double[] n) { |
| 1214 |
double b = 0; |
| 1215 |
for (double a : n) |
| 1216 |
b += a; |
| 1217 |
return b; |
| 1218 |
} |
| 1219 |
|
| 1220 |
private void adjustReassortmentPartition(Parameter part) { |
| 1221 |
double[] values = part.getParameterValues(); |
| 1222 |
|
| 1223 |
boolean stop = false; |
| 1224 |
|
| 1225 |
if (values.length == 2) { |
| 1226 |
|
| 1227 |
double tmp = values[0]; |
| 1228 |
values[0] = values[1]; |
| 1229 |
values[1] = tmp; |
| 1230 |
} else { |
| 1231 |
|
| 1232 |
while (!stop) { |
| 1233 |
values = part.getParameterValues(); |
| 1234 |
|
| 1235 |
ArrayList<Integer> list = new ArrayList<Integer>(); |
| 1236 |
|
| 1237 |
while (list.size() < tossSize) { |
| 1238 |
int z = MathUtils.nextInt(values.length - 1) + 1; |
| 1239 |
if (!list.contains(z)) { |
| 1240 |
list.add(z); |
| 1241 |
} |
| 1242 |
} |
| 1243 |
|
| 1244 |
|
| 1245 |
for (int z : list) { |
| 1246 |
if (values[z] == 0) { |
| 1247 |
values[z] = 1; |
| 1248 |
} else { |
| 1249 |
values[z] = 0; |
| 1250 |
} |
| 1251 |
} |
| 1252 |
|
| 1253 |
|
| 1254 |
if (arraySum(values) > 0) { |
| 1255 |
stop = true; |
| 1256 |
} |
| 1257 |
} |
| 1258 |
} |
| 1259 |
|
| 1260 |
for (int i = 0; i < values.length; i++) { |
| 1261 |
|
| 1262 |
|
| 1263 |
part.setParameterValueQuietly(i, values[i]); |
| 1264 |
} |
| 1265 |
|
| 1266 |
ARGPartitioningOperator.checkValidReassortmentPartition(part); |
| 1267 |
|
| 1268 |
|
| 1269 |
} |
| 1270 |
|
| 1271 |
private void drawRandomPartitioning(Parameter partitioning) { |
| 1272 |
double[] values = partLike.generatePartition(); |
| 1273 |
|
| 1274 |
for (int i = 0; i < values.length; i++) |
| 1275 |
partitioning.setParameterValueQuietly(i, values[i]); |
| 1276 |
|
| 1277 |
} |
| 1278 |
|
| 1279 |
|
| 1280 |
|
| 1281 |
|
| 1282 |
|
| 1283 |
|
| 1284 |
|
| 1285 |
|
| 1286 |
|
| 1287 |
|
| 1288 |
|
| 1289 |
|
| 1290 |
|
| 1291 |
|
| 1292 |
|
| 1293 |
|
| 1294 |
|
| 1295 |
|
| 1296 |
|
| 1297 |
|
| 1298 |
|
| 1299 |
|
| 1300 |
|
| 1301 |
|
| 1302 |
|
| 1303 |
|
| 1304 |
|
| 1305 |
|
| 1306 |
|
| 1307 |
|
| 1308 |
|
| 1309 |
|
| 1310 |
|
| 1311 |
|
| 1312 |
|
| 1313 |
|
| 1314 |
|
| 1315 |
|
| 1316 |
|
| 1317 |
|
| 1318 |
|
| 1319 |
|
| 1320 |
|
| 1321 |
|
| 1322 |
|
| 1323 |
|
| 1324 |
|
| 1325 |
|
| 1326 |
|
| 1327 |
|
| 1328 |
|
| 1329 |
|
| 1330 |
|
| 1331 |
|
| 1332 |
|
| 1333 |
|
| 1334 |
|
| 1335 |
|
| 1336 |
|
| 1337 |
|
| 1338 |
|
| 1339 |
|
| 1340 |
|
| 1341 |
|
| 1342 |
|
| 1343 |
|
| 1344 |
|
| 1345 |
|
| 1346 |
|
| 1347 |
|
| 1348 |
|
| 1349 |
|
| 1350 |
|
| 1351 |
|
| 1352 |
|
| 1353 |
|
| 1354 |
|
| 1355 |
|
| 1356 |
|
| 1357 |
|
| 1358 |
|
| 1359 |
|
| 1360 |
|
| 1361 |
|
| 1362 |
|
| 1363 |
|
| 1364 |
|
| 1365 |
|
| 1366 |
|
| 1367 |
|
| 1368 |
|
| 1369 |
|
| 1370 |
|
| 1371 |
|
| 1372 |
|
| 1373 |
|
| 1374 |
|
| 1375 |
|
| 1376 |
|
| 1377 |
|
| 1378 |
|
| 1379 |
|
| 1380 |
|
| 1381 |
|
| 1382 |
|
| 1383 |
|
| 1384 |
|
| 1385 |
|
| 1386 |
|
| 1387 |
|
| 1388 |
|
| 1389 |
|
| 1390 |
|
| 1391 |
|
| 1392 |
|
| 1393 |
|
| 1394 |
|
| 1395 |
|
| 1396 |
|
| 1397 |
|
| 1398 |
|
| 1399 |
|
| 1400 |
|
| 1401 |
|
| 1402 |
|
| 1403 |
|
| 1404 |
|
| 1405 |
|
| 1406 |
|
| 1407 |
|
| 1408 |
|
| 1409 |
|
| 1410 |
|
| 1411 |
|
| 1412 |
|
| 1413 |
|
| 1414 |
|
| 1415 |
|
| 1416 |
|
| 1417 |
|
| 1418 |
|
| 1419 |
|
| 1420 |
|
| 1421 |
|
| 1422 |
|
| 1423 |
public boolean nodeCheck() { |
| 1424 |
for (int i = 0, n = arg.getNodeCount(); i < n; i++) { |
| 1425 |
Node x = (Node) arg.getNode(i); |
| 1426 |
|
| 1427 |
if (x.leftParent != x.rightParent && |
| 1428 |
x.leftChild != x.rightChild) { |
| 1429 |
return false; |
| 1430 |
} |
| 1431 |
if (x.leftParent != null) { |
| 1432 |
if (x.leftParent.leftChild.getNumber() != i && |
| 1433 |
x.leftParent.rightChild.getNumber() != i) |
| 1434 |
return false; |
| 1435 |
} |
| 1436 |
if (x.rightParent != null) { |
| 1437 |
if (x.rightParent.leftChild.getNumber() != i && |
| 1438 |
x.rightParent.rightChild.getNumber() != i) |
| 1439 |
return false; |
| 1440 |
} |
| 1441 |
if (x.leftChild != null) { |
| 1442 |
if (x.leftChild.leftParent.getNumber() != i && |
| 1443 |
x.leftChild.rightParent.getNumber() != i) |
| 1444 |
return false; |
| 1445 |
} |
| 1446 |
if (x.rightChild != null) { |
| 1447 |
if (x.rightChild.leftParent.getNumber() != i && |
| 1448 |
x.rightChild.rightParent.getNumber() != i) |
| 1449 |
return false; |
| 1450 |
} |
| 1451 |
} |
| 1452 |
|
| 1453 |
return true; |
| 1454 |
} |
| 1455 |
|
| 1456 |
|
| 1457 |
public void sanityCheck() { |
| 1458 |
int len = arg.getNodeCount(); |
| 1459 |
for (int i = 0; i < len; i++) { |
| 1460 |
Node node = (Node) arg.getNode(i); |
| 1461 |
if (node.bifurcation) { |
| 1462 |
boolean equalChild = (node.leftChild == node.rightChild); |
| 1463 |
if ((equalChild && node.leftChild != null)) { |
| 1464 |
if (!node.leftChild.bifurcation && ((node.leftChild).leftParent == node)) |
| 1465 |
; |
| 1466 |
else { |
| 1467 |
System.err.println("Node " + (i + 1) + " is insane."); |
| 1468 |
System.err.println(arg.toGraphString()); |
| 1469 |
System.exit(-1); |
| 1470 |
} |
| 1471 |
} |
| 1472 |
} else { |
| 1473 |
if ((node.leftChild != node.rightChild)) { |
| 1474 |
System.err.println("Node " + (i + 1) + " is insane."); |
| 1475 |
System.err.println(arg.toGraphString()); |
| 1476 |
System.exit(-1); |
| 1477 |
} |
| 1478 |
} |
| 1479 |
if (!node.isRoot()) { |
| 1480 |
double d; |
| 1481 |
d = node.getHeight(); |
| 1482 |
} |
| 1483 |
} |
| 1484 |
} |
| 1485 |
|
| 1486 |
|
| 1487 |
|
| 1488 |
|
| 1489 |
|
| 1490 |
|
| 1491 |
|
| 1492 |
public double getSize() { |
| 1493 |
return size; |
| 1494 |
} |
| 1495 |
|
| 1496 |
public void setSize(double size) { |
| 1497 |
this.size = size; |
| 1498 |
} |
| 1499 |
|
| 1500 |
public double getAdaptableParameter() { |
| 1501 |
return size; |
| 1502 |
} |
| 1503 |
|
| 1504 |
public void setAdaptableParameter(double value) { |
| 1505 |
setSize(value); |
| 1506 |
} |
| 1507 |
|
| 1508 |
public double getRawParameter() { |
| 1509 |
return size; |
| 1510 |
} |
| 1511 |
|
| 1512 |
@Override |
| 1513 |
public String getAdaptableParameterName() { |
| 1514 |
return null; |
| 1515 |
} |
| 1516 |
|
| 1517 |
|
| 1518 |
|
| 1519 |
|
| 1520 |
|
| 1521 |
public double getTargetAcceptanceProbability() { |
| 1522 |
return 0.5; |
| 1523 |
} |
| 1524 |
|
| 1525 |
public String getOperatorName() { |
| 1526 |
return ARG_EVENT_OPERATOR; |
| 1527 |
} |
| 1528 |
|
| 1529 |
public static dr.xml.XMLObjectParser PARSER = new dr.xml.AbstractXMLObjectParser() { |
| 1530 |
|
| 1531 |
public String getParserName() { |
| 1532 |
return ARG_EVENT_OPERATOR; |
| 1533 |
} |
| 1534 |
|
| 1535 |
|
| 1536 |
|
| 1537 |
|
| 1538 |
|
| 1539 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 1540 |
|
| 1541 |
|
| 1542 |
|
| 1543 |
|
| 1544 |
|
| 1545 |
|
| 1546 |
|
| 1547 |
|
| 1548 |
|
| 1549 |
|
| 1550 |
AdaptationMode mode = AdaptationMode.parseMode(xo); |
| 1551 |
|
| 1552 |
ARGModel treeModel = (ARGModel) xo.getChild(ARGModel.class); |
| 1553 |
|
| 1554 |
CompoundParameter parameter1 = (CompoundParameter) xo.getChild(INTERNAL_NODES).getChild(0); |
| 1555 |
CompoundParameter parameter2 = (CompoundParameter) xo.getChild(INTERNAL_AND_ROOT).getChild(0); |
| 1556 |
CompoundParameter parameter3 = (CompoundParameter) xo.getChild(NODE_RATES).getChild(0); |
| 1557 |
|
| 1558 |
int weight = xo.getIntegerAttribute("weight"); |
| 1559 |
double size = xo.getDoubleAttribute(ADD_PROBABILITY); |
| 1560 |
if (size > 0 && size < 1) |
| 1561 |
size = Math.log(size / (1.0 - size)); |
| 1562 |
else |
| 1563 |
throw new XMLParseException(ADD_PROBABILITY + " must be between 0 and 1"); |
| 1564 |
|
| 1565 |
double belowRootProb = 0.9; |
| 1566 |
if (xo.hasAttribute(BELOW_ROOT_PROBABILITY)) { |
| 1567 |
belowRootProb = xo.getDoubleAttribute(BELOW_ROOT_PROBABILITY); |
| 1568 |
if (belowRootProb >= 1 || belowRootProb <= 0) { |
| 1569 |
throw new XMLParseException(BELOW_ROOT_PROBABILITY + " must fall in (0,1)"); |
| 1570 |
} |
| 1571 |
} |
| 1572 |
|
| 1573 |
ARGPartitionLikelihood partitionLike = (ARGPartitionLikelihood) xo.getChild(ARGPartitionLikelihood.class); |
| 1574 |
|
| 1575 |
int tossSize = 0; |
| 1576 |
if (xo.hasAttribute(ARGPartitioningOperator.TOSS_SIZE)) { |
| 1577 |
tossSize = xo.getIntegerAttribute(ARGPartitioningOperator.TOSS_SIZE); |
| 1578 |
Logger.getLogger("dr.evomodel").info(ARG_EVENT_OPERATOR + " is joint with " + ARGPartitioningOperator.OPERATOR_NAME); |
| 1579 |
if (tossSize <= 0 || tossSize >= treeModel.getNumberOfPartitions()) { |
| 1580 |
throw new XMLParseException("Toss size is incorrect"); |
| 1581 |
} |
| 1582 |
} |
| 1583 |
|
| 1584 |
ARGRatePrior ratePrior = null; |
| 1585 |
|
| 1586 |
if (xo.hasAttribute(RELAXED)) { |
| 1587 |
ratePrior = (ARGRatePrior) xo.getChild(ARGRatePrior.class); |
| 1588 |
} |
| 1589 |
|
| 1590 |
return new ARGAddRemoveEventOperator(treeModel, weight, size, |
| 1591 |
mode, parameter1, parameter2, parameter3, |
| 1592 |
belowRootProb, partitionLike, ratePrior, tossSize); |
| 1593 |
} |
| 1594 |
|
| 1595 |
public String getParserDescription() { |
| 1596 |
return "An operator that slides a subarg."; |
| 1597 |
} |
| 1598 |
|
| 1599 |
public Class getReturnType() { |
| 1600 |
return ObsoleteARGAddRemoveEventOperator.class; |
| 1601 |
} |
| 1602 |
|
| 1603 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 1604 |
return rules; |
| 1605 |
} |
| 1606 |
|
| 1607 |
private final XMLSyntaxRule[] rules;{ |
| 1608 |
rules = new XMLSyntaxRule[]{ |
| 1609 |
AttributeRule.newDoubleRule(ADD_PROBABILITY, false, |
| 1610 |
"The probability that the operator adds a new" |
| 1611 |
+ " reassortment event"), |
| 1612 |
AttributeRule.newBooleanRule(JOINT_PARTITIONING, true), |
| 1613 |
AttributeRule.newIntegerRule(ARGPartitioningOperator.TOSS_SIZE, true), |
| 1614 |
AttributeRule.newDoubleRule(ADD_PROBABILITY, true), |
| 1615 |
AttributeRule.newBooleanRule(AUTO_OPTIMIZE, true), |
| 1616 |
AttributeRule.newIntegerRule(WEIGHT, false), |
| 1617 |
new ElementRule(ARGModel.class, false), |
| 1618 |
new ElementRule(ARGPartitionLikelihood.class, false), |
| 1619 |
new ElementRule(ARGRatePrior.class, true), |
| 1620 |
new ElementRule(INTERNAL_NODES, |
| 1621 |
new XMLSyntaxRule[]{ |
| 1622 |
new ElementRule(CompoundParameter.class)}), |
| 1623 |
new ElementRule(INTERNAL_AND_ROOT, |
| 1624 |
new XMLSyntaxRule[]{ |
| 1625 |
new ElementRule(CompoundParameter.class)}), |
| 1626 |
new ElementRule(NODE_RATES, |
| 1627 |
new XMLSyntaxRule[]{ |
| 1628 |
new ElementRule(CompoundParameter.class)}), |
| 1629 |
}; |
| 1630 |
} |
| 1631 |
}; |
| 1632 |
|
| 1633 |
private class NoReassortmentEventException extends Exception { |
| 1634 |
public NoReassortmentEventException(String message) { |
| 1635 |
super(message); |
| 1636 |
} |
| 1637 |
|
| 1638 |
public NoReassortmentEventException() { |
| 1639 |
super(""); |
| 1640 |
} |
| 1641 |
|
| 1642 |
private static final long serialVersionUID = 1L; |
| 1643 |
|
| 1644 |
} |
| 1645 |
|
| 1646 |
} |
| 1647 |
|