| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
| 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 |
} |