Debian Codegraph @

src/dr/evomodelxml/branchratemodel/MixtureModelBranchRatesParser.java

raw symbols SCIP
1 /*
2 * MixtureModelBranchRatesParser.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.MixtureModelBranchRates;
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 import java.util.ArrayList;
36
37 /**
38 * @author Wai Lok Sibon Li
39 */
40 public class MixtureModelBranchRatesParser extends AbstractXMLObjectParser {
41
42 public static final String MIXTURE_MODEL_BRANCH_RATES = "mixtureModelBranchRates";
43 public static final String DISTRIBUTION = "distribution";
44 public static final String RATE_CATEGORY_QUANTILES = "rateCategoryQuantiles";
45 public static final String SINGLE_ROOT_RATE = "singleRootRate";
46 public static final String NORMALIZE = "normalize";
47 public static final String NORMALIZE_BRANCH_RATE_TO = "normalizeBranchRateTo";
48 public static final String DISTRIBUTION_INDEX = "distributionIndex";
49
50 public static final String USE_QUANTILE = "useQuantilesForRates";
51 //public static final String NORMALIZED_MEAN = "normalizedMean";
52
53
54 public String getParserName() {
55 return MIXTURE_MODEL_BRANCH_RATES;
56 }
57
58 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
59
60 ArrayList<ParametricDistributionModel> modelsList = new ArrayList<ParametricDistributionModel>();
61
62 final boolean normalize = xo.getAttribute(NORMALIZE, false);
63 final double normalizeBranchRateTo = xo.getAttribute(NORMALIZE_BRANCH_RATE_TO, Double.NaN);
64
65 final boolean useQuantilesForRates = xo.getAttribute(USE_QUANTILE, true);
66
67 TreeModel tree = (TreeModel) xo.getChild(TreeModel.class);
68
69 //while (xo.hasChildNamed(DISTRIBUTION)) {
70
71 for (int i = 0; i < xo.getChildCount(); i++) {
72 Object child = xo.getChild(i);
73 if( child instanceof XMLObject ) {
74 if( ((XMLObject) child).getName().equals(DISTRIBUTION) ) {
75 XMLObject childXML = (XMLObject) child;
76 modelsList.add((ParametricDistributionModel) childXML.getChild(0));
77 }
78 }
79 }
80
81 //Parameter rateCategoryParameter = (Parameter) xo.getElementFirstChild(RATE_CATEGORIES);
82
83 ParametricDistributionModel[] models = modelsList.toArray(new ParametricDistributionModel[modelsList.size()]);
84
85 Parameter rateCategoryQuantilesParameter = (Parameter) xo.getElementFirstChild(RATE_CATEGORY_QUANTILES);
86
87 Parameter distributionIndexParameter = (Parameter) xo.getElementFirstChild(DISTRIBUTION_INDEX);
88
89 Logger.getLogger("dr.evomodel").info("Using random discretized relaxed clock model with a mixture distribution.");
90 for(int i=0; i<models.length; i++) {
91 Logger.getLogger("dr.evomodel").info(" parametric model " + (i+1) +" = " + models[i].getModelName());
92 }
93 //Logger.getLogger("dr.evomodel").info(" rate categories = " + rateCategoryParameter.getDimension());
94 Logger.getLogger("dr.evomodel").info(" rate categories = " + rateCategoryQuantilesParameter.getDimension());
95 if(normalize) {
96 Logger.getLogger("dr.evomodel").info(" mean rate is normalized to " + normalizeBranchRateTo);
97 }
98
99 if (xo.hasAttribute(SINGLE_ROOT_RATE)) {
100 //singleRootRate = xo.getBooleanAttribute(SINGLE_ROOT_RATE);
101 Logger.getLogger("dr.evomodel").warning(" WARNING: single root rate is not implemented!");
102 }
103
104
105 if(!useQuantilesForRates) {
106 Logger.getLogger("dr.evomodel").info("Rates are set to not being drawn using quantiles. Thus they are not drawn from any particular distribution.");
107 }
108 /* if (xo.hasAttribute(NORMALIZED_MEAN)) {
109 dbr.setNormalizedMean(xo.getDoubleAttribute(NORMALIZED_MEAN));
110 }*/
111
112 return new MixtureModelBranchRates(tree, rateCategoryQuantilesParameter, models, distributionIndexParameter, useQuantilesForRates, normalize, normalizeBranchRateTo);
113 }
114
115 //************************************************************************
116 // AbstractXMLObjectParser implementation
117 //************************************************************************
118
119 public String getParserDescription() {
120 return
121 "This element returns a random discretized relaxed clock model." +
122 "The branch rates are drawn from a continuous parametric distribution.";
123 }
124
125 public Class getReturnType() {
126 return MixtureModelBranchRates.class;
127 }
128
129 public XMLSyntaxRule[] getSyntaxRules() {
130 return rules;
131 }
132
133 private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{
134 AttributeRule.newBooleanRule(SINGLE_ROOT_RATE, true, "Whether only a single rate should be used for the two children branches of the root"),
135 //AttributeRule.newDoubleRule(NORMALIZED_MEAN, true, "The mean rate to constrain branch rates to once branch lengths are taken into account"),
136 //AttributeRule.newIntegerRule(OVERSAMPLING, true, "The integer factor for oversampling the distribution model (1 means no oversampling)"),
137 AttributeRule.newBooleanRule(NORMALIZE, true, "Whether the mean rate has to be normalized to a particular value"),
138 AttributeRule.newDoubleRule(NORMALIZE_BRANCH_RATE_TO, true, "The mean rate to normalize to, if normalizing"),
139 AttributeRule.newBooleanRule(USE_QUANTILE, true, "Whether or not to use quantiles to represent rates. If false then rates are not drawn " +
140 "specifically from any of the distributions"),
141 new ElementRule(TreeModel.class),
142 //new ElementRule(DISTRIBUTION, ParametricDistributionModel.class, "The distribution model for rates among branches", false),
143 /* Can have an infinite number of rate distribution models */
144 new ElementRule(DISTRIBUTION, ParametricDistributionModel.class, "The distribution model for rates among branches", 1, Integer.MAX_VALUE),
145 new ElementRule(DISTRIBUTION_INDEX, Parameter.class, "Operator that switches between the distributions of the branch rate distribution model", false),
146 /*new ElementRule(RATE_CATEGORIES, Parameter.class, "The rate categories parameter", false), */
147 new ElementRule(RATE_CATEGORY_QUANTILES, Parameter.class, "The quantiles for", false),
148 };
149 }