Debian Codegraph @

src/dr/inferencexml/model/MarkovRandomFieldMatrixParser.java

raw symbols SCIP
1 /*
2 * MarkovRandomFieldMatrixParser.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.model;
27
28 import dr.inference.model.MarkovRandomFieldMatrix;
29 import dr.inference.model.Parameter;
30 import dr.util.Transform;
31 import dr.xml.*;
32
33 /**
34 *
35 */
36 public class MarkovRandomFieldMatrixParser extends AbstractXMLObjectParser {
37
38 public final static String MATRIX_PARAMETER = "markovRandomFieldMatrix";
39 public static final String DIAGONAL = "diagonal";
40 public static final String OFF_DIAGONAL = "offDiagonal";
41 public static final String AS_CORRELATION = "asCorrelation";
42 public static final String NUGGET = "nugget";
43 public static final String DIM = "dim";
44
45 public String getParserName() {
46 return MATRIX_PARAMETER;
47 }
48
49 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
50
51 String name = xo.getId();
52
53 final int dim = xo.getIntegerAttribute(DIM);
54
55 XMLObject cxo = xo.getChild(DIAGONAL);
56 Parameter diagonalParameter = (Parameter) cxo.getChild(Parameter.class);
57 Transform.ParsedTransform tmp = (Transform.ParsedTransform) cxo.getChild(Transform.ParsedTransform.class);
58 Transform diagonalTransform = (tmp != null) ? tmp.transform : null;
59
60 cxo = xo.getChild(OFF_DIAGONAL);
61 Parameter offDiagonalParameter = (Parameter) cxo.getChild(Parameter.class);
62 tmp = (Transform.ParsedTransform) cxo.getChild(Transform.ParsedTransform.class);
63 Transform offDiagonalTransform = (tmp != null) ? tmp.transform : null;
64
65 boolean asCorrelation = xo.getAttribute(AS_CORRELATION, false);
66
67 if (diagonalParameter.getDimension() != 1 || offDiagonalParameter.getDimension() != 1) {
68 throw new XMLParseException("Wrong parameter dimensions for GMRF");
69 }
70
71 cxo = xo.getChild(NUGGET);
72 Parameter nuggetParameter = (Parameter) cxo.getChild(Parameter.class);
73
74 return new MarkovRandomFieldMatrix(name, dim, diagonalParameter, offDiagonalParameter, nuggetParameter,
75 asCorrelation,
76 diagonalTransform, offDiagonalTransform);
77 }
78
79 //************************************************************************
80 // AbstractXMLObjectParser implementation
81 //************************************************************************
82
83 public String getParserDescription() {
84 return "A MRF matrix parameter constructed from its diagonals and first-order off diagonal.";
85 }
86
87 public XMLSyntaxRule[] getSyntaxRules() {
88 return rules;
89 }
90
91 private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{
92 new ElementRule(DIAGONAL,
93 new XMLSyntaxRule[]{
94 new ElementRule(Parameter.class),
95 new ElementRule(Transform.ParsedTransform.class, true),
96 }),
97 new ElementRule(OFF_DIAGONAL,
98 new XMLSyntaxRule[]{
99 new ElementRule(Parameter.class),
100 new ElementRule(Transform.ParsedTransform.class, true),
101 }),
102 new ElementRule(NUGGET,
103 new XMLSyntaxRule[] {
104 new ElementRule(Parameter.class),
105 }),
106 AttributeRule.newBooleanRule(AS_CORRELATION, true),
107 AttributeRule.newIntegerRule(DIM),
108 };
109
110 public Class getReturnType() {
111 return MarkovRandomFieldMatrix.class;
112 }
113 }