Search in sources :

Example 21 with SubstitutionModel

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

the class RandomBranchModel method setup.

// END: Constructor
private void setup() {
    DataType dataType = baseSubstitutionModel.getDataType();
    FrequencyModel freqModel = baseSubstitutionModel.getFrequencyModel();
    Parameter kappaParameter = new Parameter.Default("kappa", 1, baseSubstitutionModel.getKappa());
    substitutionModels = new LinkedList<SubstitutionModel>();
    branchAssignmentMap = new LinkedHashMap<NodeRef, Integer>();
    int branchClass = 0;
    for (NodeRef node : treeModel.getNodes()) {
        if (!treeModel.isRoot(node)) {
            double nodeHeight = treeModel.getNodeHeight(node);
            double parentHeight = treeModel.getNodeHeight(treeModel.getParent(node));
            double time = 0.5 * (parentHeight + nodeHeight);
            double baseOmega = baseSubstitutionModel.getOmega();
            double fixed = baseOmega * time;
            // Math.exp((random.nextGaussian() * stdev + mean));
            double epsilon = (Math.log(1 - random.nextDouble()) / (-rate));
            double value = fixed + epsilon;
            Parameter omegaParameter = new Parameter.Default("omega", 1, value);
            GY94CodonModel gy94 = new GY94CodonModel((Codons) dataType, omegaParameter, kappaParameter, freqModel);
            substitutionModels.add(gy94);
            branchAssignmentMap.put(node, branchClass);
            branchClass++;
        }
    // END: root check
    }
// END: nodes loop
}
Also used : FrequencyModel(dr.evomodel.substmodel.FrequencyModel) NodeRef(dr.evolution.tree.NodeRef) DataType(dr.evolution.datatype.DataType) Parameter(dr.inference.model.Parameter) GY94CodonModel(dr.evomodel.substmodel.codon.GY94CodonModel) SubstitutionModel(dr.evomodel.substmodel.SubstitutionModel)

Example 22 with SubstitutionModel

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

the class ArbitrarySubstitutionParameterBranchModelParser method parseXMLObject.

@Override
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    Logger.getLogger("dr.evomodel").info("\nUsing branch-specific substitution parameter branch model.");
    TreeModel tree = (TreeModel) xo.getChild(TreeModel.class);
    SubstitutionModel substitutionModel = (SubstitutionModel) xo.getChild(SubstitutionModel.class);
    if (!(substitutionModel instanceof ParameterReplaceableSubstitutionModel)) {
        throw new RuntimeException("The substitution model is not parameter replaceable!");
    }
    XMLObject cxo = xo.getChild(BRANCH_SPECIFIC_PARAMETER);
    // XMLObject dxo = xo.getChild(SINGLE_RATE);
    BranchSpecificSubstitutionModelProvider substitutionModelProvider;
    ArbitrarySubstitutionParameterBranchModel branchParameterModel;
    // assert (dxo.getChildCount() == cxo.getChildCount());
    List<Parameter> oldParameters = new ArrayList<Parameter>();
    List<BranchParameter> parameterList = new ArrayList<BranchParameter>();
    for (int i = 0; i < cxo.getChildCount(); i++) {
        // Parameter rootParameter = (Parameter) dxo.getChild(i);
        // ArbitraryBranchRates branchRateModel = (ArbitraryBranchRates) cxo.getChild(i);
        // BranchParameter branchParameter = new BranchParameter("branchSpecific.substitution.parameter",
        // tree,
        // branchRateModel,
        // rootParameter);
        // branchParameter.setId("branchSpecific." + rootParameter.getId());
        // parameterList.add(branchParameter);
        // xo.setNativeObject(branchParameter);
        BranchParameter branchParameter = (BranchParameter) cxo.getChild(i);
        parameterList.add(branchParameter);
        oldParameters.add(branchParameter.getRootParameter());
    }
    List<SubstitutionModel> substitutionModelList = new ArrayList<SubstitutionModel>();
    ParameterReplaceableSubstitutionModel rootSubstitutionModel = (ParameterReplaceableSubstitutionModel) substitutionModel;
    // List<Parameter> oldParameters = parseParameters(dxo);
    // List<Parameter> branchParameters = parseParameters(cxo);
    final int parameterCount = parameterList.size();
    int v = 0;
    for (int nodeNum = 0; nodeNum < tree.getNodeCount(); ++nodeNum) {
        NodeRef node = tree.getNode(nodeNum);
        ParameterReplaceableSubstitutionModel branchSubstitutionModel = (ParameterReplaceableSubstitutionModel) substitutionModel;
        List<Parameter> newParameters = new ArrayList<Parameter>();
        if (tree.isRoot(node)) {
            for (int i = 0; i < parameterCount; i++) {
                BranchParameter branchParameter = parameterList.get(i);
                newParameters.add(branchParameter.getRootParameter());
            }
        } else {
            for (int i = 0; i < parameterCount; i++) {
                BranchParameter branchParameter = parameterList.get(i);
                newParameters.add(branchParameter.getParameter(v));
            }
            v++;
        }
        branchSubstitutionModel = branchSubstitutionModel.factory(oldParameters, newParameters);
        substitutionModelList.add(branchSubstitutionModel);
    }
    substitutionModelProvider = new BranchSpecificSubstitutionModelProvider.Default(substitutionModelList, tree);
    branchParameterModel = new ArbitrarySubstitutionParameterBranchModel(ARBITRARY_SUBSTITUTION_PARAMETER_BRANCH_MODEL, substitutionModelProvider, parameterList, tree);
    return branchParameterModel;
}
Also used : BranchParameter(dr.inference.model.BranchParameter) ParameterReplaceableSubstitutionModel(dr.evomodel.substmodel.ParameterReplaceableSubstitutionModel) ArrayList(java.util.ArrayList) BranchSpecificSubstitutionModelProvider(dr.evomodel.substmodel.BranchSpecificSubstitutionModelProvider) ParameterReplaceableSubstitutionModel(dr.evomodel.substmodel.ParameterReplaceableSubstitutionModel) SubstitutionModel(dr.evomodel.substmodel.SubstitutionModel) TreeModel(dr.evomodel.tree.TreeModel) NodeRef(dr.evolution.tree.NodeRef) ArbitrarySubstitutionParameterBranchModel(dr.evomodel.branchmodel.ArbitrarySubstitutionParameterBranchModel) Parameter(dr.inference.model.Parameter) BranchParameter(dr.inference.model.BranchParameter)

