Debian Codegraph @

src/dr/evomodelxml/substmodel/GeneralSubstitutionModelParser.java

raw symbols SCIP
1 /*
2 * GeneralSubstitutionModelParser.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.evomodel.substmodel.FrequencyModel;
29 import dr.evomodel.substmodel.GeneralSubstitutionModel;
30 import dr.evomodel.substmodel.SVSComplexSubstitutionModel;
31 import dr.evomodel.substmodel.SVSGeneralSubstitutionModel;
32 import dr.evolution.datatype.DataType;
33 import dr.oldevomodelxml.substmodel.ComplexSubstitutionModelParser;
34 import dr.evoxml.util.DataTypeUtils;
35 import dr.inference.model.BayesianStochasticSearchVariableSelection;
36 import dr.inference.model.Parameter;
37 import dr.xml.*;
38
39 import java.util.logging.Logger;
40
41 /**
42 * Parses a GeneralSubstitutionModel or one of its more specific descendants.
43 */
44 public class GeneralSubstitutionModelParser extends AbstractXMLObjectParser {
45
46 public static final String GENERAL_SUBSTITUTION_MODEL = "generalSubstitutionModel";
47 public static final String DATA_TYPE = "dataType";
48 public static final String RATES = "rates";
49 public static final String RELATIVE_TO = "relativeTo";
50 public static final String FREQUENCIES = "frequencies";
51 public static final String INDICATOR = "rateIndicator";
52
53 public String getParserName() {
54 return GENERAL_SUBSTITUTION_MODEL;
55 }
56
57 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
58
59 Parameter ratesParameter = null;
60 FrequencyModel freqModel = null;
61
62 if (xo.hasChildNamed(FREQUENCIES)) {
63 XMLObject cxo = xo.getChild(FREQUENCIES);
64 freqModel = (FrequencyModel) cxo.getChild(FrequencyModel.class);
65 }
66
67 DataType dataType = DataTypeUtils.getDataType(xo);
68
69 if (dataType == null) dataType = (DataType) xo.getChild(DataType.class);
70
71 // if (xo.hasAttribute(DataType.DATA_TYPE)) {
72 // String dataTypeStr = xo.getStringAttribute(DataType.DATA_TYPE);
73 // if (dataTypeStr.equals(Nucleotides.DESCRIPTION)) {
74 // dataType = Nucleotides.INSTANCE;
75 // } else if (dataTypeStr.equals(AminoAcids.DESCRIPTION)) {
76 // dataType = AminoAcids.INSTANCE;
77 // } else if (dataTypeStr.equals(Codons.DESCRIPTION)) {
78 // dataType = Codons.UNIVERSAL;
79 // } else if (dataTypeStr.equals(TwoStates.DESCRIPTION)) {
80 // dataType = TwoStates.INSTANCE;
81 // }
82 // }
83
84 if (dataType == null) dataType = freqModel.getDataType();
85
86 if (dataType != freqModel.getDataType()) {
87 throw new XMLParseException("Data type of " + getParserName() + " element does not match that of its frequencyModel.");
88 }
89
90 XMLObject cxo = xo.getChild(RATES);
91 ratesParameter = (Parameter) cxo.getChild(Parameter.class);
92
93 int states = dataType.getStateCount();
94 Logger.getLogger("dr.evomodel").info(" General Substitution Model (stateCount=" + states + ")");
95
96 boolean hasRelativeRates = cxo.hasChildNamed(RELATIVE_TO) || (cxo.hasAttribute(RELATIVE_TO) && cxo.getIntegerAttribute(RELATIVE_TO) > 0);
97
98 int nonReversibleRateCount = ((dataType.getStateCount() - 1) * dataType.getStateCount());
99 int reversibleRateCount = (nonReversibleRateCount / 2);
100
101 boolean isNonReversible = ratesParameter.getDimension() == nonReversibleRateCount;
102 boolean hasIndicator = xo.hasChildNamed(INDICATOR);
103
104 if (!hasRelativeRates) {
105 Parameter indicatorParameter = null;
106
107 if (ratesParameter.getDimension() != reversibleRateCount && ratesParameter.getDimension() != nonReversibleRateCount) {
108 throw new XMLParseException("Rates parameter in " + getParserName() + " element should have " + (reversibleRateCount)
109 + " dimensions for reversible model or " + nonReversibleRateCount + " dimensions for non-reversible. " +
110 "However parameter dimension is " + ratesParameter.getDimension());
111 }
112
113 if (hasIndicator) { // this is using BSSVS
114 cxo = xo.getChild(INDICATOR);
115 indicatorParameter = (Parameter) cxo.getChild(Parameter.class);
116
117 if (indicatorParameter.getDimension() != ratesParameter.getDimension()) {
118 throw new XMLParseException("Rates and indicator parameters in " + getParserName() + " element must be the same dimension.");
119 }
120
121 boolean randomize = xo.getAttribute(ComplexSubstitutionModelParser.RANDOMIZE, false);
122 if (randomize) {
123 BayesianStochasticSearchVariableSelection.Utils.randomize(indicatorParameter,
124 dataType.getStateCount(), !isNonReversible);
125 }
126
127 }
128
129 if (isNonReversible) {
130 // if (xo.hasChildNamed(ROOT_FREQ)) {
131 // cxo = xo.getChild(ROOT_FREQ);
132 // FrequencyModel rootFreq = (FrequencyModel) cxo.getChild(FrequencyModel.class);
133 //
134 // if (dataType != rootFreq.getDataType()) {
135 // throw new XMLParseException("Data type of " + getParserName() + " element does not match that of its rootFrequencyModel.");
136 // }
137 //
138 // Logger.getLogger("dr.evomodel").info(" Using BSSVS Complex Substitution Model");
139 // return new SVSComplexSubstitutionModel(getParserName(), dataType, freqModel, ratesParameter, indicatorParameter);
140 //
141 // } else {
142 // throw new XMLParseException("Non-reversible model missing " + ROOT_FREQ + " element");
143 // }
144 Logger.getLogger("dr.evomodel").info(" Using BSSVS Complex Substitution Model");
145 return new SVSComplexSubstitutionModel(getParserName(), dataType, freqModel, ratesParameter, indicatorParameter);
146 } else {
147 Logger.getLogger("dr.evomodel").info(" Using BSSVS General Substitution Model");
148 return new SVSGeneralSubstitutionModel(getParserName(), dataType, freqModel, ratesParameter, indicatorParameter);
149 }
150
151
152 } else {
153 // if we have relativeTo attribute then we use the old GeneralSubstitutionModel
154
155 if (ratesParameter.getDimension() != reversibleRateCount - 1) {
156 throw new XMLParseException("Rates parameter in " + getParserName() + " element should have " + (reversibleRateCount - 1)
157 + " dimensions. However parameter dimension is " + ratesParameter.getDimension());
158 }
159
160 int relativeTo = 0;
161 if (hasRelativeRates) {
162 relativeTo = cxo.getIntegerAttribute(RELATIVE_TO) - 1;
163 }
164
165 if (relativeTo < 0 || relativeTo >= reversibleRateCount) {
166 throw new XMLParseException(RELATIVE_TO + " must be 1 or greater");
167 } else {
168 int t = relativeTo;
169 int s = states - 1;
170 int row = 0;
171 while (t >= s) {
172 t -= s;
173 s -= 1;
174 row += 1;
175 }
176 int col = t + row + 1;
177
178 Logger.getLogger("dr.evomodel").info(" Rates relative to "
179 + dataType.getCode(row) + "<->" + dataType.getCode(col));
180 }
181
182 if (ratesParameter == null) {
183 if (reversibleRateCount == 1) {
184 // simplest model for binary traits...
185 } else {
186 throw new XMLParseException("No rates parameter found in " + getParserName());
187 }
188 }
189
190 return new GeneralSubstitutionModel(getParserName(), dataType, freqModel, ratesParameter, relativeTo);
191 }
192 }
193
194 //************************************************************************
195 // AbstractXMLObjectParser implementation
196 //************************************************************************
197
198 public String getParserDescription() {
199 return "A general reversible model of sequence substitution for any data type.";
200 }
201
202 public Class getReturnType() {
203 return GeneralSubstitutionModelParser.class;
204 }
205
206 public XMLSyntaxRule[] getSyntaxRules() {
207 return rules;
208 }
209
210 private final XMLSyntaxRule[] rules = {
211 new XORRule(
212 new StringAttributeRule(DataType.DATA_TYPE, "The type of sequence data",
213 DataType.getRegisteredDataTypeNames(), false),
214 new ElementRule(DataType.class)
215 , true),
216 new ElementRule(FREQUENCIES, FrequencyModel.class),
217 new ElementRule(RATES,
218 new XMLSyntaxRule[]{
219 new ElementRule(Parameter.class)}
220 ),
221 new ElementRule(INDICATOR,
222 new XMLSyntaxRule[]{
223 new ElementRule(Parameter.class),
224 }, true),
225 AttributeRule.newBooleanRule(ComplexSubstitutionModelParser.RANDOMIZE,true),
226 };
227 }