use of dr.evolution.tree.NodeRef in project beast-mcmc by beast-dev.
the class ARGTraceAnalysis method analyzeARG.
/**
* Actually analyzes a particular tree using the trace given the burnin
*/
public final Tree analyzeARG(String target) {
int n = getTreeCount();
FlexibleTree meanTree = null;
for (int i = 0; i < n; i++) {
Tree tree = getARG(i);
if (TreeUtils.uniqueNewick(tree, tree.getRoot()).equals(target)) {
meanTree = new FlexibleTree(tree);
break;
}
}
if (meanTree == null)
throw new RuntimeException("No target tree in trace");
int m = meanTree.getInternalNodeCount();
for (int j = 0; j < m; j++) {
double[] heights = new double[n];
NodeRef node1 = meanTree.getInternalNode(j);
Set<String> leafSet = TreeUtils.getDescendantLeaves(meanTree, node1);
for (int i = 0; i < n; i++) {
Tree tree = getARG(i);
NodeRef node2 = TreeUtils.getCommonAncestorNode(tree, leafSet);
heights[i] = tree.getNodeHeight(node2);
}
meanTree.setNodeHeight(node1, dr.stats.DiscreteStatistics.mean(heights));
meanTree.setNodeAttribute(node1, "upper", new Double(dr.stats.DiscreteStatistics.quantile(0.975, heights)));
meanTree.setNodeAttribute(node1, "lower", new Double(dr.stats.DiscreteStatistics.quantile(0.025, heights)));
}
return meanTree;
}
use of dr.evolution.tree.NodeRef in project beast-mcmc by beast-dev.
the class ARGCoalescentLikelihood method calculateIntervals.
public void calculateIntervals() {
intervals.clear();
intervals.ensureCapacity(arg.getNodeCount());
NodeRef x;
for (int i = 0; i < arg.getInternalNodeCount(); i++) {
x = arg.getInternalNode(i);
if (arg.isReassortment(x)) {
intervals.add(new CoalescentInterval(arg.getNodeHeight(x), RECOMBINATION));
} else {
intervals.add(new CoalescentInterval(arg.getNodeHeight(x), COALESCENT));
}
}
for (int i = 0; i < arg.getExternalNodeCount(); i++) {
x = arg.getExternalNode(i);
if (arg.getNodeHeight(x) > 0.0) {
intervals.add(new CoalescentInterval(arg.getNodeHeight(x), NEW_SAMPLE));
}
}
dr.util.HeapSort.sort(intervals);
double a = 0, b = 0;
for (int i = 0; i < intervals.size(); i++) {
b = intervals.get(i).length;
intervals.get(i).length = intervals.get(i).length - a;
a = b;
}
intervalsKnown = true;
}
use of dr.evolution.tree.NodeRef in project beast-mcmc by beast-dev.
the class ClusterLabelsVirusesStatistic method determine_membership_v2.
//traverse down the tree, top down, do calculation
int[] determine_membership_v2(TreeModel treeModel) {
NodeRef root = treeModel.getRoot();
int numClusters = 1;
LinkedList<NodeRef> list = new LinkedList<NodeRef>();
list.addFirst(root);
int[] membership = new int[treeModel.getNodeCount()];
for (int i = 0; i < treeModel.getNodeCount(); i++) {
membership[i] = -1;
}
//root always given the first cluster
membership[root.getNumber()] = 0;
while (!list.isEmpty()) {
//do things with the current object
NodeRef curElement = list.pop();
//String content = "node #" + curElement.getNumber() +", taxon=" + treeModel.getNodeTaxon(curElement) + " and parent is = " ;
String content = "node #" + curElement.getNumber() + ", taxon= ";
if (treeModel.getNodeTaxon(curElement) == null) {
content += "internal node\t";
} else {
content += treeModel.getNodeTaxon(curElement).getId() + "\t";
//content += treeModel.getTaxonIndex(treeModel.getNodeTaxon(curElement)) + "\t";
}
if (treeModel.getParent(curElement) == null) {
//content += "no parent";
} else {
//content += "parent node#=" + treeModel.getParent(curElement).getNumber();
}
//cluster assignment:
if (!treeModel.isRoot(curElement)) {
if ((int) indicators.getParameterValue(curElement.getNumber()) == 1) {
numClusters++;
membership[curElement.getNumber()] = numClusters - 1;
} else {
//inherit from parent's cluster assignment
membership[curElement.getNumber()] = membership[treeModel.getParent(curElement).getNumber()];
}
}
//is not Root
content += " cluster = " + membership[curElement.getNumber()];
for (int childNum = 0; childNum < treeModel.getChildCount(curElement); childNum++) {
list.addFirst(treeModel.getChild(curElement, childNum));
}
}
return (membership);
}
use of dr.evolution.tree.NodeRef in project beast-mcmc by beast-dev.
the class PathStatistic method determine_from_membership_v3.
int[] determine_from_membership_v3() {
int[] printFromCluster = new int[treeModel.getNodeCount()];
for (int i = 0; i < treeModel.getNodeCount(); i++) {
printFromCluster[i] = -1;
}
int[] fromMembership = new int[treeModel.getNodeCount()];
for (int i = 0; i < treeModel.getNodeCount(); i++) {
fromMembership[i] = -99;
}
NodeRef root = treeModel.getRoot();
int numClusters = 1;
LinkedList<NodeRef> list = new LinkedList<NodeRef>();
list.addFirst(root);
int[] membership = new int[treeModel.getNodeCount()];
for (int i = 0; i < treeModel.getNodeCount(); i++) {
membership[i] = -1;
}
//root always given the first cluster
membership[root.getNumber()] = 0;
fromMembership[0] = -1;
while (!list.isEmpty()) {
//do things with the current object
NodeRef curElement = list.pop();
//cluster assignment:
if (!treeModel.isRoot(curElement)) {
if ((int) indicators.getParameterValue(curElement.getNumber()) == 1) {
numClusters++;
membership[curElement.getNumber()] = numClusters - 1;
fromMembership[curElement.getNumber()] = membership[treeModel.getParent(curElement).getNumber()];
} else {
//inherit from parent's cluster assignment
membership[curElement.getNumber()] = membership[treeModel.getParent(curElement).getNumber()];
fromMembership[curElement.getNumber()] = fromMembership[treeModel.getParent(curElement).getNumber()];
}
} else //is not Root
{
fromMembership[curElement.getNumber()] = -1;
}
for (int childNum = 0; childNum < treeModel.getChildCount(curElement); childNum++) {
list.addFirst(treeModel.getChild(curElement, childNum));
}
}
for (int i = 0; i < treeModel.getNodeCount(); i++) {
if ((int) indicators.getParameterValue(i) == 1) {
printFromCluster[membership[i]] = fromMembership[i];
}
}
return (printFromCluster);
}
use of dr.evolution.tree.NodeRef in project beast-mcmc by beast-dev.
the class PathStatistic method determine_from_membership_v2.
//traverse down the tree, top down, do calculation
int[] determine_from_membership_v2() {
//note: I set MAX_DIM as the most I would print, but in order to avoid bug, I
//declare the number of nodes as the most active nodes I can have.
int[] fromMembership = new int[treeModel.getNodeCount()];
for (int i = 0; i < treeModel.getNodeCount(); i++) {
fromMembership[i] = -1;
}
NodeRef root = treeModel.getRoot();
int numClusters = 1;
LinkedList<NodeRef> list = new LinkedList<NodeRef>();
list.addFirst(root);
int[] membership = new int[treeModel.getNodeCount()];
for (int i = 0; i < treeModel.getNodeCount(); i++) {
membership[i] = -1;
}
//root always given the first cluster
membership[root.getNumber()] = 0;
while (!list.isEmpty()) {
//do things with the current object
NodeRef curElement = list.pop();
//String content = "node #" + curElement.getNumber() +", taxon=" + treeModel.getNodeTaxon(curElement) + " and parent is = " ;
String content = "node #" + curElement.getNumber() + ", taxon= ";
if (treeModel.getNodeTaxon(curElement) == null) {
content += "internal node\t";
} else {
content += treeModel.getNodeTaxon(curElement).getId() + "\t";
//content += treeModel.getTaxonIndex(treeModel.getNodeTaxon(curElement)) + "\t";
}
if (treeModel.getParent(curElement) == null) {
//content += "no parent";
} else {
//content += "parent node#=" + treeModel.getParent(curElement).getNumber();
}
//cluster assignment:
if (!treeModel.isRoot(curElement)) {
if ((int) indicators.getParameterValue(curElement.getNumber()) == 1) {
// System.out.print("indicator # " + curElement.getNumber() + " ");
numClusters++;
membership[curElement.getNumber()] = numClusters - 1;
fromMembership[numClusters - 1] = membership[treeModel.getParent(curElement).getNumber()];
// System.out.println(" membership " + (numClusters-1) + " assigned from " + membership[ treeModel.getParent(curElement).getNumber()] );
} else {
//inherit from parent's cluster assignment
membership[curElement.getNumber()] = membership[treeModel.getParent(curElement).getNumber()];
}
}
//is not Root
content += " cluster = " + membership[curElement.getNumber()];
for (int childNum = 0; childNum < treeModel.getChildCount(curElement); childNum++) {
list.addFirst(treeModel.getChild(curElement, childNum));
}
}
return (fromMembership);
}
Aggregations