Search in sources :

Example 16 with FrequencyModel

use of dr.evomodel.substmodel.FrequencyModel in project beast-mcmc by beast-dev.

the class GY94CodonModelParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    Codons codons = Codons.UNIVERSAL;
    if (xo.hasAttribute(GeneticCode.GENETIC_CODE)) {
        String codeStr = xo.getStringAttribute(GeneticCode.GENETIC_CODE);
        codons = Codons.findByName(codeStr);
    }
    Parameter omegaParameter = (Parameter) xo.getElementFirstChild(OMEGA);
    int dim = omegaParameter.getDimension();
    double value = omegaParameter.getParameterValue(dim - 1);
    if (value < 0) {
        throw new RuntimeException("Negative Omega parameter value " + value);
    }
    //END: negative check
    Parameter kappaParameter = (Parameter) xo.getElementFirstChild(KAPPA);
    dim = kappaParameter.getDimension();
    value = kappaParameter.getParameterValue(dim - 1);
    if (value < 0) {
        throw new RuntimeException("Negative kappa parameter value value " + value);
    }
    //END: negative check
    FrequencyModel freqModel = (FrequencyModel) xo.getChild(FrequencyModel.class);
    GY94CodonModel codonModel = new GY94CodonModel(codons, omegaParameter, kappaParameter, freqModel);
    return codonModel;
}
Also used : FrequencyModel(dr.evomodel.substmodel.FrequencyModel) Parameter(dr.inference.model.Parameter) GY94CodonModel(dr.evomodel.substmodel.codon.GY94CodonModel) Codons(dr.evolution.datatype.Codons)

Example 17 with FrequencyModel

use of dr.evomodel.substmodel.FrequencyModel 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) {
            //                if (xo.hasChildNamed(ROOT_FREQ)) {
            //                    cxo = xo.getChild(ROOT_FREQ);
            //                    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.");
            //                    }
            //
            //                    Logger.getLogger("dr.evomodel").info("  Using BSSVS Complex Substitution Model");
            //                    return new SVSComplexSubstitutionModel(getParserName(), dataType, freqModel, ratesParameter, indicatorParameter);
            //
            //                } else {
            //                    throw new XMLParseException("Non-reversible model missing " + ROOT_FREQ + " element");
            //                }
            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(getParserName(), 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(getParserName(), dataType, freqModel, ratesParameter, relativeTo);
    }
}
Also used : FrequencyModel(dr.evomodel.substmodel.FrequencyModel) SVSComplexSubstitutionModel(dr.evomodel.substmodel.SVSComplexSubstitutionModel) SVSGeneralSubstitutionModel(dr.evomodel.substmodel.SVSGeneralSubstitutionModel) Parameter(dr.inference.model.Parameter) DataType(dr.evolution.datatype.DataType) SVSGeneralSubstitutionModel(dr.evomodel.substmodel.SVSGeneralSubstitutionModel) GeneralSubstitutionModel(dr.evomodel.substmodel.GeneralSubstitutionModel)

Example 18 with FrequencyModel

use of dr.evomodel.substmodel.FrequencyModel in project beast-mcmc by beast-dev.

the class MutationDeathModelParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    Parameter dummyFreqParameter;
    Parameter delParam = (Parameter) xo.getChild(Parameter.class);
    Logger.getLogger("dr.evomodel").info("Creating MutationDeath substitution model.\n\tInitial death rate is " + delParam.getParameterValue(0));
    MutationDeathType dT = (MutationDeathType) xo.getChild(MutationDeathType.class);
    SubstitutionModel evoModel = (SubstitutionModel) xo.getChild(SubstitutionModel.class);
    if (evoModel == null) {
        // Assuming pure survival model
        Logger.getLogger("dr.evomodel").info("\tSubstitutionModel not provided assuming pure death/survival model.");
        dummyFreqParameter = new Parameter.Default(new double[] { 1.0, 0.0 });
    } else {
        dummyFreqParameter = new Parameter.Default(dT.getStateCount());
        double[] freqs = evoModel.getFrequencyModel().getFrequencies();
        for (int i = 0; i < freqs.length; ++i) {
            dummyFreqParameter.setParameterValueQuietly(i, freqs[i]);
        }
        dummyFreqParameter.setParameterValueQuietly(dT.getStateCount() - 1, 0.0);
    }
    FrequencyModel dummyFrequencies = new FrequencyModel(dT, dummyFreqParameter);
    Parameter mutationRate;
    if (xo.hasChildNamed(MUTATION_RATE)) {
        mutationRate = (Parameter) xo.getElementFirstChild(MUTATION_RATE);
    } else {
        mutationRate = new Parameter.Default(new double[] { 1.0 });
    }
    Logger.getLogger("dr.evomodel").info("\tInitial mutation rate is " + mutationRate.getParameterValue(0));
    return new MutationDeathModel(delParam, dT, evoModel, dummyFrequencies, mutationRate);
}
Also used : FrequencyModel(dr.evomodel.substmodel.FrequencyModel) Parameter(dr.inference.model.Parameter) MutationDeathModel(dr.evomodel.substmodel.MutationDeathModel) MutationDeathType(dr.evolution.datatype.MutationDeathType) SubstitutionModel(dr.evomodel.substmodel.SubstitutionModel)

Example 19 with FrequencyModel

use of dr.evomodel.substmodel.FrequencyModel in project beast-mcmc by beast-dev.

the class PCACodonModelParser method createNewFreqModel.

