Search in sources :

Example 21 with TaxonList

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

the class Defects method readNexusFile.

/**
     * @param fileName
     * @throws java.io.IOException
     */
private static Alignment readNexusFile(String fileName) throws java.io.IOException {
    Alignment alignment = null;
    TaxonList taxonList = null;
    try {
        FileReader reader = new FileReader(fileName);
        NexusImporter importer = new NexusImporter(reader);
        boolean done = false;
        while (!done) {
            try {
                NexusImporter.NexusBlock block = importer.findNextBlock();
                if (block == NexusImporter.TAXA_BLOCK) {
                    if (taxonList != null) {
                        throw new NexusImporter.MissingBlockException("TAXA block already defined");
                    }
                    taxonList = importer.parseTaxaBlock();
                } else if (block == NexusImporter.DATA_BLOCK) {
                    // A data block doesn't need a taxon block before it
                    // but if one exists then it will use it.
                    alignment = importer.parseDataBlock(taxonList);
                    if (taxonList == null) {
                        taxonList = alignment;
                    }
                } else if (block == NexusImporter.TREES_BLOCK) {
                // ignore tree block
                } else {
                // Ignore the block..
                }
            } catch (EOFException ex) {
                done = true;
            }
        }
    } catch (Importer.ImportException ime) {
        System.err.println("Error reading alignment: " + ime);
    }
    return alignment;
}
Also used : NexusImporter(dr.evolution.io.NexusImporter) TaxonList(dr.evolution.util.TaxonList) EOFException(java.io.EOFException) FileReader(java.io.FileReader) NexusImporter(dr.evolution.io.NexusImporter) Importer(dr.evolution.io.Importer)

Example 22 with TaxonList

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

