Search in sources :

Example 1 with MultiTypeNode

use of beast.evolution.tree.MultiTypeNode in project MultiTypeTree by tgvaughan.

the class TypeChangeCounts method update.

/**
 * Update type change count array as necessary.
 */
private void update() {
    if (!dirty)
        return;
    // Zero type change count array
    for (int i = 0; i < typeChanges.length; i++) typeChanges[i] = 0;
    // Recalculate array elements
    for (Node node : mtTree.getNodesAsArray()) {
        if (node.isRoot()) {
            continue;
        }
        MultiTypeNode mtNode = (MultiTypeNode) node;
        int lastType = mtNode.getNodeType();
        for (int i = 0; i < mtNode.getChangeCount(); i++) {
            int nextType = mtNode.getChangeType(i);
            typeChanges[getOffset(lastType, nextType)] += 1;
            lastType = nextType;
        }
    }
    if (useCache)
        dirty = false;
}
Also used : MultiTypeNode(beast.evolution.tree.MultiTypeNode) CalculationNode(beast.core.CalculationNode) Node(beast.evolution.tree.Node) MultiTypeNode(beast.evolution.tree.MultiTypeNode)

Example 2 with MultiTypeNode

use of beast.evolution.tree.MultiTypeNode in project MultiTypeTree by tgvaughan.

the class TypedNodeTreeLogger method log.

@Override
public void log(long nSample, PrintStream out) {
    // Set up metadata string
    for (Node node : mtTree.getNodesAsArray()) {
        MultiTypeNode mtNode = (MultiTypeNode) node;
        mtNode.metaDataString = mtTree.getTypeLabel() + "=\"" + mtTree.getTypeSet().getTypeName(mtNode.getNodeType()) + "\"";
    }
    out.print("tree STATE_" + nSample + " = ");
    out.print(mtTree.getRoot().toSortedNewick(new int[1], true));
    out.print(";");
}
Also used : MultiTypeNode(beast.evolution.tree.MultiTypeNode) Node(beast.evolution.tree.Node) MultiTypeNode(beast.evolution.tree.MultiTypeNode)

Example 3 with MultiTypeNode

use of beast.evolution.tree.MultiTypeNode in project MultiTypeTree by tgvaughan.

the class TypeMergeSplit method mergeProposalRoot.

private double mergeProposalRoot() {
    MultiTypeNode root = (MultiTypeNode) mtTree.getRoot();
    MultiTypeNode left = (MultiTypeNode) root.getLeft();
    MultiTypeNode right = (MultiTypeNode) root.getRight();
    int leftIdx = left.getChangeCount() - 1;
    int rightIdx = right.getChangeCount() - 1;
    if (leftIdx < 0 || rightIdx < 0)
        return Double.NEGATIVE_INFINITY;
    int leftType = left.getChangeType(leftIdx);
    int rightType = right.getChangeType(rightIdx);
    if (leftType != rightType)
        return Double.NEGATIVE_INFINITY;
    int leftTypeUnder;
    double tminLeft;
    if (leftIdx > 0) {
        leftTypeUnder = left.getChangeType(leftIdx - 1);
        tminLeft = left.getChangeTime(leftIdx - 1);
    } else {
        leftTypeUnder = left.getNodeType();
        tminLeft = left.getHeight();
    }
    int rightTypeUnder;
    double tminRight;
    if (rightIdx > 0) {
        rightTypeUnder = right.getChangeType(rightIdx - 1);
        tminRight = right.getChangeTime(rightIdx - 1);
    } else {
        rightTypeUnder = right.getNodeType();
        tminRight = right.getHeight();
    }
    if (leftTypeUnder != rightTypeUnder)
        return Double.NEGATIVE_INFINITY;
    left.removeChange(leftIdx);
    right.removeChange(rightIdx);
    root.setNodeType(leftTypeUnder);
    return -Math.log((root.getHeight() - tminRight) * (root.getHeight() - tminLeft) * (migModel.getNTypes() - 1));
}
Also used : MultiTypeNode(beast.evolution.tree.MultiTypeNode)

