Debian Codegraph @

src/dr/oldevomodelxml/substmodel/ComplexSubstitutionModelParser.java

raw symbols SCIP
1 /*
2 * ComplexSubstitutionModelParser.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.ComplexSubstitutionModel;
30 import dr.oldevomodel.substmodel.FrequencyModel;
31 import dr.oldevomodel.substmodel.SVSComplexSubstitutionModel;
32 import dr.evoxml.util.DataTypeUtils;
33 import dr.inference.model.BayesianStochasticSearchVariableSelection;
34 import dr.inference.model.Parameter;
35 import dr.xml.*;
36
37 import java.util.logging.Logger;
38
39 /**
40 */
41 public class ComplexSubstitutionModelParser extends AbstractXMLObjectParser {
42
43 public static final String COMPLEX_SUBSTITUTION_MODEL = "complexSubstitutionModel";
44 public static final String RATES = "rates";
45 public static final String ROOT_FREQUENCIES = "rootFrequencies";
46 public static final String INDICATOR = "rateIndicator";
47 public static final String RANDOMIZE = "randomizeIndicator";
48 public static final String NORMALIZATION = "normalize";
49 public static final String MAX_CONDITION_NUMBER = "maxConditionNumber";
50 public static final String CONNECTED = "mustBeConnected";
51 public static final String MAX_ITERATIONS = "maxIterations";
52 public static final String CHECK_CONDITIONING = "checkConditioning";
53
54 public String[] getParserNames() {
55 return new String[]{
56 getParserName(), "beast_" + getParserName()
57 };
58 }
59
60 public String getParserName() {
61 return COMPLEX_SUBSTITUTION_MODEL;
62 }
63
64 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
65
66 DataType dataType = DataTypeUtils.getDataType(xo);
67
68 if (dataType == null) dataType = (DataType) xo.getChild(DataType.class);
69
70 XMLObject cxo = xo.getChild(RATES);
71
72 Parameter ratesParameter = (Parameter) cxo.getChild(Parameter.class);
73
74 int rateCount = (dataType.getStateCount() - 1) * dataType.getStateCount();
75
76 if (ratesParameter.getDimension() != rateCount) {
77 throw new XMLParseException("Rates parameter in " + getParserName() + " element should have " + (rateCount)
78 + " dimensions. However parameter dimension is " + ratesParameter.getDimension());
79 }
80
81
82 cxo = xo.getChild(ROOT_FREQUENCIES);
83 FrequencyModel rootFreq = (FrequencyModel) cxo.getChild(FrequencyModel.class);
84
85 if (dataType != rootFreq.getDataType()) {
86 throw new XMLParseException("Data type of " + getParserName() + " element does not match that of its rootFrequencyModel.");
87 }
88
89 Parameter indicators = null;
90
91 if (xo.hasChildNamed(INDICATOR)) {
92 indicators = (Parameter) ((XMLObject) xo.getChild(INDICATOR)).getChild(Parameter.class);
93 if (ratesParameter.getDimension() != indicators.getDimension())
94 throw new XMLParseException("Rate parameter dimension must match indicator parameter dimension");
95 }
96
97 StringBuffer sb = new StringBuffer().append("Constructing a complex substitution model using\n")
98 .append("\tRate parameters: ").append(ratesParameter.getId())
99 .append("\n").append("\tRoot frequency model: ").append(rootFreq.getId()).append("\n");
100
101 ComplexSubstitutionModel model;
102
103 if (indicators == null)
104 model = new ComplexSubstitutionModel(xo.getId(), dataType, rootFreq, ratesParameter);
105 else {
106
107 boolean randomize = xo.getAttribute(RANDOMIZE, false);
108 boolean connected = xo.getAttribute(CONNECTED, false);
109 model = new SVSComplexSubstitutionModel(xo.getId(), dataType, rootFreq, ratesParameter, indicators);
110 if (randomize) {
111 BayesianStochasticSearchVariableSelection.Utils.randomize(indicators,
112 dataType.getStateCount(), false);
113
114 boolean valid = !Double.isInfinite(model.getLogLikelihood());
115 if (!valid) {
116 throw new XMLParseException("Poor tolerance in complex substitution model. Please retry analysis using BEAGLE");
117 }
118 }
119 sb.append("\tBSSVS indicators: ").append(indicators.getId()).append("\n");
120 sb.append("\tGraph must be connected: ").append(connected).append("\n");
121 }
122
123 boolean doNormalization = xo.getAttribute(NORMALIZATION, true);
124 model.setNormalization(doNormalization);
125 sb.append("\tNormalized: ").append(doNormalization).append("\n");
126
127 boolean checkConditioning = xo.getAttribute(CHECK_CONDITIONING, true);
128 model.setCheckConditioning(checkConditioning);
129
130 if (checkConditioning) {
131 double maxConditionNumber = xo.getAttribute(MAX_CONDITION_NUMBER, 1000);
132 model.setMaxConditionNumber(maxConditionNumber);
133 sb.append("\tMax. condition number: ").append(maxConditionNumber).append("\n");
134 }
135
136 int maxIterations = xo.getAttribute(MAX_ITERATIONS, 1000);
137 model.setMaxIterations(maxIterations);
138 sb.append("\tMax iterations: ").append(maxIterations).append("\n");
139
140 sb.append("\t\tPlease cite Edwards, Suchard et al. (2011)\n");
141
142 Logger.getLogger("dr.evomodel.substmodel").info(sb.toString());
143 return model;
144 }
145
146 //************************************************************************
147 // AbstractXMLObjectParser implementation
148 //************************************************************************
149
150 public String getParserDescription() {
151 return "A general reversible model of sequence substitution for any data type with stochastic variable selection.";
152 }
153
154 public Class getReturnType() {
155 return ComplexSubstitutionModelParser.class;
156 }
157
158 public XMLSyntaxRule[] getSyntaxRules() {
159 return rules;
160 }
161
162 private final XMLSyntaxRule[] rules = {
163 new XORRule(
164 new StringAttributeRule(DataType.DATA_TYPE, "The type of sequence data",
165 DataType.getRegisteredDataTypeNames(), false),
166 new ElementRule(DataType.class)
167 ),
168 AttributeRule.newBooleanRule(RANDOMIZE, true),
169 new ElementRule(ROOT_FREQUENCIES, FrequencyModel.class),
170 new ElementRule(RATES,
171 new XMLSyntaxRule[]{
172 new ElementRule(Parameter.class)}
173 ),
174 new ElementRule(INDICATOR,
175 new XMLSyntaxRule[]{
176 new ElementRule(Parameter.class)
177 }, true),
178 AttributeRule.newBooleanRule(NORMALIZATION, true),
179 AttributeRule.newDoubleRule(MAX_CONDITION_NUMBER, true),
180 AttributeRule.newBooleanRule(CONNECTED, true),
181 AttributeRule.newIntegerRule(MAX_ITERATIONS, true),
182 AttributeRule.newBooleanRule(CHECK_CONDITIONING, true),
183 };
184
185 }