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();
}
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;
}
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;
}
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);
}
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;
}
}
Aggregations