Debian Codegraph @

src/dr/evomodelxml/coalescent/OrnsteinUhlenbeckPriorLikelihoodParser.java

raw symbols SCIP
1 /*
2 * OrnsteinUhlenbeckPriorLikelihoodParser.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.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 {// (todo) Parser is still in a bad state
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 // optional
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 //new ElementRule(DATA, new XMLSyntaxRule[]{new ElementRule(Statistic.class)}),
107 //new ElementRule(TIMES, new XMLSyntaxRule[]{new ElementRule(Statistic.class)}),
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 // you can't have a XOR (b AND c), yikes
125 // make all optional and check in parser
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 }