use of dr.evomodel.substmodel.FrequencyModel 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.evomodel.substmodel.FrequencyModel 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.evomodel.substmodel.FrequencyModel in project beast-mcmc by beast-dev.
the class MutationDeathModelParser method parseXMLObject.
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
Parameter dummyFreqParameter;
Parameter delParam = (Parameter) xo.getChild(Parameter.class);
Logger.getLogger("dr.evomodel").info("Creating MutationDeath substitution model.\n\tInitial death rate is " + delParam.getParameterValue(0));
MutationDeathType dT = (MutationDeathType) xo.getChild(MutationDeathType.class);
SubstitutionModel evoModel = (SubstitutionModel) xo.getChild(SubstitutionModel.class);
if (evoModel == null) {
// Assuming pure survival model
Logger.getLogger("dr.evomodel").info("\tSubstitutionModel not provided assuming pure death/survival model.");
dummyFreqParameter = new Parameter.Default(new double[] { 1.0, 0.0 });
} else {
dummyFreqParameter = new Parameter.Default(dT.getStateCount());
double[] freqs = evoModel.getFrequencyModel().getFrequencies();
for (int i = 0; i < freqs.length; ++i) {
dummyFreqParameter.setParameterValueQuietly(i, freqs[i]);
}
dummyFreqParameter.setParameterValueQuietly(dT.getStateCount() - 1, 0.0);
}
FrequencyModel dummyFrequencies = new FrequencyModel(dT, dummyFreqParameter);
Parameter mutationRate;
if (xo.hasChildNamed(MUTATION_RATE)) {
mutationRate = (Parameter) xo.getElementFirstChild(MUTATION_RATE);
} else {
mutationRate = new Parameter.Default(new double[] { 1.0 });
}
Logger.getLogger("dr.evomodel").info("\tInitial mutation rate is " + mutationRate.getParameterValue(0));
return new MutationDeathModel(delParam, dT, evoModel, dummyFrequencies, mutationRate);
}
use of dr.evomodel.substmodel.FrequencyModel in project beast-mcmc by beast-dev.
the class PCACodonModelParser method createNewFreqModel.
// read frequencies from XML and return FrequencyModel object
private FrequencyModel createNewFreqModel(DataType codons, AbstractPCARateMatrix type) throws XMLParseException {
double[] freqs = type.getFrequencies();
double sum = 0;
for (int j = 0; j < freqs.length; j++) {
sum += freqs[j];
}
if (Math.abs(sum - 1.0) > 1e-8) {
throw new XMLParseException("Frequencies do not sum to 1 (they sum to " + sum + ")");
}
FrequencyModel fm = new FrequencyModel(codons, freqs);
Logger.getLogger("dr.evomodel").info("Using frequencies from data file");
return fm;
}
use of dr.evomodel.substmodel.FrequencyModel in project beast-mcmc by beast-dev.
the class MG94CodonModelParser 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 alphaParam = (Parameter) xo.getElementFirstChild(ALPHA);
Parameter betaParam = (Parameter) xo.getElementFirstChild(BETA);
FrequencyModel freqModel = (FrequencyModel) xo.getChild(FrequencyModel.class);
MG94CodonModel codonModel;
if (xo.hasChildNamed(GTR_MODEL)) {
//TODO: change this into constructing a MG94CodonModel (needs to be written), which is started underneath
codonModel = new MG94CodonModel(codons, alphaParam, betaParam, freqModel);
Parameter rateACValue = null;
if (xo.hasChildNamed(A_TO_C)) {
rateACValue = (Parameter) xo.getElementFirstChild(A_TO_C);
}
Parameter rateAGValue = null;
if (xo.hasChildNamed(A_TO_G)) {
rateAGValue = (Parameter) xo.getElementFirstChild(A_TO_G);
}
Parameter rateATValue = null;
if (xo.hasChildNamed(A_TO_T)) {
rateATValue = (Parameter) xo.getElementFirstChild(A_TO_T);
}
Parameter rateCGValue = null;
if (xo.hasChildNamed(C_TO_G)) {
rateCGValue = (Parameter) xo.getElementFirstChild(C_TO_G);
}
Parameter rateCTValue = null;
if (xo.hasChildNamed(C_TO_T)) {
rateCTValue = (Parameter) xo.getElementFirstChild(C_TO_T);
}
Parameter rateGTValue = null;
if (xo.hasChildNamed(G_TO_T)) {
rateGTValue = (Parameter) xo.getElementFirstChild(G_TO_T);
}
int countNull = 0;
if (rateACValue == null)
countNull++;
if (rateAGValue == null)
countNull++;
if (rateATValue == null)
countNull++;
if (rateCGValue == null)
countNull++;
if (rateCTValue == null)
countNull++;
if (rateGTValue == null)
countNull++;
if (countNull != 1)
throw new XMLParseException("Only five parameters may be specified in GTR, leave exactly one out, the others will be specifed relative to the one left out.");
if (xo.hasAttribute(KAPPA)) {
System.err.print("using GTR rates -- overrides KAPPA");
}
} else if (xo.hasChildNamed(KAPPA)) {
Parameter kappaParam = (Parameter) xo.getElementFirstChild(KAPPA);
codonModel = new MG94HKYCodonModel(codons, alphaParam, betaParam, kappaParam, freqModel);
// System.err.println("setting up MG94HKYCodonModel");
} else {
//resort to standard MG94 without nucleotide rate bias
codonModel = new MG94CodonModel(codons, alphaParam, betaParam, freqModel);
}
if (!xo.getAttribute(NORMALIZED, true)) {
// codonModel.setNormalization(false);
// Logger.getLogger("dr.app.beagle.evomodel").info("MG94CodonModel normalization: false");
}
return codonModel;
}
Aggregations