Search in sources :

Example 1 with GeneralSubstitutionModel

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);
}
Also used : FrequencyModel(dr.oldevomodel.substmodel.FrequencyModel) Parameter(dr.inference.model.Parameter) DataType(dr.evolution.datatype.DataType) GeneralSubstitutionModel(dr.oldevomodel.substmodel.GeneralSubstitutionModel)

Example 2 with GeneralSubstitutionModel

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);
}
Also used : FrequencyModel(dr.oldevomodel.substmodel.FrequencyModel) DataType(dr.evolution.datatype.DataType) Parameter(dr.inference.model.Parameter) GeneralSubstitutionModel(dr.oldevomodel.substmodel.GeneralSubstitutionModel)

Example 3 with GeneralSubstitutionModel

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);
}
Also used : FrequencyModel(dr.oldevomodel.substmodel.FrequencyModel) DataType(dr.evolution.datatype.DataType) Parameter(dr.inference.model.Parameter) GeneralSubstitutionModel(dr.oldevomodel.substmodel.GeneralSubstitutionModel)

Example 4 with GeneralSubstitutionModel

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);
    }
}
Also used : FrequencyModel(dr.oldevomodel.substmodel.FrequencyModel) GeneralSubstitutionModel(dr.oldevomodel.substmodel.GeneralSubstitutionModel)

Example 5 with GeneralSubstitutionModel

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);
    }
}
Also used : FrequencyModel(dr.oldevomodel.substmodel.FrequencyModel) GeneralSubstitutionModel(dr.oldevomodel.substmodel.GeneralSubstitutionModel)

Aggregations

FrequencyModel (dr.oldevomodel.substmodel.FrequencyModel)7 GeneralSubstitutionModel (dr.oldevomodel.substmodel.GeneralSubstitutionModel)7 Parameter (dr.inference.model.Parameter)5 DataType (dr.evolution.datatype.DataType)4 SitePatterns (dr.evolution.alignment.SitePatterns)1 ExchangeOperator (dr.evomodel.operators.ExchangeOperator)1 SubtreeSlideOperator (dr.evomodel.operators.SubtreeSlideOperator)1 WilsonBalding (dr.evomodel.operators.WilsonBalding)1 DefaultTreeModel (dr.evomodel.tree.DefaultTreeModel)1 ArrayLogFormatter (dr.inference.loggers.ArrayLogFormatter)1 MCLogger (dr.inference.loggers.MCLogger)1 TabDelimitedFormatter (dr.inference.loggers.TabDelimitedFormatter)1 MCMC (dr.inference.mcmc.MCMC)1 MCMCOptions (dr.inference.mcmc.MCMCOptions)1 ArrayTraceList (dr.inference.trace.ArrayTraceList)1 Trace (dr.inference.trace.Trace)1 TraceCorrelation (dr.inference.trace.TraceCorrelation)1 GammaSiteModel (dr.oldevomodel.sitemodel.GammaSiteModel)1 SVSComplexSubstitutionModel (dr.oldevomodel.substmodel.SVSComplexSubstitutionModel)1 SVSGeneralSubstitutionModel (dr.oldevomodel.substmodel.SVSGeneralSubstitutionModel)1