| 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.BayesianSkylineLikelihood; |
| 29 |
import dr.evomodel.tree.TreeModel; |
| 30 |
import dr.inference.model.Parameter; |
| 31 |
import dr.xml.*; |
| 32 |
|
| 33 |
import java.util.logging.Logger; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public class BayesianSkylineLikelihoodParser extends AbstractXMLObjectParser { |
| 38 |
|
| 39 |
public static final String SKYLINE_LIKELIHOOD = "generalizedSkyLineLikelihood"; |
| 40 |
public static final String POPULATION_SIZES = "populationSizes"; |
| 41 |
public static final String GROUP_SIZES = "groupSizes"; |
| 42 |
|
| 43 |
public static final String TYPE = "type"; |
| 44 |
public static final String STEPWISE = "stepwise"; |
| 45 |
public static final String LINEAR = "linear"; |
| 46 |
public static final String EXPONENTIAL = "exponential"; |
| 47 |
|
| 48 |
public String getParserName() { |
| 49 |
return SKYLINE_LIKELIHOOD; |
| 50 |
} |
| 51 |
|
| 52 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 53 |
|
| 54 |
XMLObject cxo = xo.getChild(POPULATION_SIZES); |
| 55 |
Parameter param = (Parameter) cxo.getChild(Parameter.class); |
| 56 |
|
| 57 |
cxo = xo.getChild(GROUP_SIZES); |
| 58 |
Parameter param2 = (Parameter) cxo.getChild(Parameter.class); |
| 59 |
|
| 60 |
cxo = xo.getChild(CoalescentLikelihoodParser.POPULATION_TREE); |
| 61 |
TreeModel treeModel = (TreeModel) cxo.getChild(TreeModel.class); |
| 62 |
|
| 63 |
int type = BayesianSkylineLikelihood.LINEAR_TYPE; |
| 64 |
String typeName = LINEAR; |
| 65 |
if (xo.hasAttribute(LINEAR) && !xo.getBooleanAttribute(LINEAR)) { |
| 66 |
type = BayesianSkylineLikelihood.STEPWISE_TYPE; |
| 67 |
typeName = STEPWISE; |
| 68 |
} |
| 69 |
|
| 70 |
if (xo.hasAttribute(TYPE)) { |
| 71 |
if (xo.getStringAttribute(TYPE).equalsIgnoreCase(STEPWISE)) { |
| 72 |
type = BayesianSkylineLikelihood.STEPWISE_TYPE; |
| 73 |
typeName = STEPWISE; |
| 74 |
} else if (xo.getStringAttribute(TYPE).equalsIgnoreCase(LINEAR)) { |
| 75 |
type = BayesianSkylineLikelihood.LINEAR_TYPE; |
| 76 |
typeName = LINEAR; |
| 77 |
} else if (xo.getStringAttribute(TYPE).equalsIgnoreCase(EXPONENTIAL)) { |
| 78 |
type = BayesianSkylineLikelihood.EXPONENTIAL_TYPE; |
| 79 |
typeName = EXPONENTIAL; |
| 80 |
} else throw new XMLParseException("Unknown Bayesian Skyline type: " + xo.getStringAttribute(TYPE)); |
| 81 |
} |
| 82 |
|
| 83 |
if (param2.getDimension() > (treeModel.getExternalNodeCount()-1)) { |
| 84 |
throw new XMLParseException("There are more groups (" + param2.getDimension() |
| 85 |
+ ") than coalescent nodes in the tree (" + (treeModel.getExternalNodeCount()-1) + ")."); |
| 86 |
} |
| 87 |
|
| 88 |
Logger.getLogger("dr.evomodel").info("Bayesian skyline plot: " + param.getDimension() + " " + typeName + " control points"); |
| 89 |
|
| 90 |
return new BayesianSkylineLikelihood(treeModel, param, param2, type); |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
public String getParserDescription() { |
| 98 |
return "This element represents the likelihood of the tree given the population size vector."; |
| 99 |
} |
| 100 |
|
| 101 |
public Class getReturnType() { |
| 102 |
return BayesianSkylineLikelihood.class; |
| 103 |
} |
| 104 |
|
| 105 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 106 |
return rules; |
| 107 |
} |
| 108 |
|
| 109 |
private final XMLSyntaxRule[] rules;{ |
| 110 |
rules = new XMLSyntaxRule[]{ |
| 111 |
new XORRule( |
| 112 |
AttributeRule.newBooleanRule(LINEAR), |
| 113 |
AttributeRule.newStringRule(TYPE) |
| 114 |
), |
| 115 |
new ElementRule(POPULATION_SIZES, new XMLSyntaxRule[]{ |
| 116 |
new ElementRule(Parameter.class) |
| 117 |
}), |
| 118 |
new ElementRule(GROUP_SIZES, new XMLSyntaxRule[]{ |
| 119 |
new ElementRule(Parameter.class) |
| 120 |
}), |
| 121 |
new ElementRule(CoalescentLikelihoodParser.POPULATION_TREE, new XMLSyntaxRule[]{ |
| 122 |
new ElementRule(TreeModel.class) |
| 123 |
}), |
| 124 |
}; |
| 125 |
} |
| 126 |
} |