Example 4 with MultiTypeNode

use of beast.evolution.tree.MultiTypeNode in project MultiTypeTree by tgvaughan.

the class TypeBirthDeath method retypeSubtreeRecurse.

/**
 * Assigns a new type to all branches within the subtree attached to node.
 *
 * @param type
 * @param node
 * @param prevNode
 */
private void retypeSubtreeRecurse(int type, MultiTypeNode node, MultiTypeNode prevNode) {
    if (node.isLeaf())
        throw new IllegalArgumentException("Leaf attached to subtree: cannot retype!");
    node.setNodeType(type);
    if (prevNode == node.getParent()) {
        MultiTypeNode left = (MultiTypeNode) node.getLeft();
        MultiTypeNode right = (MultiTypeNode) node.getRight();
        if (left.getChangeCount() > 0)
            left.setChangeType(left.getChangeCount() - 1, type);
        else
            retypeSubtreeRecurse(type, left, node);
        if (right.getChangeCount() > 0)
            right.setChangeType(right.getChangeCount() - 1, type);
        else
            retypeSubtreeRecurse(type, right, node);
    } else {
        MultiTypeNode sister = (MultiTypeNode) getOtherChild(node, prevNode);
        if (!node.isRoot() && node.getChangeCount() == 0)
            retypeSubtreeRecurse(type, (MultiTypeNode) node.getParent(), node);
        if (sister.getChangeCount() > 0)
            sister.setChangeType(sister.getChangeCount() - 1, type);
        else
            retypeSubtreeRecurse(type, sister, node);
    }
}
Also used : MultiTypeNode(beast.evolution.tree.MultiTypeNode)

Example 5 with MultiTypeNode

use of beast.evolution.tree.MultiTypeNode in project MultiTypeTree by tgvaughan.

the class TypeBirthDeath method getIllegalTypesRecurse.

/**
 * Recursive method used by getIllegalTypes.
 *
 * @param node
 * @param prevNode
 */
private void getIllegalTypesRecurse(Set<Integer> illegalTypes, MultiTypeNode node, MultiTypeNode prevNode) throws Exception {
    if (node.isLeaf())
        throw new Exception("Leaf in sub-tree.");
    if (prevNode == node.getParent()) {
        MultiTypeNode left = (MultiTypeNode) node.getLeft();
        MultiTypeNode right = (MultiTypeNode) node.getRight();
        if (left.getChangeCount() > 0) {
            if (left.getChangeCount() > 1)
                illegalTypes.add(left.getChangeType(left.getChangeCount() - 2));
            else
                illegalTypes.add(left.getNodeType());
        } else
            getIllegalTypesRecurse(illegalTypes, left, node);
        if (right.getChangeCount() > 0)
            if (right.getChangeCount() > 1)
                illegalTypes.add(right.getChangeType(right.getChangeCount() - 2));
            else
                illegalTypes.add(right.getNodeType());
        else
            getIllegalTypesRecurse(illegalTypes, right, node);
    } else {
        MultiTypeNode sister = (MultiTypeNode) getOtherChild(node, prevNode);
        if (!node.isRoot()) {
            if (node.getChangeCount() > 0)
                illegalTypes.add(node.getChangeType(0));
            else
                getIllegalTypesRecurse(illegalTypes, (MultiTypeNode) node.getParent(), node);
        }
        if (sister.getChangeCount() > 0) {
            if (sister.getChangeCount() > 1)
                illegalTypes.add(sister.getChangeType(sister.getChangeCount() - 2));
            else
                illegalTypes.add(sister.getNodeType());
        } else
            getIllegalTypesRecurse(illegalTypes, sister, node);
    }
}
Also used : MultiTypeNode(beast.evolution.tree.MultiTypeNode)

Aggregations

MultiTypeNode (beast.evolution.tree.MultiTypeNode)34 Node (beast.evolution.tree.Node)20 CalculationNode (beast.core.CalculationNode)1 RealParameter (beast.core.parameter.RealParameter)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1