Search in sources :

Example 16 with Node

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

the class RandomLocalClockModel method recalculateScaleFactor.

private void recalculateScaleFactor() {
    BooleanParameter indicators = indicatorParamInput.get();
    RealParameter rates = rateParamInput.get();
    calculateUnscaledBranchRates(m_tree.getRoot(), 1.0, indicators, rates);
    double timeTotal = 0.0;
    double branchTotal = 0.0;
    for (int i = 0; i < m_tree.getNodeCount(); i++) {
        Node node = m_tree.getNode(i);
        if (!node.isRoot()) {
            double branchInTime = node.getParent().getHeight() - node.getHeight();
            double branchLength = branchInTime * unscaledBranchRates[node.getNr()];
            timeTotal += branchInTime;
            branchTotal += branchLength;
        }
    }
    scaleFactor = timeTotal / branchTotal;
    scaleFactor *= meanRate.getValue();
}
Also used : Node(beast.evolution.tree.Node) RealParameter(beast.core.parameter.RealParameter) BooleanParameter(beast.core.parameter.BooleanParameter)

Example 17 with Node

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

the class GeneTreeForSpeciesTreeDistribution method calculateLogP.

@Override
public double calculateLogP() {
    logP = 0;
    for (final PriorityQueue<Double> m_interval : intervalsInput) {
        m_interval.clear();
    }
    Arrays.fill(nrOfLineages, 0);
    final TreeInterface stree = speciesTreeInput.get();
    final Node[] speciesNodes = stree.getNodesAsArray();
    traverseLineageTree(speciesNodes, treeInput.get().getRoot());
    // if the gene tree does not fit the species tree, logP = -infinity by now
    if (logP == 0) {
        traverseSpeciesTree(stree.getRoot());
    }
    // System.err.println("logp=" + logP);
    return logP;
}
Also used : Node(beast.evolution.tree.Node) TreeInterface(beast.evolution.tree.TreeInterface)

Example 18 with Node

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

the class SpeciesTreePrior method calculateLogP.

@Override
public double calculateLogP() {
    logP = 0;
    // make sure the root branch length is positive
    // if (m_rootHeightParameter.get().getValue() < m_speciesTree.get().getRoot().getHeight()) {
    // logP = Double.NEGATIVE_INFINITY;
    // return logP;
    // }
    final Node[] speciesNodes = treeInput.get().getNodesAsArray();
    try {
        switch(popFunction) {
            case constant:
                // constant pop size function
                logP += gamma2Prior.calcLogP(popSizesBottom);
                // }
                break;
            case linear:
                for (int i = 0; i < speciesNodes.length; i++) {
                    final Node node = speciesNodes[i];
                    final double popSizeBottom;
                    if (node.isLeaf()) {
                        // Gamma(4, psi) prior
                        popSizeBottom = popSizesBottom.getValue(i);
                        logP += gamma4Prior.logDensity(popSizeBottom);
                    }
                    final double popSizeTop = popSizesTop.getValue(i);
                    logP += gamma2Prior.logDensity(popSizeTop);
                }
                break;
            case linear_with_constant_root:
                for (int i = 0; i < speciesNodes.length; i++) {
                    final Node node = speciesNodes[i];
                    if (node.isLeaf()) {
                        final double popSizeBottom = popSizesBottom.getValue(i);
                        logP += gamma4Prior.logDensity(popSizeBottom);
                    }
                    if (!node.isRoot()) {
                        if (i < speciesNodes.length - 1) {
                            final double popSizeTop = popSizesTop.getArrayValue(i);
                            logP += gamma2Prior.logDensity(popSizeTop);
                        } else {
                            final int nodeIndex = treeInput.get().getRoot().getNr();
                            final double popSizeTop = popSizesTop.getArrayValue(nodeIndex);
                            logP += gamma2Prior.logDensity(popSizeTop);
                        }
                    }
                }
                break;
        }
    } catch (Exception e) {
        // exceptions can be thrown by the gamma priors
        e.printStackTrace();
        return Double.NEGATIVE_INFINITY;
    }
    return logP;
}
Also used : Node(beast.evolution.tree.Node)

Example 19 with Node

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

the class ScaleOperatorTest method testTreeScaling.

@Test
public void testTreeScaling() {
    String newick = "((0:1.0,1:1.0)4:1.0,(2:1.0,3:1.0)5:0.5)6:0.0;";
    TreeParser tree = new TreeParser(newick, false, false, false, 0);
    Node[] node = tree.getNodesAsArray();
    ScaleOperator operator = new ScaleOperator();
    operator.initByName("tree", tree, "weight", 1.0);
    operator.proposal();
    // leaf node
    node = tree.getNodesAsArray();
    assertEquals(0.0, node[0].getHeight(), EPSILON);
    assertEquals(0.0, node[1].getHeight(), EPSILON);
    // leaf node, not scaled
    assertEquals(0.5, node[2].getHeight(), EPSILON);
    assertEquals(0.5, node[3].getHeight(), EPSILON);
    // internal nodes, all scaled
    // first determine scale factor
    double scale = node[4].getHeight() / 1.0;
    assertEquals(1.0 * scale, node[4].getHeight(), EPSILON);
    assertEquals(1.5 * scale, node[5].getHeight(), EPSILON);
    assertEquals(2.0 * scale, node[6].getHeight(), EPSILON);
}
Also used : TreeParser(beast.util.TreeParser) Node(beast.evolution.tree.Node) ScaleOperator(beast.evolution.operators.ScaleOperator) Test(org.junit.Test)

Example 20 with Node

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

the class TreeAnnotator method processMetaData.

private void processMetaData(Node node) {
    for (Node child : node.getChildren()) {
        processMetaData(child);
    }
    Set<String> metaDataNames = node.getMetaDataNames();
    if (metaDataNames != null && !metaDataNames.isEmpty()) {
        String metadata = "";
        for (String name : metaDataNames) {
            Object value = node.getMetaData(name);
            metadata += name + "=";
            if (value instanceof Object[]) {
                Object[] values = (Object[]) value;
                metadata += "{";
                for (int i = 0; i < values.length; i++) {
                    metadata += values[i].toString();
                    if (i < values.length - 1) {
                        metadata += ",";
                    }
                }
                metadata += "}";
            } else {
                metadata += value.toString();
            }
            metadata += ",";
        }
        metadata = metadata.substring(0, metadata.length() - 1);
        node.metaDataString = metadata;
    }
}
Also used : Node(beast.evolution.tree.Node)

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