Search in sources :

Example 6 with MultiTypeNode

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

the class TypeBirthDeath method getIllegalTypes.

/**
 * Populates illegalTypes set with types of subtree containing node and no
 * intervening migration events.
 *
 * @param changeIdx Position of event r.
 * @param node Node on which event r sits.
 * @throws Exception when subtree contains a leaf node.
 */
public void getIllegalTypes(int changeIdx, MultiTypeNode node) throws Exception {
    illegalTypes.clear();
    if (changeIdx < 0) {
        MultiTypeNode startNode = findDecendentNodeWithMigration((MultiTypeNode) node.getLeft());
        if (startNode.getChangeCount() == 0)
            // Termiated at leaf: move would be irreversable
            throw new Exception("Leaf in sub-tree.");
        if (startNode.getChangeCount() == 1)
            illegalTypes.add(startNode.getNodeType());
        else
            illegalTypes.add(startNode.getChangeType(startNode.getChangeCount() - 2));
        getIllegalTypesRecurse(illegalTypes, (MultiTypeNode) startNode.getParent(), startNode);
    } else {
        if (changeIdx == 0) {
            illegalTypes.add(node.getNodeType());
        } else {
            illegalTypes.add(node.getChangeType(changeIdx - 1));
        }
        if (changeIdx + 1 < node.getChangeCount()) {
            illegalTypes.add(node.getChangeType(changeIdx + 1));
        } else {
            getIllegalTypesRecurse(illegalTypes, (MultiTypeNode) node.getParent(), node);
        }
    }
}
Also used : MultiTypeNode(beast.evolution.tree.MultiTypeNode)

Example 7 with MultiTypeNode

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

the class TypePairBirthDeath method birthProposal.

/**
 * Type change pair birth proposal.
 *
 * @param node Node above which selected edge lies
 * @param edgeNum Number of selected edge
 * @param n Number of nodes on tree.
 * @param m Number of type changes currently on tree.
 * @return log of Hastings factor of move.
 */
private double birthProposal(Node node, int edgeNum, int n, int m) {
    MultiTypeNode mtNode = (MultiTypeNode) node;
    int ridx = edgeNum;
    int sidx = edgeNum - 1;
    double ts, tr;
    int oldEdgeType;
    if (sidx < 0) {
        ts = node.getHeight();
        oldEdgeType = mtNode.getNodeType();
    } else {
        ts = mtNode.getChangeTime(sidx);
        oldEdgeType = mtNode.getChangeType(sidx);
    }
    if (ridx > mtNode.getChangeCount() - 1)
        tr = node.getParent().getHeight();
    else
        tr = mtNode.getChangeTime(ridx);
    int newEdgeType;
    do {
        newEdgeType = Randomizer.nextInt(migModel.getNTypes());
    } while (newEdgeType == oldEdgeType);
    double tau1 = Randomizer.nextDouble() * (tr - ts) + ts;
    double tau2 = Randomizer.nextDouble() * (tr - ts) + ts;
    double tauMin = Math.min(tau1, tau2);
    double tauMax = Math.max(tau1, tau2);
    mtNode.insertChange(edgeNum, oldEdgeType, tauMax);
    mtNode.insertChange(edgeNum, newEdgeType, tauMin);
    return Math.log((migModel.getNTypes() - 1) * (m + 2 * n - 2) * (tr - ts) * (tr - ts)) - Math.log(2 * (m + 2 * n));
}
Also used : MultiTypeNode(beast.evolution.tree.MultiTypeNode)

Example 8 with MultiTypeNode

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

the class TypedWilsonBalding method retypeRootBranches.

/**
 * Retype branches with nChanges between srcNode and the root (srcNode's
 * parent) and nChangesSister between the root and srcNode's sister.
 *
 * @param srcNode
 * @return Probability of new state.
 */
private double retypeRootBranches(Node srcNode) throws NoValidPathException {
    double logProb = 0.0;
    Node srcNodeP = srcNode.getParent();
    Node srcNodeS = getOtherChild(srcNodeP, srcNode);
    // Select new root colour:
    ((MultiTypeNode) srcNodeP).setNodeType(Randomizer.nextInt(migModel.getNTypes()));
    // Incorporate probability of new root colour:
    logProb += Math.log(1.0 / migModel.getNTypes());
    // Recolour branches conditional on root type:
    logProb += retypeBranch(srcNode);
    logProb += retypeBranch(srcNodeS);
    // Return probability of new colouring given boundary conditions:
    return logProb;
}
Also used : MultiTypeNode(beast.evolution.tree.MultiTypeNode) Node(beast.evolution.tree.Node) MultiTypeNode(beast.evolution.tree.MultiTypeNode)

Example 9 with MultiTypeNode

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

the class TypedWilsonBaldingRandom method getRootBranchTypeProb.

/**
 * Get probability of the colouring along the branch between srcNode
 * and its parent, and between that parent and srcNode's sister.
 * @param srcNode
 * @return
 */
private double getRootBranchTypeProb(Node srcNode) {
    Node srcNodeS = getOtherChild(srcNode.getParent(), srcNode);
    double mu = muInput.get();
    double T = 2.0 * srcNode.getParent().getHeight() - srcNode.getHeight() - srcNodeS.getHeight();
    int n = ((MultiTypeNode) srcNode).getChangeCount() + ((MultiTypeNode) srcNodeS).getChangeCount();
    int N = migModel.getNTypes();
    if (N == 0)
        return 0.0;
    else
        return -mu * T + n * Math.log(mu / (N - 1));
}
Also used : Node(beast.evolution.tree.Node) MultiTypeNode(beast.evolution.tree.MultiTypeNode)

Example 10 with MultiTypeNode

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

the class TypeChangeTimeCondition method calculateLogP.

@Override
public double calculateLogP() {
    update();
    logP = 0.0;
    for (Node node : mtTree.getNodesAsArray()) {
        if (node.isRoot())
            continue;
        if (node.getHeight() > h2.getValue() || node.getParent().getHeight() < h1.getValue())
            continue;
        MultiTypeNode mtNode = (MultiTypeNode) node;
        int lastType = mtNode.getNodeType();
        for (int i = 0; i < mtNode.getChangeCount(); i++) {
            if (mtNode.getChangeTime(i) > h1.getValue() && mtNode.getChangeTime(i) < h2.getValue() && toTypes.contains(lastType) && fromTypes.contains(mtNode.getChangeType(i))) {
                logP = Double.NEGATIVE_INFINITY;
                return logP;
            }
            lastType = mtNode.getChangeType(i);
        }
    }
    return logP;
}
Also used : MultiTypeNode(beast.evolution.tree.MultiTypeNode) Node(beast.evolution.tree.Node) 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