Debian Codegraph @

src/dr/evomodelxml/substmodel/GLMSubstitutionModelParser.java

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