use of dr.oldevomodel.substmodel.GeneralSubstitutionModel in project beast-mcmc by beast-dev.
the class BinarySubstitutionModelParser method parseXMLObject.
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
Parameter ratesParameter;
XMLObject cxo = xo.getChild(GeneralSubstitutionModelParser.FREQUENCIES);
FrequencyModel freqModel = (FrequencyModel) cxo.getChild(FrequencyModel.class);
DataType dataType = freqModel.getDataType();
if (dataType != TwoStates.INSTANCE)
throw new XMLParseException("Frequency model must have binary (two state) data type.");
int relativeTo = 0;
ratesParameter = new Parameter.Default(0);
return new GeneralSubstitutionModel(dataType, freqModel, ratesParameter, relativeTo);
}
use of dr.oldevomodel.substmodel.GeneralSubstitutionModel in project beast-mcmc by beast-dev.
the class LewisMkSubstitutionModelParser method parseXMLObject.
// public static XMLObjectParser PARSER=new LewisMkSubstitutionModelParser();
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
XMLObject cxo = xo.getChild(FREQUENCIES);
FrequencyModel freqModel = (FrequencyModel) cxo.getChild(FrequencyModel.class);
DataType dataType = freqModel.getDataType();
int k = dataType.getStateCount();
System.err.println("Number of states " + k);
Parameter ratesParameter;
if (xo.hasAttribute(TOTAL_ORDER) && xo.getBooleanAttribute(TOTAL_ORDER)) {
// TOTAL ORDERING OF THE STATES BASED ON DATATYPE
ratesParameter = new Parameter.Default(k * (k - 1) / 2, 0);
int j = k - 1;
for (int i = 0; i < (k - 1) * k / 2; i = i + j + 1) {
ratesParameter.setParameterValue(i, 1);
j -= 1;
}
} else if (xo.hasChildNamed(ORDER)) {
// USER-SPECIFIED ORDERING OF THE STATES
ratesParameter = new Parameter.Default(k * (k - 1) / 2, 0);
for (int i = 0; i < xo.getChildCount(); ++i) {
if (xo.getChildName(i).equals(ORDER)) {
cxo = (XMLObject) xo.getChild(i);
if (cxo.getName().equals(ORDER)) {
int from = dataType.getState(cxo.getStringAttribute(STATE).charAt(0));
int to = dataType.getState(cxo.getStringAttribute(ADJACENT).charAt(0));
if (from > to) {
// SWAP: from should have the smaller state number
to += from;
from = to - from;
to -= from;
}
int ratesIndex = (from * (2 * k - 3) - from * from) / 2 + to - 1;
ratesParameter.setParameterValue(ratesIndex, 1);
}
}
}
} else {
ratesParameter = new Parameter.Default(k * (k - 1) / 2, 1);
}
System.err.println(ratesParameter.toString());
System.err.println("Infinitesimal matrix:");
for (int i = 0; i < k; ++i) {
for (int j = 0; j < k; ++j) {
int from, to;
if (i < j) {
from = i;
to = j;
} else {
from = j;
to = i;
}
// This is right now!!! Thanks, Marc!
int ratesIndex = (from * (2 * k - 3) - from * from) / 2 + to - 1;
if (i != j)
System.err.print(Double.toString(ratesParameter.getValue(ratesIndex)) + "\t(" + ratesIndex + ")\t");
else
System.err.print("-\t\t");
}
// newline
System.err.println("");
}
System.err.println("");
if (!checkConnected(ratesParameter.getValues(), k)) {
throw (new XMLParseException("The state transitions form a disconnected graph! This model is not suited for this case."));
}
return new GeneralSubstitutionModel(dataType, freqModel, ratesParameter, 0);
}
use of dr.oldevomodel.substmodel.GeneralSubstitutionModel in project beast-mcmc by beast-dev.
the class MkModelParser method parseXMLObject.
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
XMLObject cxo = xo.getChild(GeneralSubstitutionModelParser.FREQUENCIES);
FrequencyModel freqModel = (FrequencyModel) cxo.getChild(FrequencyModel.class);
DataType dataType = freqModel.getDataType();
int rateCount = ((dataType.getStateCount() - 1) * dataType.getStateCount()) / 2 - 1;
Parameter ratesParameter = new Parameter.Default(rateCount, 1.0);
Logger.getLogger("dr.evolution").info("Creating an Mk substitution model with data type: " + dataType.getType() + "on " + dataType.getStateCount() + " states.");
int relativeTo = 1;
return new GeneralSubstitutionModel(dataType, freqModel, ratesParameter, relativeTo);
}
use of dr.oldevomodel.substmodel.GeneralSubstitutionModel in project beast-mcmc by beast-dev.
the class BinaryCovarionModelTest method testTransitionProbabilitiesUnequalBaseFreqsUnequalRateFreqsEqualRates.
public void testTransitionProbabilitiesUnequalBaseFreqsUnequalRateFreqsEqualRates() {
// with alpha == 1, the transition probability should be the same as binary jukes cantor
alpha.setParameterValue(0, 1.0);
switchingRate.setParameterValue(0, 1.0);
frequencies.setParameterValue(0, 0.25);
frequencies.setParameterValue(1, 0.75);
hiddenFrequencies.setParameterValue(0, 0.1);
hiddenFrequencies.setParameterValue(1, 0.9);
FrequencyModel freqModel = new FrequencyModel(TwoStates.INSTANCE, frequencies);
GeneralSubstitutionModel modelToCompare = new GeneralSubstitutionModel(TwoStates.INSTANCE, freqModel, null, 0);
model.setupMatrix();
double[] matrix = new double[16];
double[] m = new double[4];
double[] pi = model.getFrequencyModel().getFrequencies();
for (double distance = 0.01; distance <= 1.005; distance += 0.01) {
model.getTransitionProbabilities(distance, matrix);
modelToCompare.getTransitionProbabilities(distance, m);
double pChange = (matrix[1] + matrix[3]) * pi[0] + (matrix[4] + matrix[6]) * pi[1] + (matrix[9] + matrix[11]) * pi[2] + (matrix[12] + matrix[14]) * pi[3];
double pChange2 = m[1] * frequencies.getParameterValue(0) + m[2] * frequencies.getParameterValue(1);
// System.out.println(distance + "\t" + pChange2 + "\t" + pChange);
assertEquals(pChange2, pChange, 1e-14);
}
}
use of dr.oldevomodel.substmodel.GeneralSubstitutionModel in project beast-mcmc by beast-dev.
the class BinaryCovarionModelTest method testTransitionProbabilitiesUnequalBaseFreqsEqualRates.
public void testTransitionProbabilitiesUnequalBaseFreqsEqualRates() {
// with alpha == 1, the transition probability should be the same as binary jukes cantor
alpha.setParameterValue(0, 1.0);
switchingRate.setParameterValue(0, 1.0);
frequencies.setParameterValue(0, 0.25);
frequencies.setParameterValue(1, 0.75);
FrequencyModel freqModel = new FrequencyModel(TwoStates.INSTANCE, frequencies);
GeneralSubstitutionModel modelToCompare = new GeneralSubstitutionModel(TwoStates.INSTANCE, freqModel, null, 0);
model.setupMatrix();
double[] matrix = new double[16];
double[] m = new double[4];
double[] pi = model.getFrequencyModel().getFrequencies();
for (double distance = 0.01; distance <= 1.005; distance += 0.01) {
model.getTransitionProbabilities(distance, matrix);
modelToCompare.getTransitionProbabilities(distance, m);
double pChange = (matrix[1] + matrix[3]) * pi[0] + (matrix[4] + matrix[6]) * pi[1] + (matrix[9] + matrix[11]) * pi[2] + (matrix[12] + matrix[14]) * pi[3];
double pChange2 = m[1] * frequencies.getParameterValue(0) + m[2] * frequencies.getParameterValue(1);
System.out.println(distance + "\t" + pChange2 + "\t" + pChange);
assertEquals(pChange2, pChange, 1e-14);
}
}
Aggregations