Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * ObsoleteARGNewEventOperator.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 package dr.evomodel.arg.operators;
35
36 import dr.evolution.tree.NodeRef;
37 import dr.evolution.tree.TreeUtils;
38 import dr.evomodel.arg.ARGModel;
39 import dr.evomodel.arg.ARGModel.Node;
40 import dr.evomodelxml.tree.TreeModelParser;
41 import dr.inference.model.CompoundParameter;
42 import dr.inference.model.Parameter;
43 import dr.inference.operators.*;
44 import dr.math.MathUtils;
45 import dr.math.functionEval.GammaFunction;
46 import dr.xml.*;
47
48 import java.util.ArrayList;
49
50 /**
51 * Implements the subtree slide move.
52 *
53 * @author Marc Suchard
54 * @version $Id: ObsoleteARGAddRemoveEventOperator.java,v 1.18.2.4 2006/11/06 01:38:30 msuchard Exp $
55 */
56
57 public class ObsoleteARGNewEventOperator extends AbstractAdaptableOperator {
58 // SimpleMCMCOperator implements AdaptableMCMCOperator {
59
60 public static final String SUBTREE_SLIDE = "newARGEvent";
61 public static final String SWAP_RATES = "swapRates";
62 public static final String SWAP_TRAITS = "swapTraits";
63 public static final String MAX_VALUE = "maxTips";
64 public static final String SINGLE_PARTITION = "singlePartitionProbability";
65 public static final String IS_RECOMBINATION = "isRecombination";
66
67 public static final String JUST_INTERNAL = "justInternalNodes";
68 public static final String INTERNAL_AND_ROOT = "internalAndRootNodes";
69 public static final String NODE_RATES = TreeModelParser.NODE_RATES;
70
71 private ARGModel arg = null;
72 private double size = 1.0;
73 private boolean gaussian = false;
74 private double singlePartitionProbability = 0.0;
75 private boolean isRecombination = false;
76 // private boolean swapRates;
77 // private boolean swapTraits;
78 // private int mode = AdaptableMCMCOperator.DEFAULT;
79 private CompoundParameter internalNodeParameters;
80 private CompoundParameter internalAndRootNodeParameters;
81 private CompoundParameter nodeRates;
82 // private int maxTips = 1;
83
84 public ObsoleteARGNewEventOperator(ARGModel arg, int weight, double size, boolean gaussian,
85 boolean swapRates, boolean swapTraits, AdaptationMode mode,
86 CompoundParameter param1,
87 CompoundParameter param2,
88 CompoundParameter param3,
89 double singlePartitionProbability, boolean isRecombination) {
90 super(mode);
91 this.arg = arg;
92 setWeight(weight);
93 // this.maxTips = maxTips;
94 this.size = size;
95 this.gaussian = gaussian;
96 // this.swapRates = swapRates;
97 // this.swapTraits = swapTraits;
98 this.internalNodeParameters = param1;
99 this.internalAndRootNodeParameters = param2;
100 this.nodeRates = param3;
101 this.singlePartitionProbability = singlePartitionProbability;
102 this.isRecombination = isRecombination;
103
104 // this.mode = mode;
105 }
106
107
108 /**
109 * Do a add/remove reassortment node operation
110 *
111 * @return the log-transformed hastings ratio
112 */
113 public double doOperation() {
114 // System.err.println("Starting AddRemove Operation");
115
116 double logq = 0;
117 try {
118 if (MathUtils.nextDouble() < 0.5)
119 logq = AddOperation();
120 else
121 logq = RemoveOperation();
122 } catch (Exception ofe) {
123 // System.err.println(ofe);
124 if (ofe.getMessage().compareTo("No reassortment nodes to remove.") != 0) {
125 System.err.println("Catch: " + ofe.getMessage());
126 System.exit(-1);
127 }
128 }
129 // if (arg.isBifurcationDoublyLinked(arg.getRoot()))
130 // throw new OperatorFailedException("trouble with double-rooted root");
131 // System.err.println("logq = "+logq);
132 return logq;
133 }
134
135 private int findPotentialNodesToRemove(ArrayList<NodeRef> list) {
136 int count = 0;
137 int n = arg.getNodeCount();
138 // int max = arg.getReassortmentNodeCount();
139 Node root = (Node) arg.getRoot();
140 for (int i = 0; i < n; i++) {
141 Node node = (Node) arg.getNode(i);
142 if (node.isReassortment()
143 && ((node.leftParent != root && node.leftParent.isBifurcation()) ||
144 (node.rightParent != root && node.rightParent.isBifurcation()))
145 // && !(
146 // (node.leftParent == root && node.rightParent.isReassortment()) ||
147 // (node.rightParent== root && node.leftParent.isReassortment()))
148 // && !(node.leftParent == root && node.rightParent == root)
149 // && (node.leftParent.isBifurcation() || node.rightParent.isBifurcation())
150 ) {
151 if (list != null)
152 list.add(node);
153 count++;
154 }
155 }
156 // System.err.printf("max = %d, available = %d\n",max,count);
157 return count;
158 }
159
160 private double RemoveOperation() throws ARGOperatorFailedException {
161 double logq = 0;
162
163 // System.err.println("Starting remove ARG operation.");
164 // System.err.println("Before remove:\n"+arg.toGraphString());
165
166 // 1. Draw reassortment node uniform randomly
167
168 ArrayList<NodeRef> potentialNodes = new ArrayList<NodeRef>();
169
170 int totalPotentials = findPotentialNodesToRemove(potentialNodes);
171 if (totalPotentials == 0)
172 throw new ARGOperatorFailedException("No reassortment nodes to remove.");
173 // System.err.println("potentials exist!");
174 Node recNode = (Node) potentialNodes.get(MathUtils.nextInt(totalPotentials));
175 // logq += Math.log(totalPotentials);
176
177 double reverseReassortmentHeight = 0;
178 double reverseBifurcationHeight = 0;
179
180 double treeHeight = arg.getNodeHeight(arg.getRoot());
181
182 // System.err.println("RecNode to remove = "+recNode.number);
183 reverseReassortmentHeight = arg.getNodeHeight(recNode);
184
185 arg.beginTreeEdit();
186 boolean doneSomething = false;
187 Node recParent = recNode.leftParent;
188 Node recChild = recNode.leftChild;
189 if (recNode.leftParent == recNode.rightParent) { // Doubly linked.
190 Node recGrandParent = recParent.leftParent;
191 // reverseBifurcationHeight = arg.getNodeHeight(recParent);
192
193 // reverseReassortmentSpan = arg.getNodeHeight(recParent) - arg.getNodeHeight(recChild);
194 // reverseBifurcationSpan = arg.getNodeHeight(recGrandParent) - arg.getNodeHeight(recChild);
195 // if (arg.isRoot(recParent)) { // This case should never happen as double links
196 // arg.setRoot(recChild); // to root can not be added or removed.
197 // System.err.println("doubled link root?");
198 // System.exit(-1);
199 // } else {
200 //Node recGrandParent = recParent1.leftParent; // And currently recParent must be a bifurcatio
201 //System.err.println("recGrand ="+recGrandParent.number);
202 arg.doubleRemoveChild(recGrandParent, recParent);
203 arg.doubleRemoveChild(recNode, recChild);
204 if (recGrandParent.bifurcation)
205 arg.singleAddChild(recGrandParent, recChild);
206 else
207 arg.doubleAddChild(recGrandParent, recChild);
208 // }
209 doneSomething = true;
210 // There are not left/right choices to be made for doubly linked removals.
211 // End doubly linked.
212 } else { // Two different parents.
213 Node recParent1 = recNode.leftParent;
214 Node recParent2 = recNode.rightParent;
215 if (!recParent1.bifurcation || recParent1.isRoot()) { // One orientation is valid
216 recParent1 = recNode.rightParent;
217 recParent2 = recNode.leftParent;
218 } else if (recParent2.bifurcation && !recParent2.isRoot()) { // Both orientations are valid
219 if (MathUtils.nextDouble() < 0.5) { // choose equally likely
220 recParent1 = recNode.rightParent;
221 recParent2 = recNode.leftParent;
222 }
223 logq += Math.log(2);
224 }
225
226 Node recGrandParent = recParent1.leftParent; // And currently recParent must be a bifurcatio
227
228 Node otherChild = recParent1.leftChild;
229 if (otherChild == recNode)
230 otherChild = recParent1.rightChild;
231
232 if (recGrandParent.bifurcation)
233 arg.singleRemoveChild(recGrandParent, recParent1);
234 else
235 arg.doubleRemoveChild(recGrandParent, recParent1);
236
237 arg.singleRemoveChild(recParent1, otherChild);
238 if (recParent2.bifurcation)
239 arg.singleRemoveChild(recParent2, recNode);
240 else
241 arg.doubleRemoveChild(recParent2, recNode);
242 arg.doubleRemoveChild(recNode, recChild);
243 if (otherChild != recChild) {
244 if (recGrandParent.bifurcation)
245 arg.singleAddChild(recGrandParent, otherChild);
246 else
247 arg.doubleAddChild(recGrandParent, otherChild);
248 if (recParent2.bifurcation)
249 arg.singleAddChild(recParent2, recChild);
250 else
251 arg.doubleAddChild(recParent2, recChild);
252 } else {
253 if (recGrandParent.bifurcation)
254 arg.singleAddChildWithOneParent(recGrandParent, otherChild);
255 else
256 arg.doubleAddChildWithOneParent(recGrandParent, otherChild);
257 if (recParent2.bifurcation)
258 arg.singleAddChildWithOneParent(recParent2, recChild);
259 else
260 arg.doubleAddChildWithOneParent(recParent2, recChild);
261 }
262 // System.err.println("Sanity check in Remove Operator");
263 // sanityCheck();
264 // System.err.println("End Remove Operator in sanity check");
265 doneSomething = true;
266
267 recParent = recParent1;
268 }
269
270 reverseBifurcationHeight = arg.getNodeHeight(recParent);
271
272 if (doneSomething) {
273 try {
274 arg.contractARGWithRecombinant(recParent, recNode,
275 internalNodeParameters, internalAndRootNodeParameters, nodeRates);
276 } catch (Exception e) {
277 System.err.println("here");
278 System.err.println(e);
279
280 }
281 }
282
283 arg.pushTreeSizeChangedEvent();
284 arg.endTreeEdit();
285 // try {
286 // arg.checkTreeIsValid();
287 // } catch (MutableTree.InvalidTreeException ite) {
288 // throw new RuntimeException(ite.toString() + "\n" + arg.toString()
289 // + "\n" + Tree.Utils.uniqueNewick(arg, arg.getRoot()));
290 // }
291
292 // double d1 = findPotentialAttachmentPoints(reverseBifurcationHeight,null);
293 // double d2 = findPotentialAttachmentPoints(reverseReassortmentHeight,null);
294 // System.err.printf("d1 = %5.4f, d2 = %5.4f\n",d1,d2);
295
296 // logq -= Math.log(findPotentialAttachmentPoints(reverseBifurcationHeight,null));
297 // logq -= Math.log(findPotentialAttachmentPoints(reverseReassortmentHeight,null));
298
299 // int nodes = arg.getInternalNodeCount() - 1;
300 //
301 // logq += 3*Math.log(2);
302 //
303 // logq = 100;
304
305 // System.err.println("logq remove = "+logq);
306 logq = 0;
307 // logq -= 10;
308 // logq += Math.log(totalPotentials);
309 return logq; // 1 / total potentials * 1 / 2 (if valid) * length1 * length2 * attachmentSisters
310 }
311
312 private static double lnGamma(double x) {
313 if (x == 1 || x == 2)
314 return 0.0;
315 return GammaFunction.logGamma(x);
316 }
317
318 private void checkAllHeights() {
319 int len = arg.getInternalNodeCount();
320 System.err.println("# internal nodes = " + len);
321 int n = internalNodeParameters.getParameterCount();
322 System.err.println("VSCP (" + n + ")");
323 for (int i = 0; i < n; i++) {
324 System.err.println(internalNodeParameters.getParameterValue(i));
325 }
326 n = arg.getInternalNodeCount();
327 System.err.println("Checking all internal nodes (" + n + ") via tree:");
328 for (int i = 0; i < n; i++) {
329 NodeRef node = arg.getInternalNode(i);
330 System.err.print(TreeUtils.uniqueNewick(arg, node) + " ");
331 System.err.println(((Node) node).getHeight());
332 }
333 }
334
335 private int findPotentialAttachmentPoints(double time, ArrayList<NodeRef> list) {
336 int count = 0;
337 for (int i = 0, n = arg.getNodeCount(); i < n; i++) {
338 NodeRef nr = arg.getNode(i);
339 if (!arg.isRoot(nr) && arg.getNodeHeight(nr) < time) {
340 if (arg.getNodeHeight(arg.getParent(nr, 0)) > time) {
341 if (list != null)
342 list.add(nr);
343 count++;
344 }
345 if (arg.isReassortment(nr) && arg.getNodeHeight(arg.getParent(nr, 1)) > time) {
346 if (list != null)
347 list.add(nr);
348 count++;
349 }
350 }
351 }
352 return count;
353 }
354
355 private double drawRandomPartitioning(Parameter partitioning) {
356 double logq = 0;
357 int len = arg.getNumberOfPartitions();
358 if (len == 2) {
359 // boolean first = MathUtils.nextBoolean();
360 if (partitioning != null) {
361 if (MathUtils.nextBoolean())
362 partitioning.setParameterValueQuietly(0, 1.0);
363 else
364 partitioning.setParameterValueQuietly(1, 1.0);
365 }
366 return Math.log(2);
367 }
368 if (isRecombination) {
369 logq += drawRandomRecombination(partitioning);
370 } else {
371 logq += drawRandomReassortment(partitioning);
372 }
373 return logq;
374 }
375
376
377 /* Draws a new partitioning.
378 * With probability singlePartitionProbability, one bit is set;
379 * otherwise, all bits are selected via a random permutation
380 *
381 */
382 private double drawRandomReassortment(Parameter partitioning) {
383 int len = arg.getNumberOfPartitions();
384 double logq = 0;
385 if (MathUtils.nextDouble() < singlePartitionProbability) {
386 if (partitioning != null)
387 partitioning.setParameterValueQuietly(MathUtils.nextInt(len), 1.0);
388 return Math.log(len);
389 }
390 int[] permutation = MathUtils.permuted(len);
391 int cut = MathUtils.nextInt(len - 1);
392 for (int i = 0; i < len; i++) {
393 logq += Math.log(i + 1);
394 if (i > cut && partitioning != null)
395 partitioning.setParameterValueQuietly(permutation[i], 1.0);
396 }
397 logq += Math.log(len - 1);
398 return logq;
399 }
400
401 /* Draws a new partitioning.
402 * A break-pt is drawn uniformly
403 *
404 */
405 private double drawRandomRecombination(Parameter partitioning) {
406 int len = arg.getNumberOfPartitions();
407 double logq = 0;
408 double leftValue = MathUtils.nextInt(2);
409 double rightValue = 1.0 - leftValue;
410 logq += Math.log(2);
411 if (partitioning != null) {
412 int cut = MathUtils.nextInt(len - 1);
413 for (int i = 0; i <= cut; i++)
414 partitioning.setParameterValueQuietly(i, leftValue);
415 for (int i = cut + 1; i < len; i++)
416 partitioning.setParameterValueQuietly(i, rightValue);
417 }
418 logq += Math.log(len - 1);
419 return logq;
420 }
421
422
423 private double AddOperation() throws ARGOperatorFailedException {
424
425 double logq = 0;
426
427 // Draw attachment point for new bifurcation
428
429 // System.err.println("Starting add operation.");
430
431 ArrayList<NodeRef> potentialBifurcationChildren = new ArrayList<NodeRef>();
432 ArrayList<NodeRef> potentialReassortmentChildren = new ArrayList<NodeRef>();
433
434 double treeHeight = arg.getNodeHeight(arg.getRoot());
435
436 double newBifurcationHeight = treeHeight * MathUtils.nextDouble();
437 double newReassortmentHeight = treeHeight * MathUtils.nextDouble();
438 if (newReassortmentHeight > newBifurcationHeight) {
439 double temp = newReassortmentHeight;
440 newReassortmentHeight = newBifurcationHeight;
441 newBifurcationHeight = temp;
442 }
443
444 // logq += 2.0 * Math.log(treeHeight); // Uniform before sorting
445
446 int totalPotentialBifurcationChildren = findPotentialAttachmentPoints(
447 newBifurcationHeight, potentialBifurcationChildren);
448 int totalPotentialReassortmentChildren = findPotentialAttachmentPoints(
449 newReassortmentHeight, potentialReassortmentChildren);
450
451 if (totalPotentialBifurcationChildren == 0 || totalPotentialReassortmentChildren == 0) {
452 throw new RuntimeException("Unable to find attachment points.");
453 }
454
455 Node recNode = (Node) potentialReassortmentChildren.get(MathUtils
456 .nextInt(totalPotentialReassortmentChildren));
457 // logq += Math.log(totalPotentialReassortmentChildren);
458
459 Node recParentL = recNode.leftParent;
460 Node recParentR = recNode.rightParent;
461 Node recParent = recParentL;
462 if (recParentL != recParentR) {
463 if (arg.getNodeHeight(recParentL) < newReassortmentHeight)
464 recParent = recParentR;
465 else if (arg.getNodeHeight(recParentR) > newReassortmentHeight
466 && MathUtils.nextDouble() > 0.5)
467 recParent = recParentR;
468 }
469
470
471 Node sisNode = (Node) potentialBifurcationChildren.get(MathUtils
472 .nextInt(totalPotentialBifurcationChildren));
473 // logq += Math.log(totalPotentialBifurcationChildren);
474
475 Node sisParentL = sisNode.leftParent;
476 Node sisParentR = sisNode.rightParent;
477 Node sisParent = sisParentL;
478 if (sisParentL != sisParentR) {
479 if (arg.getNodeHeight(sisParentL) < newBifurcationHeight)
480 sisParent = sisParentR;
481 else if (arg.getNodeHeight(sisParentR) > newBifurcationHeight
482 && MathUtils.nextDouble() > 0.5)
483 sisParent = sisParentR;
484 }
485
486 double newBifurcationRateCategory = 1.0;
487 double newReassortmentRateCategory = 1.0;
488
489 Node newBifurcation = arg.new Node();
490 newBifurcation.heightParameter = new Parameter.Default(newBifurcationHeight);
491 newBifurcation.rateParameter = new Parameter.Default(newBifurcationRateCategory);
492 newBifurcation.setupHeightBounds();
493
494 Node newReassortment = arg.new Node();
495 newReassortment.bifurcation = false;
496 newReassortment.heightParameter = new Parameter.Default(newReassortmentHeight);
497 newReassortment.rateParameter = new Parameter.Default(newReassortmentRateCategory);
498 newReassortment.setupHeightBounds();
499
500 arg.beginTreeEdit();
501 if (sisParent.bifurcation)
502 arg.singleRemoveChild(sisParent, sisNode);
503 else
504 arg.doubleRemoveChild(sisParent, sisNode);
505 if (sisNode != recNode) {
506 if (recParent.bifurcation)
507 arg.singleRemoveChild(recParent, recNode);
508 else
509 arg.doubleRemoveChild(recParent, recNode);
510 }
511 if (sisParent.bifurcation)
512 arg.singleAddChild(sisParent, newBifurcation);
513 else
514 arg.doubleAddChild(sisParent, newBifurcation);
515 if (sisNode != recNode)
516 arg.singleAddChild(newBifurcation, sisNode);
517 arg.doubleAddChild(newReassortment, recNode);
518
519 Parameter partitioning = new Parameter.Default(arg.getNumberOfPartitions());
520
521 // logq +=
522 drawRandomPartitioning(partitioning);
523 // System.err.println("point 1");
524
525 if (sisNode != recNode) {
526 arg.addChildAsRecombinant(newBifurcation, recParent,
527 newReassortment, partitioning);
528 } else {
529 arg.addChildAsRecombinant(newBifurcation, newBifurcation,
530 newReassortment, partitioning);
531 }
532
533 arg.expandARGWithRecombinant(newBifurcation, newReassortment,
534 internalNodeParameters,
535 internalAndRootNodeParameters,
536 nodeRates);
537
538 // System.err.println("point 2");
539
540 arg.pushTreeSizeChangedEvent();
541
542 arg.endTreeEdit();
543 // try {
544 // arg.checkTreeIsValid();
545 // } catch (MutableTree.InvalidTreeException ite) {
546 // throw new RuntimeException(ite.toString() + "\n" + arg.toString()
547 // + "\n" + Tree.Utils.uniqueNewick(arg, arg.getRoot()));
548 // }
549
550 // double d1 = findPotentialNodesToRemove(null);
551 // System.err.printf("d1 = %5.4f\n",d1);
552
553 // logq -= Math.log(findPotentialNodesToRemove(null));
554 // if (!(recNode.leftParent.isBifurcation() && recNode.rightParent.isRoot()) &&
555 // !(recNode.rightParent.isBifurcation() && recNode.leftParent.isRoot()))
556 // logq -= Math.log(2.0);
557
558 // System.err.println("End add ARG operation.");
559
560 // int nodes = arg.getInternalNodeCount() - 1;
561
562 //
563 /* int i = findPotentialNodesToRemove(null);
564 if (i==0) {
565 System.err.println("why can't i remove this one?");
566 System.err.println("graph:"+arg.toGraphString());
567 System.exit(1);
568 return Double.NEGATIVE_INFINITY ;
569 }*/
570 logq = 0;
571 // logq -= Math.log(findPotentialNodesToRemove(null));
572 // System.err.println("logq add = "+logq);
573 // System.err.println("After add:\n"+arg.toGraphString());
574 return logq;
575 }
576
577 // if( !recParent1.bifurcation || recParent1.isRoot() ) { // One orientation is valid
578
579
580 public void sanityCheck() {
581 int len = arg.getNodeCount();
582 for (int i = 0; i < len; i++) {
583 Node node = (Node) arg.getNode(i);
584 if (node.bifurcation) {
585 boolean equalChild = (node.leftChild == node.rightChild);
586 if ((equalChild && node.leftChild != null)) {
587 if (!node.leftChild.bifurcation && ((node.leftChild).leftParent == node))
588 ;
589 else {
590 System.err.println("Node " + (i + 1) + " is insane.");
591 System.err.println(arg.toGraphString());
592 System.exit(-1);
593 }
594 }
595 } else {
596 if ((node.leftChild != node.rightChild)) {
597 System.err.println("Node " + (i + 1) + " is insane.");
598 System.err.println(arg.toGraphString());
599 System.exit(-1);
600 }
601 }
602 if (!node.isRoot()) {
603 double d;
604 d = node.getHeight();
605 }
606 }
607 }
608
609 private int times = 0;
610
611 private double getDelta() {
612 if (!gaussian) {
613 return (MathUtils.nextDouble() * size) - (size / 2.0);
614 } else {
615 return MathUtils.nextGaussian() * size;
616 }
617 }
618
619
620 public double getSize() {
621 return size;
622 }
623
624 public void setSize(double size) {
625 this.size = size;
626 }
627
628 public double getAdaptableParameter() {
629 return Math.log(getSize());
630 }
631
632 public void setAdaptableParameter(double value) {
633 setSize(Math.exp(value));
634 }
635
636 public double getRawParameter() {
637 return getSize();
638 }
639
640 // public int getMode() {
641 // return mode;
642 // }
643
644 public String getAdaptableParameterName() {
645 return "size";
646 }
647
648 public String getOperatorName() {
649 return SUBTREE_SLIDE;
650 }
651
652 public static dr.xml.XMLObjectParser PARSER = new dr.xml.AbstractXMLObjectParser() {
653
654 public String getParserName() {
655 return SUBTREE_SLIDE;
656 }
657
658 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
659
660 boolean swapRates = false;
661 boolean swapTraits = false;
662
663 double singlePartitionProbability = 0.0;
664 boolean isRecombination = false;
665
666 AdaptationMode mode = AdaptationMode.parseMode(xo);
667
668 // int mode = AdaptableMCMCOperator.DEFAULT;
669 // if (xo.hasAttribute(AUTO_OPTIMIZE)) {
670 // if (xo.getBooleanAttribute(AUTO_OPTIMIZE)) {
671 // mode = AdaptableMCMCOperator.ADAPTATION_ON;
672 // } else {
673 // mode = AdaptableMCMCOperator.ADAPTATION_OFF;
674 // }
675 // }
676
677 if (xo.hasAttribute(SINGLE_PARTITION)) {
678 singlePartitionProbability = xo.getDoubleAttribute(SINGLE_PARTITION);
679 }
680
681 if (xo.hasAttribute(IS_RECOMBINATION)) {
682 isRecombination = xo.getBooleanAttribute(IS_RECOMBINATION);
683
684 }
685
686 if (xo.hasAttribute(SWAP_RATES)) {
687 swapRates = xo.getBooleanAttribute(SWAP_RATES);
688 }
689 if (xo.hasAttribute(SWAP_TRAITS)) {
690 swapTraits = xo.getBooleanAttribute(SWAP_TRAITS);
691 }
692 Object obj = xo.getChild(ARGModel.class);
693 //TreeModel tmp = (TreeModel)xo.getChild(TreeModel.class);
694 ARGModel treeModel = null;
695 //if( (tmp.TREE_MODEL).compareTo(VariableSizeTreeModel.TREE_MODEL) == 0) {
696 if (obj instanceof ARGModel) {
697 //System.err.println("Found VSTM");
698 treeModel = (ARGModel) obj;
699 } else {
700 System.err.println("Must specify a variable size tree model to use the AddRemoveSubtreeOperators");
701 System.exit(-1);
702 }
703
704 // bug, getChild(String) returns an xmlObject; not in XML rules
705 CompoundParameter parameter1 = null; //(CompoundParameter) xo.getChild(JUST_INTERNAL);
706 CompoundParameter parameter2 = null; //CompoundParameter) xo.getChild(INTERNAL_AND_ROOT);
707 CompoundParameter parameter3 = null; //(CompoundParameter) xo.getChild(NODE_RATES);
708
709 int weight = xo.getIntegerAttribute("weight");
710 // int maxTips = xo.getIntegerAttribute(MAX_VALUE);
711 double size = xo.getDoubleAttribute("size");
712 boolean gaussian = xo.getBooleanAttribute("gaussian");
713 return new ObsoleteARGNewEventOperator(treeModel, weight, size, gaussian, swapRates, swapTraits,
714 mode, parameter1, parameter2, parameter3, singlePartitionProbability, isRecombination);
715 }
716
717 public String getParserDescription() {
718 return "An operator that slides a subarg.";
719 }
720
721 public Class getReturnType() {
722 return ObsoleteARGAddRemoveEventOperator.class;
723 }
724
725 public XMLSyntaxRule[] getSyntaxRules() {
726 return rules;
727 }
728
729 private final XMLSyntaxRule[] rules = {
730 AttributeRule.newIntegerRule("weight"),
731 // AttributeRule.newIntegerRule(MAX_VALUE),
732 AttributeRule.newDoubleRule("size"),
733 AttributeRule.newBooleanRule("gaussian"),
734 AttributeRule.newBooleanRule(SWAP_RATES, true),
735 AttributeRule.newBooleanRule(SWAP_TRAITS, true),
736 AttributeRule.newBooleanRule(AUTO_OPTIMIZE, true),
737 new ElementRule(ARGModel.class)//,
738 // new ElementRule(Parameter.class)
739 };
740 };
741
742 }