use of dr.inference.model.Parameter in project beast-mcmc by beast-dev.
the class ComplexSubstitutionModelParser method parseXMLObject.
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
Parameter ratesParameter;
XMLObject cxo;
if (xo.hasChildNamed(FREQUENCIES)) {
cxo = xo.getChild(FREQUENCIES);
} else {
cxo = xo.getChild(ROOT_FREQUENCIES);
}
FrequencyModel freqModel = (FrequencyModel) cxo.getChild(FrequencyModel.class);
DataType dataType = freqModel.getDataType();
cxo = xo.getChild(RATES);
int states = dataType.getStateCount();
Logger.getLogger("dr.app.beagle.evomodel").info(" Complex Substitution Model (stateCount=" + states + ")");
ratesParameter = (Parameter) cxo.getChild(Parameter.class);
int rateCount = (dataType.getStateCount() - 1) * dataType.getStateCount();
if (ratesParameter == null) {
if (rateCount == 1) {
// simplest model for binary traits...
} else {
throw new XMLParseException("No rates parameter found in " + getParserName());
}
} else if (ratesParameter.getDimension() != rateCount) {
throw new XMLParseException("Rates parameter in " + getParserName() + " element should have " + rateCount + " dimensions.");
}
boolean checkConditioning = xo.getAttribute(CHECK_CONDITIONING, true);
if (!xo.hasChildNamed(INDICATOR)) {
if (!checkConditioning) {
return new ComplexSubstitutionModel(COMPLEX_SUBSTITUTION_MODEL, dataType, freqModel, ratesParameter) {
protected EigenSystem getDefaultEigenSystem(int stateCount) {
return new ComplexColtEigenSystem(stateCount, false, ColtEigenSystem.defaultMaxConditionNumber, ColtEigenSystem.defaultMaxIterations);
}
};
} else {
return new ComplexSubstitutionModel(COMPLEX_SUBSTITUTION_MODEL, dataType, freqModel, ratesParameter);
}
}
cxo = xo.getChild(INDICATOR);
Parameter indicatorParameter = (Parameter) cxo.getChild(Parameter.class);
if (indicatorParameter == null || ratesParameter == null || indicatorParameter.getDimension() != ratesParameter.getDimension())
throw new XMLParseException("Rates and indicator parameters in " + getParserName() + " element must be the same dimension.");
if (xo.hasAttribute(BSSVS_TOLERANCE)) {
double tolerance = xo.getAttribute(BSSVS_TOLERANCE, BayesianStochasticSearchVariableSelection.Utils.getTolerance());
if (tolerance > BayesianStochasticSearchVariableSelection.Utils.getTolerance()) {
// Only increase smallest allowed tolerance
BayesianStochasticSearchVariableSelection.Utils.setTolerance(tolerance);
Logger.getLogger("dr.app.beagle.evomodel").info("\tIncreasing BSSVS tolerance to " + tolerance);
}
}
if (xo.hasAttribute(BSSVS_SCALAR)) {
double scalar = xo.getAttribute(BSSVS_SCALAR, BayesianStochasticSearchVariableSelection.Utils.getScalar());
if (scalar < BayesianStochasticSearchVariableSelection.Utils.getScalar()) {
BayesianStochasticSearchVariableSelection.Utils.setScalar(scalar);
Logger.getLogger("dr.app.beagle.evomodel").info("\tDecreasing BSSVS scalar to " + scalar);
}
}
SVSComplexSubstitutionModel model;
if (!checkConditioning) {
model = new SVSComplexSubstitutionModel(SVS_COMPLEX_SUBSTITUTION_MODEL, dataType, freqModel, ratesParameter, indicatorParameter) {
protected EigenSystem getDefaultEigenSystem(int stateCount) {
return new ComplexColtEigenSystem(stateCount, false, ColtEigenSystem.defaultMaxConditionNumber, ColtEigenSystem.defaultMaxIterations);
}
};
} else {
model = new SVSComplexSubstitutionModel(SVS_COMPLEX_SUBSTITUTION_MODEL, dataType, freqModel, ratesParameter, indicatorParameter);
}
boolean randomize = xo.getAttribute(RANDOMIZE, false);
if (randomize) {
// Randomization may need multiple tries
int tries = 0;
boolean valid = false;
while (!valid && tries < maxRandomizationTries) {
BayesianStochasticSearchVariableSelection.Utils.randomize(indicatorParameter, dataType.getStateCount(), false);
valid = !Double.isInfinite(model.getLogLikelihood());
tries++;
}
Logger.getLogger("dr.app.beagle.evomodel").info("\tRandomization attempts: " + tries);
}
if (!xo.getAttribute(NORMALIZED, true)) {
model.setNormalization(false);
Logger.getLogger("dr.app.beagle.evomodel").info("\tNormalization: false");
}
Logger.getLogger("dr.app.beagle.evomodel").info("\t\tPlease cite: Edwards, Suchard et al. (2011)\n");
return model;
}
use of dr.inference.model.Parameter in project beast-mcmc by beast-dev.
the class GY94CodonModelParser method parseXMLObject.
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
Codons codons = Codons.UNIVERSAL;
if (xo.hasAttribute(GeneticCode.GENETIC_CODE)) {
String codeStr = xo.getStringAttribute(GeneticCode.GENETIC_CODE);
codons = Codons.findByName(codeStr);
}
Parameter omegaParameter = (Parameter) xo.getElementFirstChild(OMEGA);
int dim = omegaParameter.getDimension();
double value = omegaParameter.getParameterValue(dim - 1);
if (value < 0) {
throw new RuntimeException("Negative Omega parameter value " + value);
}
//END: negative check
Parameter kappaParameter = (Parameter) xo.getElementFirstChild(KAPPA);
dim = kappaParameter.getDimension();
value = kappaParameter.getParameterValue(dim - 1);
if (value < 0) {
throw new RuntimeException("Negative kappa parameter value value " + value);
}
//END: negative check
FrequencyModel freqModel = (FrequencyModel) xo.getChild(FrequencyModel.class);
GY94CodonModel codonModel = new GY94CodonModel(codons, omegaParameter, kappaParameter, freqModel);
return codonModel;
}
use of dr.inference.model.Parameter in project beast-mcmc by beast-dev.
the class GeneralSubstitutionModelParser method parseXMLObject.
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
Parameter ratesParameter = null;
FrequencyModel freqModel = null;
if (xo.hasChildNamed(FREQUENCIES)) {
XMLObject cxo = xo.getChild(FREQUENCIES);
freqModel = (FrequencyModel) cxo.getChild(FrequencyModel.class);
}
DataType dataType = DataTypeUtils.getDataType(xo);
if (dataType == null)
dataType = (DataType) xo.getChild(DataType.class);
if (dataType == null)
dataType = freqModel.getDataType();
if (dataType != freqModel.getDataType()) {
throw new XMLParseException("Data type of " + getParserName() + " element does not match that of its frequencyModel.");
}
XMLObject cxo = xo.getChild(RATES);
ratesParameter = (Parameter) cxo.getChild(Parameter.class);
int states = dataType.getStateCount();
Logger.getLogger("dr.evomodel").info(" General Substitution Model (stateCount=" + states + ")");
boolean hasRelativeRates = cxo.hasChildNamed(RELATIVE_TO) || (cxo.hasAttribute(RELATIVE_TO) && cxo.getIntegerAttribute(RELATIVE_TO) > 0);
int nonReversibleRateCount = ((dataType.getStateCount() - 1) * dataType.getStateCount());
int reversibleRateCount = (nonReversibleRateCount / 2);
boolean isNonReversible = ratesParameter.getDimension() == nonReversibleRateCount;
boolean hasIndicator = xo.hasChildNamed(INDICATOR);
if (!hasRelativeRates) {
Parameter indicatorParameter = null;
if (ratesParameter.getDimension() != reversibleRateCount && ratesParameter.getDimension() != nonReversibleRateCount) {
throw new XMLParseException("Rates parameter in " + getParserName() + " element should have " + (reversibleRateCount) + " dimensions for reversible model or " + nonReversibleRateCount + " dimensions for non-reversible. " + "However parameter dimension is " + ratesParameter.getDimension());
}
if (hasIndicator) {
// this is using BSSVS
cxo = xo.getChild(INDICATOR);
indicatorParameter = (Parameter) cxo.getChild(Parameter.class);
if (indicatorParameter.getDimension() != ratesParameter.getDimension()) {
throw new XMLParseException("Rates and indicator parameters in " + getParserName() + " element must be the same dimension.");
}
boolean randomize = xo.getAttribute(ComplexSubstitutionModelParser.RANDOMIZE, false);
if (randomize) {
BayesianStochasticSearchVariableSelection.Utils.randomize(indicatorParameter, dataType.getStateCount(), !isNonReversible);
}
}
if (isNonReversible) {
// if (xo.hasChildNamed(ROOT_FREQ)) {
// cxo = xo.getChild(ROOT_FREQ);
// FrequencyModel rootFreq = (FrequencyModel) cxo.getChild(FrequencyModel.class);
//
// if (dataType != rootFreq.getDataType()) {
// throw new XMLParseException("Data type of " + getParserName() + " element does not match that of its rootFrequencyModel.");
// }
//
// Logger.getLogger("dr.evomodel").info(" Using BSSVS Complex Substitution Model");
// return new SVSComplexSubstitutionModel(getParserName(), dataType, freqModel, ratesParameter, indicatorParameter);
//
// } else {
// throw new XMLParseException("Non-reversible model missing " + ROOT_FREQ + " element");
// }
Logger.getLogger("dr.evomodel").info(" Using BSSVS Complex Substitution Model");
return new SVSComplexSubstitutionModel(getParserName(), dataType, freqModel, ratesParameter, indicatorParameter);
} else {
Logger.getLogger("dr.evomodel").info(" Using BSSVS General Substitution Model");
return new SVSGeneralSubstitutionModel(getParserName(), dataType, freqModel, ratesParameter, indicatorParameter);
}
} else {
if (ratesParameter.getDimension() != reversibleRateCount - 1) {
throw new XMLParseException("Rates parameter in " + getParserName() + " element should have " + (reversibleRateCount - 1) + " dimensions. However parameter dimension is " + ratesParameter.getDimension());
}
int relativeTo = 0;
if (hasRelativeRates) {
relativeTo = cxo.getIntegerAttribute(RELATIVE_TO) - 1;
}
if (relativeTo < 0 || relativeTo >= reversibleRateCount) {
throw new XMLParseException(RELATIVE_TO + " must be 1 or greater");
} else {
int t = relativeTo;
int s = states - 1;
int row = 0;
while (t >= s) {
t -= s;
s -= 1;
row += 1;
}
int col = t + row + 1;
Logger.getLogger("dr.evomodel").info(" Rates relative to " + dataType.getCode(row) + "<->" + dataType.getCode(col));
}
if (ratesParameter == null) {
if (reversibleRateCount == 1) {
// simplest model for binary traits...
} else {
throw new XMLParseException("No rates parameter found in " + getParserName());
}
}
return new GeneralSubstitutionModel(getParserName(), dataType, freqModel, ratesParameter, relativeTo);
}
}
use of dr.inference.model.Parameter in project beast-mcmc by beast-dev.
the class MarkovModulatedHiddenClassRewardParser method parseXMLObject.
@Override
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
MarkovModulatedSubstitutionModel substitutionModel = (MarkovModulatedSubstitutionModel) xo.getChild(MarkovModulatedSubstitutionModel.class);
HiddenDataType hiddenDataType = (HiddenDataType) substitutionModel.getDataType();
int classNumber = xo.getIntegerAttribute(CLASS_NUMBER);
int hiddenClassCount = hiddenDataType.getHiddenClassCount();
if (classNumber < 1 || classNumber > hiddenClassCount) {
throw new XMLParseException("Invalid class number in " + xo.getId());
}
// Use zero-indexed number
classNumber--;
int stateCount = hiddenDataType.getStateCount() / hiddenClassCount;
// Construct reward parameter
Parameter parameter = new Parameter.Default(stateCount * hiddenClassCount, 0.0);
for (int i = 0; i < stateCount; ++i) {
parameter.setParameterValue(i + classNumber * stateCount, 1.0);
}
if (xo.hasAttribute(NAME)) {
parameter.setId((String) xo.getAttribute(NAME));
} else {
parameter.setId(substitutionModel.getId() + "_" + Integer.toString(classNumber + 1));
}
return parameter;
}
use of dr.inference.model.Parameter in project beast-mcmc by beast-dev.
the class MarkovModulatedSubstitutionModelParser method parseXMLObject.
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
DataType dataType = DataTypeUtils.getDataType(xo);
if (!(dataType instanceof HiddenDataType)) {
throw new XMLParseException("Must construct " + MARKOV_MODULATED_MODEL + " with hidden data types");
}
Parameter switchingRates = (Parameter) xo.getElementFirstChild(SWITCHING_RATES);
List<SubstitutionModel> substModels = new ArrayList<SubstitutionModel>();
for (int i = 0; i < xo.getChildCount(); i++) {
Object cxo = xo.getChild(i);
if (cxo instanceof SubstitutionModel) {
substModels.add((SubstitutionModel) cxo);
}
}
boolean geometricRates = xo.getAttribute(GEOMETRIC_RATES, false);
Parameter rateScalar = xo.hasChildNamed(RATE_SCALAR) ? (Parameter) xo.getChild(RATE_SCALAR).getChild(Parameter.class) : null;
SiteRateModel siteRateModel = (SiteRateModel) xo.getChild(SiteRateModel.class);
if (siteRateModel != null) {
if (siteRateModel.getCategoryCount() != substModels.size() && substModels.size() % siteRateModel.getCategoryCount() != 0) {
throw new XMLParseException("Number of gamma categories must equal number of substitution models in " + xo.getId());
}
}
MarkovModulatedSubstitutionModel mmsm = new MarkovModulatedSubstitutionModel(xo.getId(), substModels, switchingRates, dataType, null, rateScalar, geometricRates, siteRateModel);
if (xo.getAttribute(RENORMALIZE, false)) {
mmsm.setNormalization(true);
}
return mmsm;
}
Aggregations