Search in sources :

Example 36 with TaxonList

use of dr.evolution.util.TaxonList in project beast-mcmc by beast-dev.

the class RestrictedPartialsParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    String name = xo.getId();
    TreeModel tree = (TreeModel) xo.getChild(TreeModel.class);
    TaxonList taxa = MonophylyStatisticParser.parseTaxonListOrTaxa(xo.getChild(MonophylyStatisticParser.MRCA));
    Parameter meanParameter = (Parameter) xo.getElementFirstChild(MultivariateDistributionLikelihood.MVN_MEAN);
    Parameter priorSampleSize = (Parameter) xo.getElementFirstChild(AbstractMultivariateTraitLikelihood.PRIOR_SAMPLE_SIZE);
    RestrictedPartials rp = null;
    try {
        rp = new RestrictedPartials(name, tree, taxa, meanParameter, priorSampleSize);
    } catch (TreeUtils.MissingTaxonException e) {
        throw new XMLParseException("Unable to find taxa for " + xo.getId());
    }
    return rp;
}
Also used : TreeModel(dr.evomodel.tree.TreeModel) TaxonList(dr.evolution.util.TaxonList) Parameter(dr.inference.model.Parameter) RestrictedPartials(dr.evomodel.continuous.RestrictedPartials) TreeUtils(dr.evolution.tree.TreeUtils)

Example 37 with TaxonList

use of dr.evolution.util.TaxonList in project beast-mcmc by beast-dev.

the class TreeModelParser 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);
    boolean fixHeights = xo.getAttribute(FIX_HEIGHTS, false);
    boolean fixTree = xo.getAttribute(FIX_TREE, false);
    TreeModel treeModel = new TreeModel(xo.getId(), tree, fixHeights, fixTree);
    Logger.getLogger("dr.evomodel").info("\nCreating the 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)) {
                ParameterParser.replaceParameter(cxo, treeModel.getRootHeightParameter());
            } 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);
                Parameter newParameter = treeModel.getLeafHeightParameter(node);
                ParameterParser.replaceParameter(cxo, newParameter);
                Taxon taxon = treeModel.getTaxon(index);
                setPrecisionBounds(newParameter, taxon);
            } else if (cxo.getName().equals(LEAF_HEIGHTS)) {
                // get a set of leaf height parameters out as a compound parameter...
                TaxonList taxa = (TaxonList) cxo.getChild(TaxonList.class);
                Parameter offsetParameter = (Parameter) cxo.getChild(Parameter.class);
                CompoundParameter leafHeights = new CompoundParameter("leafHeights");
                for (Taxon taxon : taxa) {
                    int index = treeModel.getTaxonIndex(taxon);
                    if (index == -1) {
                        throw new XMLParseException("taxon " + taxon.getId() + " not found for leafHeight element in treeModel element");
                    }
                    NodeRef node = treeModel.getExternalNode(index);
                    Parameter newParameter = treeModel.getLeafHeightParameter(node);
                    leafHeights.addParameter(newParameter);
                    setPrecisionBounds(newParameter, taxon);
                }
                ParameterParser.replaceParameter(cxo, leafHeights);
            } 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);
                boolean asMatrix = cxo.getAttribute(AS_MATRIX, 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");
                }
                Parameter newParameter = asMatrix ? treeModel.createNodeTraitsParameterAsMatrix(name, dim, initialValues, rootNode, internalNodes, leafNodes, fireTreeEvents) : treeModel.createNodeTraitsParameter(name, dim, initialValues, rootNode, internalNodes, leafNodes, fireTreeEvents);
                ParameterParser.replaceParameter(cxo, newParameter);
            } 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));
        }
    }
    // AR this is doubling up the number of bounds on each node.
    //        treeModel.setupHeightBounds();
    //System.err.println("done constructing treeModel");
    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 : TaxonList(dr.evolution.util.TaxonList) Taxon(dr.evolution.util.Taxon) CompoundParameter(dr.inference.model.CompoundParameter) TreeModel(dr.evomodel.tree.TreeModel) NodeRef(dr.evolution.tree.NodeRef) Tree(dr.evolution.tree.Tree) CompoundParameter(dr.inference.model.CompoundParameter) Parameter(dr.inference.model.Parameter)

Example 38 with TaxonList

