| 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.OrnsteinUhlenbeckPriorLikelihood; |
| 29 |
import dr.evomodel.coalescent.VariableDemographicModel; |
| 30 |
import dr.inference.distribution.ParametricDistributionModel; |
| 31 |
import dr.inference.model.Parameter; |
| 32 |
import dr.inference.model.Statistic; |
| 33 |
import dr.xml.*; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public class OrnsteinUhlenbeckPriorLikelihoodParser extends AbstractXMLObjectParser { |
| 38 |
|
| 39 |
public static final String OU = "Ornstein-Uhlenbeck"; |
| 40 |
public static final String LOG_SPACE = "logUnits"; |
| 41 |
public static final String NORMALIZE = "normalize"; |
| 42 |
|
| 43 |
public static final String DATA = "data"; |
| 44 |
public static final String TIMES = "times"; |
| 45 |
|
| 46 |
public static final String MEAN = "mean"; |
| 47 |
public static final String SIGMA = "sigma"; |
| 48 |
public static final String LAMBDA = "lambda"; |
| 49 |
|
| 50 |
public String getParserDescription() { |
| 51 |
return ""; |
| 52 |
} |
| 53 |
|
| 54 |
public Class getReturnType() { |
| 55 |
return OrnsteinUhlenbeckPriorLikelihood.class; |
| 56 |
} |
| 57 |
|
| 58 |
public String getParserName() { |
| 59 |
return OU; |
| 60 |
} |
| 61 |
|
| 62 |
private Parameter getParam(XMLObject xo, String name) throws XMLParseException { |
| 63 |
final XMLObject object = xo.getChild(name); |
| 64 |
|
| 65 |
if (object == null) { |
| 66 |
return null; |
| 67 |
} |
| 68 |
final Object child = object.getChild(0); |
| 69 |
if (child instanceof Parameter) { |
| 70 |
return (Parameter) child; |
| 71 |
} |
| 72 |
|
| 73 |
double x = object.getDoubleChild(0); |
| 74 |
return new Parameter.Default(x); |
| 75 |
} |
| 76 |
|
| 77 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 78 |
|
| 79 |
Parameter mean = getParam(xo, MEAN); |
| 80 |
Parameter sigma = getParam(xo, SIGMA); |
| 81 |
Parameter lambda = getParam(xo, LAMBDA); |
| 82 |
|
| 83 |
final boolean logSpace = xo.getAttribute(LOG_SPACE, false); |
| 84 |
final boolean normalize = xo.getAttribute(NORMALIZE, false); |
| 85 |
|
| 86 |
VariableDemographicModel m = (VariableDemographicModel) xo.getChild(VariableDemographicModel.class); |
| 87 |
|
| 88 |
if (m != null) { |
| 89 |
ParametricDistributionModel popMeanPrior = (ParametricDistributionModel) xo.getChild(ParametricDistributionModel.class); |
| 90 |
return new OrnsteinUhlenbeckPriorLikelihood(mean, sigma, lambda, m, logSpace, normalize, popMeanPrior); |
| 91 |
} |
| 92 |
|
| 93 |
final XMLObject cxo1 = xo.getChild(DATA); |
| 94 |
Parameter dataParameter = (Parameter) cxo1.getChild(Parameter.class); |
| 95 |
final XMLObject cxo2 = xo.getChild(TIMES); |
| 96 |
final Parameter timesParameter = (Parameter) cxo2.getChild(Parameter.class); |
| 97 |
|
| 98 |
return new OrnsteinUhlenbeckPriorLikelihood(mean, sigma, lambda, dataParameter, timesParameter, logSpace, normalize); |
| 99 |
} |
| 100 |
|
| 101 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 102 |
|
| 103 |
return new XMLSyntaxRule[]{ |
| 104 |
AttributeRule.newBooleanRule(LOG_SPACE, true), |
| 105 |
AttributeRule.newBooleanRule(NORMALIZE, true), |
| 106 |
|
| 107 |
|
| 108 |
new XORRule( |
| 109 |
new ElementRule(MEAN, Double.class), |
| 110 |
new ElementRule(MEAN, Parameter.class), |
| 111 |
true |
| 112 |
), |
| 113 |
new XORRule( |
| 114 |
new ElementRule(SIGMA, Double.class), |
| 115 |
new ElementRule(SIGMA, Parameter.class) |
| 116 |
), |
| 117 |
|
| 118 |
new XORRule( |
| 119 |
new ElementRule(LAMBDA, Double.class), |
| 120 |
new ElementRule(LAMBDA, Parameter.class), |
| 121 |
true |
| 122 |
), |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
new ElementRule(VariableDemographicModel.class, true), |
| 127 |
|
| 128 |
new ElementRule(DATA, new XMLSyntaxRule[]{new ElementRule(Statistic.class)}, true), |
| 129 |
new ElementRule(TIMES, new XMLSyntaxRule[]{new ElementRule(Statistic.class)}, true) |
| 130 |
|
| 131 |
}; |
| 132 |
} |
| 133 |
} |