use of beast.evolution.tree.Node in project beast2 by CompEvol.
the class CladeSystem method getLogCladeCredibility.
public double getLogCladeCredibility(Node node, BitSet bits) {
double logCladeCredibility = 0.0;
if (node.isLeaf()) {
int index = getTaxonIndex(node);
bits.set(2 * index);
} else {
BitSet bits2 = new BitSet();
for (int i = 0; i < node.getChildCount(); i++) {
Node node1 = node.getChild(i);
logCladeCredibility += getLogCladeCredibility(node1, bits2);
}
for (int i = 1; i < bits2.length(); i = i + 2) {
bits2.set(i, false);
}
if (node.isFake()) {
int index = getTaxonIndex(node.getDirectAncestorChild());
bits2.set(2 * index + 1);
}
logCladeCredibility += Math.log(getCladeCredibility(bits2));
if (bits != null) {
bits.or(bits2);
}
}
return logCladeCredibility;
}
use of beast.evolution.tree.Node in project beast2 by CompEvol.
the class TreeSetParser method parseNewick.
public Node parseNewick(String str) {
try {
if (str == null || str.length() == 0) {
return null;
}
m_chars = str.toCharArray();
m_iTokenStart = str.indexOf('(');
if (m_iTokenStart < 0) {
return null;
}
m_iTokenEnd = m_iTokenStart;
Vector<Node> stack = new Vector<>();
Vector<Boolean> isFirstChild = new Vector<>();
Vector<String> metaDataString = new Vector<>();
stack.add(new Node());
isFirstChild.add(true);
stack.lastElement().setHeight(DEFAULT_LENGTH);
metaDataString.add(null);
boolean isLabel = true;
while (m_iTokenEnd < m_chars.length) {
switch(nextToken()) {
case BRACE_OPEN:
{
Node node2 = new Node();
node2.setHeight(DEFAULT_LENGTH);
stack.add(node2);
isFirstChild.add(true);
metaDataString.add(null);
isLabel = true;
}
break;
case BRACE_CLOSE:
{
if (isFirstChild.lastElement()) {
if (m_bAllowSingleChild) {
// process single child nodes
Node left = stack.lastElement();
stack.remove(stack.size() - 1);
isFirstChild.remove(isFirstChild.size() - 1);
Node dummyparent = new Node();
dummyparent.setHeight(DEFAULT_LENGTH);
dummyparent.setLeft(left);
left.setParent(dummyparent);
dummyparent.setRight(null);
Node parent = stack.lastElement();
parent.setLeft(left);
left.setParent(parent);
String metaData = metaDataString.remove(metaDataString.size() - 1);
left.metaDataString = metaData;
parseMetaData(left, metaData);
break;
} else {
// don't know how to process single child nodes
throw new IllegalArgumentException("Node with single child found.");
}
}
// process multi(i.e. more than 2)-child nodes by pairwise merging.
while (isFirstChild.elementAt(isFirstChild.size() - 2) == false) {
Node right = stack.lastElement();
stack.remove(stack.size() - 1);
isFirstChild.remove(isFirstChild.size() - 1);
Node left = stack.lastElement();
stack.remove(stack.size() - 1);
isFirstChild.remove(isFirstChild.size() - 1);
Node dummyparent = new Node();
dummyparent.setHeight(DEFAULT_LENGTH);
dummyparent.setLeft(left);
left.setParent(dummyparent);
dummyparent.setRight(right);
right.setParent(dummyparent);
stack.add(dummyparent);
isFirstChild.add(false);
String metaData = metaDataString.remove(metaDataString.size() - 1);
parseMetaData(left, metaData);
}
// last two nodes on stack merged into single parent node
Node right = stack.lastElement();
stack.remove(stack.size() - 1);
isFirstChild.remove(isFirstChild.size() - 1);
String metaData = metaDataString.remove(metaDataString.size() - 1);
parseMetaData(right, metaData);
Node left = stack.lastElement();
stack.remove(stack.size() - 1);
isFirstChild.remove(isFirstChild.size() - 1);
metaData = metaDataString.remove(metaDataString.size() - 1);
parseMetaData(left, metaData);
Node parent = stack.lastElement();
parent.setLeft(left);
left.setParent(parent);
parent.setRight(right);
right.setParent(parent);
metaData = metaDataString.lastElement();
parseMetaData(parent, metaData);
}
break;
case COMMA:
{
Node node2 = new Node();
node2.setHeight(DEFAULT_LENGTH);
stack.add(node2);
isFirstChild.add(false);
metaDataString.add(null);
isLabel = true;
}
break;
case COLON:
isLabel = false;
break;
case TEXT:
if (isLabel) {
String label = str.substring(m_iTokenStart, m_iTokenEnd);
stack.lastElement().setNr(getLabelIndex(label));
} else {
String length = str.substring(m_iTokenStart, m_iTokenEnd);
stack.lastElement().setHeight(Float.parseFloat(length));
}
break;
case META_DATA:
if (metaDataString.lastElement() == null) {
metaDataString.set(metaDataString.size() - 1, str.substring(m_iTokenStart + 1, m_iTokenEnd - 1));
} else {
metaDataString.set(metaDataString.size() - 1, metaDataString.lastElement() + ("," + str.substring(m_iTokenStart + 1, m_iTokenEnd - 1)));
}
break;
case SEMI_COLON:
// System.err.println(stack.lastElement().toString());
parseMetaData(stack.lastElement(), metaDataString.lastElement());
return stack.lastElement();
default:
throw new IllegalArgumentException("parseNewick: unknown token");
}
}
return stack.lastElement();
} catch (Exception e) {
e.printStackTrace();
throw new IllegalArgumentException(e.getMessage() + ": " + str.substring(Math.max(0, m_iTokenStart - 100), m_iTokenStart) + " >>>" + str.substring(m_iTokenStart, m_iTokenEnd) + " <<< ...");
}
// return node;
}
use of beast.evolution.tree.Node in project MultiTypeTree by tgvaughan.
the class TypeChangeCounts method update.
/**
* Update type change count array as necessary.
*/
private void update() {
if (!dirty)
return;
// Zero type change count array
for (int i = 0; i < typeChanges.length; i++) typeChanges[i] = 0;
// Recalculate array elements
for (Node node : mtTree.getNodesAsArray()) {
if (node.isRoot()) {
continue;
}
MultiTypeNode mtNode = (MultiTypeNode) node;
int lastType = mtNode.getNodeType();
for (int i = 0; i < mtNode.getChangeCount(); i++) {
int nextType = mtNode.getChangeType(i);
typeChanges[getOffset(lastType, nextType)] += 1;
lastType = nextType;
}
}
if (useCache)
dirty = false;
}
use of beast.evolution.tree.Node in project MultiTypeTree by tgvaughan.
the class TypedNodeTreeLogger method log.
@Override
public void log(long nSample, PrintStream out) {
// Set up metadata string
for (Node node : mtTree.getNodesAsArray()) {
MultiTypeNode mtNode = (MultiTypeNode) node;
mtNode.metaDataString = mtTree.getTypeLabel() + "=\"" + mtTree.getTypeSet().getTypeName(mtNode.getNodeType()) + "\"";
}
out.print("tree STATE_" + nSample + " = ");
out.print(mtTree.getRoot().toSortedNewick(new int[1], true));
out.print(";");
}
use of beast.evolution.tree.Node in project MultiTypeTree by tgvaughan.
the class TypedSubtreeExchangeEasy method proposal.
@Override
public double proposal() {
// Select source and destination nodes:
Node srcNode, srcNodeParent, destNode, destNodeParent;
if (isNarrowInput.get()) {
// Narrow exchange selection:
do {
srcNode = mtTree.getNode(Randomizer.nextInt(mtTree.getNodeCount()));
} while (srcNode.isRoot() || srcNode.getParent().isRoot());
srcNodeParent = srcNode.getParent();
destNode = getOtherChild(srcNodeParent.getParent(), srcNodeParent);
destNodeParent = destNode.getParent();
} else {
// Wide exchange selection:
do {
srcNode = mtTree.getNode(Randomizer.nextInt(mtTree.getNodeCount()));
} while (srcNode.isRoot());
srcNodeParent = srcNode.getParent();
do {
destNode = mtTree.getNode(Randomizer.nextInt(mtTree.getNodeCount()));
} while (destNode == srcNode || destNode.isRoot() || destNode.getParent() == srcNodeParent);
destNodeParent = destNode.getParent();
// as doing so can lead to infinite loops.)
if (srcNodeParent == destNode || destNodeParent == srcNode)
return Double.NEGATIVE_INFINITY;
}
// lengths:
if (destNode.getHeight() > srcNodeParent.getHeight() || srcNode.getHeight() > destNodeParent.getHeight())
return Double.NEGATIVE_INFINITY;
// Make changes to tree topology:
replace(srcNodeParent, srcNode, destNode);
replace(destNodeParent, destNode, srcNode);
// Force rejection if resulting multi-type tree invalid:
if (!mtTree.isValid())
return Double.NEGATIVE_INFINITY;
return 0.0;
}
Aggregations