use of dr.evolution.util.TaxonList in project beast-mcmc by beast-dev.

the class TMRCAStatisticParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    String name = xo.getAttribute(Statistic.NAME, xo.getId());
    Tree tree = (Tree) xo.getChild(Tree.class);
    TaxonList taxa = null;
    if (xo.hasChildNamed(MRCA)) {
        taxa = (TaxonList) xo.getElementFirstChild(MRCA);
    }
    boolean isAbsolute = xo.getAttribute(ABSOLUTE, false);
    boolean includeStem = false;
    if (xo.hasAttribute(PARENT) && xo.hasAttribute(STEM)) {
        throw new XMLParseException("Please use either " + PARENT + " or " + STEM + "!");
    } else if (xo.hasAttribute(PARENT)) {
        includeStem = xo.getBooleanAttribute(PARENT);
    } else if (xo.hasAttribute(STEM)) {
        includeStem = xo.getBooleanAttribute(STEM);
    }
    try {
        return new TMRCAStatistic(name, tree, taxa, isAbsolute, includeStem);
    } catch (TreeUtils.MissingTaxonException mte) {
        throw new XMLParseException("Taxon, " + mte + ", in " + getParserName() + "was not found in the tree.");
    }
}
Also used : TMRCAStatistic(dr.evomodel.tree.TMRCAStatistic) TaxonList(dr.evolution.util.TaxonList) Tree(dr.evolution.tree.Tree) TreeUtils(dr.evolution.tree.TreeUtils)

Example 39 with TaxonList

use of dr.evolution.util.TaxonList in project beast-mcmc by beast-dev.

the class AttributePatternsParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    String attributeName = xo.getStringAttribute(ATTRIBUTE);
    String secondaryAttributeName = xo.getAttribute(SECONDARY_ATTRIBUTE, (String) null);
    TaxonList taxa = (TaxonList) xo.getChild(TaxonList.class);
    DataType dataType = DataTypeUtils.getDataType(xo);
    if (dataType == null) {
        throw new XMLParseException("dataType expected for attributePatterns element");
    }
    // using a SimpleSiteList rather than Patterns to allow ancestral reconstruction
    SimpleSiteList patterns = new SimpleSiteList(dataType, taxa);
    int[] pattern = new int[taxa.getTaxonCount()];
    boolean attributeFound = false;
    for (int i = 0; i < taxa.getTaxonCount(); i++) {
        Taxon taxon = taxa.getTaxon(i);
        if (secondaryAttributeName == null || secondaryAttributeName.isEmpty()) {
            Object value = taxon.getAttribute(attributeName);
            if (value != null) {
                int state = dataType.getState(value.toString());
                if (state < 0) {
                    throw new XMLParseException("State for attribute, " + attributeName + ", in taxon, " + taxon.getId() + ", is unknown: " + value.toString());
                }
                pattern[i] = state;
                attributeFound = true;
            } else {
                pattern[i] = dataType.getUnknownState();
            }
        } else {
            Object value1 = taxon.getAttribute(attributeName);
            Object value2 = taxon.getAttribute(secondaryAttributeName);
            if (value1 != null && value2 != null) {
                String code = value1.toString() + CompositeDataTypeParser.COMPOSITE_STATE_SEPARATOR + value2.toString();
                int state = dataType.getState(code);
                if (state < 0) {
                    throw new XMLParseException("State for attributes, " + attributeName + " & " + secondaryAttributeName + ", in taxon, " + taxon.getId() + ", is unknown: " + code);
                }
                pattern[i] = state;
                attributeFound = true;
            } else {
                pattern[i] = dataType.getUnknownState();
            }
        }
    }
    if (!attributeFound) {
        throw new XMLParseException("The attribute, " + attributeName + " was missing in all taxa. Check the name of the attribute.");
    }
    patterns.addPattern(pattern);
    if (xo.hasAttribute(XMLParser.ID)) {
        Logger.getLogger("dr.evoxml").info("Read attribute patterns, '" + xo.getId() + "' for attribute, " + attributeName);
    } else {
        Logger.getLogger("dr.evoxml").info("Read attribute patterns for attribute, " + attributeName);
    }
    return patterns;
}
Also used : TaxonList(dr.evolution.util.TaxonList) Taxon(dr.evolution.util.Taxon) DataType(dr.evolution.datatype.DataType) SimpleSiteList(dr.evolution.alignment.SimpleSiteList)

