Search in sources :

Example 1 with StarTreeModel

use of dr.evomodel.tree.StarTreeModel in project beast-mcmc by beast-dev.

the class StarTreeModelParser method parseXMLObject.

/**
 * @return a tree object based on the XML element it was passed.
 */
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    Tree tree = (Tree) xo.getChild(Tree.class);
    StarTreeModel treeModel = new StarTreeModel(xo.getId(), tree);
    Logger.getLogger("dr.evomodel").info("Creating a star tree model, '" + xo.getId() + "'");
    for (int i = 0; i < xo.getChildCount(); i++) {
        if (xo.getChild(i) instanceof XMLObject) {
            XMLObject cxo = (XMLObject) xo.getChild(i);
            if (cxo.getName().equals(ROOT_HEIGHT)) {
                if (cxo.getRawChild(0) instanceof Reference) {
                    // treeModel.setRootHeightParameter((Parameter) cxo.getChild(Parameter.class));
                    throw new XMLParseException("Can not provide idref to a new root height parameter");
                } else {
                    ParameterParser.replaceParameter(cxo, treeModel.getRootHeightParameter());
                }
            } else if (cxo.getName().equals(SHARE_ROOT)) {
                DefaultTreeModel sharedRoot = (DefaultTreeModel) cxo.getChild(DefaultTreeModel.class);
                treeModel.setSharedRootHeightParameter(sharedRoot);
            } else if (cxo.getName().equals(LEAF_HEIGHT)) {
                String taxonName;
                if (cxo.hasAttribute(TAXON)) {
                    taxonName = cxo.getStringAttribute(TAXON);
                } else {
                    throw new XMLParseException("taxa element missing from leafHeight element in treeModel element");
                }
                int index = treeModel.getTaxonIndex(taxonName);
                if (index == -1) {
                    throw new XMLParseException("taxon " + taxonName + " not found for leafHeight element in treeModel element");
                }
                NodeRef node = treeModel.getExternalNode(index);
                ParameterParser.replaceParameter(cxo, treeModel.getLeafHeightParameter(node));
            // } else if (cxo.getName().equals(NODE_HEIGHTS)) {
            // 
            // boolean rootNode = cxo.getAttribute(ROOT_NODE, false);
            // boolean internalNodes = cxo.getAttribute(INTERNAL_NODES, false);
            // boolean leafNodes = cxo.getAttribute(LEAF_NODES, false);
            // 
            // if (!rootNode && !internalNodes && !leafNodes) {
            // throw new XMLParseException("one or more of root, internal or leaf nodes must be selected for the nodeHeights element");
            // }
            // 
            // ParameterParser.replaceParameter(cxo, treeModel.createNodeHeightsParameter(rootNode, internalNodes, leafNodes));
            // 
            // } else if (cxo.getName().equals(NODE_RATES)) {
            // 
            // boolean rootNode = cxo.getAttribute(ROOT_NODE, false);
            // boolean internalNodes = cxo.getAttribute(INTERNAL_NODES, false);
            // boolean leafNodes = cxo.getAttribute(LEAF_NODES, false);
            // double[] initialValues = null;
            // 
            // if (cxo.hasAttribute(INITIAL_VALUE)) {
            // initialValues = cxo.getDoubleArrayAttribute(INITIAL_VALUE);
            // }
            // 
            // if (!rootNode && !internalNodes && !leafNodes) {
            // throw new XMLParseException("one or more of root, internal or leaf nodes must be selected for the nodeRates element");
            // }
            // 
            // ParameterParser.replaceParameter(cxo, treeModel.createNodeRatesParameter(initialValues, rootNode, internalNodes, leafNodes));
            // 
            // } else if (cxo.getName().equals(NODE_TRAITS)) {
            // 
            // boolean rootNode = cxo.getAttribute(ROOT_NODE, false);
            // boolean internalNodes = cxo.getAttribute(INTERNAL_NODES, false);
            // boolean leafNodes = cxo.getAttribute(LEAF_NODES, false);
            // boolean fireTreeEvents = cxo.getAttribute(FIRE_TREE_EVENTS, false);
            // String name = cxo.getAttribute(NAME, "trait");
            // int dim = cxo.getAttribute(MULTIVARIATE_TRAIT, 1);
            // 
            // double[] initialValues = null;
            // if (cxo.hasAttribute(INITIAL_VALUE)) {
            // initialValues = cxo.getDoubleArrayAttribute(INITIAL_VALUE);
            // }
            // 
            // if (!rootNode && !internalNodes && !leafNodes) {
            // throw new XMLParseException("one or more of root, internal or leaf nodes must be selected for the nodeTraits element");
            // }
            // 
            // ParameterParser.replaceParameter(cxo, treeModel.createNodeTraitsParameter(name, dim, initialValues, rootNode, internalNodes, leafNodes, fireTreeEvents));
            } else if (cxo.getName().equals(LEAF_TRAIT)) {
                String name = cxo.getAttribute(NAME, "trait");
                String taxonName;
                if (cxo.hasAttribute(TAXON)) {
                    taxonName = cxo.getStringAttribute(TAXON);
                } else {
                    throw new XMLParseException("taxa element missing from leafTrait element in treeModel element");
                }
                int index = treeModel.getTaxonIndex(taxonName);
                if (index == -1) {
                    throw new XMLParseException("taxon '" + taxonName + "' not found for leafTrait element in treeModel element");
                }
                NodeRef node = treeModel.getExternalNode(index);
                Parameter parameter = treeModel.getNodeTraitParameter(node, name);
                if (parameter == null)
                    throw new XMLParseException("trait '" + name + "' not found for leafTrait (taxon, " + taxonName + ") element in treeModel element");
                ParameterParser.replaceParameter(cxo, parameter);
            } else {
                throw new XMLParseException("illegal child element in " + getParserName() + ": " + cxo.getName());
            }
        } else if (xo.getChild(i) instanceof Tree) {
        // do nothing - already handled
        } else {
            throw new XMLParseException("illegal child element in  " + getParserName() + ": " + xo.getChildName(i) + " " + xo.getChild(i));
        }
    }
    Logger.getLogger("dr.evomodel").info("  initial tree topology = " + TreeUtils.uniqueNewick(treeModel, treeModel.getRoot()));
    Logger.getLogger("dr.evomodel").info("  tree height = " + treeModel.getNodeHeight(treeModel.getRoot()));
    return treeModel;
}
Also used : NodeRef(dr.evolution.tree.NodeRef) Tree(dr.evolution.tree.Tree) StarTreeModel(dr.evomodel.tree.StarTreeModel) Parameter(dr.inference.model.Parameter) DefaultTreeModel(dr.evomodel.tree.DefaultTreeModel)

Aggregations

NodeRef (dr.evolution.tree.NodeRef)1 Tree (dr.evolution.tree.Tree)1 DefaultTreeModel (dr.evomodel.tree.DefaultTreeModel)1 StarTreeModel (dr.evomodel.tree.StarTreeModel)1 Parameter (dr.inference.model.Parameter)1