Debian Codegraph @

src/dr/evomodelxml/coalescent/BayesianSkylineLikelihoodParser.java

raw symbols SCIP
1 /*
2 * BayesianSkylineLikelihoodParser.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.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 // AbstractXMLObjectParser implementation
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 }