Debian Codegraph @

src/dr/evomodelxml/coalescent/PiecewisePopulationModelParser.java

raw symbols SCIP
1 /*
2 * PiecewisePopulationModelParser.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.evolution.util.Units;
29 import dr.evomodel.coalescent.PiecewisePopulationModel;
30 import dr.evoxml.util.XMLUnits;
31 import dr.inference.model.Parameter;
32 import dr.xml.*;
33
34 /**
35 * Parses an element from an DOM document into a PiecewisePopulation.
36 */
37 public class PiecewisePopulationModelParser extends AbstractXMLObjectParser {
38
39 public static final String PIECEWISE_POPULATION = "piecewisePopulation";
40 public static final String EPOCH_SIZES = "epochSizes";
41 public static final String POPULATION_SIZE = "populationSize";
42 public static final String GROWTH_RATES = "growthRates";
43 public static final String EPOCH_WIDTHS = "epochWidths";
44
45 public static final String WIDTHS = "widths";
46 public static final String LINEAR = "linear";
47
48 public String getParserName() {
49 return PIECEWISE_POPULATION;
50 }
51
52 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
53
54 Units.Type units = XMLUnits.Utils.getUnitsAttr(xo);
55
56 XMLObject obj = xo.getChild(EPOCH_WIDTHS);
57 double[] epochWidths = obj.getDoubleArrayAttribute(WIDTHS);
58
59 if (xo.hasChildNamed(EPOCH_SIZES)) {
60 Parameter epochSizes = (Parameter) xo.getElementFirstChild(EPOCH_SIZES);
61
62 boolean isLinear = false;
63 if (xo.hasAttribute(LINEAR)) {
64 isLinear = xo.getBooleanAttribute(LINEAR);
65 }
66
67 return new PiecewisePopulationModel(PIECEWISE_POPULATION, epochSizes, epochWidths, isLinear, units);
68 } else {
69 Parameter populationSize = (Parameter) xo.getElementFirstChild(POPULATION_SIZE);
70 Parameter growthRates = (Parameter) xo.getElementFirstChild(GROWTH_RATES);
71 return new PiecewisePopulationModel(PIECEWISE_POPULATION, populationSize, growthRates, epochWidths, units);
72 }
73 }
74
75
76 //************************************************************************
77 // AbstractXMLObjectParser implementation
78 //************************************************************************
79
80 public String getParserDescription() {
81 return "This element represents a piecewise population model";
82 }
83
84 public Class getReturnType() {
85 return PiecewisePopulationModel.class;
86 }
87
88 public XMLSyntaxRule[] getSyntaxRules() {
89 return rules;
90 }
91
92 private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{
93 new XORRule(
94 new ElementRule(EPOCH_SIZES,
95 new XMLSyntaxRule[]{new ElementRule(Parameter.class)}),
96 new AndRule(
97 new ElementRule(POPULATION_SIZE,
98 new XMLSyntaxRule[]{new ElementRule(Parameter.class)}),
99 new ElementRule(GROWTH_RATES,
100 new XMLSyntaxRule[]{new ElementRule(Parameter.class)})
101 )
102 ),
103 new ElementRule(EPOCH_WIDTHS,
104 new XMLSyntaxRule[]{AttributeRule.newDoubleArrayRule(WIDTHS)}),
105 AttributeRule.newBooleanRule(LINEAR, true)
106 };
107 }