Debian Codegraph @

src/dr/inferencexml/distribution/HierarchicalGraphLikelihoodParser.java

raw symbols SCIP
1 /*
2 * HierarchicalGraphLikelihoodParser.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.HierarchicalGraphLikelihood;
29 import dr.inference.model.Likelihood;
30 import dr.inference.model.MatrixParameter;
31 import dr.inference.model.Parameter;
32 import dr.xml.*;
33
34 /**
35 * @author Gabriela Cybis
36 */
37
38
39 public class HierarchicalGraphLikelihoodParser extends AbstractXMLObjectParser {
40
41
42
43 public static final String HIERARCHICAL_INDICATOR = "hierarchicalIndicator";
44 public static final String STRATA_INDICATOR = "strataIndicator";
45 public static final String PROB = "prob";
46
47 public String getParserName() {
48 return HierarchicalGraphLikelihood.HIERARCHICAL_GRAPH_LIKELIHOOD;
49 }
50
51 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
52
53 XMLObject cxo = xo.getChild(HIERARCHICAL_INDICATOR);
54 Parameter hierarchicalIndicator = (Parameter) cxo.getChild(Parameter.class);
55
56 cxo = xo.getChild(STRATA_INDICATOR);
57 MatrixParameter strataIndicatorMatrix = new MatrixParameter("matrix");
58 int dim = 0;
59 for (int i = 0; i < cxo.getChildCount(); i++) {
60 Parameter parameter = (Parameter) cxo.getChild(i);
61 strataIndicatorMatrix.addParameter(parameter);
62 if (i == 0)
63 dim = parameter.getDimension();
64 else if (dim != parameter.getDimension())
65 throw new XMLParseException("All parameters must have the same dimension to construct a rectangular matrix");
66 }
67
68 if (hierarchicalIndicator.getDimension()!= strataIndicatorMatrix.getRowDimension())
69 throw new XMLParseException("Hierarchical and starta parameters don't have the same dimentions");
70
71
72 cxo = xo.getChild(PROB);
73 Parameter prob = (Parameter) cxo.getChild(Parameter.class);
74
75 return new HierarchicalGraphLikelihood(hierarchicalIndicator, strataIndicatorMatrix, prob);
76
77 }
78
79 //************************************************************************
80 // AbstractXMLObjectParser implementation
81 //************************************************************************
82
83 public XMLSyntaxRule[] getSyntaxRules() {
84 return rules;
85 }
86
87 private final XMLSyntaxRule[] rules = {
88 new ElementRule(HIERARCHICAL_INDICATOR,
89 new XMLSyntaxRule[]{new ElementRule(Parameter.class)}),
90 new ElementRule(STRATA_INDICATOR,
91 new XMLSyntaxRule[]{new ElementRule(Parameter.class, 1, Integer.MAX_VALUE)}),
92 new ElementRule(PROB,
93 new XMLSyntaxRule[]{new ElementRule(Parameter.class)}),
94
95 };
96
97 public String getParserDescription() {
98 return "Calculates the likelihood of strata graph given hierarchical graph and p.";
99 }
100
101 public Class getReturnType() {
102 return Likelihood.class;
103 }
104 }