Search in sources :

Example 56 with TaxonList

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

the class AncestralTraitParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    String traitName = xo.getAttribute(TRAIT_NAME, STATES);
    String name = xo.getAttribute(NAME, traitName);
    Tree tree = (Tree) xo.getChild(Tree.class);
    TreeTraitProvider treeTraitProvider = (TreeTraitProvider) xo.getChild(TreeTraitProvider.class);
    TaxonList taxa = null;
    if (xo.hasChildNamed(MRCA)) {
        taxa = (TaxonList) xo.getElementFirstChild(MRCA);
    }
    TreeTrait trait = treeTraitProvider.getTreeTrait(traitName);
    if (trait == null) {
        throw new XMLParseException("A trait called, " + traitName + ", was not available from the TreeTraitProvider supplied to " + getParserName() + (xo.hasId() ? ", with ID " + xo.getId() : ""));
    }
    try {
        return new AncestralTrait(name, trait, tree, taxa);
    } catch (TreeUtils.MissingTaxonException mte) {
        throw new XMLParseException("Taxon, " + mte + ", in " + getParserName() + "was not found in the tree.");
    }
}
Also used : TreeTraitProvider(dr.evolution.tree.TreeTraitProvider) TaxonList(dr.evolution.util.TaxonList) Tree(dr.evolution.tree.Tree) AncestralTrait(dr.evomodel.tree.AncestralTrait) TreeTrait(dr.evolution.tree.TreeTrait) TreeUtils(dr.evolution.tree.TreeUtils)

Example 57 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 = true;
    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;
            } else {
                attributeFound = false;
            }
        } 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;
            } else {
                attributeFound = false;
            }
        }
    }
    if (!attributeFound) {
        throw new XMLParseException("The attribute, " + attributeName + " was not found 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 58 with TaxonList

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

the class TreeTraitParserUtilities method jitter.

public void jitter(XMLObject xo, int length, List<Integer> missingIndices) throws XMLParseException {
    XMLObject cxo = xo.getChild(TreeTraitParserUtilities.JITTER);
    Parameter traits = (Parameter) cxo.getChild(Parameter.class);
    // Must be included, no default value
    double[] window = cxo.getDoubleArrayAttribute(TreeTraitParserUtilities.WINDOW);
    // default = true
    boolean duplicates = cxo.getAttribute(TreeTraitParserUtilities.DUPLICATES, true);
    TaxonList taxonList = (TaxonList) cxo.getChild(TaxonList.class);
    jitter(traits, length, missingIndices, window, taxonList, duplicates, true);
}
Also used : TaxonList(dr.evolution.util.TaxonList)

Example 59 with TaxonList

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

the class EmpiricalTreeDistributionModelParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    final String fileName = xo.getStringAttribute(FILE_NAME);
    // default is random tree
    int startingTree = xo.getAttribute(STARTING_TREE, -1);
    // default is random draw
    boolean iterate = xo.getAttribute(ITERATE, false);
    if (iterate && startingTree < 0) {
        startingTree = 0;
    }
    Logger.getLogger("dr.evomodel").info("Creating the empirical tree distribution model, '" + xo.getId() + "'");
    TaxonList taxa = (TaxonList) xo.getChild(TaxonList.class);
    final File file = FileHelpers.getFile(fileName);
    Tree[] trees = null;
    NexusImporter importer = null;
    try {
        FileReader reader = new FileReader(file);
        importer = new NexusImporter(reader);
        if (!iterate) {
            // Re-order taxon numbers to original TaxonList order
            trees = importer.importTrees(taxa, true);
            reader.close();
        }
    } catch (FileNotFoundException e) {
        throw new XMLParseException(e.getMessage());
    } catch (IOException e) {
        throw new XMLParseException(e.getMessage());
    } catch (Importer.ImportException e) {
        throw new XMLParseException(e.getMessage());
    }
    if (iterate) {
        Logger.getLogger("dr.evomodel").info("    Iterate over each tree from file, " + fileName);
        return new EmpiricalTreeDistributionModel(importer, startingTree);
    } else {
        Logger.getLogger("dr.evomodel").info("    Randomly jump between " + trees.length + " trees from file, " + fileName);
        return new EmpiricalTreeDistributionModel(trees, startingTree);
    }
}
Also used : NexusImporter(dr.evolution.io.NexusImporter) TaxonList(dr.evolution.util.TaxonList) EmpiricalTreeDistributionModel(dr.evomodel.tree.EmpiricalTreeDistributionModel) Tree(dr.evolution.tree.Tree) NexusImporter(dr.evolution.io.NexusImporter) Importer(dr.evolution.io.Importer)

Example 60 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)

Aggregations

TaxonList (dr.evolution.util.TaxonList)60 Taxon (dr.evolution.util.Taxon)20 Tree (dr.evolution.tree.Tree)19 TreeUtils (dr.evolution.tree.TreeUtils)16 Taxa (dr.evolution.util.Taxa)14 Parameter (dr.inference.model.Parameter)12 TreeModel (dr.evomodel.tree.TreeModel)9 ArrayList (java.util.ArrayList)9 Alignment (dr.evolution.alignment.Alignment)6 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 NodeRef (dr.evolution.tree.NodeRef)4 BranchRateModel (dr.evomodel.branchratemodel.BranchRateModel)4 CoalescentSimulator (dr.evomodel.coalescent.CoalescentSimulator)4 SubstitutionModel (dr.evomodel.substmodel.SubstitutionModel)4 PatternList (dr.evolution.alignment.PatternList)3 Patterns (dr.evolution.alignment.Patterns)3 DataType (dr.evolution.datatype.DataType)3