Example 23 with SubstitutionModel

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

the class GammaSiteModelParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    String msg = "";
    SubstitutionModel substitutionModel = null;
    double muWeight = 1.0;
    Parameter muParam = null;
    if (xo.hasChildNamed(SUBSTITUTION_RATE)) {
        muParam = (Parameter) xo.getElementFirstChild(SUBSTITUTION_RATE);
        msg += "\n  with initial substitution rate = " + muParam.getParameterValue(0);
    } else if (xo.hasChildNamed(MUTATION_RATE)) {
        muParam = (Parameter) xo.getElementFirstChild(MUTATION_RATE);
        msg += "\n  with initial substitution rate = " + muParam.getParameterValue(0);
    } else if (xo.hasChildNamed(RELATIVE_RATE)) {
        XMLObject cxo = xo.getChild(RELATIVE_RATE);
        muParam = (Parameter) cxo.getChild(Parameter.class);
        msg += "\n  with initial relative rate = " + muParam.getParameterValue(0);
        if (cxo.hasAttribute(WEIGHT)) {
            muWeight = cxo.getDoubleAttribute(WEIGHT);
            msg += " with weight: " + muWeight;
        }
    }
    Parameter shapeParam = null;
    int catCount = 4;
    if (xo.hasChildNamed(GAMMA_SHAPE)) {
        XMLObject cxo = xo.getChild(GAMMA_SHAPE);
        catCount = cxo.getIntegerAttribute(GAMMA_CATEGORIES);
        shapeParam = (Parameter) cxo.getChild(Parameter.class);
        msg += "\n  " + catCount + " category discrete gamma with initial shape = " + shapeParam.getParameterValue(0);
    }
    Parameter invarParam = null;
    if (xo.hasChildNamed(PROPORTION_INVARIANT)) {
        invarParam = (Parameter) xo.getElementFirstChild(PROPORTION_INVARIANT);
        msg += "\n  initial proportion of invariant sites = " + invarParam.getParameterValue(0);
    }
    if (msg.length() > 0) {
        Logger.getLogger("dr.evomodel").info("\nCreating site rate model: " + msg);
    } else {
        Logger.getLogger("dr.evomodel").info("\nCreating site rate model.");
    }
    GammaSiteRateModel siteRateModel = new GammaSiteRateModel(SITE_MODEL, muParam, muWeight, shapeParam, catCount, invarParam);
    if (xo.hasChildNamed(SUBSTITUTION_MODEL)) {
        // System.err.println("Doing the substitution model stuff");
        // set this to pass it along to the OldTreeLikelihoodParser...
        substitutionModel = (SubstitutionModel) xo.getElementFirstChild(SUBSTITUTION_MODEL);
        siteRateModel.setSubstitutionModel(substitutionModel);
    }
    return siteRateModel;
}
Also used : Parameter(dr.inference.model.Parameter) XMLObject(dr.xml.XMLObject) GammaSiteRateModel(dr.evomodel.siteratemodel.GammaSiteRateModel) SubstitutionModel(dr.evomodel.substmodel.SubstitutionModel)

Example 24 with SubstitutionModel

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