// read frequencies from XML and return FrequencyModel object
private FrequencyModel createNewFreqModel(DataType codons, AbstractPCARateMatrix type) throws XMLParseException {
    double[] freqs = type.getFrequencies();
    double sum = 0;
    for (int j = 0; j < freqs.length; j++) {
        sum += freqs[j];
    }
    if (Math.abs(sum - 1.0) > 1e-8) {
        throw new XMLParseException("Frequencies do not sum to 1 (they sum to " + sum + ")");
    }
    FrequencyModel fm = new FrequencyModel(codons, freqs);
    Logger.getLogger("dr.evomodel").info("Using frequencies from data file");
    return fm;
}
Also used : FrequencyModel(dr.evomodel.substmodel.FrequencyModel)

Example 20 with FrequencyModel

use of dr.evomodel.substmodel.FrequencyModel in project beast-mcmc by beast-dev.

the class MG94CodonModelParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    Codons codons = Codons.UNIVERSAL;
    if (xo.hasAttribute(GeneticCode.GENETIC_CODE)) {
        String codeStr = xo.getStringAttribute(GeneticCode.GENETIC_CODE);
        codons = Codons.findByName(codeStr);
    }
    Parameter alphaParam = (Parameter) xo.getElementFirstChild(ALPHA);
    Parameter betaParam = (Parameter) xo.getElementFirstChild(BETA);
    FrequencyModel freqModel = (FrequencyModel) xo.getChild(FrequencyModel.class);
    MG94CodonModel codonModel;
    if (xo.hasChildNamed(GTR_MODEL)) {
        //TODO: change this into constructing a MG94CodonModel (needs to be written), which is started underneath
        codonModel = new MG94CodonModel(codons, alphaParam, betaParam, freqModel);
        Parameter rateACValue = null;
        if (xo.hasChildNamed(A_TO_C)) {
            rateACValue = (Parameter) xo.getElementFirstChild(A_TO_C);
        }
        Parameter rateAGValue = null;
        if (xo.hasChildNamed(A_TO_G)) {
            rateAGValue = (Parameter) xo.getElementFirstChild(A_TO_G);
        }
        Parameter rateATValue = null;
        if (xo.hasChildNamed(A_TO_T)) {
            rateATValue = (Parameter) xo.getElementFirstChild(A_TO_T);
        }
        Parameter rateCGValue = null;
        if (xo.hasChildNamed(C_TO_G)) {
            rateCGValue = (Parameter) xo.getElementFirstChild(C_TO_G);
        }
        Parameter rateCTValue = null;
        if (xo.hasChildNamed(C_TO_T)) {
            rateCTValue = (Parameter) xo.getElementFirstChild(C_TO_T);
        }
        Parameter rateGTValue = null;
        if (xo.hasChildNamed(G_TO_T)) {
            rateGTValue = (Parameter) xo.getElementFirstChild(G_TO_T);
        }
        int countNull = 0;
        if (rateACValue == null)
            countNull++;
        if (rateAGValue == null)
            countNull++;
        if (rateATValue == null)
            countNull++;
        if (rateCGValue == null)
            countNull++;
        if (rateCTValue == null)
            countNull++;
        if (rateGTValue == null)
            countNull++;
        if (countNull != 1)
            throw new XMLParseException("Only five parameters may be specified in GTR, leave exactly one out, the others will be specifed relative to the one left out.");
        if (xo.hasAttribute(KAPPA)) {
            System.err.print("using GTR rates -- overrides KAPPA");
        }
    } else if (xo.hasChildNamed(KAPPA)) {
        Parameter kappaParam = (Parameter) xo.getElementFirstChild(KAPPA);
        codonModel = new MG94HKYCodonModel(codons, alphaParam, betaParam, kappaParam, freqModel);
    //            System.err.println("setting up MG94HKYCodonModel");
    } else {
        //resort to standard MG94 without nucleotide rate bias
        codonModel = new MG94CodonModel(codons, alphaParam, betaParam, freqModel);
    }
    if (!xo.getAttribute(NORMALIZED, true)) {
    //            codonModel.setNormalization(false);
    //            Logger.getLogger("dr.app.beagle.evomodel").info("MG94CodonModel normalization: false");
    }
    return codonModel;
}
Also used : FrequencyModel(dr.evomodel.substmodel.FrequencyModel) MG94CodonModel(dr.evomodel.substmodel.codon.MG94CodonModel) Parameter(dr.inference.model.Parameter) Codons(dr.evolution.datatype.Codons) MG94HKYCodonModel(dr.evomodel.substmodel.codon.MG94HKYCodonModel)

Aggregations

FrequencyModel (dr.evomodel.substmodel.FrequencyModel)57 Parameter (dr.inference.model.Parameter)42 GammaSiteRateModel (dr.evomodel.siteratemodel.GammaSiteRateModel)23 BranchRateModel (dr.evomodel.branchratemodel.BranchRateModel)22 HKY (dr.evomodel.substmodel.nucleotide.HKY)21 HomogeneousBranchModel (dr.evomodel.branchmodel.HomogeneousBranchModel)19 TreeModel (dr.evomodel.tree.TreeModel)19 DefaultBranchRateModel (dr.evomodel.branchratemodel.DefaultBranchRateModel)17 ArrayList (java.util.ArrayList)14 BranchModel (dr.evomodel.branchmodel.BranchModel)12 Partition (dr.app.beagle.tools.Partition)11 SubstitutionModel (dr.evomodel.substmodel.SubstitutionModel)11 BeagleSequenceSimulator (dr.app.beagle.tools.BeagleSequenceSimulator)10 DataType (dr.evolution.datatype.DataType)10 NewickImporter (dr.evolution.io.NewickImporter)9 Tree (dr.evolution.tree.Tree)9 Vector (dr.math.matrixAlgebra.Vector)9 PatternList (dr.evolution.alignment.PatternList)7 ImportException (dr.evolution.io.Importer.ImportException)7 BeagleTreeLikelihood (dr.evomodel.treelikelihood.BeagleTreeLikelihood)7