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);
}
use of dr.evolution.tree.NodeRef in project beast-mcmc by beast-dev.
the class TreeClusterGibbsOperator method determine_membership.
//traverse down the tree, top down, do calculation
static int[] determine_membership(TreeModel treeModel, int[] cutNodes, int numCuts) {
//TEMPORARY SOLUTION
//load in the titer, corresponding to the taxon #.
TiterImporter titer = null;
FileReader fileReader;
try {
fileReader = new FileReader("/Users/charles/Documents/research/antigenic/GenoPheno/data/taxon_y_titer.txt");
titer = new TiterImporter(fileReader);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
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 (isCutNode(curElement.getNumber(), cutNodes, numCuts)) {
//if(isCutNode(curElement.getNumber())){
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 ARGLikelihood method calculateLogLikelihood.
// **************************************************************
// Likelihood IMPLEMENTATION
// **************************************************************
/**
* Calculate the log likelihood of the current state.
*
* @return the log likelihood.
*/
protected double calculateLogLikelihood() {
if (reconstructTree) {
reconstructTree();
}
NodeRef root = tree.getRoot();
if (rootPartials == null) {
rootPartials = new double[patternCount * stateCount];
}
if (patternLogLikelihoods == null) {
patternLogLikelihoods = new double[patternCount];
}
if (!integrateAcrossCategories) {
if (siteCategories == null) {
siteCategories = new int[patternCount];
}
for (int i = 0; i < patternCount; i++) {
siteCategories[i] = siteModel.getCategoryOfSite(i);
}
}
try {
traverse(tree, root);
} catch (NegativeBranchLengthException e) {
System.err.println("Negative branch length found, trying to return 0 likelihood");
return Double.NEGATIVE_INFINITY;
}
//so change flags to reflect this.
for (int i = 0; i < nodeCount; i++) {
updateNode[i] = false;
}
//********************************************************************
double logL = 0.0;
for (int i = 0; i < patternCount; i++) {
// System.err.printf("Pattern %2d: %5.4f %5.4f\n",i,patternLogLikelihoods[i],patternWeights[i]);
logL += patternLogLikelihoods[i] * patternWeights[i];
}
return logL;
}
use of dr.evolution.tree.NodeRef in project beast-mcmc by beast-dev.
the class AbstractARGLikelihood method updateNodeAndChildren.
/**
* Set update flag for a node and its children
*/
protected void updateNodeAndChildren(NodeRef node) {
updateNode[node.getNumber()] = true;
for (int i = 0; i < treeModel.getChildCount(node); i++) {
NodeRef child = treeModel.getChild(node, i);
updateNode[child.getNumber()] = true;
}
likelihoodKnown = false;
}
Aggregations