Search in sources :

Example 6 with FlexibleNode

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

the class MigrateTreeImporter 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.
     */
FlexibleNode readBranch(HashMap<String, Taxon> translationList) throws IOException, ImportException {
    double length = 0.0;
    FlexibleNode branch;
    clearLastMetaComment();
    if (nextCharacter() == '(') {
        // is an internal node
        branch = readInternalNode(translationList);
    } else {
        // is an external node
        branch = readExternalNode(translationList);
    }
    if (getLastDelimiter() != ':' && getLastDelimiter() != ',' && getLastDelimiter() != ')') {
        String label = readToken(",():;");
        if (label.length() > 0) {
            branch.setAttribute("label", label);
        }
    }
    if (getLastDelimiter() == ':') {
        length = readDouble(" ,():;");
        if (getLastMetaComment() != null) {
            parseMigrationString(getLastMetaComment(), branch);
            clearLastMetaComment();
        }
    }
    branch.setLength(length);
    return branch;
}
Also used : FlexibleNode(dr.evolution.tree.FlexibleNode)

Example 7 with FlexibleNode

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

the class NexusImporter method readExternalNode.

//	private void labelNode(FlexibleNode node, String label, String value) {
//		// Attempt to format the value as a number
//		Number number = null;
//		try {
//			number = Integer.valueOf(value);
//		} catch (NumberFormatException nfe1) {
//			try {
//				number = Double.valueOf(value);
//			} catch (NumberFormatException nfe2) {
//				//
//			}
//		}
//		if (number != null) {
//			node.setAttribute(label, number);
//		} else {
//			node.setAttribute(label, value);
//		}
//	}
/**
     * Reads an external node in.
     */
FlexibleNode readExternalNode(HashMap<String, Taxon> translationList) throws ImportException, IOException {
    FlexibleNode node = new FlexibleNode();
    String label = readToken(":(),;");
    Taxon taxon;
    if (translationList.size() > 0) {
        taxon = translationList.get(label);
        if (taxon == null) {
            // taxon not found in taxon list...
            throw new UnknownTaxonException("Taxon in tree, '" + label + "' is unknown");
        }
    } else {
        taxon = new Taxon(label);
    }
    if (getLastMetaComment() != null) {
        if (!ignoreMetaComments) {
            // \[&label[=value][,label[=value]>[,/..]]\]
            try {
                parseMetaCommentPairs(getLastMetaComment(), node);
            } catch (BadFormatException bfe) {
            // ignore it
            }
        }
        clearLastMetaComment();
    }
    node.setTaxon(taxon);
    return node;
}
Also used : FlexibleNode(dr.evolution.tree.FlexibleNode)

Example 8 with FlexibleNode

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

the class NexusImporter 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.
     */
FlexibleNode readBranch(HashMap<String, Taxon> translationList) throws IOException, ImportException {
    double length = 0.0;
    FlexibleNode branch;
    clearLastMetaComment();
    if (nextCharacter() == '(') {
        // is an internal node
        branch = readInternalNode(translationList);
    } else {
        // is an external node
        branch = readExternalNode(translationList);
    }
    if (getLastDelimiter() != ':' && getLastDelimiter() != ',' && getLastDelimiter() != ')') {
        String label = readToken(",():;");
        if (label.length() > 0) {
            branch.setAttribute("label", label);
        }
    }
    if (getLastDelimiter() == ':') {
        length = readDouble(",():;");
        if (getLastMetaComment() != null) {
            if (!ignoreMetaComments) {
                // \[&label[=value][,label[=value]>[,/..]]\]
                try {
                    parseMetaCommentPairs(getLastMetaComment(), branch);
                } catch (BadFormatException bfe) {
                // ignore it
                }
            }
            clearLastMetaComment();
        }
    }
    branch.setLength(length);
    return branch;
}
Also used : FlexibleNode(dr.evolution.tree.FlexibleNode)

Example 9 with FlexibleNode

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

the class MigrateTreeImporter method readExternalNode.

/**
     * Reads an external node in.
     */
FlexibleNode readExternalNode(HashMap<String, Taxon> translationList) throws ImportException, IOException {
    FlexibleNode node = new FlexibleNode();
    String label = readToken(":(),;");
    Taxon taxon;
    if (translationList.size() > 0) {
        taxon = translationList.get(label);
        if (taxon == null) {
            // taxon not found in taxon list...
            throw new UnknownTaxonException("Taxon in tree, '" + label + "' is unknown");
        }
    } else {
        taxon = new Taxon(label);
    }
    if (getLastMetaComment() != null) {
        parseMigrationString(getLastMetaComment(), node);
        clearLastMetaComment();
    }
    node.setTaxon(taxon);
    int pop = Integer.parseInt(label.split("\\.")[0]);
    node.setAttribute(POP, (pop - 1));
    return node;
}
Also used : FlexibleNode(dr.evolution.tree.FlexibleNode) Taxon(dr.evolution.util.Taxon)

Example 10 with FlexibleNode

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

the class NewickImporter method importTrees.

/**
     * importTrees.
     */
public Tree[] importTrees(TaxonList taxonList) throws IOException, ImportException {
    boolean done = false;
    ArrayList<FlexibleTree> array = new ArrayList<FlexibleTree>();
    do {
        try {
            skipUntil("(");
            unreadCharacter('(');
            FlexibleNode root = readInternalNode(taxonList);
            FlexibleTree tree = new FlexibleTree(root, false, true);
            array.add(tree);
            if (taxonList == null) {
                taxonList = tree;
            }
            if (readCharacter() != ';') {
                throw new BadFormatException("Expecting ';' after tree");
            }
        } catch (EOFException e) {
            done = true;
        }
    } while (!done);
    Tree[] trees = new Tree[array.size()];
    array.toArray(trees);
    return trees;
}
Also used : FlexibleTree(dr.evolution.tree.FlexibleTree) FlexibleNode(dr.evolution.tree.FlexibleNode) ArrayList(java.util.ArrayList) EOFException(java.io.EOFException) FlexibleTree(dr.evolution.tree.FlexibleTree) Tree(dr.evolution.tree.Tree)

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