Search in sources :

Example 1 with ComplexSubstitutionModel

use of dr.oldevomodel.substmodel.ComplexSubstitutionModel in project beast-mcmc by beast-dev.

the class ComplexSubstitutionModelParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    DataType dataType = DataTypeUtils.getDataType(xo);
    if (dataType == null)
        dataType = (DataType) xo.getChild(DataType.class);
    XMLObject cxo = xo.getChild(RATES);
    Parameter ratesParameter = (Parameter) cxo.getChild(Parameter.class);
    int rateCount = (dataType.getStateCount() - 1) * dataType.getStateCount();
    if (ratesParameter.getDimension() != rateCount) {
        throw new XMLParseException("Rates parameter in " + getParserName() + " element should have " + (rateCount) + " dimensions.  However parameter dimension is " + ratesParameter.getDimension());
    }
    cxo = xo.getChild(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.");
    }
    Parameter indicators = null;
    if (xo.hasChildNamed(INDICATOR)) {
        indicators = (Parameter) ((XMLObject) xo.getChild(INDICATOR)).getChild(Parameter.class);
        if (ratesParameter.getDimension() != indicators.getDimension())
            throw new XMLParseException("Rate parameter dimension must match indicator parameter dimension");
    }
    StringBuffer sb = new StringBuffer().append("Constructing a complex substitution model using\n").append("\tRate parameters: ").append(ratesParameter.getId()).append("\n").append("\tRoot frequency model: ").append(rootFreq.getId()).append("\n");
    ComplexSubstitutionModel model;
    if (indicators == null)
        model = new ComplexSubstitutionModel(xo.getId(), dataType, rootFreq, ratesParameter);
    else {
        boolean randomize = xo.getAttribute(RANDOMIZE, false);
        boolean connected = xo.getAttribute(CONNECTED, false);
        model = new SVSComplexSubstitutionModel(xo.getId(), dataType, rootFreq, ratesParameter, indicators);
        if (randomize) {
            BayesianStochasticSearchVariableSelection.Utils.randomize(indicators, dataType.getStateCount(), false);
            boolean valid = !Double.isInfinite(model.getLogLikelihood());
            if (!valid) {
                throw new XMLParseException("Poor tolerance in complex substitution model.  Please retry analysis using BEAGLE");
            }
        }
        sb.append("\tBSSVS indicators: ").append(indicators.getId()).append("\n");
        sb.append("\tGraph must be connected: ").append(connected).append("\n");
    }
    boolean doNormalization = xo.getAttribute(NORMALIZATION, true);
    model.setNormalization(doNormalization);
    sb.append("\tNormalized: ").append(doNormalization).append("\n");
    boolean checkConditioning = xo.getAttribute(CHECK_CONDITIONING, true);
    model.setCheckConditioning(checkConditioning);
    if (checkConditioning) {
        double maxConditionNumber = xo.getAttribute(MAX_CONDITION_NUMBER, 1000);
        model.setMaxConditionNumber(maxConditionNumber);
        sb.append("\tMax. condition number: ").append(maxConditionNumber).append("\n");
    }
    int maxIterations = xo.getAttribute(MAX_ITERATIONS, 1000);
    model.setMaxIterations(maxIterations);
    sb.append("\tMax iterations: ").append(maxIterations).append("\n");
    sb.append("\t\tPlease cite Edwards, Suchard et al. (2011)\n");
    Logger.getLogger("dr.evomodel.substmodel").info(sb.toString());
    return model;
}
Also used : FrequencyModel(dr.oldevomodel.substmodel.FrequencyModel) SVSComplexSubstitutionModel(dr.oldevomodel.substmodel.SVSComplexSubstitutionModel) SVSComplexSubstitutionModel(dr.oldevomodel.substmodel.SVSComplexSubstitutionModel) ComplexSubstitutionModel(dr.oldevomodel.substmodel.ComplexSubstitutionModel) DataType(dr.evolution.datatype.DataType) Parameter(dr.inference.model.Parameter)

Example 2 with ComplexSubstitutionModel

use of dr.oldevomodel.substmodel.ComplexSubstitutionModel 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)

Aggregations

DataType (dr.evolution.datatype.DataType)2 Parameter (dr.inference.model.Parameter)2 ComplexSubstitutionModel (dr.oldevomodel.substmodel.ComplexSubstitutionModel)2 FrequencyModel (dr.oldevomodel.substmodel.FrequencyModel)2 SVSComplexSubstitutionModel (dr.oldevomodel.substmodel.SVSComplexSubstitutionModel)2