Debian Codegraph @

src/dr/inferencexml/distribution/MultivariateOUModelParser.java

raw symbols SCIP
1 /*
2 * MultivariateOUModelParser.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.inferencexml.distribution.MultivariateOUModel;
29 import dr.oldevomodel.substmodel.SubstitutionModel;
30 import dr.inference.distribution.GeneralizedLinearModel;
31 import dr.inference.model.DesignMatrix;
32 import dr.inference.model.MatrixParameter;
33 import dr.inference.model.Parameter;
34 import dr.inferencexml.distribution.GeneralizedLinearModelParser;
35 import dr.xml.*;
36
37 /**
38 *
39 */
40 public class MultivariateOUModelParser extends AbstractXMLObjectParser {
41
42 public static final String MVOU_MODEL = "multivariateOUModel";
43 public static final String MVOU_TYPE = "MVOU";
44 public static final String DATA = "data";
45 public static final String TIME = "times";
46 public static final String DESIGN = "design";
47
48 public String getParserName() {
49 return MVOU_MODEL;
50 }
51
52 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
53
54 SubstitutionModel substitutionModel = (SubstitutionModel) xo.getChild(SubstitutionModel.class);
55 Parameter effectParameter = (Parameter) ((XMLObject) xo.getChild(DATA)).getChild(Parameter.class);
56 Parameter timesParameter = (Parameter) ((XMLObject) xo.getChild(TIME)).getChild(Parameter.class);
57 Parameter designParameter = (Parameter) ((XMLObject) xo.getChild(DESIGN)).getChild(Parameter.class);
58 MatrixParameter gammaParameter = (MatrixParameter) xo.getChild(MatrixParameter.class);
59
60 if (effectParameter.getDimension() != timesParameter.getDimension() ||
61 effectParameter.getDimension() != designParameter.getDimension()) {
62 // System.err.println("dim(effect) " +effectParameter.getDimension());
63 // System.err.println("dim(times) "+timesParameter.getDimension());
64 // System.err.println("dim(design) "+designParameter.getDimension());
65 throw new XMLParseException("dim(" + effectParameter.getStatisticName() +
66 ") != dim(" + timesParameter.getStatisticName() + ") != dim(" + designParameter.getStatisticName() +
67 ") in " + xo.getName() + " element");
68 }
69
70 MultivariateOUModel glm = new MultivariateOUModel(substitutionModel, effectParameter, gammaParameter,
71 timesParameter.getParameterValues(), designParameter.getParameterValues());
72
73 addIndependentParameters(xo, glm, effectParameter);
74
75 // todo Confirm that design vector is consistent with substitution model
76 // todo Confirm that design vector is ordered 1,\ldots,K,1,\ldots,K, etc.
77
78 return glm;
79
80 }
81
82 public void addIndependentParameters(XMLObject xo, GeneralizedLinearModel glm,
83 Parameter dependentParam) throws XMLParseException {
84 int totalCount = xo.getChildCount();
85
86 for (int i = 0; i < totalCount; i++) {
87 if (xo.getChildName(i).compareTo(GeneralizedLinearModelParser.INDEPENDENT_VARIABLES) == 0) {
88 XMLObject cxo = (XMLObject) xo.getChild(i);
89 Parameter independentParam = (Parameter) cxo.getChild(Parameter.class);
90 DesignMatrix designMatrix = (DesignMatrix) cxo.getChild(DesignMatrix.class);
91 checkDimensions(independentParam, dependentParam, designMatrix);
92 glm.addIndependentParameter(independentParam, designMatrix, null);
93 }
94 }
95 }
96
97 private void checkDimensions(Parameter independentParam, Parameter dependentParam, DesignMatrix designMatrix)
98 throws XMLParseException {
99 if ((dependentParam.getDimension() != designMatrix.getRowDimension()) ||
100 (independentParam.getDimension() != designMatrix.getColumnDimension())) {
101 System.err.println(dependentParam.getDimension());
102 System.err.println(independentParam.getDimension());
103 System.err.println(designMatrix.getRowDimension() + " rows");
104 System.err.println(designMatrix.getColumnDimension() + " cols");
105 throw new XMLParseException(
106 "dim(" + dependentParam.getId() + ") != dim(" + designMatrix.getId() + " %*% " + independentParam.getId() + ")"
107 );
108 }
109 }
110
111 //************************************************************************
112 // AbstractXMLObjectParser implementation
113 //************************************************************************
114
115 public XMLSyntaxRule[] getSyntaxRules() {
116 return rules;
117 }
118
119 private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{
120 new ElementRule(SubstitutionModel.class),
121 new ElementRule(MatrixParameter.class),
122 new ElementRule(DATA, new XMLSyntaxRule[]{
123 new ElementRule(Parameter.class)}),
124 new ElementRule(TIME, new XMLSyntaxRule[]{
125 new ElementRule(Parameter.class)}),
126 new ElementRule(DESIGN, new XMLSyntaxRule[]{
127 new ElementRule(Parameter.class)}),
128 new ElementRule(GeneralizedLinearModelParser.INDEPENDENT_VARIABLES,
129 new XMLSyntaxRule[]{new ElementRule(MatrixParameter.class)}, 0, 3),
130 };
131
132 public String getParserDescription() {
133 return "Describes a multivariate OU process";
134 }
135
136 public Class getReturnType() {
137 return MultivariateOUModel.class;
138 }
139 }