Search in sources :

Example 56 with Tree

use of beast.evolution.tree.Tree in project beast2 by CompEvol.

the class PriorListInputEditor method getTaxonCandidates.

Set<Taxon> getTaxonCandidates(MRCAPrior prior) {
    Set<Taxon> candidates = new HashSet<>();
    Tree tree = prior.treeInput.get();
    String[] taxa = null;
    if (tree.m_taxonset.get() != null) {
        try {
            TaxonSet set = tree.m_taxonset.get();
            set.initAndValidate();
            taxa = set.asStringList().toArray(new String[0]);
        } catch (Exception e) {
            taxa = prior.treeInput.get().getTaxaNames();
        }
    } else {
        taxa = prior.treeInput.get().getTaxaNames();
    }
    for (String taxon : taxa) {
        candidates.add(doc.getTaxon(taxon));
    }
    return candidates;
}
Also used : Taxon(beast.evolution.alignment.Taxon) Tree(beast.evolution.tree.Tree) TaxonSet(beast.evolution.alignment.TaxonSet) HashSet(java.util.HashSet)

Example 57 with Tree

use of beast.evolution.tree.Tree in project beast2 by CompEvol.

the class TreeAnnotator method setTreeHeightsByCA.

boolean setTreeHeightsByCA(Tree targetTree, Target targetOption) throws IOException {
    progressStream.println("Setting node heights...");
    progressStream.println("0              25             50             75            100");
    progressStream.println("|--------------|--------------|--------------|--------------|");
    int reportStepSize = totalTreesUsed / 60;
    if (reportStepSize < 1)
        reportStepSize = 1;
    int reported = 0;
    // this call increments the clade counts and it shouldn't
    // this is remedied with removeClades call after while loop below
    CladeSystem cladeSystem = new CladeSystem(targetTree);
    final int clades = cladeSystem.getCladeMap().size();
    // allocate posterior tree nodes order once
    int[] postOrderList = new int[clades];
    BitSet[] ctarget = new BitSet[clades];
    BitSet[] ctree = new BitSet[clades];
    for (int k = 0; k < clades; ++k) {
        ctarget[k] = new BitSet();
        ctree[k] = new BitSet();
    }
    cladeSystem.getTreeCladeCodes(targetTree, ctarget);
    // temp collecting heights inside loop allocated once
    double[][] hs = new double[clades][treeSet.totalTrees];
    // heights total sum from posterior trees
    double[] ths = new double[clades];
    int totalTreesUsed = 0;
    int counter = 0;
    treeSet.reset();
    while (treeSet.hasNext()) {
        Tree tree = treeSet.next();
        TreeUtils.preOrderTraversalList(tree, postOrderList);
        cladeSystem.getTreeCladeCodes(tree, ctree);
        for (int k = 0; k < clades; ++k) {
            int j = postOrderList[k];
            for (int i = 0; i < clades; ++i) {
                if (CollectionUtils.isSubSet(ctarget[i], ctree[j])) {
                    hs[i][counter] = tree.getNode(j).getHeight();
                }
            }
        }
        for (int k = 0; k < clades; ++k) {
            ths[k] += hs[k][counter];
        }
        totalTreesUsed += 1;
        if (counter > 0 && counter % reportStepSize == 0 && reported < 61) {
            while (1000 * reported < 61000 * (counter + 1) / this.totalTreesUsed) {
                progressStream.print("*");
                reported++;
            }
            progressStream.flush();
        }
        counter++;
    }
    if (targetOption != Target.USER_TARGET_TREE)
        targetTree.initAndValidate();
    cladeSystem.removeClades(targetTree.getRoot(), true);
    for (int k = 0; k < clades; ++k) {
        ths[k] /= totalTreesUsed;
        final Node node = targetTree.getNode(k);
        node.setHeight(ths[k]);
        String attributeName = "CAheight";
        double[] values = hs[k];
        double min = values[0];
        double max = values[0];
        for (double d : values) {
            min = Math.min(d, min);
            max = Math.max(d, max);
        }
        if (Math.abs(min - max) > 1e-10) {
            annotateMeanAttribute(node, attributeName + "_mean", values);
            annotateMedianAttribute(node, attributeName + "_median", values);
            annotateHPDAttribute(node, attributeName + "_95%_HPD", 0.95, values);
            annotateRangeAttribute(node, attributeName + "_range", values);
        }
    }
    assert (totalTreesUsed == this.totalTreesUsed);
    this.totalTreesUsed = totalTreesUsed;
    progressStream.println();
    progressStream.println();
    return true;
}
Also used : Node(beast.evolution.tree.Node) Tree(beast.evolution.tree.Tree)

