Search in sources :

Example 6 with NodeRef

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

Example 7 with NodeRef

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

Example 8 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 9 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 10 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)

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