Debian Codegraph @

src/dr/evomodelxml/coalescent/VariableDemographicModelParser.java

raw symbols SCIP
1 /*
2 * VariableDemographicModelParser.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.coalescent;
27
28 import dr.evomodel.coalescent.VariableDemographicModel;
29 import dr.evomodel.tree.TreeModel;
30 import dr.evomodelxml.speciation.SpeciesBindingsParser;
31 import dr.inference.model.Parameter;
32 import dr.xml.*;
33
34 import java.util.logging.Logger;
35
36 /**
37 */
38 public class VariableDemographicModelParser extends AbstractXMLObjectParser {
39 public static final String MODEL_NAME = "variableDemographic";
40 public static final String POPULATION_SIZES = "populationSizes";
41 public static final String INDICATOR_PARAMETER = "indicators";
42 public static final String POPULATION_TREES = "trees";
43 private static final String PLOIDY = SpeciesBindingsParser.PLOIDY;
44 public static final String POP_TREE = "ptree";
45
46 public static final String LOG_SPACE = "logUnits";
47 public static final String USE_MIDPOINTS = "useMidpoints";
48
49 public static final String TYPE = "type";
50 //public static final String STEPWISE = "stepwise";
51 //public static final String LINEAR = "linear";
52 //public static final String EXPONENTIAL = "exponential";
53
54 public static final String demoElementName = "demographic";
55
56 public String getParserName() {
57 return MODEL_NAME;
58 }
59
60 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
61
62 XMLObject cxo = xo.getChild(POPULATION_SIZES);
63 Parameter popParam = (Parameter) cxo.getChild(Parameter.class);
64
65 cxo = xo.getChild(INDICATOR_PARAMETER);
66 Parameter indicatorParam = (Parameter) cxo.getChild(Parameter.class);
67
68 cxo = xo.getChild(POPULATION_TREES);
69
70 final int nc = cxo.getChildCount();
71 TreeModel[] treeModels = new TreeModel[nc];
72 double[] populationFactor = new double[nc];
73
74 for (int k = 0; k < treeModels.length; ++k) {
75 final XMLObject child = (XMLObject) cxo.getChild(k);
76 populationFactor[k] = child.hasAttribute(PLOIDY) ? child.getDoubleAttribute(PLOIDY) : 1.0;
77
78 treeModels[k] = (TreeModel) child.getChild(TreeModel.class);
79 }
80
81 VariableDemographicModel.Type type = VariableDemographicModel.Type.STEPWISE;
82
83 if (xo.hasAttribute(TYPE)) {
84 final String s = xo.getStringAttribute(TYPE);
85 if (s.equalsIgnoreCase(VariableDemographicModel.Type.STEPWISE.toString())) {
86 type = VariableDemographicModel.Type.STEPWISE;
87 } else if (s.equalsIgnoreCase(VariableDemographicModel.Type.LINEAR.toString())) {
88 type = VariableDemographicModel.Type.LINEAR;
89 } else if (s.equalsIgnoreCase(VariableDemographicModel.Type.EXPONENTIAL.toString())) {
90 type = VariableDemographicModel.Type.EXPONENTIAL;
91 } else {
92 throw new XMLParseException("Unknown Bayesian Skyline type: " + s);
93 }
94 }
95
96 final boolean logSpace = xo.getAttribute(LOG_SPACE, false) || type == VariableDemographicModel.Type.EXPONENTIAL;
97 final boolean useMid = xo.getAttribute(USE_MIDPOINTS, false);
98
99 Logger.getLogger("dr.evomodel").info("Variable demographic: " + type.toString() + " control points");
100
101 return new VariableDemographicModel(treeModels, populationFactor, popParam, indicatorParam, type,
102 logSpace, useMid);
103
104 }
105
106 //************************************************************************
107 // AbstractXMLObjectParser implementation
108 //************************************************************************
109
110 public String getParserDescription() {
111 return "This element represents the likelihood of the tree given the population size vector.";
112 }
113
114 public Class getReturnType() {
115 return VariableDemographicModel.class;
116 }
117
118 public XMLSyntaxRule[] getSyntaxRules() {
119 return rules;
120 }
121
122 private final XMLSyntaxRule[] rules = {
123 AttributeRule.newStringRule(TYPE, true),
124 AttributeRule.newBooleanRule(LOG_SPACE, true),
125 AttributeRule.newBooleanRule(USE_MIDPOINTS, true),
126
127 new ElementRule(POPULATION_SIZES, new XMLSyntaxRule[]{
128 new ElementRule(Parameter.class)
129 }),
130 new ElementRule(INDICATOR_PARAMETER, new XMLSyntaxRule[]{
131 new ElementRule(Parameter.class)
132 }),
133 new ElementRule(POPULATION_TREES, new XMLSyntaxRule[]{
134 new ElementRule(POP_TREE, new XMLSyntaxRule[]{
135 AttributeRule.newDoubleRule(PLOIDY, true),
136 new ElementRule(TreeModel.class),
137 }, 1, Integer.MAX_VALUE)
138 })
139 };
140 }