Search in sources :

Example 16 with FlexibleNode

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

the class NexusImporter method readInternalNode.

/**
     * Reads a node in. This could be a polytomy. Calls readBranch on each branch
     * in the node.
     */
FlexibleNode readInternalNode(HashMap<String, Taxon> translationList) throws IOException, ImportException {
    FlexibleNode node = new FlexibleNode();
    // read the opening '('
    readCharacter();
    // read the first child
    node.addChild(readBranch(translationList));
    if (getLastDelimiter() != ',' && !suppressWarnings) {
        java.util.logging.Logger.getLogger("dr.evolution.io").warning("Internal node only has a single child.");
    }
    // this allows one or more children
    while (getLastDelimiter() == ',') {
        node.addChild(readBranch(translationList));
    }
    // should have had a closing ')'
    if (getLastDelimiter() != ')') {
        throw new BadFormatException("Missing closing ')' in tree in TREES block");
    }
    readToken(":(),;");
    if (getLastMetaComment() != null) {
        if (!ignoreMetaComments) {
            // \[&label[=value][,label[=value]>[,/..]]\]
            try {
                parseMetaCommentPairs(getLastMetaComment(), node);
            } catch (BadFormatException bfe) {
            // ignore it
            }
        }
        clearLastMetaComment();
    }
    // find the next delimiter
    return node;
}
Also used : FlexibleNode(dr.evolution.tree.FlexibleNode)

Example 17 with FlexibleNode

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

the class NexusImporter method readNextTree.

private Tree readNextTree(HashMap<String, Taxon> translationList, String[] lastToken, TaxonList taxonList) throws ImportException, IOException {
    try {
        Tree tree = null;
        String token = lastToken[0];
        if (token.equalsIgnoreCase("UTREE") || token.equalsIgnoreCase("TREE")) {
            if (nextCharacter() == '*') {
                // Star is used to specify a default tree - ignore it
                readCharacter();
            }
            String token2 = readToken("=;");
            // Save tree comment and attach it later
            final String comment = getLastMetaComment();
            clearLastMetaComment();
            if (getLastDelimiter() != '=') {
                throw new BadFormatException("Missing label for tree'" + token2 + "' or missing '=' in TREE command of TREES block");
            }
            try {
                if (nextCharacter() != '(') {
                    throw new BadFormatException("Missing tree definition in TREE command of TREES block");
                }
                // tree special comments
                final String scomment = getLastMetaComment();
                clearLastMetaComment();
                FlexibleNode root = readInternalNode(translationList);
                if (translationList != null) {
                    // this ensures that if a translation list is used, the external node numbers
                    // of the trees correspond as well.
                    Map<Taxon, Integer> taxonNumberMap = new HashMap<Taxon, Integer>();
                    int count = 0;
                    for (String label : translationList.keySet()) {
                        Taxon taxon = translationList.get(label);
                        int number;
                        if (taxonList != null) {
                            // Map back to original numbering from TaxonList
                            number = taxonList.getTaxonIndex(taxon);
                        } else {
                            // Old functionality
                            try {
                                number = Integer.parseInt(label) - 1;
                            } catch (NumberFormatException nfe) {
                                number = count;
                            }
                        }
                        taxonNumberMap.put(taxon, number);
                        count++;
                    }
                    tree = new FlexibleTree(root, false, true, taxonNumberMap);
                } else {
                    tree = new FlexibleTree(root, false, true, null);
                }
                tree.setId(token2);
                if (getLastDelimiter() == ':') {
                    // in case the root has a branch length, skip it
                    readToken(";");
                    if (getLastMetaComment() != null) {
                        // \[&label[=value][,label[=value]>[,/..]]\]
                        try {
                            parseMetaCommentPairs(getLastMetaComment(), root);
                        } catch (BadFormatException bfe) {
                        // ignore it
                        }
                        clearLastMetaComment();
                    }
                }
                if (getLastDelimiter() != ';') {
                    throw new BadFormatException("Expecting ';' after tree, '" + token2 + "', TREE command of TREES block");
                }
                if (scomment != null) {
                    // below is correct only if [&W] appears on it own
                    String c = scomment;
                    while (c.length() > 0) {
                        final char ch = c.charAt(0);
                        if (ch == ';') {
                            c = c.substring(1);
                            continue;
                        }
                        if (ch == 'R') {
                            // we only have rooted trees anyway
                            c = c.substring(1);
                        } else if (ch == 'W') {
                            int e = c.indexOf(';');
                            if (e < 0)
                                e = c.length();
                            try {
                                final Float value = new Float(c.substring(2, e));
                                tree.setAttribute("weight", value);
                            } catch (NumberFormatException ex) {
                            // don't fail, ignore
                            }
                            c = c.substring(e);
                        } else {
                            c = c.substring(1);
                        }
                    }
                }
                if (comment != null) {
                    try {
                        parseMetaCommentPairs(comment, tree);
                    } catch (Importer.BadFormatException e) {
                        // set generic comment attribute
                        tree.setAttribute("comment", comment);
                    }
                }
            } catch (EOFException e) {
                // If we reach EOF we may as well return what we have?
                return tree;
            }
            token = readToken(";");
        } else if (token.equalsIgnoreCase("ENDBLOCK") || token.equalsIgnoreCase("END")) {
            return null;
        } else {
            throw new BadFormatException("Unknown command '" + token + "' in TREES block");
        }
        //added this to escape readNextTree loop correctly -- AJD
        lastToken[0] = token;
        return tree;
    } catch (EOFException e) {
        return null;
    }
}
Also used : HashMap(java.util.HashMap) FlexibleTree(dr.evolution.tree.FlexibleTree) FlexibleNode(dr.evolution.tree.FlexibleNode) FlexibleTree(dr.evolution.tree.FlexibleTree) Tree(dr.evolution.tree.Tree)

