Search in sources :

Example 1 with AncestralTaxonInTree

use of dr.evomodel.continuous.AncestralTaxonInTree in project beast-mcmc by beast-dev.

the class AncestralTraitTreeModel method getNodeHeight.

public double getNodeHeight(NodeRef iNode) {
    assert (iNode != null);
    checkShadowTree();
    double height;
    ShadowNode node = (ShadowNode) iNode;
    int originalNumber = node.getOriginalNumber();
    if (originalNumber >= 0) {
        height = treeModel.getNodeHeight(node.getOriginalNode());
    } else {
        final AncestralTaxonInTree ancestor = node.ancestor;
        if (ancestor.isOnAncestralPath()) {
            // TODO Refactor into subclasses
            double ancestorHeight = ancestor.getHeight();
            if (node.isExternal()) {
                ancestorHeight -= ancestor.getPseudoBranchLength();
            }
            double rootHeight = treeModel.getNodeHeight(treeModel.getRoot());
            ancestorHeight = Math.min(rootHeight, ancestorHeight);
            height = ancestorHeight;
        } else {
            // Below is the original
            if (node.isExternal()) {
                height = treeModel.getNodeHeight(node.parent.parent.getOriginalNode()) - ancestor.getPseudoBranchLength();
            } else {
                height = treeModel.getNodeHeight(node.parent.getOriginalNode());
            }
        }
    }
    return height;
}
Also used : AncestralTaxonInTree(dr.evomodel.continuous.AncestralTaxonInTree)

Example 2 with AncestralTaxonInTree

use of dr.evomodel.continuous.AncestralTaxonInTree in project beast-mcmc by beast-dev.

the class AncestralTraitTreeModel method buildRecursivelyShadowTree.

private ShadowNode buildRecursivelyShadowTree(NodeRef originalNode, ShadowNode parentNode) {
    final int originalNumber = originalNode.getNumber();
    final int newNumber = mapOriginalToShadowNumber(originalNumber);
    ShadowNode newNode = new ShadowNode(newNumber, originalNode, null);
    newNode.parent = parentNode;
    storeNode(newNode);
    NodeRef originalChild0 = treeModel.getChild(originalNode, 0);
    NodeRef originalChild1 = treeModel.getChild(originalNode, 1);
    ShadowNode recurse0 = newNode;
    ShadowNode recurse1 = newNode;
    if (nodeToClampMap.containsKey(originalNode.getNumber())) {
        List<AncestralTaxonInTree> ancestors = nodeToClampMap.get(originalNode.getNumber());
        if (ancestors.size() > 1) {
            sortByTime(ancestors);
        }
        // Add tips
        for (AncestralTaxonInTree ancestor : ancestors) {
            final int newTipNumber = treeExternalCount + ancestor.getIndex();
            ShadowNode newTipNode = new ShadowNode(newTipNumber, null, ancestor);
            ShadowNode newInternalNode = new ShadowNode(externalCount + treeInternalCount + ancestor.getIndex(), null, ancestor);
            if (ancestor.getPathChildNumber() == 0) {
                recurse0.child0 = newInternalNode;
                newInternalNode.parent = recurse0;
                newInternalNode.child1 = newTipNode;
                newTipNode.parent = newInternalNode;
                // recurse0.child0 is free
                recurse0 = newInternalNode;
            } else {
                recurse1.child1 = newInternalNode;
                newInternalNode.parent = recurse1;
                newInternalNode.child0 = newTipNode;
                newTipNode.parent = newInternalNode;
                // recurse1.child1 is free
                recurse1 = newInternalNode;
            }
            storeNode(newTipNode);
            storeNode(newInternalNode);
        }
    }
    if (!treeModel.isExternal(originalNode)) {
        recurse0.child0 = buildRecursivelyShadowTree(originalChild0, recurse0);
        recurse1.child1 = buildRecursivelyShadowTree(originalChild1, recurse1);
    }
    return newNode;
}
Also used : AncestralTaxonInTree(dr.evomodel.continuous.AncestralTaxonInTree)

Example 3 with AncestralTaxonInTree

use of dr.evomodel.continuous.AncestralTaxonInTree in project beast-mcmc by beast-dev.

the class AncestralTraitTreeModelParser method parseNodeTraits.