the class SubstitutionModelDelegate method updateSubstitutionModels.

// END: getStateFrequencies
@Override
public void updateSubstitutionModels(Beagle beagle, boolean flipBuffers) {
    for (int i = 0; i < eigenCount; i++) {
        if (flipBuffers) {
            eigenBufferHelper.flipOffset(i);
        }
        SubstitutionModel substitutionModel = substitutionModelList.get(i);
        EigenDecomposition ed = substitutionModel.getEigenDecomposition();
        beagle.setEigenDecomposition(eigenBufferHelper.getOffsetIndex(i), ed.getEigenVectors(), ed.getInverseEigenVectors(), ed.getEigenValues());
    }
}
Also used : EigenDecomposition(dr.evomodel.substmodel.EigenDecomposition) SubstitutionModel(dr.evomodel.substmodel.SubstitutionModel)

Example 25 with SubstitutionModel

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

the class EpochBranchModelParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    Logger.getLogger("dr.evomodel").info("\nUsing multi-epoch branch model.");
    MutableTreeModel treeModel = (MutableTreeModel) xo.getChild(MutableTreeModel.class);
    SubstitutionModel ancestralSubstitutionModel = (SubstitutionModel) xo.getChild(SubstitutionModel.class);
    List<Epoch> epochs = new ArrayList<Epoch>();
    for (int i = 0; i < xo.getChildCount(); i++) {
        if (xo.getChild(i) instanceof XMLObject) {
            XMLObject xoc = (XMLObject) xo.getChild(i);
            if (xoc.getName().equals(EPOCH)) {
                Parameter tt = null;
                if (xoc.hasAttribute(TRANSITION_TIME)) {
                    double t = xoc.getAttribute(TRANSITION_TIME, 0.0);
                    tt = new Parameter.Default(1, t);
                }
                SubstitutionModel s = (SubstitutionModel) xoc.getChild(SubstitutionModel.class);
                if (xoc.hasChildNamed(TRANSITION_TIME)) {
                    if (tt != null) {
                        throw new XMLParseException("An epoch cannot have a transitionTime attribute and a parameter");
                    }
                    tt = (Parameter) xoc.getElementFirstChild(TRANSITION_TIME);
                }
                epochs.add(new Epoch(s, tt));
            }
        }
    }
    Collections.sort(epochs);
    List<SubstitutionModel> substitutionModels = new ArrayList<SubstitutionModel>();
    CompoundParameter transitionTimes = new CompoundParameter("epochTimes");
    for (Epoch epoch : epochs) {
        substitutionModels.add(epoch.substitutionModel);
        transitionTimes.addParameter(epoch.timeParameter);
    }
    substitutionModels.add(ancestralSubstitutionModel);
    return new EpochBranchModel(treeModel, substitutionModels, transitionTimes);
}
Also used : EpochBranchModel(dr.evomodel.branchmodel.EpochBranchModel) ArrayList(java.util.ArrayList) XMLObject(dr.xml.XMLObject) SubstitutionModel(dr.evomodel.substmodel.SubstitutionModel) CompoundParameter(dr.inference.model.CompoundParameter) MutableTreeModel(dr.evolution.tree.MutableTreeModel) CompoundParameter(dr.inference.model.CompoundParameter) Parameter(dr.inference.model.Parameter) XMLParseException(dr.xml.XMLParseException)

Aggregations

SubstitutionModel (dr.evomodel.substmodel.SubstitutionModel)25 TreeModel (dr.evomodel.tree.TreeModel)13 Parameter (dr.inference.model.Parameter)13 FrequencyModel (dr.evomodel.substmodel.FrequencyModel)11 GammaSiteRateModel (dr.evomodel.siteratemodel.GammaSiteRateModel)9 ArrayList (java.util.ArrayList)9 HomogeneousBranchModel (dr.evomodel.branchmodel.HomogeneousBranchModel)8 BranchRateModel (dr.evomodel.branchratemodel.BranchRateModel)8 BranchModel (dr.evomodel.branchmodel.BranchModel)7 XMLObject (dr.xml.XMLObject)6 PatternList (dr.evolution.alignment.PatternList)5 Partition (dr.app.beagle.tools.Partition)4 NodeRef (dr.evolution.tree.NodeRef)4 Tree (dr.evolution.tree.Tree)4 TaxonList (dr.evolution.util.TaxonList)4 EpochBranchModel (dr.evomodel.branchmodel.EpochBranchModel)4 DefaultBranchRateModel (dr.evomodel.branchratemodel.DefaultBranchRateModel)4 TipStatesModel (dr.evomodel.tipstatesmodel.TipStatesModel)4 BeagleTreeLikelihood (dr.evomodel.treelikelihood.BeagleTreeLikelihood)4 PartialsRescalingScheme (dr.evomodel.treelikelihood.PartialsRescalingScheme)4