Search in sources :

Example 46 with Tree

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

the class StarBeastStartState method getInitialisedStateNodes.

@Override
public void getInitialisedStateNodes(final List<StateNode> stateNodes) {
    if (hasCalibrations) {
        stateNodes.add((Tree) calibratedYule.get().treeInput.get());
    } else {
        stateNodes.add(speciesTreeInput.get());
    }
    for (final Tree g : genes.get()) {
        stateNodes.add(g);
    }
    final RealParameter popm = popMean.get();
    if (popm != null) {
        stateNodes.add(popm);
    }
    final RealParameter brate = birthRate.get();
    if (brate != null) {
        stateNodes.add(brate);
    }
    final SpeciesTreePrior speciesTreePrior = speciesTreePriorInput.get();
    if (speciesTreePrior != null) {
        final RealParameter popb = speciesTreePrior.popSizesBottomInput.get();
        if (popb != null) {
            stateNodes.add(popb);
        }
        final RealParameter popt = speciesTreePrior.popSizesTopInput.get();
        if (popt != null) {
            stateNodes.add(popt);
        }
    }
}
Also used : RandomTree(beast.evolution.tree.RandomTree) Tree(beast.evolution.tree.Tree) ClusterTree(beast.util.ClusterTree) RealParameter(beast.core.parameter.RealParameter)

Example 47 with Tree

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

the class StarBeastStartState method initWithCalibrations.

private void initWithCalibrations() throws MathException {
    final CalibratedYuleModel cYule = calibratedYule.get();
    final Tree spTree = (Tree) cYule.treeInput.get();
    final List<CalibrationPoint> cals = cYule.calibrationsInput.get();
    final CalibratedYuleModel cym = new CalibratedYuleModel();
    cym.getOutputs().addAll(cYule.getOutputs());
    for (final CalibrationPoint cal : cals) {
        cym.setInputValue("calibrations", cal);
    }
    cym.setInputValue("tree", spTree);
    cym.setInputValue("type", CalibratedYuleModel.Type.NONE);
    cym.initAndValidate();
    final Tree t = cym.compatibleInitialTree();
    assert spTree.getLeafNodeCount() == t.getLeafNodeCount();
    spTree.assignFromWithoutID(t);
    // final CalibratedYuleInitialTree ct = new CalibratedYuleInitialTree();
    // ct.initByName("initial", spTree, "calibrations", cYule.calibrationsInput.get());
    // ct.initStateNodes();
    final double rootHeight = spTree.getRoot().getHeight();
    randomInitGeneTrees(rootHeight);
    cYule.initAndValidate();
}
Also used : RandomTree(beast.evolution.tree.RandomTree) Tree(beast.evolution.tree.Tree) ClusterTree(beast.util.ClusterTree)

Example 48 with Tree

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

the class StarBeastStartState method initWithMRCACalibrations.

private void initWithMRCACalibrations(List<MRCAPrior> calibrations) {
    final Tree spTree = speciesTreeInput.get();
    final RandomTree rnd = new RandomTree();
    rnd.setInputValue("taxonset", spTree.getTaxonset());
    for (final MRCAPrior cal : calibrations) {
        rnd.setInputValue("constraint", cal);
    }
    ConstantPopulation pf = new ConstantPopulation();
    pf.setInputValue("popSize", new RealParameter("1.0"));
    rnd.setInputValue("populationModel", pf);
    rnd.initAndValidate();
    spTree.assignFromWithoutID((Tree) rnd);
    final double rootHeight = spTree.getRoot().getHeight();
    randomInitGeneTrees(rootHeight);
}
Also used : ConstantPopulation(beast.evolution.tree.coalescent.ConstantPopulation) RandomTree(beast.evolution.tree.RandomTree) MRCAPrior(beast.math.distributions.MRCAPrior) RandomTree(beast.evolution.tree.RandomTree) Tree(beast.evolution.tree.Tree) ClusterTree(beast.util.ClusterTree) RealParameter(beast.core.parameter.RealParameter)

