Search in sources :

Example 1 with SubstitutionEpochModel

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

the class SequenceSimulator method getTransitionProbabilities.

// traverse
void getTransitionProbabilities(Tree tree, NodeRef node, int rateCategory, double[] probs) {
    NodeRef parent = tree.getParent(node);
    final double branchRate = m_branchRateModel.getBranchRate(tree, node);
    // Get the operational time of the branch
    final double branchTime = branchRate * (tree.getNodeHeight(parent) - tree.getNodeHeight(node));
    if (branchTime < 0.0) {
        throw new RuntimeException("Negative branch length: " + branchTime);
    }
    double branchLength = m_siteModel.getRateForCategory(rateCategory) * branchTime;
    if (m_siteModel.getSubstitutionModel() instanceof SubstitutionEpochModel) {
        ((SubstitutionEpochModel) m_siteModel.getSubstitutionModel()).getTransitionProbabilities(tree.getNodeHeight(node), tree.getNodeHeight(parent), branchLength, probs);
        return;
    }
    m_siteModel.getSubstitutionModel().getTransitionProbabilities(branchLength, probs);
}
Also used : NodeRef(dr.evolution.tree.NodeRef) SubstitutionEpochModel(dr.oldevomodel.substmodel.SubstitutionEpochModel)

Example 2 with SubstitutionEpochModel

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

the class SubstitutionEpochModelParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    DataType dataType = null;
    FrequencyModel freqModel = null;
    List<SubstitutionModel> modelList = new ArrayList<SubstitutionModel>();
    XMLObject cxo = xo.getChild(MODELS);
    for (int i = 0; i < cxo.getChildCount(); i++) {
        SubstitutionModel model = (SubstitutionModel) cxo.getChild(i);
        if (dataType == null) {
            dataType = model.getDataType();
        } else if (dataType != model.getDataType())
            throw new XMLParseException("Substitution models across epoches must use the same data type.");
        if (freqModel == null) {
            freqModel = model.getFrequencyModel();
        } else if (freqModel != model.getFrequencyModel())
            throw new XMLParseException("Substitution models across epoches must currently use the same frequency model.\nHarass Marc to fix this.");
        modelList.add(model);
    }
    Parameter transitionTimes = (Parameter) xo.getChild(Parameter.class);
    if (transitionTimes.getDimension() != modelList.size() - 1) {
        throw new XMLParseException("# of transition times must equal # of substitution models - 1\n" + transitionTimes.getDimension() + "\n" + modelList.size());
    }
    return new SubstitutionEpochModel(SUBSTITUTION_EPOCH_MODEL, modelList, transitionTimes, dataType, freqModel);
}
Also used : FrequencyModel(dr.oldevomodel.substmodel.FrequencyModel) ArrayList(java.util.ArrayList) DataType(dr.evolution.datatype.DataType) Parameter(dr.inference.model.Parameter) SubstitutionEpochModel(dr.oldevomodel.substmodel.SubstitutionEpochModel) AbstractSubstitutionModel(dr.oldevomodel.substmodel.AbstractSubstitutionModel) SubstitutionModel(dr.oldevomodel.substmodel.SubstitutionModel)

Example 3 with SubstitutionEpochModel

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

the class EpochTreeLikelihood method traverse.

/**
	  * Traverse the tree calculating partial likelihoods.
	  *
	  * @return whether the partials for this node were recalculated.
	  */
protected boolean traverse(Tree tree, NodeRef node) {
    boolean update = false;
    int nodeNum = node.getNumber();
    NodeRef parent = tree.getParent(node);
    // First update the transition probability matrix(ices) for this branch
    if (parent != null && updateNode[nodeNum]) {
        final double branchRate = branchRateModel.getBranchRate(tree, node);
        // Get the operational time of the branch
        final double parentNodeHeight = tree.getNodeHeight(parent);
        final double nodeHeight = tree.getNodeHeight(node);
        final double branchTime = branchRate * (parentNodeHeight - nodeHeight);
        if (branchTime < 0.0) {
            throw new RuntimeException("Negative branch length: " + branchTime);
        }
        likelihoodCore.setNodeMatrixForUpdate(nodeNum);
        for (int i = 0; i < categoryCount; i++) {
            double branchLength = siteModel.getRateForCategory(i) * branchTime;
            ((SubstitutionEpochModel) siteModel.getSubstitutionModel()).getTransitionProbabilities(nodeHeight, parentNodeHeight, branchLength, probabilities);
            likelihoodCore.setNodeMatrix(nodeNum, i, probabilities);
        }
        update = true;
    }
    // If the node is internal, update the partial likelihoods.
    if (!tree.isExternal(node)) {
        // Traverse down the two child nodes
        NodeRef child1 = tree.getChild(node, 0);
        final boolean update1 = traverse(tree, child1);
        NodeRef child2 = tree.getChild(node, 1);
        final boolean update2 = traverse(tree, child2);
        // If either child node was updated then update this node too
        if (update1 || update2) {
            final int childNum1 = child1.getNumber();
            final int childNum2 = child2.getNumber();
            likelihoodCore.setNodePartialsForUpdate(nodeNum);
            if (integrateAcrossCategories) {
                likelihoodCore.calculatePartials(childNum1, childNum2, nodeNum);
            } else {
                likelihoodCore.calculatePartials(childNum1, childNum2, nodeNum, siteCategories);
            }
            if (parent == null) {
                // No parent this is the root of the tree -
                // calculate the pattern likelihoods
                double[] frequencies = frequencyModel.getFrequencies();
                double[] partials = getRootPartials();
                likelihoodCore.calculateLogLikelihoods(partials, frequencies, patternLogLikelihoods);
            }
            update = true;
        }
    }
    return update;
}
Also used : NodeRef(dr.evolution.tree.NodeRef) SubstitutionEpochModel(dr.oldevomodel.substmodel.SubstitutionEpochModel)

Aggregations

SubstitutionEpochModel (dr.oldevomodel.substmodel.SubstitutionEpochModel)3 NodeRef (dr.evolution.tree.NodeRef)2 DataType (dr.evolution.datatype.DataType)1 Parameter (dr.inference.model.Parameter)1 AbstractSubstitutionModel (dr.oldevomodel.substmodel.AbstractSubstitutionModel)1 FrequencyModel (dr.oldevomodel.substmodel.FrequencyModel)1 SubstitutionModel (dr.oldevomodel.substmodel.SubstitutionModel)1 ArrayList (java.util.ArrayList)1