Search in sources :

Example 6 with Clade

use of dr.evolution.tree.Clade in project beast-mcmc by beast-dev.

the class AbstractCladeImportanceDistribution method getParentClade.

/**
     * Finds the parent of a given clade in a list of clades. The parent is the
     * direct parent and not the grandparent or so.
     *
     * @param clades - list of clades in which we are searching the parent
     * @param child  - the child of whom we are searching the parent
     * @return the parent clade if found, otherwise itself
     */
protected Clade getParentClade(List<Clade> clades, Clade child) {
    Clade parent = null;
    BitSet childBits = child.getBits();
    int parentSize = Integer.MAX_VALUE;
    // minimum cardinality (least taxa) -> that's the parent :-)
    for (int i = 0; i < clades.size(); i++) {
        Clade tmp = clades.get(i);
        if (!child.equals(tmp) && containsClade(tmp.getBits(), childBits)) {
            if (parent == null || parentSize > tmp.getSize()) {
                parent = tmp;
                parentSize = parent.getSize();
            }
        }
    }
    // if there isn't a parent, then you probably asked for the whole tree
    if (parent == null) {
        parent = child;
    }
    return parent;
}
Also used : BitSet(java.util.BitSet) Clade(dr.evolution.tree.Clade)

Example 7 with Clade

use of dr.evolution.tree.Clade in project beast-mcmc by beast-dev.

the class AbstractCladeImportanceDistribution method getNonComplementaryClades.

/**
     * creates a list with all clades but just the non-complementary ones.
     * ((A,B),(C,D)) just {A,B,C,D} and {A,B} are inserted. {A,B} is
     * complementary to {C,D}
     *
     * @param tree - the tree from which the clades are extracted
     * @param node - the starting node. All clades below starting at this branch
     *             are added
     */
protected Clade getNonComplementaryClades(Tree tree, NodeRef node, List<Clade> parentClades, List<Clade> childClade, HashMap<String, Integer> taxonMap) {
    // create a new bit set for this clade
    BitSet bits = new BitSet();
    Clade c = null;
    // check if the node is external
    if (tree.isExternal(node)) {
        // if so, the only taxon in the clade is I
        // int index = node.getNumber();
        String taxon = tree.getTaxon(node.getNumber()).getId();
        int index = taxonMap.get(taxon);
        bits.set(index);
        c = new Clade(bits, tree.getNodeHeight(node));
    } else {
        // otherwise, call all children and add its taxon together to one
        // clade
        NodeRef childNode = tree.getChild(node, 0);
        // add just my first child to the list
        // the second child is complementary to the first
        Clade leftChild = getNonComplementaryClades(tree, childNode, parentClades, childClade, taxonMap);
        bits.or(leftChild.getBits());
        childNode = tree.getChild(node, 1);
        // add just my first child to the list
        // the second child is complementary to the first
        Clade rightChild = getNonComplementaryClades(tree, childNode, parentClades, childClade, taxonMap);
        bits.or(rightChild.getBits());
        c = new Clade(bits, tree.getNodeHeight(node));
        if (leftChild.getSize() >= 2) {
            parentClades.add(c);
            childClade.add(leftChild);
        } else if (rightChild.getSize() >= 2) {
            parentClades.add(c);
            childClade.add(rightChild);
        }
    }
    return c;
}
Also used : NodeRef(dr.evolution.tree.NodeRef) BitSet(java.util.BitSet) Clade(dr.evolution.tree.Clade)

Example 8 with Clade

use of dr.evolution.tree.Clade in project beast-mcmc by beast-dev.

the class WeightedMultiplicativeBinary method calculateTreeProbabilityLog.

/**
     * Calculates the probability of a given tree.
     *
     * @param tree - the tree to be analyzed
     * @return estimated posterior probability in log
     */
private double calculateTreeProbabilityLog(Tree tree) {
    double prob = 0.0;
    // calculate the number of possible splits
    final double splits = Math.pow(2, tree.getExternalNodeCount() - 1) - 1;
    List<Clade> clades = new ArrayList<Clade>();
    List<Clade> parentClades = new ArrayList<Clade>();
    // get clades contained in the tree
    getClades(tree, tree.getRoot(), parentClades, clades);
    // tree probability
    for (Clade c : clades) {
        // set the occurrences to epsilon
        double occurrences = EPSILON;
        if (cladeProbabilities.containsKey(c.getBits())) {
            // if we observed this clade in the trace, add the occurrences
            // to epsilon
            occurrences += cladeProbabilities.get(c.getBits()).getSampleCount();
        }
        // multiply the conditional clade probability to the tree
        // probability
        prob += Math.log(occurrences / (samples + (splits * EPSILON)));
    }
    return prob;
}
Also used : Clade(dr.evolution.tree.Clade)

Example 9 with Clade

use of dr.evolution.tree.Clade in project beast-mcmc by beast-dev.

the class AbstractImportanceDistributionOperator method getAbsoluteNodeHeights.

private double[] getAbsoluteNodeHeights(List<Clade> clades) {
    double[] nodeHeights = new double[clades.size()];
    int count = 0;
    for (Clade c : clades) {
        nodeHeights[count] = c.getHeight();
        count++;
    }
    return nodeHeights;
}
Also used : Clade(dr.evolution.tree.Clade)

Example 10 with Clade

use of dr.evolution.tree.Clade in project beast-mcmc by beast-dev.

the class AbstractImportanceDistributionOperator method extractClades.

/**
     * Creates a list with all clades of the tree
     *
     * @param tree   - the tree from which the clades are extracted
     * @param node   - the starting node. All clades below starting at this branch
     *               are added
     * @param clades - the list in which the clades are stored
     * @param bits   - a bit set to which the current bits of the clades are added
     */
private void extractClades(Tree tree, NodeRef node, List<Clade> clades, BitSet bits) {
    // create a new bit set for this clade
    BitSet bits2 = new BitSet();
    // check if the node is external
    if (tree.isExternal(node)) {
        // if so, the only taxon in the clade is I
        int index = node.getNumber();
        bits2.set(index);
    } else {
        // clade
        for (int i = 0; i < tree.getChildCount(node); i++) {
            NodeRef child = tree.getChild(node, i);
            extractClades(tree, child, clades, bits2);
        }
        // add my bit set to the list
        clades.add(new Clade(bits2, tree.getNodeHeight(node)));
    }
    // this is needed for adding all children clades together
    if (bits != null) {
        bits.or(bits2);
    }
}
Also used : NodeRef(dr.evolution.tree.NodeRef) Clade(dr.evolution.tree.Clade)

Aggregations

Clade (dr.evolution.tree.Clade)25 NodeRef (dr.evolution.tree.NodeRef)13 BitSet (java.util.BitSet)7 InvalidTreeException (dr.evolution.tree.MutableTree.InvalidTreeException)1