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(LEWIS_MK_MODEL, dataType, freqModel, ratesParameter, -1);
}
use of dr.evolution.datatype.DataType in project beast-mcmc by beast-dev.
the class MarkovModulatedGY94CodonModelParser method parseXMLObject.
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
DataType dataType = DataTypeUtils.getDataType(xo);
HiddenCodons codons;
if (dataType instanceof HiddenCodons)
codons = (HiddenCodons) dataType;
else
throw new XMLParseException("Must construct " + MARKOV_MODULATED_YANG_MODEL + " with hidden codons");
Parameter omegaParam = (Parameter) xo.getElementFirstChild(OMEGA);
Parameter kappaParam = (Parameter) xo.getElementFirstChild(KAPPA);
Parameter switchingParam = (Parameter) xo.getElementFirstChild(SWITCHING_RATES);
FrequencyModel freqModel = (FrequencyModel) xo.getChild(FrequencyModel.class);
EigenSystem eigenSystem;
if (xo.getAttribute(DIAGONALIZATION, "default").compareToIgnoreCase("colt") == 0)
eigenSystem = new ColtEigenSystem(dataType.getStateCount());
else
eigenSystem = new DefaultEigenSystem(dataType.getStateCount());
return new MarkovModulatedGY94CodonModel(codons, switchingParam, omegaParam, kappaParam, freqModel, eigenSystem);
}
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) {
PatternList patternList = (PatternList) obj;
if (xo.getAttribute(COMPRESS, false) && (patternList.getDataType() instanceof HiddenDataType)) {
double[] hiddenFrequencies = patternList.getStateFrequencies();
int hiddenCount = ((HiddenDataType) patternList.getDataType()).getHiddenClassCount();
int baseStateCount = hiddenFrequencies.length / hiddenCount;
frequencies = new double[baseStateCount];
for (int j = 0; j < baseStateCount; ++j) {
for (int k = 0; k < hiddenCount; ++k) {
frequencies[j] += hiddenFrequencies[j + k * baseStateCount];
}
}
} else {
// TODO
if (xo.hasAttribute(COMPOSITION)) {
String type = xo.getStringAttribute(COMPOSITION);
if (type.equalsIgnoreCase(FREQ_3x4)) {
frequencies = getEmpirical3x4Freqs(patternList);
}
} else {
frequencies = patternList.getStateFrequencies();
}
// END: composition check
}
break;
}
// END: patternList check
}
StringBuilder sb = new StringBuilder("\nCreating 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("= {");
if (xo.getAttribute(NORMALIZE, false)) {
double sum = 0;
for (int j = 0; j < freqsParam.getDimension(); j++) sum += freqsParam.getParameterValue(j);
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());
}
}
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 OldGLMSubstitutionModelParser method parseXMLObject.
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
DataType dataType = DataTypeUtils.getDataType(xo);
if (dataType == null)
dataType = (DataType) xo.getChild(DataType.class);
int rateCount = (dataType.getStateCount() - 1) * dataType.getStateCount();
LogLinearModel glm = (LogLinearModel) xo.getChild(GeneralizedLinearModel.class);
int length = glm.getXBeta().length;
if (length != rateCount) {
throw new XMLParseException("Rates parameter in " + getParserName() + " element should have " + (rateCount) + " dimensions. However GLM dimension is " + length);
}
XMLObject cxo = xo.getChild(dr.oldevomodelxml.substmodel.ComplexSubstitutionModelParser.ROOT_FREQUENCIES);
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.");
}
return new OldGLMSubstitutionModel(xo.getId(), dataType, rootFreq, glm);
}
use of dr.evolution.datatype.DataType in project beast-mcmc by beast-dev.
the class GLMSubstitutionModelParser method parseXMLObject.
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
DataType dataType = DataTypeUtils.getDataType(xo);
if (dataType == null)
dataType = (DataType) xo.getChild(DataType.class);
int rateCount = (dataType.getStateCount() - 1) * dataType.getStateCount();
LogLinearModel glm = (LogLinearModel) xo.getChild(GeneralizedLinearModel.class);
int length = glm.getXBeta().length;
if (length != rateCount) {
throw new XMLParseException("Rates parameter in " + getParserName() + " element should have " + (rateCount) + " dimensions. However GLM dimension is " + length);
}
XMLObject cxo = xo.getChild(ComplexSubstitutionModelParser.ROOT_FREQUENCIES);
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.");
}
return new GLMSubstitutionModel(xo.getId(), dataType, rootFreq, glm);
}
Aggregations