Debian Codegraph @

src/dr/evomodel/arg/operators/ARGAddRemoveEventOperator.java

raw symbols SCIP
1 /*
2 * ARGAddRemoveEventOperator.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 /*
27 * AddRemoveSubtreeOperator.java
28 *
29 * (c) 2002-2005 BEAST Development Core Team
30 *
31 * This package may be distributed under the
32 * Lesser Gnu Public Licence (LGPL)
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 * Adds and removes re-assortment events.
59 *
60 * @author Erik Bloomquist
61 * @author Marc Suchard
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; //Translates into add probability of 50%
83 private double probBelowRoot = 0.9; //Transformed in constructor for computational efficiency
84
85 private ARGPartitionLikelihood partLike;
86 private ARGRatePrior ratePrior;
87
88
89 // private int mode = AdaptableMCMCOperator.ADAPTATION_OFF;
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 // this.isRecombination = arg.isRecombinationPartitionType();
120 //// this.mode = mode;
121 // this.flipMean = flipMean;
122 //
123 // if (flipMean > -1) {
124 // this.pd = new PoissonDistribution(flipMean);
125 // logPoissonNormalizingFactor = Math.log(pd.cdf(arg.getNumberOfPartitions() - 1) - pd.cdf(0));
126 // }
127 //
128 // if (isRecombination) {
129 // logPartitionSize = Math.log(arg.getNumberOfPartitions() - 1);
130 // } else if (arg.getNumberOfPartitions() > 40) {
131 // //The -1 factor only matters for smaller problems,
132 // //so we ignore when partition size gets big.
133 // //Difference is very, very tiny.
134 // logPartitionSize = (arg.getNumberOfPartitions() - 1) * LOG_TWO;
135 // } else {
136 // logPartitionSize = Math.log(Math.pow(2, arg.getNumberOfPartitions() - 1) - 1);
137 // }
138
139 setWeight(weight);
140
141 this.probBelowRoot = belowRootProbability;
142
143 //This is for computational efficiency
144 probBelowRoot = -Math.log(1 - Math.sqrt(probBelowRoot));
145
146 this.tossSize = tossSize;
147 }
148
149
150 /**
151 * Do a add/remove re-assortment node operation
152 *
153 * @return the log-transformed hastings ratio
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 // This is the ugly mixture proposal
193 // double meanRoot = 4.0 / treeHeight;
194 // double case1 = 0.95;
195 //
196 // if(MathUtils.nextDouble() < case1){
197 // newReassortmentHeight = MathUtils.nextDouble() * treeHeight;
198 // newBifurcationHeight = MathUtils.nextDouble() * treeHeight;
199 //
200 // logHastings += 2.0*Math.log(treeHeight) - Math.log(2.0*case1);
201 // }else{
202 // newReassortmentHeight = MathUtils.nextDouble() * treeHeight;
203 // double additional = MathUtils.nextExponential(meanRoot);
204 // logHastings += Math.log(treeHeight) + additional*meanRoot -
205 // Math.log((1-case1)*meanRoot);
206 //
207 // newBifurcationHeight = additional + treeHeight;
208 // }
209
210 if (newBifurcationHeight < newReassortmentHeight) {
211 double temp = newBifurcationHeight;
212 newBifurcationHeight = newReassortmentHeight;
213 newReassortmentHeight = temp;
214 }
215
216 //2. Find the possible re-assortment and bifurcation points.
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 //3. Choose your re-assortment location.
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 // logHastings += LOG_TWO;
251 } else if (tester[0]) {
252 //nothing to do, setup above
253 } else if (tester[1]) {
254 splitReassortLeftParent = false;
255 reassortSplitParent = reassortRightParent;
256 } else {
257 assert false;
258 }
259 }
260
261
262 // if (recParentL != recParentR) {
263 // boolean[] tester = {arg.getNodeHeight(recParentL) > newReassortmentHeight,
264 // arg.getNodeHeight(recParentR) > newReassortmentHeight};
265 //
266 // if (tester[0] && tester[1]) {
267 // if (MathUtils.nextBoolean()) {
268 // recParent = recParentR;
269 // }
270 //
271 // logHastings += LOG_TWO;
272 // } else if (tester[0]) {
273 // recParent = recParentL;
274 // } else {
275 // recParent = recParentR;
276 // }
277 // }
278
279 //4. Choose your bifurcation location.
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 // logHastings += LOG_TWO;
301 } else if (tester[0]) {
302 //nothing to do
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 //During the delete step, this guy gets cancelled out!
314 logHastings += LOG_TWO;
315
316
317 // if (sisParentL != sisParentR) {
318 // boolean[] tester = {arg.getNodeHeight(sisParentL) > newBifurcationHeight,
319 // arg.getNodeHeight(sisParentR) > newBifurcationHeight};
320 //
321 // if (tester[0] && tester[1]) {
322 // if (MathUtils.nextBoolean()) {
323 // sisParent = sisParentR;
324 // }
325 // logHastings += LOG_TWO;
326 // } else if (tester[0]) {
327 // sisParent = sisParentL;
328 // } else {
329 // sisParent = sisParentR;
330 // }
331 // }
332
333 //5. Make the new nodes.
334 //Note: The height stuff is taken care of below.
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 // logHastings += ratePrior.getAddHastingsRatio(reassortmentValues);
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 //6. Begin editing the tree.
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 //bifurcation on left, reassortment on right
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 //bifurcation on right, reassortment on left
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 // arg.expandARGWithRecombinant(newBifurcation, newReassortment,
597 // internalNodeParameters,
598 // internalAndRootNodeParameters,
599 // nodeRates);
600 assert nodeCheck() : arg.toARGSummary();
601
602 } else {
603
604 assert newReassortmentHeight < treeHeight;
605
606
607 //New bifurcation takes the place of the old root.
608 //Much easier to program.
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 // arg.expandARGWithRecombinant(newBifurcation, newReassortment,
662 // internalNodeParameters,
663 // internalAndRootNodeParameters,
664 // nodeRates);
665
666 assert nodeCheck();
667
668
669 }
670
671
672 //6a. This is when we do not create a new root.
673 // if (newBifurcationHeight < treeHeight) {
674 // newBifurcation.heightParameter = new Parameter.Default(newBifurcationHeight);
675 // newReassortment.heightParameter = new Parameter.Default(newReassortmentHeight);
676 // newBifurcation.setupHeightBounds();
677 // newReassortment.setupHeightBounds();
678 //
679 // if (sisParent.bifurcation)
680 // arg.singleRemoveChild(sisParent, bifurcationChild);
681 // else
682 // arg.doubleRemoveChild(sisParent, bifurcationChild);
683 // if (bifurcationChild != reassortChild) {
684 // if (recParent.bifurcation)
685 // arg.singleRemoveChild(recParent, reassortChild);
686 // else
687 // arg.doubleRemoveChild(recParent, reassortChild);
688 // }
689 // if (sisParent.bifurcation)
690 // arg.singleAddChild(sisParent, newBifurcation);
691 // else
692 // arg.doubleAddChild(sisParent, newBifurcation);
693 // if (bifurcationChild != reassortChild)
694 // arg.singleAddChild(newBifurcation, bifurcationChild);
695 // arg.doubleAddChild(newReassortment, reassortChild);
696 //
697 // Parameter partitioning = new Parameter.Default(arg.getNumberOfPartitions());
698 // drawRandomPartitioning(partitioning);
699 //
700 // if (bifurcationChild != reassortChild) {
701 // arg.addChildAsRecombinant(newBifurcation, recParent,
702 // newReassortment, partitioning);
703 // } else {
704 // arg.addChildAsRecombinant(newBifurcation, newBifurcation,
705 // newReassortment, partitioning);
706 // }
707 // arg.expandARGWithRecombinant(newBifurcation, newReassortment,
708 // internalNodeParameters,
709 // internalAndRootNodeParameters,
710 // nodeRates);
711 // assert nodeCheck();
712 //
713 // //6b. But here we do.
714 // } else if (newReassortmentHeight < treeHeight) {
715 //
716 // newReassortment.heightParameter = new Parameter.Default(newReassortmentHeight);
717 // newReassortment.setupHeightBounds();
718 //
719 // bifurcationChild = newBifurcation;
720 // if (arg.isRoot(recParent))
721 // recParent = newBifurcation;
722 //
723 //
724 // Node root = (Node) arg.getRoot();
725 // Node rootLeftChild = root.leftChild;
726 // Node rootRightChild = root.rightChild;
727 //
728 // arg.singleRemoveChild(root, rootLeftChild);
729 // arg.singleRemoveChild(root, rootRightChild);
730 // arg.singleAddChild(newBifurcation, rootLeftChild);
731 // arg.singleAddChild(newBifurcation, rootRightChild);
732 //
733 // if (recParent.isBifurcation())
734 // arg.singleRemoveChild(recParent, reassortChild);
735 // else
736 // arg.doubleRemoveChild(recParent, reassortChild);
737 //
738 // arg.doubleAddChild(newReassortment, reassortChild);
739 // arg.singleAddChild(root, newBifurcation);
740 //
741 // Parameter partitioning = new Parameter.Default(arg.getNumberOfPartitions());
742 // drawRandomPartitioning(partitioning);
743 //
744 //
745 // arg.addChildAsRecombinant(root, recParent, newReassortment, partitioning);
746 //
747 // newBifurcation.heightParameter = new Parameter.Default(root.getHeight());
748 //
749 // newBifurcation.setupHeightBounds();
750 // root.heightParameter.setParameterValue(0, newBifurcationHeight);
751 //
752 //
753 // arg.expandARGWithRecombinant(newBifurcation, newReassortment,
754 // internalNodeParameters, internalAndRootNodeParameters,
755 // nodeRates);
756 //
757 // assert nodeCheck();
758 //
759 // } else {
760 //
761 // Node root = (Node) arg.getRoot();
762 // Node rootLeftChild = root.leftChild;
763 // Node rootRightChild = root.rightChild;
764 //
765 // arg.singleRemoveChild(root, rootLeftChild);
766 // arg.singleRemoveChild(root, rootRightChild);
767 // arg.singleAddChild(newBifurcation, rootLeftChild);
768 // arg.singleAddChild(newBifurcation, rootRightChild);
769 //
770 // arg.doubleAddChild(newReassortment, newBifurcation);
771 // arg.doubleAddChild(root, newReassortment);
772 //
773 // Parameter partitioning = new Parameter.Default(arg.getNumberOfPartitions());
774 // drawRandomPartitioning(partitioning);
775 //
776 // newReassortment.partitioning = partitioning;
777 //
778 // newBifurcation.heightParameter = new Parameter.Default(arg.getNodeHeight(root));
779 // newReassortment.heightParameter = new Parameter.Default(newReassortmentHeight);
780 // root.heightParameter.setParameterValueQuietly(0, newBifurcationHeight);
781 //
782 // newBifurcation.setupHeightBounds();
783 // newReassortment.setupHeightBounds();
784 //
785 // arg.expandARGWithRecombinant(newBifurcation, newReassortment,
786 // internalNodeParameters, internalAndRootNodeParameters,
787 // nodeRates);
788 //
789 // assert nodeCheck();
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 //Do all the backwards stuff now. :(
804
805 assert nodeCheck();
806
807 logHastings -= Math.log((double) findPotentialNodesToRemove(null));
808 // logHastings -= Math.log((double)arg.getReassortmentNodeCount());
809
810 // if (newReassortment.leftParent != newReassortment.rightParent){
811 // if(newReassortment.leftParent.bifurcation
812 // && newReassortment.rightParent.bifurcation)
813 // logHastings -= LOG_TWO;
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 //You're done, return the hastings ratio!
826
827 // System.out.println(logHastings);
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 // 1. Draw reassortment node uniform randomly
902
903 ArrayList<NodeRef> potentialNodes = new ArrayList<NodeRef>();
904 int totalPotentials = findPotentialNodesToRemove(potentialNodes);
905
906 if (totalPotentials == 0)
907 throw new NoReassortmentEventException();
908
909 // logHastings += Math.log((double)arg.getReassortmentNodeCount());
910 logHastings += Math.log((double) totalPotentials);
911
912 // double diff =(double)arg.getReassortmentNodeCount() - totalPotentials;
913 //
914 // if(MathUtils.nextDouble() < diff/totalPotentials)
915 // throw new NoReassortmentEventException();
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 //You should never go here.
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 // arg.contractARGWithRecombinant(recParent, recNode,
1098 // internalNodeParameters, internalAndRootNodeParameters, nodeRates);
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 //Do the backwards stuff now :(
1137
1138 double afterTreeHeight = arg.getNodeHeight(arg.getRoot());
1139
1140 // This is the ugly mixture proposal
1141 // double meanRoot = 4.0 / afterTreeHeight;
1142 // double case1 = 0.95;
1143 //
1144 // if(beforeBifurcationHeight < afterTreeHeight){
1145 // logHastings -= 2.0*Math.log(afterTreeHeight) - Math.log(2.0*case1);
1146 // }else{
1147 // double additional = beforeBifurcationHeight - afterTreeHeight;
1148 // logHastings -= Math.log(afterTreeHeight) + additional*meanRoot -
1149 // Math.log((1-case1)*meanRoot);
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 // if (!beforeBifurcationChild.bifurcation &&
1167 // arg.getNodeHeight(beforeBifurcationChild.leftParent) > beforeBifurcationHeight &&
1168 // arg.getNodeHeight(beforeBifurcationChild.rightParent) > beforeBifurcationHeight) {
1169 // logHastings -= LOG_TWO;
1170 //
1171 // }
1172
1173 // if (!beforeReassortChild.bifurcation &&
1174 // arg.getNodeHeight(beforeReassortChild.leftParent) > beforeReassortmentHeight &&
1175 // arg.getNodeHeight(beforeReassortChild.rightParent) > beforeReassortmentHeight) {
1176 // logHastings -= LOG_TWO;
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 // Only option is to swap partitions
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 // private int arraySum(int[] n) {
1280 // int b = 0;
1281 // for (int a : n)
1282 // b += a;
1283 // return b;
1284 // }
1285 //
1286 //
1287 //
1288 // private void drawReassortmentPartitionAllFlip(Parameter partition) {
1289 // int numberOfPartitionsMinusOne = arg.getNumberOfPartitions() - 1;
1290 //
1291 // int[] n = new int[numberOfPartitionsMinusOne];
1292 //
1293 // while (arraySum(n) == 0) {
1294 // for (int i = 0; i < n.length; i++) {
1295 // if (MathUtils.nextBoolean()) {
1296 // n[i] = 1;
1297 // } else {
1298 // n[i] = 0;
1299 // }
1300 // }
1301 // }
1302 //
1303 // partition.setParameterValueQuietly(0, 0);
1304 //
1305 // for (int i = 0; i < numberOfPartitionsMinusOne; i++) {
1306 // partition.setParameterValueQuietly(i + 1, n[i]);
1307 // }
1308 //
1309 //
1310 //
1311 // assert ARGPartitioningOperator.checkValidReassortmentPartition(partition);
1312 //
1313 // }
1314 //
1315 // private int nextFlipSize() {
1316 // int x = Poisson.nextPoisson(flipMean);
1317 // while (x > arg.getNumberOfPartitions() - 1 || x == 0) {
1318 // x = Poisson.nextPoisson(flipMean);
1319 // }
1320 //
1321 // assert x != arg.getNumberOfPartitions();
1322 // assert x != 0;
1323 //
1324 //
1325 // return x;
1326 // }
1327 //
1328 // private void drawReassortmentPartitionFixedFlip(Parameter partition) {
1329 // int numberOfPartitionsMinusOne = arg.getNumberOfPartitions() - 1;
1330 // int flipSize = nextFlipSize();
1331 //
1332 // if (flipSize == numberOfPartitionsMinusOne) {
1333 // for (int i = 1; i <= flipSize; i++)
1334 // partition.setParameterValueQuietly(i, 1.0);
1335 // return;
1336 // }
1337 //
1338 // double replaceValue = 1.0;
1339 // if (MathUtils.nextBoolean()) {
1340 // replaceValue = 0.0;
1341 //
1342 // for (int i = 1; i <= numberOfPartitionsMinusOne; i++) {
1343 // partition.setParameterValueQuietly(i, 1.0);
1344 // }
1345 // }
1346 //
1347 //
1348 // ArrayList<Integer> a = new ArrayList<Integer>(flipSize);
1349 // while (a.size() < flipSize) {
1350 // int b = MathUtils.nextInt(numberOfPartitionsMinusOne) + 1;
1351 // if (!a.contains(b)) {
1352 // a.add(b);
1353 // }
1354 // }
1355 //
1356 //
1357 // for (int b : a) {
1358 // partition.setParameterValueQuietly(b, replaceValue);
1359 // }
1360 //
1361 // }
1362 //
1363 // private void drawRecombinationPartition(Parameter partition) {
1364 // int lengthMinusOne = arg.getNumberOfPartitions() - 1;
1365 //
1366 // int cut = MathUtils.nextInt(lengthMinusOne);
1367 //
1368 // int leftValue = 0; //At one time, these values could switch.
1369 // int rightValue = 1;
1370 //
1371 // for (int i = 0; i < cut + 1; i++)
1372 // partition.setParameterValueQuietly(i, leftValue);
1373 // for (int i = cut + 1; i < partition.getDimension(); i++)
1374 // partition.setParameterValueQuietly(i, rightValue);
1375 // }
1376
1377 /* Draws a new partitioning.
1378 * With probability singlePartitionProbability, one bit is set;
1379 * otherwise, all bits are selected via a random permutation
1380 *
1381 */
1382 /* private double drawRandomReassortment(Parameter partitioning) {
1383 int len = arg.getNumberOfPartitions();
1384 double logq = 0;
1385 if (MathUtils.nextDouble() < singlePartitionProbability) {
1386 if (partitioning != null)
1387 partitioning.setParameterValueQuietly(MathUtils.nextInt(len), 1.0);
1388 return Math.log(len);
1389 }
1390 int[] permutation = MathUtils.permuted(len);
1391 int cut = MathUtils.nextInt(len - 1);
1392 for (int i = 0; i < len; i++) {
1393 logq += Math.log(i + 1);
1394 if (i > cut && partitioning != null)
1395 partitioning.setParameterValueQuietly(permutation[i], 1.0);
1396 }
1397 logq += Math.log(len - 1);
1398 return logq;
1399 } */
1400
1401 /* Draws a new partitioning.
1402 * A break-pt is drawn uniformly
1403 *
1404 */
1405 /* private double drawRandomRecombination(Parameter partitioning) {
1406 int len = arg.getNumberOfPartitions();
1407 double logq = 0;
1408 double leftValue = MathUtils.nextInt(2);
1409 double rightValue = 1.0 - leftValue;
1410 logq += Math.log(2);
1411 if (partitioning != null) {
1412 int cut = MathUtils.nextInt(len - 1);
1413 for (int i = 0; i <= cut; i++)
1414 partitioning.setParameterValueQuietly(i, leftValue);
1415 for (int i = cut + 1; i < len; i++)
1416 partitioning.setParameterValueQuietly(i, rightValue);
1417 }
1418 logq += Math.log(len - 1);
1419 return logq;
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 ////Coercible MCMC Operator stuff
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 // public int getMode() {
1518 // return mode;
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 //public String[] getParserNames(){
1536 // return new String[]{getParserName(),"argEventOperator",};
1537 //}
1538
1539 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
1540
1541 // int mode = AdaptableMCMCOperator.DEFAULT;
1542 // if (xo.hasAttribute(AUTO_OPTIMIZE)) {
1543 // if (xo.getBooleanAttribute(AUTO_OPTIMIZE)) {
1544 // mode = AdaptableMCMCOperator.ADAPTATION_ON;
1545 // } else {
1546 // mode = AdaptableMCMCOperator.ADAPTATION_OFF;
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