Example 49 with Tree

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

the class YuleModel method sample.

/**
 * Sampling only implemented for no-origin case currently.
 */
@Override
public void sample(State state, Random random) {
    if (sampledFlag)
        return;
    sampledFlag = true;
    // Cause conditional parameters to be sampled
    sampleConditions(state, random);
    Tree tree = (Tree) treeInput.get();
    RealParameter birthRate = birthDiffRateParameterInput.get();
    // Simulate tree conditional on new parameters
    List<Node> activeLineages = new ArrayList<>();
    for (Node oldLeaf : tree.getExternalNodes()) {
        Node newLeaf = new Node(oldLeaf.getID());
        newLeaf.setNr(oldLeaf.getNr());
        newLeaf.setHeight(0.0);
        activeLineages.add(newLeaf);
    }
    int nextNr = activeLineages.size();
    double t = 0.0;
    while (activeLineages.size() > 1) {
        int k = activeLineages.size();
        double a = birthRate.getValue() * k;
        t += -Math.log(random.nextDouble()) / a;
        Node node1 = activeLineages.get(random.nextInt(k));
        Node node2;
        do {
            node2 = activeLineages.get(random.nextInt(k));
        } while (node2.equals(node1));
        Node newParent = new Node();
        newParent.setNr(nextNr++);
        newParent.setHeight(t);
        newParent.addChild(node1);
        newParent.addChild(node2);
        activeLineages.remove(node1);
        activeLineages.remove(node2);
        activeLineages.add(newParent);
    }
    tree.assignFromWithoutID(new Tree(activeLineages.get(0)));
}
Also used : Node(beast.evolution.tree.Node) ArrayList(java.util.ArrayList) Tree(beast.evolution.tree.Tree) RealParameter(beast.core.parameter.RealParameter)

Example 50 with Tree

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

the class NodeReheight method calcMaxHeight.

/**
 * calculate maximum height that node nodeIndex can become restricted
 * by nodes on the left and right
 */
private double calcMaxHeight(final int[] reverseOrder, final int nodeIndex) {
    // find maximum height between two species. Only upper right part is populated
    final double[][] maxHeight = new double[nrOfSpecies][nrOfSpecies];
    for (int i = 0; i < nrOfSpecies; i++) {
        Arrays.fill(maxHeight[i], Double.POSITIVE_INFINITY);
    }
    // calculate for every species tree the maximum allowable merge point
    for (int i = 0; i < nrOfGeneTrees; i++) {
        final Tree tree = geneTreesInput.get().get(i);
        findMaximaInGeneTree(tree.getRoot(), new boolean[nrOfSpecies], m_taxonMap.get(i), maxHeight);
    }
    // find species on the left of selected node
    final boolean[] isLowerSpecies = new boolean[nrOfSpecies];
    final Node[] nodes = treeInput.get().getNodesAsArray();
    for (int i = 0; i < nodeIndex; i++) {
        final Node node = nodes[reverseOrder[i]];
        if (node.isLeaf()) {
            isLowerSpecies[node.getNr()] = true;
        }
    }
    // find species on the right of selected node
    final boolean[] isUpperSpecies = new boolean[nrOfSpecies];
    for (int i = nodeIndex + 1; i < nodes.length; i++) {
        final Node node = nodes[reverseOrder[i]];
        if (node.isLeaf()) {
            isUpperSpecies[node.getNr()] = true;
        }
    }
    // find max
    double max = Double.POSITIVE_INFINITY;
    for (int i = 0; i < nrOfSpecies; i++) {
        if (isLowerSpecies[i]) {
            for (int j = 0; j < nrOfSpecies; j++) {
                if (j != i && isUpperSpecies[j]) {
                    final int x = Math.min(i, j);
                    final int y = Math.max(i, j);
                    max = Math.min(max, maxHeight[x][y]);
                }
            }
        }
    }
    return max;
}
Also used : Node(beast.evolution.tree.Node) 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