| 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.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 |
|
| 51 |
|
| 52 |
|
| 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 |
|
| 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 |
} |