Search in sources :

Example 11 with FlexibleNode

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

the class NewickImporter method importNextTree.

/**
     * import the next tree.
     * return the tree or null if no more trees are available
     */
public Tree importNextTree() throws IOException, ImportException {
    FlexibleTree tree = null;
    try {
        skipUntil("(");
        unreadCharacter('(');
        FlexibleNode root = readInternalNode(lastTree);
        tree = new FlexibleTree(root, false, true);
    } catch (EOFException e) {
    //
    }
    lastTree = tree;
    return tree;
}
Also used : FlexibleTree(dr.evolution.tree.FlexibleTree) FlexibleNode(dr.evolution.tree.FlexibleNode) EOFException(java.io.EOFException)

Example 12 with FlexibleNode

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

the class NewickImporter method importTree.

/**
     * importTree.
     */
public Tree importTree(TaxonList taxonList) throws IOException, ImportException {
    setCommentDelimiters('[', ']', '\0', '\0', '&');
    try {
        skipUntil("(");
        unreadCharacter('(');
        final FlexibleNode root = readInternalNode(taxonList);
        if (getLastMetaComment() != null) {
            root.setAttribute(COMMENT, getLastMetaComment());
        }
        return new FlexibleTree(root, false, true);
    } catch (EOFException e) {
        throw new ImportException("incomplete tree");
    }
}
Also used : FlexibleTree(dr.evolution.tree.FlexibleTree) FlexibleNode(dr.evolution.tree.FlexibleNode) EOFException(java.io.EOFException)

Example 13 with FlexibleNode

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

the class NewickImporter method readBranch.

/**
     * Reads a branch in. This could be a node or a tip (calls readNode or readTip
     * accordingly). It then reads the branch length and SimpleNode that will
     * point at the new node or tip.
     */
private FlexibleNode readBranch(TaxonList taxonList) throws IOException, ImportException {
    double length = 0.0;
    FlexibleNode branch;
    if (nextCharacter() == '(') {
        // is an internal node
        branch = readInternalNode(taxonList);
    } else {
        // is an external node
        branch = readExternalNode(taxonList);
    }
    final String comment = getLastMetaComment();
    if (comment != null) {
        branch.setAttribute(COMMENT, comment);
        clearLastMetaComment();
    }
    if (getLastDelimiter() == ':') {
        length = readDouble(",():;");
    }
    branch.setLength(length);
    return branch;
}
Also used : FlexibleNode(dr.evolution.tree.FlexibleNode)

Example 14 with FlexibleNode

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

the class TaxaOriginTrait method getIncomingJumpOrigins.

private HashMap<String, String> getIncomingJumpOrigins(FlexibleTree tree) {
    HashMap<String, String> out = new HashMap<String, String>();
    FlexibleNode[] tips = getTipsOfInterest(tree);
    FlexibleNode mrca = findCommonAncestor(tree, tips);
    HashSet<FlexibleNode> taxaSet = new HashSet<FlexibleNode>(Arrays.asList(tips));
    HashSet<FlexibleNode> tipSet = getTipSet(tree, mrca);
    if (!taxaSet.containsAll(tipSet)) {
        System.out.println("WARNING: mixed traits in a clade");
    }
    if (!mrca.getAttribute(attributeName).equals(traitName)) {
        out.put(traitName, "Multiple");
        System.out.println("Multiple origin found.");
    } else {
        boolean sameTrait = true;
        FlexibleNode currentParent = mrca;
        while (sameTrait) {
            currentParent = (FlexibleNode) tree.getParent(currentParent);
            if (currentParent == null) {
                out.put(traitName, "root");
                break;
            } else {
                String parentTrait = (String) currentParent.getAttribute(attributeName);
                if (!parentTrait.equals(traitName)) {
                    sameTrait = false;
                    out.put(traitName, parentTrait);
                }
            }
        }
    }
    return out;
}
Also used : HashMap(java.util.HashMap) FlexibleNode(dr.evolution.tree.FlexibleNode) HashSet(java.util.HashSet)

Example 15 with FlexibleNode

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

the class TaxaOriginTrait method findCommonAncestor.

private FlexibleNode findCommonAncestor(FlexibleTree tree, FlexibleNode[] nodes) {
    HashSet<FlexibleNode> doneNodes = new HashSet<FlexibleNode>();
    FlexibleNode currentParent = nodes[0];
    for (FlexibleNode node : nodes) {
        doneNodes.add(node);
        boolean ancestorOfAllDoneNodes = false;
        currentParent = node;
        if (getTipSet(tree, currentParent).containsAll(doneNodes)) {
            ancestorOfAllDoneNodes = true;
        }
        while (!ancestorOfAllDoneNodes) {
            currentParent = (FlexibleNode) tree.getParent(currentParent);
            if (getTipSet(tree, currentParent).containsAll(doneNodes)) {
                ancestorOfAllDoneNodes = true;
            }
        }
    }
    return currentParent;
}
Also used : FlexibleNode(dr.evolution.tree.FlexibleNode) HashSet(java.util.HashSet)

Aggregations

FlexibleNode (dr.evolution.tree.FlexibleNode)28 FlexibleTree (dr.evolution.tree.FlexibleTree)12 EOFException (java.io.EOFException)6 IOException (java.io.IOException)5 Tree (dr.evolution.tree.Tree)4 Taxon (dr.evolution.util.Taxon)4 NexusExporter (dr.app.tools.NexusExporter)3 NodeRef (dr.evolution.tree.NodeRef)3 PrintStream (java.io.PrintStream)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 DecimalFormat (java.text.DecimalFormat)1