Debian Codegraph @

src/dr/evomodelxml/substmodel/ComplexSubstitutionModelParser.java

raw symbols SCIP
1 /*
2 * ComplexSubstitutionModelParser.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.*;
30 import dr.inference.model.BayesianStochasticSearchVariableSelection;
31 import dr.inference.model.Parameter;
32 import dr.xml.*;
33
34 import java.util.logging.Logger;
35
36 /**
37 * @author Marc Suchard
38 */
39 public class ComplexSubstitutionModelParser extends AbstractXMLObjectParser {
40
41 public static final String COMPLEX_SUBSTITUTION_MODEL = "complexSubstitutionModel";
42 public static final String SVS_COMPLEX_SUBSTITUTION_MODEL = "svsComplexSubstitutionModel";
43 public static final String DATA_TYPE = "dataType";
44 public static final String RATES = "rates";
45 public static final String FREQUENCIES = "frequencies";
46 public static final String ROOT_FREQUENCIES = "rootFrequencies";
47 public static final String RANDOMIZE = "randomizeIndicator";
48 public static final String INDICATOR = "rateIndicator";
49 public static final String BSSVS_TOLERANCE = "bssvsTolerance";
50 public static final String BSSVS_SCALAR = "bssvsScalar";
51 public static final String CHECK_CONDITIONING = "checkConditioning";
52 public static final String NORMALIZED = "normalized";
53
54 public static final int maxRandomizationTries = 100;
55
56 public String getParserName() {
57 return COMPLEX_SUBSTITUTION_MODEL;
58 }
59
60 public String[] getParserNames() {
61 return new String[]{COMPLEX_SUBSTITUTION_MODEL, SVS_COMPLEX_SUBSTITUTION_MODEL};
62 }
63
64
65 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
66
67 Parameter ratesParameter;
68
69 XMLObject cxo;
70 if (xo.hasChildNamed(FREQUENCIES)) {
71 cxo = xo.getChild(FREQUENCIES);
72 } else {
73 cxo = xo.getChild(ROOT_FREQUENCIES);
74 }
75 FrequencyModel freqModel = (FrequencyModel) cxo.getChild(FrequencyModel.class);
76
77 DataType dataType = freqModel.getDataType();
78
79 cxo = xo.getChild(RATES);
80
81 int states = dataType.getStateCount();
82
83 Logger.getLogger("dr.app.beagle.evomodel").info(" Complex Substitution Model (stateCount=" + states + ")");
84
85 ratesParameter = (Parameter) cxo.getChild(Parameter.class);
86
87 int rateCount = (dataType.getStateCount() - 1) * dataType.getStateCount();
88
89 if (ratesParameter == null) {
90
91 if (rateCount == 1) {
92 // simplest model for binary traits...
93 } else {
94 throw new XMLParseException("No rates parameter found in " + getParserName());
95 }
96 } else if (ratesParameter.getDimension() != rateCount) {
97 throw new XMLParseException("Rates parameter in " + getParserName() + " element should have " + rateCount + " dimensions.");
98 }
99
100 boolean checkConditioning = xo.getAttribute(CHECK_CONDITIONING, true);
101
102 if (!xo.hasChildNamed(INDICATOR)) {
103 if (!checkConditioning) {
104 return new ComplexSubstitutionModel(COMPLEX_SUBSTITUTION_MODEL, dataType, freqModel, ratesParameter) {
105 protected EigenSystem getDefaultEigenSystem(int stateCount) {
106 return new ComplexColtEigenSystem(stateCount, false, ColtEigenSystem.defaultMaxConditionNumber, ColtEigenSystem.defaultMaxIterations);
107 }
108 };
109 } else {
110 return new ComplexSubstitutionModel(COMPLEX_SUBSTITUTION_MODEL, dataType, freqModel, ratesParameter);
111 }
112 }
113
114 cxo = xo.getChild(INDICATOR);
115
116 Parameter indicatorParameter = (Parameter) cxo.getChild(Parameter.class);
117 if (indicatorParameter == null || ratesParameter == null || indicatorParameter.getDimension() != ratesParameter.getDimension())
118 throw new XMLParseException("Rates and indicator parameters in " + getParserName() + " element must be the same dimension.");
119
120 if (xo.hasAttribute(BSSVS_TOLERANCE)) {
121 double tolerance = xo.getAttribute(BSSVS_TOLERANCE,
122 BayesianStochasticSearchVariableSelection.Utils.getTolerance());
123 if (tolerance > BayesianStochasticSearchVariableSelection.Utils.getTolerance()) {
124 // Only increase smallest allowed tolerance
125 BayesianStochasticSearchVariableSelection.Utils.setTolerance(tolerance);
126 Logger.getLogger("dr.app.beagle.evomodel").info("\tIncreasing BSSVS tolerance to " + tolerance);
127 }
128 }
129
130 if (xo.hasAttribute(BSSVS_SCALAR)) {
131 double scalar = xo.getAttribute(BSSVS_SCALAR,
132 BayesianStochasticSearchVariableSelection.Utils.getScalar());
133 if (scalar < BayesianStochasticSearchVariableSelection.Utils.getScalar()) {
134 BayesianStochasticSearchVariableSelection.Utils.setScalar(scalar);
135 Logger.getLogger("dr.app.beagle.evomodel").info("\tDecreasing BSSVS scalar to " + scalar);
136 }
137 }
138
139 SVSComplexSubstitutionModel model;
140 if (!checkConditioning) {
141 model = new SVSComplexSubstitutionModel(SVS_COMPLEX_SUBSTITUTION_MODEL, dataType, freqModel, ratesParameter, indicatorParameter) {
142 protected EigenSystem getDefaultEigenSystem(int stateCount) {
143 return new ComplexColtEigenSystem(stateCount, false, ColtEigenSystem.defaultMaxConditionNumber, ColtEigenSystem.defaultMaxIterations);
144 }
145 };
146 } else {
147 model = new SVSComplexSubstitutionModel(SVS_COMPLEX_SUBSTITUTION_MODEL, dataType, freqModel, ratesParameter, indicatorParameter);
148 }
149 boolean randomize = xo.getAttribute(RANDOMIZE, false);
150 if (randomize) {
151 // Randomization may need multiple tries
152 int tries = 0;
153 boolean valid = false;
154
155 while (!valid && tries < maxRandomizationTries) {
156 BayesianStochasticSearchVariableSelection.Utils.randomize(indicatorParameter,
157 dataType.getStateCount(), false);
158 valid = !Double.isInfinite(model.getLogLikelihood());
159 tries++;
160 }
161 Logger.getLogger("dr.app.beagle.evomodel").info("\tRandomization attempts: " + tries);
162 }
163 if (!xo.getAttribute(NORMALIZED, true)) {
164 model.setNormalization(false);
165 Logger.getLogger("dr.app.beagle.evomodel").info("\tNormalization: false");
166 }
167 Logger.getLogger("dr.app.beagle.evomodel").info("\t\tPlease cite: Edwards, Suchard et al. (2011)\n");
168 return model;
169 }
170
171 //************************************************************************
172 // AbstractXMLObjectParser implementation
173 //************************************************************************
174
175 public String getParserDescription() {
176 return "A general irreversible model of sequence substitution for any data type.";
177 }
178
179 public Class getReturnType() {
180 return SVSComplexSubstitutionModel.class;
181 }
182
183 public XMLSyntaxRule[] getSyntaxRules() {
184 return rules;
185 }
186
187 private final XMLSyntaxRule[] rules = {
188 new XORRule(
189 new StringAttributeRule(DataType.DATA_TYPE, "The type of sequence data",
190 DataType.getRegisteredDataTypeNames(), false),
191 new ElementRule(DataType.class),
192 true // Optional
193 ),
194 AttributeRule.newBooleanRule(RANDOMIZE, true),
195 new XORRule(
196 new ElementRule(FREQUENCIES, FrequencyModel.class),
197 new ElementRule(ROOT_FREQUENCIES, FrequencyModel.class)),
198 new ElementRule(RATES,
199 new XMLSyntaxRule[]{
200 new ElementRule(Parameter.class, true)}
201 ),
202 new ElementRule(INDICATOR,
203 new XMLSyntaxRule[]{
204 new ElementRule(Parameter.class)
205 }, true),
206 AttributeRule.newDoubleRule(BSSVS_TOLERANCE, true),
207 AttributeRule.newDoubleRule(BSSVS_SCALAR, true),
208 AttributeRule.newBooleanRule(CHECK_CONDITIONING, true),
209 AttributeRule.newBooleanRule(NORMALIZED, true),
210 };
211 }