Debian Codegraph @

src/dr/evomodelxml/operators/SubtreeSlideOperatorParser.java

raw symbols SCIP
1 /*
2 * SubtreeSlideOperatorParser.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 package dr.evomodelxml.operators;
27
28 import dr.evomodel.operators.SubtreeSlideOperator;
29 import dr.evomodel.tree.TreeModel;
30 import dr.inference.operators.AdaptableMCMCOperator;
31 import dr.inference.operators.AdaptationMode;
32 import dr.inference.operators.MCMCOperator;
33 import dr.xml.*;
34
35 /**
36 */
37 public class SubtreeSlideOperatorParser extends AbstractXMLObjectParser {
38
39 public static final String SUBTREE_SLIDE = "subtreeSlide";
40 public static final String SWAP_RATES = "swapInRandomRate";
41 public static final String SWAP_TRAITS = "swapInRandomTrait";
42 public static final String DIRICHLET_BRANCHES = "branchesAreScaledDirichlet";
43 public static final String TARGET_ACCEPTANCE = "targetAcceptance";
44
45 public static final String TRAIT = "trait";
46
47 public String getParserName() {
48 return SUBTREE_SLIDE;
49 }
50
51 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
52
53 boolean swapRates = xo.getAttribute(SWAP_RATES, false);
54 boolean swapTraits = xo.getAttribute(SWAP_TRAITS, false);
55 boolean scaledDirichletBranches = xo.getAttribute(DIRICHLET_BRANCHES, false);
56
57 AdaptationMode mode = AdaptationMode.DEFAULT;
58 if (xo.hasAttribute(AdaptableMCMCOperator.AUTO_OPTIMIZE)) {
59 if (xo.getBooleanAttribute(AdaptableMCMCOperator.AUTO_OPTIMIZE)) {
60 mode = AdaptationMode.ADAPTATION_ON;
61 } else {
62 mode = AdaptationMode.ADAPTATION_OFF;
63 }
64 }
65
66 TreeModel treeModel = (TreeModel) xo.getChild(TreeModel.class);
67 final double weight = xo.getDoubleAttribute(MCMCOperator.WEIGHT);
68
69 final double targetAcceptance = xo.getAttribute(TARGET_ACCEPTANCE, 0.234);
70
71 final double size = xo.getAttribute("size", 1.0);
72
73 if (Double.isInfinite(size) || size <= 0.0) {
74 throw new XMLParseException("size attribute must be positive and not infinite. was " + size +
75 " for tree " + treeModel.getId() );
76 }
77
78 final boolean gaussian = xo.getBooleanAttribute("gaussian");
79 SubtreeSlideOperator operator = new SubtreeSlideOperator(treeModel, weight, size, gaussian,
80 swapRates, swapTraits, scaledDirichletBranches, mode, targetAcceptance);
81
82 return operator;
83 }
84
85 public String getParserDescription() {
86 return "An operator that slides a subtree.";
87 }
88
89 public Class getReturnType() {
90 return SubtreeSlideOperator.class;
91 }
92
93 public XMLSyntaxRule[] getSyntaxRules() {
94 return rules;
95 }
96
97 private final XMLSyntaxRule[] rules = {
98 AttributeRule.newDoubleRule(MCMCOperator.WEIGHT),
99 // Make size optional. If not given or equals zero, size is set to half of average tree branch length.
100 AttributeRule.newDoubleRule("size", true),
101 AttributeRule.newDoubleRule(TARGET_ACCEPTANCE, true),
102 AttributeRule.newBooleanRule("gaussian"),
103 AttributeRule.newBooleanRule(SWAP_RATES, true),
104 AttributeRule.newBooleanRule(SWAP_TRAITS, true),
105 AttributeRule.newBooleanRule(AdaptableMCMCOperator.AUTO_OPTIMIZE, true),
106 new ElementRule(TreeModel.class)
107 };
108
109 }