Example 58 with Tree

use of beast.evolution.tree.Tree in project beast2 by CompEvol.

the class TipDatesScaler method proposal.

@Override
public double proposal() {
    Tree tree = treeInput.get(this);
    // randomly select leaf node
    int i = Randomizer.nextInt(taxonIndices.length);
    Node node = tree.getNode(taxonIndices[i]);
    double upper = node.getParent().getHeight();
    // double lower = 0.0;
    // final double newValue = (Randomizer.nextDouble() * (upper -lower)) + lower;
    // scale node
    double scale = (scaleFactor + (Randomizer.nextDouble() * ((1.0 / scaleFactor) - scaleFactor)));
    final double newValue = node.getHeight() * scale;
    // check the tree does not get negative branch lengths
    if (newValue > upper) {
        return Double.NEGATIVE_INFINITY;
    }
    node.setHeight(newValue);
    return -Math.log(scale);
}
Also used : Node(beast.evolution.tree.Node) Tree(beast.evolution.tree.Tree)

Example 59 with Tree

use of beast.evolution.tree.Tree in project beast2 by CompEvol.

the class Uniform method proposal.

/**
 * change the parameter and return the hastings ratio.
 *
 * @return log of Hastings Ratio, or Double.NEGATIVE_INFINITY if proposal should not be accepted *
 */
@Override
public double proposal() {
    final Tree tree = treeInput.get(this);
    // randomly select internal node
    final int nodeCount = tree.getNodeCount();
    // Abort if no non-root internal nodes
    if (tree.getInternalNodeCount() == 1)
        return Double.NEGATIVE_INFINITY;
    Node node;
    do {
        final int nodeNr = nodeCount / 2 + 1 + Randomizer.nextInt(nodeCount / 2);
        node = tree.getNode(nodeNr);
    } while (node.isRoot() || node.isLeaf());
    final double upper = node.getParent().getHeight();
    final double lower = Math.max(node.getLeft().getHeight(), node.getRight().getHeight());
    final double newValue = (Randomizer.nextDouble() * (upper - lower)) + lower;
    node.setHeight(newValue);
    return 0.0;
}
Also used : Node(beast.evolution.tree.Node) Tree(beast.evolution.tree.Tree)

Example 60 with Tree

use of beast.evolution.tree.Tree in project beast2 by CompEvol.

the class CalibratedYuleInitialTree method initStateNodes.

@Override
public void initStateNodes() {
    // Would have been nice to use the MCMC CalibratedYuleModel beastObject directly, but at this point
    // it does not exist since the tree being initialized is one of its arguments. So, build a temporary
    // one using the initializer tree.
    final List<CalibrationPoint> cals = calibrationsInput.get();
    final CalibratedYuleModel cym = new CalibratedYuleModel();
    for (final CalibrationPoint cal : cals) {
        cym.setInputValue("calibrations", cal);
    }
    cym.setInputValue("tree", this);
    cym.setInputValue("type", CalibratedYuleModel.Type.NONE);
    cym.initAndValidate();
    Tree t;
    try {
        t = cym.compatibleInitialTree();
    } catch (MathException e) {
        throw new IllegalArgumentException(e.getMessage());
    }
    m_initial.get().assignFromWithoutID(t);
}
Also used : MathException(org.apache.commons.math.MathException) Tree(beast.evolution.tree.Tree)

Aggregations

Tree (beast.evolution.tree.Tree)70 Alignment (beast.evolution.alignment.Alignment)29 Test (org.junit.Test)26 TreeLikelihood (beast.evolution.likelihood.TreeLikelihood)22 SiteModel (beast.evolution.sitemodel.SiteModel)21 BeagleTreeLikelihood (beast.evolution.likelihood.BeagleTreeLikelihood)19 RealParameter (beast.core.parameter.RealParameter)17 UncertainAlignmentTest (test.beast.evolution.alignment.UncertainAlignmentTest)17 Node (beast.evolution.tree.Node)14 Frequencies (beast.evolution.substitutionmodel.Frequencies)13 ArrayList (java.util.ArrayList)10 TaxonSet (beast.evolution.alignment.TaxonSet)9 RandomTree (beast.evolution.tree.RandomTree)7 ClusterTree (beast.util.ClusterTree)7 ParametricDistribution (beast.math.distributions.ParametricDistribution)6 BEASTInterface (beast.core.BEASTInterface)5 CompoundDistribution (beast.core.util.CompoundDistribution)5 Sequence (beast.evolution.alignment.Sequence)5 Taxon (beast.evolution.alignment.Taxon)5 HKY (beast.evolution.substitutionmodel.HKY)5