Debian Codegraph @

src/dr/evomodelxml/branchratemodel/DiscretizedBranchRatesParser.java

raw symbols SCIP
1 /*
2 * DiscretizedBranchRatesParser.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.branchratemodel;
27
28 import dr.evomodel.branchratemodel.DiscretizedBranchRates;
29 import dr.evomodel.tree.TreeModel;
30 import dr.inference.distribution.ParametricDistributionModel;
31 import dr.inference.model.Parameter;
32 import dr.xml.*;
33
34 import java.util.logging.Logger;
35
36 /**
37 * @author Alexei Drummond
38 */
39 public class DiscretizedBranchRatesParser extends AbstractXMLObjectParser {
40
41 public static final String DISCRETIZED_BRANCH_RATES = "discretizedBranchRates";
42 public static final String DISTRIBUTION = "distribution";
43 public static final String RATE_CATEGORIES = "rateCategories";
44 public static final String SINGLE_ROOT_RATE = "singleRootRate";
45 public static final String OVERSAMPLING = "overSampling";
46 public static final String NORMALIZE = "normalize";
47 public static final String NORMALIZE_BRANCH_RATE_TO = "normalizeBranchRateTo";
48 public static final String RANDOMIZE_RATES = "randomizeRates";
49 public static final String KEEP_RATES = "keepRates";
50 public static final String CACHED_RATES = "cachedRates";
51
52 //public static final String NORMALIZED_MEAN = "normalizedMean";
53
54
55 public String getParserName() {
56 return DISCRETIZED_BRANCH_RATES;
57 }
58
59 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
60
61 final int overSampling = xo.getAttribute(OVERSAMPLING, 1);
62
63 //final boolean normalize = xo.getBooleanAttribute(NORMALIZE, false);
64 final boolean normalize = xo.getAttribute(NORMALIZE, false);
65 /*if(xo.hasAttribute(NORMALIZE))
66 normalize = xo.getBooleanAttribute(NORMALIZE);
67 }*/
68 //final double normalizeBranchRateTo = xo.getDoubleAttribute(NORMALIZE_BRANCH_RATE_TO);
69 final double normalizeBranchRateTo = xo.getAttribute(NORMALIZE_BRANCH_RATE_TO, Double.NaN);
70
71 TreeModel tree = (TreeModel) xo.getChild(TreeModel.class);
72 ParametricDistributionModel distributionModel = (ParametricDistributionModel) xo.getElementFirstChild(DISTRIBUTION);
73
74 Parameter rateCategoryParameter = (Parameter) xo.getElementFirstChild(RATE_CATEGORIES);
75
76 Logger.getLogger("dr.evomodel").info("\nUsing discretized relaxed clock model.");
77 Logger.getLogger("dr.evomodel").info(" over sampling = " + overSampling);
78 Logger.getLogger("dr.evomodel").info(" parametric model = " + distributionModel.getModelName());
79 Logger.getLogger("dr.evomodel").info(" rate categories = " + rateCategoryParameter.getDimension());
80 if(normalize) {
81 Logger.getLogger("dr.evomodel").info(" mean rate is normalized to " + normalizeBranchRateTo);
82 }
83
84 if (xo.hasAttribute(SINGLE_ROOT_RATE)) {
85 //singleRootRate = xo.getBooleanAttribute(SINGLE_ROOT_RATE);
86 Logger.getLogger("dr.evomodel").warning(" WARNING: single root rate is not implemented!");
87 }
88
89 final boolean randomizeRates = xo.getAttribute(RANDOMIZE_RATES, true);
90 final boolean keepRates = xo.getAttribute(KEEP_RATES, false);
91
92 final boolean cachedRates = xo.getAttribute(CACHED_RATES, false);
93
94 if (randomizeRates && keepRates) {
95 throw new XMLParseException("Unable to both randomize and keep current rate categories");
96 }
97
98 /* if (xo.hasAttribute(NORMALIZED_MEAN)) {
99 dbr.setNormalizedMean(xo.getDoubleAttribute(NORMALIZED_MEAN));
100 }*/
101
102 return new DiscretizedBranchRates(tree, rateCategoryParameter, distributionModel, overSampling, normalize,
103 normalizeBranchRateTo, randomizeRates, keepRates, cachedRates);
104 }
105
106 //************************************************************************
107 // AbstractXMLObjectParser implementation
108 //************************************************************************
109
110 public String getParserDescription() {
111 return
112 "This element returns an discretized relaxed clock model." +
113 "The branch rates are drawn from a discretized parametric distribution.";
114 }
115
116 public Class getReturnType() {
117 return DiscretizedBranchRates.class;
118 }
119
120 public XMLSyntaxRule[] getSyntaxRules() {
121 return rules;
122 }
123
124 private final XMLSyntaxRule[] rules = {
125 AttributeRule.newBooleanRule(SINGLE_ROOT_RATE, true, "Whether only a single rate should be used for the two children branches of the root"),
126 //AttributeRule.newDoubleRule(NORMALIZED_MEAN, true, "The mean rate to constrain branch rates to once branch lengths are taken into account"),
127 AttributeRule.newIntegerRule(OVERSAMPLING, true, "The integer factor for oversampling the distribution model (1 means no oversampling)"),
128 AttributeRule.newBooleanRule(NORMALIZE, true, "Whether the mean rate has to be normalized to a particular value"),
129 AttributeRule.newDoubleRule(NORMALIZE_BRANCH_RATE_TO, true, "The mean rate to normalize to, if normalizing"),
130 AttributeRule.newBooleanRule(RANDOMIZE_RATES, true, "Randomize initial categories"),
131 AttributeRule.newBooleanRule(KEEP_RATES, true, "Keep current rate category specification"),
132 AttributeRule.newBooleanRule(CACHED_RATES, true, "Cache rates between steps (default off)"),
133 new ElementRule(TreeModel.class),
134 new ElementRule(DISTRIBUTION, ParametricDistributionModel.class, "The distribution model for rates among branches", false),
135 new ElementRule(RATE_CATEGORIES, Parameter.class, "The rate categories parameter", false),
136 };
137 }