Example 40 with TaxonList

use of dr.evolution.util.TaxonList in project beast-mcmc by beast-dev.

the class TipDateSamplingComponentGenerator method generate.

protected void generate(final InsertionPoint point, final Object item, final String prefix, final XMLWriter writer) {
    TipDateSamplingComponentOptions comp = (TipDateSamplingComponentOptions) options.getComponentOptions(TipDateSamplingComponentOptions.class);
    TaxonList taxa = comp.getTaxonSet();
    switch(point) {
        case IN_TREE_MODEL:
            {
                writeLeafHeightParameters(writer, (PartitionTreeModel) item, taxa);
            }
            break;
        case AFTER_TREE_MODEL:
            if (options.getPartitionTreeModels().size() > 1) {
                // we have multiple treeModels with some or all the same taxa - create a JointParameter for each...
                writeJointParameters(writer, taxa);
            }
            if (comp.tipDateSamplingType == TipDateSamplingType.SAMPLE_JOINT) {
                writer.writeOpenTag("compoundParameter", new Attribute[] { new Attribute.Default<String>(XMLParser.ID, "treeModel.tipDates") });
                for (int i = 0; i < taxa.getTaxonCount(); i++) {
                    Taxon taxon = taxa.getTaxon(i);
                    writer.writeIDref(ParameterParser.PARAMETER, "age(" + taxon.getId() + ")");
                }
                writer.writeCloseTag("compoundParameter");
            }
            break;
        case IN_MCMC_PRIOR:
            if (comp.tipDateSamplingType == TipDateSamplingType.SAMPLE_INDIVIDUALLY || comp.tipDateSamplingType == TipDateSamplingType.SAMPLE_PRECISION) {
            // nothing to do - individual parameter priors are written automatically
            } else if (comp.tipDateSamplingType == TipDateSamplingType.SAMPLE_JOINT) {
            }
            break;
        case IN_FILE_LOG_PARAMETERS:
            if (comp.tipDateSamplingType == TipDateSamplingType.SAMPLE_INDIVIDUALLY || comp.tipDateSamplingType == TipDateSamplingType.SAMPLE_PRECISION) {
                for (int i = 0; i < taxa.getTaxonCount(); i++) {
                    Taxon taxon = taxa.getTaxon(i);
                    writer.writeIDref(ParameterParser.PARAMETER, "age(" + taxon.getId() + ")");
                }
            } else if (comp.tipDateSamplingType == TipDateSamplingType.SAMPLE_JOINT) {
                writer.writeIDref(ParameterParser.PARAMETER, "treeModel.tipDates");
            }
            break;
        default:
            throw new IllegalArgumentException("This insertion point is not implemented for " + this.getClass().getName());
    }
}
Also used : Attribute(dr.util.Attribute) TaxonList(dr.evolution.util.TaxonList) Taxon(dr.evolution.util.Taxon) PartitionTreeModel(dr.app.beauti.options.PartitionTreeModel)

Aggregations

TaxonList (dr.evolution.util.TaxonList)59 Tree (dr.evolution.tree.Tree)18 Taxon (dr.evolution.util.Taxon)18 TreeUtils (dr.evolution.tree.TreeUtils)15 Taxa (dr.evolution.util.Taxa)13 Parameter (dr.inference.model.Parameter)13 ArrayList (java.util.ArrayList)13 TreeModel (dr.evomodel.tree.TreeModel)11 Alignment (dr.evolution.alignment.Alignment)6 SitePatterns (dr.evolution.alignment.SitePatterns)5 SimpleTree (dr.evolution.tree.SimpleTree)5 SimpleAlignment (dr.evolution.alignment.SimpleAlignment)4 Importer (dr.evolution.io.Importer)4 ImportException (dr.evolution.io.Importer.ImportException)4 NexusImporter (dr.evolution.io.NexusImporter)4 CoalescentLikelihood (dr.evomodel.coalescent.CoalescentLikelihood)4 CoalescentSimulator (dr.evomodel.coalescent.CoalescentSimulator)4 NodeRef (dr.evolution.tree.NodeRef)3 SubstitutionModel (dr.evomodel.substmodel.SubstitutionModel)3 CompoundLikelihood (dr.inference.model.CompoundLikelihood)3