the class PartitionedTreeModelParser 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);
    AbstractOutbreak outbreak = (AbstractOutbreak) xo.getElementFirstChild(OUTBREAK);
    PartitionedTreeModel treeModel;
    if (xo.hasAttribute(STARTING_TT_FILE)) {
        treeModel = new PartitionedTreeModel(xo.getId(), tree, outbreak, xo.getStringAttribute(STARTING_TT_FILE));
    } else {
        treeModel = new PartitionedTreeModel(xo.getId(), tree, outbreak);
    }
    Logger.getLogger("dr.evomodel").info("Creating the partitioned 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);
                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 {
                if (!cxo.getName().equals(OUTBREAK)) {
                    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) NodeRef(dr.evolution.tree.NodeRef) Tree(dr.evolution.tree.Tree) CompoundParameter(dr.inference.model.CompoundParameter) Parameter(dr.inference.model.Parameter)

Example 23 with TaxonList

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

the class SpeciationLikelihoodParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    XMLObject cxo = xo.getChild(MODEL);
    final SpeciationModel specModel = (SpeciationModel) cxo.getChild(SpeciationModel.class);
    cxo = xo.getChild(TREE);
    final Tree tree = (Tree) cxo.getChild(Tree.class);
    Set<Taxon> excludeTaxa = null;
    if (xo.hasChildNamed(INCLUDE)) {
        excludeTaxa = new HashSet<Taxon>();
        for (int i = 0; i < tree.getTaxonCount(); i++) {
            excludeTaxa.add(tree.getTaxon(i));
        }
        cxo = xo.getChild(INCLUDE);
        for (int i = 0; i < cxo.getChildCount(); i++) {
            TaxonList taxonList = (TaxonList) cxo.getChild(i);
            for (int j = 0; j < taxonList.getTaxonCount(); j++) {
                excludeTaxa.remove(taxonList.getTaxon(j));
            }
        }
    }
    if (xo.hasChildNamed(EXCLUDE)) {
        excludeTaxa = new HashSet<Taxon>();
        cxo = xo.getChild(EXCLUDE);
        for (int i = 0; i < cxo.getChildCount(); i++) {
            TaxonList taxonList = (TaxonList) cxo.getChild(i);
            for (int j = 0; j < taxonList.getTaxonCount(); j++) {
                excludeTaxa.add(taxonList.getTaxon(j));
            }
        }
    }
    if (excludeTaxa != null) {
        Logger.getLogger("dr.evomodel").info("Speciation model excluding " + excludeTaxa.size() + " taxa from prior - " + (tree.getTaxonCount() - excludeTaxa.size()) + " taxa remaining.");
    }
    final XMLObject cal = xo.getChild(CALIBRATION);
    if (cal != null) {
        if (excludeTaxa != null) {
            throw new XMLParseException("Sorry, not implemented: internal calibration prior + excluded taxa");
        }
        List<Distribution> dists = new ArrayList<Distribution>();
        List<Taxa> taxa = new ArrayList<Taxa>();
        List<Boolean> forParent = new ArrayList<Boolean>();
        // (Statistic) cal.getChild(Statistic.class);
        Statistic userPDF = null;
        for (int k = 0; k < cal.getChildCount(); ++k) {
            final Object ck = cal.getChild(k);
            if (DistributionLikelihood.class.isInstance(ck)) {
                dists.add(((DistributionLikelihood) ck).getDistribution());
            } else if (Distribution.class.isInstance(ck)) {
                dists.add((Distribution) ck);
            } else if (Taxa.class.isInstance(ck)) {
                final Taxa tx = (Taxa) ck;
                taxa.add(tx);
                forParent.add(tx.getTaxonCount() == 1);
            } else if (Statistic.class.isInstance(ck)) {
                if (userPDF != null) {
                    throw new XMLParseException("more than one userPDF correction???");
                }
                userPDF = (Statistic) cal.getChild(Statistic.class);
            } else {
                XMLObject cko = (XMLObject) ck;
                assert cko.getChildCount() == 2;
                for (int i = 0; i < 2; ++i) {
                    final Object chi = cko.getChild(i);
                    if (DistributionLikelihood.class.isInstance(chi)) {
                        dists.add(((DistributionLikelihood) chi).getDistribution());
                    } else if (Distribution.class.isInstance(chi)) {
                        dists.add((Distribution) chi);
                    } else if (Taxa.class.isInstance(chi)) {
                        taxa.add((Taxa) chi);
                        boolean fp = ((Taxa) chi).getTaxonCount() == 1;
                        if (cko.hasAttribute(PARENT)) {
                            boolean ufp = cko.getBooleanAttribute(PARENT);
                            if (fp && !ufp) {
                                throw new XMLParseException("forParent==false for a single taxon?? (must be true)");
                            }
                            fp = ufp;
                        }
                        forParent.add(fp);
                    } else {
                        assert false;
                    }
                }
            }
        }
        if (dists.size() != taxa.size()) {
            throw new XMLParseException("Mismatch in number of distributions and taxa specs");
        }
        try {
            final String correction = cal.getAttribute(CORRECTION, EXACT);
            final CalibrationPoints.CorrectionType type = correction.equals(EXACT) ? CalibrationPoints.CorrectionType.EXACT : (correction.equals(APPROX) ? CalibrationPoints.CorrectionType.APPROXIMATED : (correction.equals(NONE) ? CalibrationPoints.CorrectionType.NONE : (correction.equals(PEXACT) ? CalibrationPoints.CorrectionType.PEXACT : null)));
            if (cal.hasAttribute(CORRECTION) && type == null) {
                throw new XMLParseException("correction type == " + correction + "???");
            }
            final CalibrationPoints calib = new CalibrationPoints(tree, specModel.isYule(), dists, taxa, forParent, userPDF, type);
            final SpeciationLikelihood speciationLikelihood = new SpeciationLikelihood(tree, specModel, null, calib);
            return speciationLikelihood;
        } catch (IllegalArgumentException e) {
            throw new XMLParseException(e.getMessage());
        }
    }
    return new SpeciationLikelihood(tree, specModel, excludeTaxa, null);
}
Also used : CalibrationPoints(dr.evomodel.speciation.CalibrationPoints) TaxonList(dr.evolution.util.TaxonList) Taxon(dr.evolution.util.Taxon) ArrayList(java.util.ArrayList) SpeciationModel(dr.evomodel.speciation.SpeciationModel) SpeciationLikelihood(dr.evomodel.speciation.SpeciationLikelihood) Taxa(dr.evolution.util.Taxa) Statistic(dr.inference.model.Statistic) Distribution(dr.math.distributions.Distribution) Tree(dr.evolution.tree.Tree) DistributionLikelihood(dr.inference.distribution.DistributionLikelihood)

Example 24 with TaxonList

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

the class Coevolve method readNexusFile.

/**
     * @param fileName
     * @throws java.io.IOException
     */
private Alignment readNexusFile(String fileName) throws java.io.IOException {
    Alignment alignment = null;
    TaxonList taxonList = null;
    try {
        FileReader reader = new FileReader(fileName);
        NexusImporter importer = new NexusImporter(reader);
        boolean done = false;
        while (!done) {
            try {
                NexusImporter.NexusBlock block = importer.findNextBlock();
                if (block == NexusImporter.TAXA_BLOCK) {
                    if (taxonList != null) {
                        throw new NexusImporter.MissingBlockException("TAXA block already defined");
                    }
                    taxonList = importer.parseTaxaBlock();
                } else if (block == NexusImporter.DATA_BLOCK) {
                    // A data block doesn't need a taxon block before it
                    // but if one exists then it will use it.
                    alignment = importer.parseDataBlock(taxonList);
                    if (taxonList == null) {
                        taxonList = alignment;
                    }
                } else if (block == NexusImporter.TREES_BLOCK) {
                // ignore tree block
                } else {
                // Ignore the block..
                }
            } catch (EOFException ex) {
                done = true;
            }
        }
    } catch (Importer.ImportException ime) {
        System.err.println("Error reading alignment: " + ime);
    }
    return alignment;
}
Also used : NexusImporter(dr.evolution.io.NexusImporter) TaxonList(dr.evolution.util.TaxonList) NexusImporter(dr.evolution.io.NexusImporter) Importer(dr.evolution.io.Importer)

Example 25 with TaxonList

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

the class CoalescentLikelihoodParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    XMLObject cxo = xo.getChild(MODEL);
    DemographicModel demoModel = (DemographicModel) cxo.getChild(DemographicModel.class);
    List<TreeModel> trees = new ArrayList<TreeModel>();
    List<Double> popFactors = new ArrayList<Double>();
    MultiLociTreeSet treesSet = demoModel instanceof MultiLociTreeSet ? (MultiLociTreeSet) demoModel : null;
    for (int k = 0; k < xo.getChildCount(); ++k) {
        final Object child = xo.getChild(k);
        if (child instanceof XMLObject) {
            cxo = (XMLObject) child;
            if (cxo.getName().equals(POPULATION_TREE)) {
                final TreeModel t = (TreeModel) cxo.getChild(TreeModel.class);
                assert t != null;
                trees.add(t);
                popFactors.add(cxo.getAttribute(POPULATION_FACTOR, 1.0));
            }
        }
    //                in the future we may have arbitrary multi-loci element
    //                else if( child instanceof MultiLociTreeSet )  {
    //                    treesSet = (MultiLociTreeSet)child;
    //                }
    }
    TreeModel treeModel = null;
    if (trees.size() == 1 && popFactors.get(0) == 1.0) {
        treeModel = trees.get(0);
    } else if (trees.size() > 1) {
        treesSet = new MultiLociTreeSet.Default(trees, popFactors);
    } else if (!(trees.size() == 0 && treesSet != null)) {
        throw new XMLParseException("Incorrectly constructed likelihood element");
    }
    TaxonList includeSubtree = null;
    if (xo.hasChildNamed(INCLUDE)) {
        includeSubtree = (TaxonList) xo.getElementFirstChild(INCLUDE);
    }
    List<TaxonList> excludeSubtrees = new ArrayList<TaxonList>();
    if (xo.hasChildNamed(EXCLUDE)) {
        cxo = xo.getChild(EXCLUDE);
        for (int i = 0; i < cxo.getChildCount(); i++) {
            excludeSubtrees.add((TaxonList) cxo.getChild(i));
        }
    }
    if (treeModel != null) {
        try {
            return new CoalescentLikelihood(treeModel, includeSubtree, excludeSubtrees, demoModel);
        } catch (TreeUtils.MissingTaxonException mte) {
            throw new XMLParseException("treeModel missing a taxon from taxon list in " + getParserName() + " element");
        }
    } else {
        if (includeSubtree != null || excludeSubtrees.size() > 0) {
            throw new XMLParseException("Include/Exclude taxa not supported for multi locus sets");
        }
        // a base - and modifing it will probsbly result in a bigger mess.
        return new OldAbstractCoalescentLikelihood(treesSet, demoModel);
    }
}
Also used : TaxonList(dr.evolution.util.TaxonList) ArrayList(java.util.ArrayList) DemographicModel(dr.evomodel.coalescent.DemographicModel) TreeModel(dr.evomodel.tree.TreeModel) CoalescentLikelihood(dr.evomodel.coalescent.CoalescentLikelihood) OldAbstractCoalescentLikelihood(dr.evomodel.coalescent.OldAbstractCoalescentLikelihood) OldAbstractCoalescentLikelihood(dr.evomodel.coalescent.OldAbstractCoalescentLikelihood) MultiLociTreeSet(dr.evomodel.coalescent.MultiLociTreeSet) TreeUtils(dr.evolution.tree.TreeUtils)

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