Example 18 with FlexibleNode

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

the class MigrateTreeImporter method readInternalNode.

/**
     * Reads a node in. This could be a polytomy. Calls readBranch on each branch
     * in the node.
     */
FlexibleNode readInternalNode(HashMap<String, Taxon> translationList) throws IOException, ImportException {
    FlexibleNode node = new FlexibleNode();
    // read the opening '('
    readCharacter();
    // read the first child
    FlexibleNode firstChild = readBranch(translationList);
    node.addChild(firstChild);
    // an internal node must have at least 2 children
    if (getLastDelimiter() != ',') {
        throw new BadFormatException("Missing ',' in tree in TREES block");
    }
    // read subsequent children
    do {
        node.addChild(readBranch(translationList));
    } while (getLastDelimiter() == ',');
    // should have had a closing ')'
    if (getLastDelimiter() != ')') {
        throw new BadFormatException("Missing closing ')' in tree in TREES block");
    }
    readToken(":(),;");
    if (getLastMetaComment() != null) {
        parseMigrationString(getLastMetaComment(), node);
        clearLastMetaComment();
    }
    // find the next delimiter
    return node;
}
Also used : FlexibleNode(dr.evolution.tree.FlexibleNode)

Example 19 with FlexibleNode

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

the class NewickImporter method readExternalNode.

/**
     * Reads an external node in.
     */
private FlexibleNode readExternalNode(TaxonList taxonList) throws IOException, ImportException {
    FlexibleNode node = new FlexibleNode();
    String label = readToken(":(),;");
    Taxon taxon;
    if (taxonList != null) {
        // if a taxon list is given then the taxon must be in it...
        int index = taxonList.getTaxonIndex(label);
        if (index != -1) {
            taxon = taxonList.getTaxon(index);
        } else {
            throw new UnknownTaxonException("Taxon in tree, '" + label + "' is unknown");
        }
    } else {
        // No taxon list given so create new taxa
        taxon = new Taxon(label);
    }
    node.setTaxon(taxon);
    return node;
}
Also used : FlexibleNode(dr.evolution.tree.FlexibleNode) Taxon(dr.evolution.util.Taxon)

Example 20 with FlexibleNode

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

the class NewickImporter method readInternalNode.

/**
     * Reads a node in. This could be a polytomy. Calls readBranch on each branch
     * in the node.
     */
private FlexibleNode readInternalNode(TaxonList taxonList) throws IOException, ImportException {
    FlexibleNode node = new FlexibleNode();
    // read the opening '('
    final char ch = readCharacter();
    assert ch == '(';
    // read the first child
    node.addChild(readBranch(taxonList));
    if (getLastDelimiter() != ',') {
        java.util.logging.Logger.getLogger("dr.evolution.io").warning("Internal node only has a single child.");
    }
    // read subsequent children
    while (getLastDelimiter() == ',') {
        node.addChild(readBranch(taxonList));
    }
    // should have had a closing ')'
    if (getLastDelimiter() != ')') {
        throw new BadFormatException("Missing closing ')' in tree");
    }
    // If there is a label before the colon, store it:
    try {
        String label = readToken(",():;");
        if (label.length() > 0) {
            node.setAttribute("label", label);
        }
    } catch (IOException ioe) {
    // probably an end of file without a terminal ';'
    // we are going to allow this and return the nodes...
    }
    return node;
}
Also used : FlexibleNode(dr.evolution.tree.FlexibleNode) IOException(java.io.IOException)

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