Search in sources :

Example 31 with Node

use of beast.evolution.tree.Node 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 32 with Node

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

the class TypedWilsonBalding method getRootBranchTypeProb.

/**
 * Obtain joint probability of typing along branches between srcNode and
 * the root, the sister of srcNode and the root, and the node type of the
 * root.
 *
 * @param srcNode
 * @return
 */
protected double getRootBranchTypeProb(Node srcNode) {
    double logProb = 0.0;
    Node srcNodeP = srcNode.getParent();
    Node srcNodeS = getOtherChild(srcNodeP, srcNode);
    // Probability of node type:
    logProb += Math.log(1.0 / migModel.getNTypes());
    // Probability of branch types conditional on node types:
    logProb += getBranchTypeProb(srcNode);
    logProb += getBranchTypeProb(srcNodeS);
    return logProb;
}
Also used : Node(beast.evolution.tree.Node) MultiTypeNode(beast.evolution.tree.MultiTypeNode)

Example 33 with Node

use of beast.evolution.tree.Node 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 34 with Node

use of beast.evolution.tree.Node 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)

Example 35 with Node

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

the class MultiTypeTreeScale method proposal.

@Override
public double proposal() {
    // Choose scale factor:
    double u = Randomizer.nextDouble();
    double f = u * scaleFactorInput.get() + (1.0 - u) / scaleFactorInput.get();
    // Keep track of Hastings ratio:
    double logf = Math.log(f);
    double logHR = -2 * logf;
    // Scale colour change times on external branches:
    if (!useOldTreeScalerInput.get()) {
        for (Node leaf : mtTree.getExternalNodes()) {
            double lold = leaf.getParent().getHeight() - leaf.getHeight();
            double lnew = f * leaf.getParent().getHeight() - leaf.getHeight();
            for (int c = 0; c < ((MultiTypeNode) leaf).getChangeCount(); c++) {
                double oldTime = ((MultiTypeNode) leaf).getChangeTime(c);
                double newTime = leaf.getHeight() + (oldTime - leaf.getHeight()) * lnew / lold;
                ((MultiTypeNode) leaf).setChangeTime(c, newTime);
            }
            logHR += ((MultiTypeNode) leaf).getChangeCount() * Math.log(lnew / lold);
        }
    } else {
        for (Node leaf : mtTree.getExternalNodes()) {
            for (int c = 0; c < ((MultiTypeNode) leaf).getChangeCount(); c++) {
                double oldTime = ((MultiTypeNode) leaf).getChangeTime(c);
                ((MultiTypeNode) leaf).setChangeTime(c, f * oldTime);
                logHR += logf;
            }
        }
    }
    // Scale internal node heights and colour change times:
    for (Node node : mtTree.getInternalNodes()) {
        node.setHeight(node.getHeight() * f);
        logHR += logf;
        for (int c = 0; c < ((MultiTypeNode) node).getChangeCount(); c++) {
            double oldTime = ((MultiTypeNode) node).getChangeTime(c);
            ((MultiTypeNode) node).setChangeTime(c, f * oldTime);
            logHR += logf;
        }
    }
    // Reject invalid tree scalings:
    if (f < 1.0) {
        for (Node leaf : mtTree.getExternalNodes()) {
            if (leaf.getParent().getHeight() < leaf.getHeight())
                return Double.NEGATIVE_INFINITY;
            if (((MultiTypeNode) leaf).getChangeCount() > 0 && ((MultiTypeNode) leaf).getChangeTime(0) < leaf.getHeight())
                if (!useOldTreeScalerInput.get())
                    throw new IllegalStateException("Scaled colour change time " + "has dipped below age of leaf - this should never " + "happen!");
                else
                    return Double.NEGATIVE_INFINITY;
        }
    }
    // Scale parameters:
    for (int pidx = 0; pidx < parametersInput.get().size(); pidx++) {
        RealParameter param = parametersInput.get().get(pidx);
        for (int i = 0; i < param.getDimension(); i++) {
            if (!indicatorsUsed || indicatorsInput.get().get(pidx).getValue(i)) {
                double oldValue = param.getValue(i);
                double newValue = oldValue * f;
                if (newValue < param.getLower() || newValue > param.getUpper())
                    return Double.NEGATIVE_INFINITY;
                param.setValue(i, newValue);
                logHR += logf;
            }
        }
    }
    // Scale parameters inversely:
    for (int pidx = 0; pidx < parametersInverseInput.get().size(); pidx++) {
        RealParameter param = parametersInverseInput.get().get(pidx);
        for (int i = 0; i < param.getDimension(); i++) {
            if (!indicatorsInverseUsed || indicatorsInverseInput.get().get(pidx).getValue(i)) {
                double oldValue = param.getValue(i);
                double newValue = oldValue / f;
                if (newValue < param.getLower() || newValue > param.getUpper())
                    return Double.NEGATIVE_INFINITY;
                param.setValue(i, newValue);
                logHR -= logf;
            }
        }
    }
    // Return Hastings ratio:
    return logHR;
}
Also used : MultiTypeNode(beast.evolution.tree.MultiTypeNode) Node(beast.evolution.tree.Node) MultiTypeNode(beast.evolution.tree.MultiTypeNode) RealParameter(beast.core.parameter.RealParameter)

Aggregations

Node (beast.evolution.tree.Node)140 Conversion (bacter.Conversion)24 MultiTypeNode (beast.evolution.tree.MultiTypeNode)22 Locus (bacter.Locus)17 ArrayList (java.util.ArrayList)15 Tree (beast.evolution.tree.Tree)14 Test (org.junit.Test)9 CalculationNode (beast.core.CalculationNode)8 RealParameter (beast.core.parameter.RealParameter)8 TreeInterface (beast.evolution.tree.TreeInterface)7 ClusterTree (beast.util.ClusterTree)7 ConversionGraph (bacter.ConversionGraph)6 Alignment (beast.evolution.alignment.Alignment)6 TaxonSet (beast.evolution.alignment.TaxonSet)6 SiteModel (beast.evolution.sitemodel.SiteModel)5 BitSet (java.util.BitSet)5 StateNode (beast.core.StateNode)4 JukesCantor (beast.evolution.substitutionmodel.JukesCantor)4 TreeParser (beast.util.TreeParser)3 CFEventList (bacter.CFEventList)2