Debian Codegraph @

src/dr/oldevomodelxml/substmodel/GLMSubstitutionModelParser.java

raw symbols SCIP
1 /*
2 * GLMSubstitutionModelParser.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.oldevomodelxml.substmodel;
27
28 import dr.evolution.datatype.DataType;
29 import dr.oldevomodel.substmodel.FrequencyModel;
30 import dr.oldevomodel.substmodel.GLMSubstitutionModel;
31 import dr.oldevomodel.substmodel.SubstitutionModel;
32 import dr.evoxml.util.DataTypeUtils;
33 import dr.inference.distribution.GeneralizedLinearModel;
34 import dr.inference.distribution.LogLinearModel;
35 import dr.xml.*;
36
37 /**
38 */
39 public class GLMSubstitutionModelParser extends AbstractXMLObjectParser {
40
41 public static final String GLM_SUBSTITUTION_MODEL = "glmSubstitutionModel";
42
43
44 public String getParserName() {
45 return GLM_SUBSTITUTION_MODEL;
46 }
47
48 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
49
50 DataType dataType = DataTypeUtils.getDataType(xo);
51
52 if (dataType == null) dataType = (DataType) xo.getChild(DataType.class);
53
54 int rateCount = (dataType.getStateCount() - 1) * dataType.getStateCount();
55
56 LogLinearModel glm = (LogLinearModel) xo.getChild(GeneralizedLinearModel.class);
57
58 int length = glm.getXBeta().length;
59
60 if (length != rateCount) {
61 throw new XMLParseException("Rates parameter in " + getParserName() + " element should have " + (rateCount) + " dimensions. However GLM dimension is " + length);
62 }
63
64 XMLObject cxo = xo.getChild(ComplexSubstitutionModelParser.ROOT_FREQUENCIES);
65 FrequencyModel rootFreq = (FrequencyModel) cxo.getChild(FrequencyModel.class);
66
67 if (dataType != rootFreq.getDataType()) {
68 throw new XMLParseException("Data type of " + getParserName() + " element does not match that of its rootFrequencyModel.");
69 }
70
71 return new GLMSubstitutionModel(xo.getId(), dataType, rootFreq, glm);
72 }
73
74 //************************************************************************
75 // AbstractXMLObjectParser implementation
76 //************************************************************************
77
78 public String getParserDescription() {
79 return "A general model of sequence substitution for any data type where the rates come from the generalized linear model.";
80 }
81
82 public Class getReturnType() {
83 return SubstitutionModel.class;
84 }
85
86 public XMLSyntaxRule[] getSyntaxRules() {
87 return rules;
88 }
89
90 private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{
91 new XORRule(
92 new StringAttributeRule(DataType.DATA_TYPE, "The type of sequence data",
93 DataType.getRegisteredDataTypeNames(), false),
94 new ElementRule(DataType.class)
95 ),
96 new ElementRule(ComplexSubstitutionModelParser.ROOT_FREQUENCIES, FrequencyModel.class),
97 new ElementRule(GeneralizedLinearModel.class),
98 };
99
100 }