use of dr.evolution.datatype.DataType in project beast-mcmc by beast-dev.
the class FrequencyModelParser method parseXMLObject.
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
DataType dataType = DataTypeUtils.getDataType(xo);
Parameter freqsParam = (Parameter) xo.getElementFirstChild(FREQUENCIES);
double[] frequencies = null;
for (int i = 0; i < xo.getChildCount(); i++) {
Object obj = xo.getChild(i);
if (obj instanceof PatternList) {
frequencies = ((PatternList) obj).getStateFrequencies();
break;
}
}
StringBuilder sb = new StringBuilder("Creating state frequencies model '" + freqsParam.getParameterName() + "': ");
if (frequencies != null) {
if (freqsParam.getDimension() != frequencies.length) {
throw new XMLParseException("dimension of frequency parameter and number of sequence states don't match!");
}
for (int j = 0; j < frequencies.length; j++) {
freqsParam.setParameterValue(j, frequencies[j]);
}
sb.append("Using empirical frequencies from data ");
} else {
sb.append("Initial frequencies ");
}
sb.append("= {");
double sum = 0;
for (int j = 0; j < freqsParam.getDimension(); j++) {
sum += freqsParam.getParameterValue(j);
}
if (xo.getAttribute(NORMALIZE, false)) {
for (int j = 0; j < freqsParam.getDimension(); j++) {
if (sum != 0)
freqsParam.setParameterValue(j, freqsParam.getParameterValue(j) / sum);
else
freqsParam.setParameterValue(j, 1.0 / freqsParam.getDimension());
}
sum = 1.0;
}
if (Math.abs(sum - 1.0) > 1e-8) {
throw new XMLParseException("Frequencies do not sum to 1 (they sum to " + sum + ")");
}
NumberFormat format = NumberFormat.getNumberInstance();
format.setMaximumFractionDigits(5);
sb.append(format.format(freqsParam.getParameterValue(0)));
for (int j = 1; j < freqsParam.getDimension(); j++) {
sb.append(", ");
sb.append(format.format(freqsParam.getParameterValue(j)));
}
sb.append("}");
Logger.getLogger("dr.evomodel").info(sb.toString());
return new FrequencyModel(dataType, freqsParam);
}
use of dr.evolution.datatype.DataType in project beast-mcmc by beast-dev.
the class SubstitutionEpochModelParser method parseXMLObject.
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
DataType dataType = null;
FrequencyModel freqModel = null;
List<SubstitutionModel> modelList = new ArrayList<SubstitutionModel>();
XMLObject cxo = xo.getChild(MODELS);
for (int i = 0; i < cxo.getChildCount(); i++) {
SubstitutionModel model = (SubstitutionModel) cxo.getChild(i);
if (dataType == null) {
dataType = model.getDataType();
} else if (dataType != model.getDataType())
throw new XMLParseException("Substitution models across epoches must use the same data type.");
if (freqModel == null) {
freqModel = model.getFrequencyModel();
} else if (freqModel != model.getFrequencyModel())
throw new XMLParseException("Substitution models across epoches must currently use the same frequency model.\nHarass Marc to fix this.");
modelList.add(model);
}
Parameter transitionTimes = (Parameter) xo.getChild(Parameter.class);
if (transitionTimes.getDimension() != modelList.size() - 1) {
throw new XMLParseException("# of transition times must equal # of substitution models - 1\n" + transitionTimes.getDimension() + "\n" + modelList.size());
}
return new SubstitutionEpochModel(SUBSTITUTION_EPOCH_MODEL, modelList, transitionTimes, dataType, freqModel);
}
use of dr.evolution.datatype.DataType 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.evolution.datatype.DataType 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.evolution.datatype.DataType in project beast-mcmc by beast-dev.
the class TwoStateMarkovRewardsTest method testTwoStateRewards.
public void testTwoStateRewards() {
DataType dataType = TwoStates.INSTANCE;
FrequencyModel freqModel = new FrequencyModel(TwoStates.INSTANCE, new double[] { 0.5, 0.5 });
Parameter rates = new Parameter.Default(new double[] { 4.0, 6.0 });
ComplexSubstitutionModel twoStateModel = new ComplexSubstitutionModel("two", dataType, freqModel, rates) {
};
twoStateModel.setNormalization(false);
MarkovJumpsSubstitutionModel markovRewards = new MarkovJumpsSubstitutionModel(twoStateModel, MarkovJumpsType.REWARDS);
double[] r = new double[2];
double[] q = new double[4];
double[] c = new double[4];
int mark = 0;
double weight = 1.0;
r[mark] = weight;
markovRewards.setRegistration(r);
twoStateModel.getInfinitesimalMatrix(q);
System.out.println("Q = " + new Vector(q));
System.out.println("Reward for state 0");
double time = 1.0;
markovRewards.computeCondStatMarkovJumps(time, c);
System.out.println("Reward conditional on X(0) = i, X(t) = j: " + new Vector(c));
double endTime = 10.0;
int steps = 10;
for (time = 0.0; time < endTime; time += (endTime / steps)) {
markovRewards.computeCondStatMarkovJumps(time, c);
// start = 0, end = 0
System.out.println(time + "," + c[0]);
}
}
Aggregations