private static void parseNodeTraits(XMLObject cxo, Tree tree, List<AncestralTaxonInTree> ancestors) throws XMLParseException {
    String name = cxo.getAttribute(NAME, "trait");
    int dim = cxo.getAttribute(MULTIVARIATE_TRAIT, 1);
    // double[] initialValues = null;
    // if (cxo.hasAttribute(INITIAL_VALUE)) {
    // initialValues = cxo.getDoubleArrayAttribute(INITIAL_VALUE);
    // }
    final int rowDim = dim;
    final int colDim = tree.getExternalNodeCount() + ancestors.size();
    FastMatrixParameter parameter = new FastMatrixParameter(name, rowDim, colDim, 0.0);
    parameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, rowDim * colDim));
    int parameterIndex = 0;
    for (int i = 0; i < tree.getExternalNodeCount(); i++) {
        // if (leafNodes) {
        // nodes[i].addTraitParameter(name, parameter.getParameter(parameterIndex), initialValues, firesTreeEvents);
        ++parameterIndex;
    // }
    }
    for (AncestralTaxonInTree ancestor : ancestors) {
        ++parameterIndex;
    }
    ParameterParser.replaceParameter(cxo, parameter);
}
Also used : AncestralTaxonInTree(dr.evomodel.continuous.AncestralTaxonInTree) FastMatrixParameter(dr.inference.model.FastMatrixParameter) Parameter(dr.inference.model.Parameter) FastMatrixParameter(dr.inference.model.FastMatrixParameter)

Example 4 with AncestralTaxonInTree

use of dr.evomodel.continuous.AncestralTaxonInTree in project beast-mcmc by beast-dev.

the class AncestralTraitTreeModel method recursiveSetupMrcaClamps.

private static void recursiveSetupMrcaClamps(Tree tree, NodeRef node, BitSet tips, Map<BitSet, AncestralTaxonInTree> clampList, Map<Integer, List<AncestralTaxonInTree>> nodeToClampMap) {
    if (tree.isExternal(node)) {
        tips.set(node.getNumber());
    } else {
        for (int i = 0; i < tree.getChildCount(node); i++) {
            NodeRef child = tree.getChild(node, i);
            BitSet childTips = new BitSet();
            recursiveSetupMrcaClamps(tree, child, childTips, clampList, nodeToClampMap);
            tips.or(childTips);
        }
        if (clampList.containsKey(tips)) {
            AncestralTaxonInTree partials = clampList.get(tips);
            partials.setNode(node);
            addAncestralTaxonToMap(nodeToClampMap, node.getNumber(), partials);
        }
    }
}
Also used : AncestralTaxonInTree(dr.evomodel.continuous.AncestralTaxonInTree)

Example 5 with AncestralTaxonInTree

use of dr.evomodel.continuous.AncestralTaxonInTree in project beast-mcmc by beast-dev.

the class AncestralTraitTreeModel method setupAncestralPathClamps.

// private void addAllAncestralNodeHeightParameterBelow(NodeRef node) {
// addAncestralNodeHeightParameter(node);
// for (int i = 0; i < treeModel.getChildCount(node); ++i) {
// addAllAncestralNodeHeightParameterBelow(treeModel.getChild(node, i));
// }
// }
private void setupAncestralPathClamps(Tree tree, Map<BitSet, AncestralTaxonInTree> clampList, Map<Integer, List<AncestralTaxonInTree>> nodeToClampMap) {
    hasAncestralPathTaxa = false;
    ancestralPathNodeHeightParameters.clear();
    for (int i = 0; i < tree.getExternalNodeCount(); ++i) {
        NodeRef node = tree.getExternalNode(i);
        BitSet tip = new BitSet();
        tip.set(node.getNumber());
        if (clampList.containsKey(tip)) {
            AncestralTaxonInTree partials = clampList.get(tip);
            partials.setTipNode(node);
            addAncestralNodeHeightParameter(node);
            double pathHeight = partials.getHeight();
            assert (pathHeight > 0.0);
            NodeRef parent = tree.getParent(node);
            double parentHeight = tree.getNodeHeight(parent);
            boolean isChild0 = tree.getChild(parent, 0) == node;
            while (parentHeight < pathHeight && parent != tree.getRoot()) {
                node = parent;
                addAncestralNodeHeightParameter(node);
                parent = tree.getParent(parent);
                parentHeight = tree.getNodeHeight(parent);
                isChild0 = tree.getChild(parent, 0) == node;
            }
            addAncestralNodeHeightParameter(parent);
            partials.setNode(parent, isChild0 ? 0 : 1);
            addAncestralTaxonToMap(nodeToClampMap, parent.getNumber(), partials);
            hasAncestralPathTaxa = true;
        }
    }
}
Also used : AncestralTaxonInTree(dr.evomodel.continuous.AncestralTaxonInTree)

Aggregations

AncestralTaxonInTree (dr.evomodel.continuous.AncestralTaxonInTree)7 NodeRef (dr.evolution.tree.NodeRef)2 FastMatrixParameter (dr.inference.model.FastMatrixParameter)2 Parameter (dr.inference.model.Parameter)2 MutableTreeModel (dr.evolution.tree.MutableTreeModel)1 TreeUtils (dr.evolution.tree.TreeUtils)1 Date (dr.evolution.util.Date)1 Taxa (dr.evolution.util.Taxa)1 Taxon (dr.evolution.util.Taxon)1 TaxonList (dr.evolution.util.TaxonList)1