Debian Codegraph @

src/dr/evomodelxml/coalescent/GaussianProcessSkytrackLikelihoodParser.java

raw symbols SCIP
1 /*
2 * GaussianProcessSkytrackLikelihoodParser.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.evolution.tree.Tree;
29 //import dr.evomodel.coalescent.GMRFSkyrideLikelihood;
30 import dr.evomodel.coalescent.GaussianProcessMultilocusSkytrackLikelihood;
31 import dr.evomodel.coalescent.GaussianProcessSkytrackLikelihood;
32 import dr.evomodel.tree.TreeModel;
33 //import dr.inference.model.MatrixParameter;
34 import dr.inference.model.Parameter;
35 import dr.xml.*;
36
37 import java.util.ArrayList;
38 import java.util.List;
39 //import java.util.logging.Logger;
40
41 /**
42 *
43 */
44 public class GaussianProcessSkytrackLikelihoodParser extends AbstractXMLObjectParser {
45
46 // public static final String SKYLINE_LIKELIHOOD = "gmrfSkyrideLikelihood";
47 public static final String SKYTRACK_LIKELIHOOD = "gpSkytrackLikelihood";
48
49 public static final String LAMBDA_BOUND_PARAMETER = "lambdaBoundParameter";
50 public static final String POPULATION_PARAMETER = "populationSizes";
51 public static final String NUMBER_POINTS="pointsParameter";
52 public static final String GPTYPE="GPtype";
53 public static final String GPCOUNTS="GPcounts";
54 public static final String COALFACTOR="coalfactor";
55 public static final String CHANGE_POINTS="changePoints";
56 public static final String COALCOUNT="CoalCounts";
57 public static final String TMRCA="timeMRCA";
58 // public static final String GROUP_SIZES = "groupSizes";
59 public static final String PRECISION_PARAMETER = "precisionParameter";
60 public static final String POPULATION_TREE = "populationTree";
61 public static final String LAMBDA_PARAMETER = "lambdaParameter";
62 public static final String BETA_PARAMETER = "betaParameter";
63 public static final String ALPHA_PARAMETER = "alphaParameter";
64 // public static final String COVARIATE_MATRIX = "covariateMatrix";
65 public static final String RANDOMIZE_TREE = "randomizeTree";
66 // public static final String TIME_AWARE_SMOOTHING = "timeAwareSmoothing";
67
68 public static final String RESCALE_BY_ROOT_ISSUE = "rescaleByRootHeight";
69 // public static final String GRID_POINTS = "gridPoints";
70 // public static final String OLD_SKYRIDE = "oldSkyride"; //True=No multiple loci
71 // public static final String NUM_GRID_POINTS = "numGridPoints";
72 // public static final String CUT_OFF = "cutOff";
73 // public static final String PHI_PARAMETER = "phiParameter";
74
75 // public static final String LATENT_PARAMETER = "latentPointParameter";
76
77
78 public String getParserName() {
79 return SKYTRACK_LIKELIHOOD;
80 }
81
82
83
84 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
85
86
87 XMLObject cxo = xo.getChild(POPULATION_PARAMETER);
88 Parameter popParameter = (Parameter) cxo.getChild(Parameter.class);
89
90
91
92
93 cxo = xo.getChild(PRECISION_PARAMETER);
94 Parameter precParameter = (Parameter) cxo.getChild(Parameter.class);
95
96 // cxo = xo.getChild(LAMBDA_BOUND_PARAMETER);
97 // Parameter lambda_bound = (Parameter) cxo.getChild(Parameter.class);
98 //
99 // cxo = xo.getChild(LAMBDA_PARAMETER);
100 // Parameter lambda_parameter = (Parameter) cxo.getChild(Parameter.class);
101
102
103 cxo = xo.getChild(POPULATION_TREE);
104
105 List<Tree> treeList = new ArrayList<Tree>();
106 for (int i = 0; i < cxo.getChildCount(); i++) {
107 Object testObject = cxo.getChild(i);
108 if (testObject instanceof Tree) {
109 treeList.add((TreeModel) testObject);
110 }
111 }
112
113 // TreeModel treeModel = (TreeModel) cxo.getChild(TreeModel.class);
114
115 // cxo = xo.getChild(GROUP_SIZES);
116 // Parameter groupParameter = null;
117 // if (cxo != null) {
118 // groupParameter = (Parameter) cxo.getChild(Parameter.class);
119 //
120 // if (popParameter.getDimension() != groupParameter.getDimension())
121 // throw new XMLParseException("Population and group size parameters must have the same length");
122 // }
123
124 Parameter lambda_parameter;
125 if (xo.getChild(LAMBDA_PARAMETER) != null) {
126 cxo = xo.getChild(LAMBDA_PARAMETER);
127 lambda_parameter = (Parameter) cxo.getChild(Parameter.class);
128 } else {
129 lambda_parameter = new Parameter.Default(1.0);
130
131 }
132
133 Parameter GPtype;
134 if (xo.getChild(GPTYPE) != null) {
135 cxo = xo.getChild(GPTYPE);
136 GPtype = (Parameter) cxo.getChild(Parameter.class);
137 } else {
138 GPtype = new Parameter.Default(1.0);
139
140 }
141
142 Parameter Tmrca;
143 if (xo.getChild(TMRCA) != null) {
144 cxo = xo.getChild(TMRCA);
145 Tmrca = (Parameter) cxo.getChild(Parameter.class);
146 } else {
147 Tmrca = new Parameter.Default(1.0);
148
149 }
150
151
152 Parameter numPoints;
153 if (xo.getChild(NUMBER_POINTS) != null) {
154 cxo = xo.getChild(NUMBER_POINTS);
155 numPoints = (Parameter) cxo.getChild(Parameter.class);
156 } else {
157 numPoints = new Parameter.Default(1.0);
158
159 }
160
161 Parameter CoalCounts;
162 if (xo.getChild(COALCOUNT) != null) {
163 cxo = xo.getChild(COALCOUNT);
164 CoalCounts = (Parameter) cxo.getChild(Parameter.class);
165 } else {
166 CoalCounts = new Parameter.Default(1.0);
167
168 }
169
170 Parameter GPcounts;
171 if (xo.getChild(GPCOUNTS) != null) {
172 cxo = xo.getChild(GPCOUNTS);
173 GPcounts = (Parameter) cxo.getChild(Parameter.class);
174 } else {
175 GPcounts = new Parameter.Default(1.0);
176
177 }
178
179
180 Parameter coalfactor;
181 if (xo.getChild(COALFACTOR) != null) {
182 cxo = xo.getChild(COALFACTOR);
183 coalfactor = (Parameter) cxo.getChild(Parameter.class);
184 } else {
185 coalfactor = new Parameter.Default(1.0);
186
187 }
188
189 Parameter lambda_bound;
190 if (xo.getChild(LAMBDA_BOUND_PARAMETER) != null) {
191 cxo = xo.getChild(LAMBDA_BOUND_PARAMETER);
192 lambda_bound = (Parameter) cxo.getChild(Parameter.class);
193 } else {
194 lambda_bound = new Parameter.Default(1.0);
195 }
196
197 Parameter alpha_parameter;
198 if (xo.getChild(ALPHA_PARAMETER) != null) {
199 cxo = xo.getChild(ALPHA_PARAMETER);
200 alpha_parameter = (Parameter) cxo.getChild(Parameter.class);
201 } else {
202 alpha_parameter = new Parameter.Default(0.001);
203 }
204
205 Parameter beta_parameter;
206 if (xo.getChild(BETA_PARAMETER) != null) {
207 cxo = xo.getChild(BETA_PARAMETER);
208 beta_parameter = (Parameter) cxo.getChild(Parameter.class);
209 } else {
210 beta_parameter = new Parameter.Default(0.001);
211 }
212
213 Parameter change_points;
214 if (xo.getChild(CHANGE_POINTS) != null) {
215 cxo = xo.getChild(CHANGE_POINTS);
216 change_points = (Parameter) cxo.getChild(Parameter.class);
217 } else {
218 change_points = new Parameter.Default(0,1);
219 }
220
221 /*
222
223
224 Parameter gridPoints = null;
225 if (xo.getChild(GRID_POINTS) != null) {
226 cxo = xo.getChild(GRID_POINTS);
227 gridPoints = (Parameter) cxo.getChild(Parameter.class);
228 }
229 */
230
231
232 // Parameter numGridPoints = new Parameter.Default(0,1);
233 // if (xo.getChild(NUM_GRID_POINTS) != null) {
234 // cxo = xo.getChild(NUM_GRID_POINTS);
235 // numGridPoints = (Parameter) cxo.getChild(Parameter.class);
236 // }
237
238 // Parameter cutOff = null;
239 // if (xo.getChild(CUT_OFF) != null) {
240 // cxo = xo.getChild(CUT_OFF);
241 // cutOff = (Parameter) cxo.getChild(Parameter.class);
242 // }
243 //
244 // Parameter phi = null;
245 // if (xo.getChild(PHI_PARAMETER) != null) {
246 // cxo = xo.getChild(PHI_PARAMETER);
247 // phi = (Parameter) cxo.getChild(Parameter.class);
248 // }
249 //
250 //
251 // Parameter beta = null;
252 // if (xo.getChild(BETA_PARAMETER) != null) {
253 // cxo = xo.getChild(BETA_PARAMETER);
254 // beta = (Parameter) cxo.getChild(Parameter.class);
255 // }
256
257 // MatrixParameter dMatrix = null;
258 // if (xo.getChild(COVARIATE_MATRIX) != null) {
259 // cxo = xo.getChild(COVARIATE_MATRIX);
260 // dMatrix = (MatrixParameter) cxo.getChild(MatrixParameter.class);
261 // }
262
263 // boolean timeAwareSmoothing = GMRFSkyrideLikelihood.TIME_AWARE_IS_ON_BY_DEFAULT;
264 // if (xo.hasAttribute(TIME_AWARE_SMOOTHING)) {
265 // timeAwareSmoothing = xo.getBooleanAttribute(TIME_AWARE_SMOOTHING);
266 // }
267
268 // if ((dMatrix != null && beta == null) || (dMatrix == null && beta != null))
269 // throw new XMLParseException("Must specify both a set of regression coefficients and a design matrix.");
270 //
271 // if (dMatrix != null) {
272 // if (dMatrix.getRowDimension() != popParameter.getDimension())
273 // throw new XMLParseException("Design matrix row dimension must equal the population parameter length.");
274 // if (dMatrix.getColumnDimension() != beta.getDimension())
275 // throw new XMLParseException("Design matrix column dimension must equal the regression coefficient length.");
276 // }
277
278 if (xo.getAttribute(RANDOMIZE_TREE, false)) {
279 for (Tree tree : treeList) {
280 if (tree instanceof TreeModel) {
281 GaussianProcessSkytrackLikelihood.checkTree((TreeModel) tree);
282 } else {
283 throw new XMLParseException("Can not randomize a fixed tree");
284 }
285 }
286 }
287
288
289 // XMLObject latentChild = xo.getChild(LATENT_PARAMETER);
290 // Parameter latentPoints = (Parameter) latentChild.getChild(Parameter.class);
291
292
293 boolean rescaleByRootHeight = xo.getAttribute(RESCALE_BY_ROOT_ISSUE, true);
294
295 // Logger.getLogger("dr.evomodel").info("The " + SKYTRACK_LIKELIHOOD + " has " +
296 // (timeAwareSmoothing ? "time aware smoothing" : "uniform smoothing"));
297
298 if (treeList.size()==1) {
299 return new GaussianProcessSkytrackLikelihood(treeList, precParameter,
300 rescaleByRootHeight, lambda_bound, lambda_parameter, popParameter,alpha_parameter,beta_parameter, change_points,
301 GPtype, GPcounts, coalfactor, CoalCounts, numPoints,Tmrca);
302 } else {
303 return new GaussianProcessMultilocusSkytrackLikelihood(treeList, precParameter, rescaleByRootHeight, lambda_bound,
304 lambda_parameter, popParameter, alpha_parameter, beta_parameter, change_points, GPtype,GPcounts,coalfactor, CoalCounts, numPoints,Tmrca);
305 }
306 }
307
308 //************************************************************************
309 // AbstractXMLObjectParser implementation
310 //************************************************************************
311
312 public String getParserDescription() {
313 return "This element represents the likelihood of the tree given the population size vector.";
314 }
315
316 public Class getReturnType() {
317 return GaussianProcessSkytrackLikelihood.class;
318 }
319
320 public XMLSyntaxRule[] getSyntaxRules() {
321 return rules;
322 }
323
324 private final XMLSyntaxRule[] rules = {
325 new ElementRule(POPULATION_PARAMETER, new XMLSyntaxRule[]{
326 new ElementRule(Parameter.class)
327 }),
328 new ElementRule(CHANGE_POINTS, new XMLSyntaxRule[]{
329 new ElementRule(Parameter.class)
330 }),
331 new ElementRule(PRECISION_PARAMETER, new XMLSyntaxRule[]{
332 new ElementRule(Parameter.class)
333 }),
334
335 new ElementRule(LAMBDA_BOUND_PARAMETER, new XMLSyntaxRule[]{
336 new ElementRule(Parameter.class)
337 }),
338 new ElementRule(LAMBDA_PARAMETER, new XMLSyntaxRule[]{
339 new ElementRule(Parameter.class)
340 }),
341 new ElementRule(ALPHA_PARAMETER, new XMLSyntaxRule[]{
342 new ElementRule(Parameter.class)
343 }),
344 new ElementRule(BETA_PARAMETER, new XMLSyntaxRule[]{
345 new ElementRule(Parameter.class)
346 }),
347 // new ElementRule(PHI_PARAMETER, new XMLSyntaxRule[]{
348 // new ElementRule(Parameter.class)
349 // }, true), // Optional
350 new ElementRule(POPULATION_TREE, new XMLSyntaxRule[]{
351 new ElementRule(TreeModel.class, 1, Integer.MAX_VALUE)
352 }),
353 // new ElementRule(GROUP_SIZES, new XMLSyntaxRule[]{
354 // new ElementRule(Parameter.class)
355 // }, true),
356 // new ElementRule(LATENT_PARAMETER,Parameter.class),
357
358 AttributeRule.newBooleanRule(RESCALE_BY_ROOT_ISSUE, true),
359 AttributeRule.newBooleanRule(RANDOMIZE_TREE, true),
360 // AttributeRule.newBooleanRule(TIME_AWARE_SMOOTHING, true),
361 // AttributeRule.newBooleanRule(OLD_SKYRIDE, true)
362 };
363
364 }