Debian Codegraph @

src/dr/inferencexml/distribution/TwoPieceLocationScaleDistributionModelParser.java

raw symbols SCIP
1 /*
2 * TwoPieceLocationScaleDistributionModelParser.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.inferencexml.distribution;
27
28 import dr.inference.distribution.TwoPieceLocationScaleDistributionModel;
29 import dr.inference.model.Parameter;
30 import dr.math.distributions.Distribution;
31 import dr.xml.*;
32
33 /**
34 * Reads a normal distribution model from a DOM Document element.
35 */
36 public class TwoPieceLocationScaleDistributionModelParser extends AbstractXMLObjectParser {
37
38 public static final String DISTRIBUTION_MODEL = "twoPieceLocationScaleDistributionModel";
39 public static final String LOCATION = "location";
40 public static final String SIGMA = "sigma";
41 public static final String GAMMA = "gamma";
42 public static final String PARAMETERIZATION = "parameterization";
43
44 public String getParserName() {
45 return DISTRIBUTION_MODEL;
46 }
47
48 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
49
50 Parameter locationParam;
51 Parameter sigmaParam;
52 Parameter gammaParam;
53
54 XMLObject cxo = xo.getChild(LOCATION);
55 if (cxo.getChild(0) instanceof Parameter) {
56 locationParam = (Parameter) cxo.getChild(Parameter.class);
57 } else {
58 locationParam = new Parameter.Default(cxo.getDoubleChild(0));
59 }
60
61 String parameterizationLabel = (String) xo.getAttribute(PARAMETERIZATION);
62 TwoPieceLocationScaleDistributionModel.Parameterization parameterization =
63 TwoPieceLocationScaleDistributionModel.Parameterization.parseFromString(parameterizationLabel);
64 if (parameterization == null) {
65 throw new XMLParseException("Unrecognized parameterization '" + parameterizationLabel + "'");
66 }
67
68 cxo = xo.getChild(SIGMA);
69 if (cxo.getChild(0) instanceof Parameter) {
70 sigmaParam = (Parameter) cxo.getChild(Parameter.class);
71 } else {
72 sigmaParam = new Parameter.Default(cxo.getDoubleChild(0));
73 }
74
75 cxo = xo.getChild(GAMMA);
76 if (cxo.getChild(0) instanceof Parameter) {
77 gammaParam = (Parameter) cxo.getChild(Parameter.class);
78 } else {
79 gammaParam = new Parameter.Default(cxo.getDoubleChild(0));
80 }
81
82 Distribution distribution = (Distribution) xo.getChild(Distribution.class);
83
84 return new TwoPieceLocationScaleDistributionModel(locationParam, distribution,
85 sigmaParam, gammaParam, parameterization);
86
87 }
88
89 //************************************************************************
90 // AbstractXMLObjectParser implementation
91 //************************************************************************
92
93 public XMLSyntaxRule[] getSyntaxRules() {
94 return rules;
95 }
96
97 private final XMLSyntaxRule[] rules = {
98 AttributeRule.newStringArrayRule(PARAMETERIZATION),
99 new ElementRule(LOCATION,
100 new XMLSyntaxRule[]{
101 new XORRule(
102 new ElementRule(Parameter.class),
103 new ElementRule(Double.class)
104 )}
105 ),
106 new ElementRule(SIGMA,
107 new XMLSyntaxRule[]{
108 new XORRule(
109 new ElementRule(Parameter.class),
110 new ElementRule(Double.class)
111 )}, true
112 ),
113 new ElementRule(GAMMA,
114 new XMLSyntaxRule[]{
115 new XORRule(
116 new ElementRule(Parameter.class),
117 new ElementRule(Double.class)
118 )}, true
119 ),
120 new ElementRule(Distribution.class),
121 };
122
123 public String getParserDescription() {
124 return "Describes a two-piece location-scale distribution with a given location and two scales " +
125 "that can be used in a distributionLikelihood element";
126 }
127
128 public Class getReturnType() {
129 return TwoPieceLocationScaleDistributionModelParser.class;
130 }
131
132 }