Search in sources :

Example 71 with NodeRef

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);
}
Also used : NodeRef(dr.evolution.tree.NodeRef) FileNotFoundException(java.io.FileNotFoundException) FileReader(java.io.FileReader) LinkedList(java.util.LinkedList)

Example 72 with NodeRef

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;
}
Also used : NodeRef(dr.evolution.tree.NodeRef)

Example 73 with NodeRef

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;
}
Also used : NodeRef(dr.evolution.tree.NodeRef)

Example 74 with NodeRef

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;
}
Also used : NodeRef(dr.evolution.tree.NodeRef)

Example 75 with NodeRef

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);
}
Also used : NodeRef(dr.evolution.tree.NodeRef) LinkedList(java.util.LinkedList)

Aggregations

NodeRef (dr.evolution.tree.NodeRef)426 ArrayList (java.util.ArrayList)38 LinkedList (java.util.LinkedList)20 Taxon (dr.evolution.util.Taxon)18 Tree (dr.evolution.tree.Tree)14 TreeModel (dr.evomodel.tree.TreeModel)14 Parameter (dr.inference.model.Parameter)14 Clade (dr.evolution.tree.Clade)13 MutableTree (dr.evolution.tree.MutableTree)9 Node (dr.evomodel.arg.ARGModel.Node)9 MultivariateTraitTree (dr.evolution.tree.MultivariateTraitTree)8 BranchMapModel (dr.evomodel.epidemiology.casetocase.BranchMapModel)8 BitSet (java.util.BitSet)8 FixedBitSet (jebl.util.FixedBitSet)8 FlexibleTree (dr.evolution.tree.FlexibleTree)7 AbstractCase (dr.evomodel.epidemiology.casetocase.AbstractCase)7 Matrix (dr.math.matrixAlgebra.Matrix)6 CompoundParameter (dr.inference.model.CompoundParameter)5 TimeScale (dr.evolution.util.TimeScale)4 MicrosatelliteSamplerTreeModel (dr.evomodel.tree.MicrosatelliteSamplerTreeModel)4