| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 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 |
|
| 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 |
|
| 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 |
} |