use of dr.evolution.tree.Clade in project beast-mcmc by beast-dev.
the class AbstractImportanceDistributionOperator method createTree.
private double createTree(NodeRef node, Clade c) throws InvalidTreeException {
double prob = 0.0;
if (c.getSize() == 2) {
// this clade only contains two tips
// the split between them is trivial
int leftTipIndex = c.getBits().nextSetBit(0);
int rightTipIndex = c.getBits().nextSetBit(leftTipIndex + 1);
NodeRef leftTip = externalNodes.get(leftTipIndex);
NodeRef rightTip = externalNodes.get(rightTipIndex);
removeChildren(node);
NodeRef leftParent = tree.getParent(leftTip);
if (leftParent != null)
tree.removeChild(leftParent, leftTip);
NodeRef rightParent = tree.getParent(rightTip);
if (rightParent != null)
tree.removeChild(rightParent, rightTip);
tree.addChild(node, leftTip);
tree.addChild(node, rightTip);
} else {
Clade[] clades = new Clade[2];
prob = splitClade(c, clades);
NodeRef leftChild, rightChild;
if (clades[0].getSize() == 1) {
int tipIndex = clades[0].getBits().nextSetBit(0);
leftChild = externalNodes.get(tipIndex);
} else {
leftChild = internalNodes.poll();
// TODO set the node height for the new node
tree.setNodeHeight(leftChild, tree.getNodeHeight(node) * 0.5);
prob += createTree(leftChild, clades[0]);
}
if (clades[1].getSize() == 1) {
int tipIndex = clades[1].getBits().nextSetBit(0);
rightChild = externalNodes.get(tipIndex);
} else {
rightChild = internalNodes.poll();
// TODO set the node height for the new node
tree.setNodeHeight(rightChild, tree.getNodeHeight(node) * 0.5);
prob += createTree(rightChild, clades[1]);
}
removeChildren(node);
NodeRef leftParent = tree.getParent(leftChild);
if (leftParent != null)
tree.removeChild(leftParent, leftChild);
NodeRef rightParent = tree.getParent(rightChild);
if (rightParent != null)
tree.removeChild(rightParent, rightChild);
tree.addChild(node, leftChild);
tree.addChild(node, rightChild);
}
return prob;
}
use of dr.evolution.tree.Clade in project beast-mcmc by beast-dev.
the class AbstractImportanceDistributionOperator method assignCladeHeights.
/**
* Creates a list with all clades of the tree
*
* @param node - the starting node. All clades below starting at this branch
* are added
* @param clades - the list in which the clades are stored
*/
private void assignCladeHeights(NodeRef node, HashMap<Clade, Double> clades, BitSet bits) {
// create a new bit set for this clade
BitSet bits2 = new BitSet();
// check if the node is external
if (tree.isExternal(node)) {
// if so, the only taxon in the clade is I
int index = node.getNumber();
bits2.set(index);
} else {
// clade
for (int i = 0; i < tree.getChildCount(node); i++) {
NodeRef child = tree.getChild(node, i);
assignCladeHeights(child, clades, bits2);
}
Clade c = new Clade(bits2, tree.getNodeHeight(node));
if (clades.containsKey(c)) {
tree.setNodeHeight(node, clades.get(c));
clades.remove(c);
}
}
// this is needed for adding all children clades together
if (bits != null) {
bits.or(bits2);
}
}
use of dr.evolution.tree.Clade in project beast-mcmc by beast-dev.
the class AbstractImportanceDistributionOperator method doImportanceDistributionOperation.
protected double doImportanceDistributionOperation(Likelihood likelihood) {
final NodeRef root = tree.getRoot();
BitSet all = new BitSet();
all.set(0, (tree.getNodeCount() + 1) / 2);
Clade rootClade = new Clade(all, tree.getNodeHeight(root));
internalNodes.clear();
fillInternalNodes(root);
// remove the root
internalNodes.poll();
externalNodes.clear();
fillExternalNodes(root);
double prob;
double back = probabilityEstimater.getTreeProbability(tree);
try {
tree.beginTreeEdit();
List<Clade> originalClades = new ArrayList<Clade>();
extractClades(tree, tree.getRoot(), originalClades, null);
double[] originalNodeHeights = getAbsoluteNodeHeights(originalClades);
Arrays.sort(originalNodeHeights);
back += getChanceForNodeHeights(originalNodeHeights);
prob = createTree(root, rootClade);
assignDummyHeights(root);
// assignCladeHeights(tree.getRoot(), originalClades, null);
// double[] originalNodeHeights = getAbsoluteNodeHeights(originalClades);
// Arrays.sort(originalNodeHeights);
// prob += setMissingNodeHeights(tree.getChild(tree.getRoot(),0));
// prob += setMissingNodeHeights(tree.getChild(tree.getRoot(),1));
prob += setNodeHeights(originalNodeHeights);
// List<Clade> newClades = new ArrayList<Clade>();
// extractClades(tree, tree.getRoot(), newClades, null);
tree.endTreeEdit();
tree.checkTreeIsValid();
} catch (InvalidTreeException e) {
throw new RuntimeException(e.getMessage());
}
tree.pushTreeChangedEvent(root);
return back - prob;
}
use of dr.evolution.tree.Clade in project beast-mcmc by beast-dev.
the class ConditionalCladeFrequency method setNodeHeights.
public double setNodeHeights(TreeModel tree, Likelihood likelihood) {
double prob = 0.0;
NodeRef node = tree.getRoot();
Clade currentClade = getClade(tree, node);
int childcount = tree.getChildCount(node);
for (int i = 0; i < childcount; i++) {
NodeRef child = tree.getChild(node, i);
if (!tree.isExternal(child)) {
// prob += setNodeHeights(tree, child, currentClade, likelihood,
// prior);
}
}
return prob;
}
use of dr.evolution.tree.Clade in project beast-mcmc by beast-dev.
the class ConditionalCladeFrequency method getTreeProbability.
/**
* Calculates the probability of a given tree.
*
* @param tree - the tree to be analyzed
* @return estimated posterior probability in log
*/
public double getTreeProbability(Tree tree, HashMap<String, Integer> taxonMap) {
double prob = 0.0;
List<Clade> clades = new ArrayList<Clade>();
List<Clade> parentClades = new ArrayList<Clade>();
// get clades contained in the tree
getNonComplementaryClades(tree, tree.getRoot(), parentClades, clades, taxonMap);
int size = clades.size();
// tree probability
for (int i = 0; i < size; i++) {
Clade c = clades.get(i);
// get the bits of the clade
Clade parent = parentClades.get(i);
// set the occurrences to epsilon
double tmp = EPSILON;
double parentOccurrences = 0.0;
BitSet parentBits = parent.getBits();
if (cladeProbabilities.containsKey(parentBits)) {
// if we observed this clade in the trace, add the
// occurrences
// to epsilon
parentOccurrences += cladeProbabilities.get(parentBits).getSampleCount();
}
if (cladeCoProbabilities.containsKey(parentBits)) {
// if we observed the parent clade
HashMap<BitSet, Clade> conditionalProbs = cladeCoProbabilities.get(parentBits);
BitSet bits = c.getBits();
if (conditionalProbs.containsKey(bits)) {
// if we observed this conditional clade in the trace,
// add
// the occurrences to epsilon
tmp += conditionalProbs.get(bits).getSampleCount();
}
}
// add epsilon for each clade
final double splits = Math.pow(2, parent.getSize() - 1) - 1;
parentOccurrences += EPSILON * splits;
// multiply the conditional clade probability to the tree
// probability
prob += Math.log(tmp / parentOccurrences);
}
return prob;
}
Aggregations