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;
}
use of dr.evolution.tree.NodeRef in project beast-mcmc by beast-dev.
the class AbstractARGLikelihood method updateNodeAndDescendents.
/**
* Set update flag for a node and its children
*/
protected void updateNodeAndDescendents(NodeRef node) {
updateNode[node.getNumber()] = true;
for (int i = 0; i < treeModel.getChildCount(node); i++) {
NodeRef child = treeModel.getChild(node, i);
updateNodeAndDescendents(child);
}
likelihoodKnown = false;
}
use of dr.evolution.tree.NodeRef in project beast-mcmc by beast-dev.
the class simulateClusters method determine_membership_v3.
//traverse down the tree, top down, do calculation
int[] determine_membership_v3(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;
//parent cluster label
int parentClusterLabel = membership[treeModel.getParent(curElement).getNumber()];
//assign distCluster
for (int i = 0; i < (numClusters - 1); i++) {
distCluster[numClusters - 1][i] = distCluster[parentClusterLabel][i] + 1;
distCluster[i][numClusters - 1] = distCluster[i][parentClusterLabel] + 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);
}
Aggregations