Debian Codegraph @

src/dr/evomodelxml/substmodel/MarkovModulatedSubstitutionModelParser.java

raw symbols SCIP
1 /*
2 * MarkovModulatedSubstitutionModelParser.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.HiddenDataType;
29 import dr.evomodel.siteratemodel.SiteRateModel;
30 import dr.evomodel.substmodel.MarkovModulatedSubstitutionModel;
31 import dr.evomodel.substmodel.SubstitutionModel;
32 import dr.evolution.datatype.DataType;
33 import dr.evoxml.util.DataTypeUtils;
34 import dr.inference.model.Parameter;
35 import dr.xml.*;
36
37 import java.util.ArrayList;
38 import java.util.List;
39
40 /**
41 * @author Marc A. Suchard
42 */
43 public class MarkovModulatedSubstitutionModelParser extends AbstractXMLObjectParser {
44
45 public static final String MARKOV_MODULATED_MODEL = "markovModulatedSubstitutionModel";
46 // public static final String HIDDEN_COUNT = "hiddenCount";
47 public static final String SWITCHING_RATES = "switchingRates";
48 // public static final String DIAGONALIZATION = "diagonalization";
49 public static final String RATE_SCALAR = "rateScalar";
50 public static final String GEOMETRIC_RATES = "geometricRates";
51 public static final String RENORMALIZE = "renormalize";
52
53 public String getParserName() {
54 return MARKOV_MODULATED_MODEL;
55 }
56
57 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
58
59 DataType dataType = DataTypeUtils.getDataType(xo);
60 if (!(dataType instanceof HiddenDataType)) {
61 throw new XMLParseException("Must construct " + MARKOV_MODULATED_MODEL + " with hidden data types");
62 }
63
64 Parameter switchingRates = (Parameter) xo.getElementFirstChild(SWITCHING_RATES);
65
66 List<SubstitutionModel> substModels = new ArrayList<SubstitutionModel>();
67 for (int i = 0; i < xo.getChildCount(); i++) {
68 Object cxo = xo.getChild(i);
69 if (cxo instanceof SubstitutionModel) {
70 substModels.add((SubstitutionModel) cxo);
71 }
72 }
73
74 boolean geometricRates = xo.getAttribute(GEOMETRIC_RATES, false);
75
76 Parameter rateScalar = xo.hasChildNamed(RATE_SCALAR) ?
77 (Parameter) xo.getChild(RATE_SCALAR).getChild(Parameter.class) : null;
78
79 SiteRateModel siteRateModel = (SiteRateModel) xo.getChild(SiteRateModel.class);
80 if (siteRateModel != null) {
81 if (siteRateModel.getCategoryCount() != substModels.size() && substModels.size() % siteRateModel.getCategoryCount() != 0) {
82 throw new XMLParseException(
83 "Number of gamma categories must equal number of substitution models in " + xo.getId());
84 }
85 }
86
87 MarkovModulatedSubstitutionModel mmsm = new MarkovModulatedSubstitutionModel(xo.getId(), substModels, switchingRates, dataType, null,
88 rateScalar, geometricRates, siteRateModel);
89
90 if (xo.getAttribute(RENORMALIZE, false)) {
91 mmsm.setNormalization(true);
92 }
93
94 return mmsm;
95 }
96
97 public String getParserDescription() {
98 return "This element represents the a Markov-modulated substitution model.";
99 }
100
101 public Class getReturnType() {
102 return MarkovModulatedSubstitutionModel.class;
103 }
104
105 public XMLSyntaxRule[] getSyntaxRules() {
106 return rules;
107 }
108
109 private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{
110
111 AttributeRule.newStringRule(DataType.DATA_TYPE),
112 // AttributeRule.newStringRule(GeneticCode.GENETIC_CODE),
113 // new ElementRule(OMEGA,
114 // new XMLSyntaxRule[]{new ElementRule(Parameter.class)}),
115 // new ElementRule(KAPPA,
116 // new XMLSyntaxRule[]{new ElementRule(Parameter.class)}),
117 new ElementRule(SWITCHING_RATES,
118 new XMLSyntaxRule[]{new ElementRule(Parameter.class)}),
119 // new ElementRule(FrequencyModel.class),
120 // AttributeRule.newStringRule(DIAGONALIZATION),
121 new ElementRule(SubstitutionModel.class, 1, Integer.MAX_VALUE),
122 AttributeRule.newBooleanRule(GEOMETRIC_RATES, true),
123 AttributeRule.newBooleanRule(RENORMALIZE, true),
124 new ElementRule(RATE_SCALAR,
125 new XMLSyntaxRule[]{new ElementRule(Parameter.class)}, true),
126
127 new ElementRule(SiteRateModel.class, true),
128 };
129 }