Search in sources :

Example 56 with DataType

use of dr.evolution.datatype.DataType 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) {
            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(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(dataType, freqModel, ratesParameter, relativeTo);
    }
}
Also used : FrequencyModel(dr.oldevomodel.substmodel.FrequencyModel) SVSComplexSubstitutionModel(dr.oldevomodel.substmodel.SVSComplexSubstitutionModel) SVSGeneralSubstitutionModel(dr.oldevomodel.substmodel.SVSGeneralSubstitutionModel) Parameter(dr.inference.model.Parameter) DataType(dr.evolution.datatype.DataType) GeneralSubstitutionModel(dr.oldevomodel.substmodel.GeneralSubstitutionModel) SVSGeneralSubstitutionModel(dr.oldevomodel.substmodel.SVSGeneralSubstitutionModel)

Example 57 with DataType

use of dr.evolution.datatype.DataType in project beast-mcmc by beast-dev.

the class TimeIrreversibleTest method testComplexSubstitutionModel.

private double[] testComplexSubstitutionModel(Original test, double[] rates) {
    System.out.println("\n*** Complex Substitution Model Test: " + test + " ***");
    Parameter ratesP = new Parameter.Default(rates);
    DataType dataType = test.getDataType();
    FrequencyModel freqModel = new FrequencyModel(dataType, new Parameter.Default(test.getFrequencies()));
    ComplexSubstitutionModel substModel = new ComplexSubstitutionModel("Complex Substitution Model Test", dataType, freqModel, ratesP);
    double logL = substModel.getLogLikelihood();
    System.out.println("Prior = " + logL);
    double[] finiteTimeProbs = null;
    if (!Double.isInfinite(logL)) {
        finiteTimeProbs = new double[substModel.getDataType().getStateCount() * substModel.getDataType().getStateCount()];
        substModel.getTransitionProbabilities(time, finiteTimeProbs);
        System.out.println("Probs = ");
        printRateMatrix(finiteTimeProbs, substModel.getDataType().getStateCount());
    }
    //            assertEquals(1, 1, 1e-10);
    return finiteTimeProbs;
}
Also used : FrequencyModel(dr.oldevomodel.substmodel.FrequencyModel) SVSComplexSubstitutionModel(dr.oldevomodel.substmodel.SVSComplexSubstitutionModel) ComplexSubstitutionModel(dr.oldevomodel.substmodel.ComplexSubstitutionModel) Parameter(dr.inference.model.Parameter) DataType(dr.evolution.datatype.DataType)

Example 58 with DataType

use of dr.evolution.datatype.DataType in project beast-mcmc by beast-dev.

the class TimeIrreversibleTest method testSVSComplexSubstitutionModel.

private double[] testSVSComplexSubstitutionModel(Original test, double[] rates) {
    System.out.println("\n*** SVS Complex Substitution Model Test: " + test + " ***");
    double[] indicators = test.getIndicators();
    Parameter ratesP = new Parameter.Default(rates);
    Parameter indicatorsP = new Parameter.Default(indicators);
    DataType dataType = test.getDataType();
    FrequencyModel freqModel = new FrequencyModel(dataType, new Parameter.Default(test.getFrequencies()));
    SVSComplexSubstitutionModel substModel = new SVSComplexSubstitutionModel("SVS Complex Substitution Model Test", dataType, freqModel, ratesP, indicatorsP);
    double logL = substModel.getLogLikelihood();
    System.out.println("Prior = " + logL);
    double[] finiteTimeProbs = null;
    if (!Double.isInfinite(logL)) {
        finiteTimeProbs = new double[substModel.getDataType().getStateCount() * substModel.getDataType().getStateCount()];
        substModel.getTransitionProbabilities(time, finiteTimeProbs);
        System.out.println("Probs = ");
        printRateMatrix(finiteTimeProbs, substModel.getDataType().getStateCount());
    }
    //            assertEquals(1, 1, 1e-10);
    return finiteTimeProbs;
}
Also used : FrequencyModel(dr.oldevomodel.substmodel.FrequencyModel) SVSComplexSubstitutionModel(dr.oldevomodel.substmodel.SVSComplexSubstitutionModel) Parameter(dr.inference.model.Parameter) DataType(dr.evolution.datatype.DataType)

Aggregations

DataType (dr.evolution.datatype.DataType)58 Parameter (dr.inference.model.Parameter)24 FrequencyModel (dr.oldevomodel.substmodel.FrequencyModel)11 FrequencyModel (dr.evomodel.substmodel.FrequencyModel)10 Sequence (dr.evolution.sequence.Sequence)6 GeneralDataType (dr.evolution.datatype.GeneralDataType)4 Taxon (dr.evolution.util.Taxon)4 SVSComplexSubstitutionModel (dr.oldevomodel.substmodel.SVSComplexSubstitutionModel)4 ArrayList (java.util.ArrayList)4 PartitionSubstitutionModel (dr.app.beauti.options.PartitionSubstitutionModel)3 PatternList (dr.evolution.alignment.PatternList)3 HiddenDataType (dr.evolution.datatype.HiddenDataType)3 NodeRef (dr.evolution.tree.NodeRef)3 Tree (dr.evolution.tree.Tree)3 TaxonList (dr.evolution.util.TaxonList)3 GeneralSubstitutionModel (dr.evomodel.substmodel.GeneralSubstitutionModel)3 GeneralSubstitutionModel (dr.oldevomodel.substmodel.GeneralSubstitutionModel)3 SimpleAlignment (dr.evolution.alignment.SimpleAlignment)2 Codons (dr.evolution.datatype.Codons)2 TreeTraitProvider (dr.evolution.tree.TreeTraitProvider)2