Search in sources :

Example 1 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 2 with TaxonList

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

the class ARGTrace method loadARGTrace.

/**
 * Loads the trace for with trees from a reader
 *
 * @return the TreeTrace
 */
public static ARGTrace loadARGTrace(Reader r) throws IOException, Importer.ImportException {
    BufferedReader reader = new BufferedReader(r);
    ARGTrace trace = new ARGTrace();
    dr.evolution.util.TaxonList taxonList = null;
    int minState = -1;
    int stepSize = 0;
    String line;
    ARGModel nullARG = new ARGModel(null, null, 0, 0);
    ArrayList<String> nameList = new ArrayList<String>();
    taxonList = (TaxonList) nullARG;
    while ((line = reader.readLine()) != null) {
        // String line = reader.readLine();
        line.trim();
        if (line.toUpperCase().startsWith("ARG")) {
            StringTokenizer st = new StringTokenizer(line, "=");
            nameList.add(st.nextToken());
            ARGModel arg = nullARG.fromGraphStringCompressed(st.nextToken().trim());
            trace.add(arg);
        }
    }
    if (nameList.size() < 2) {
        throw new Importer.ImportException("Less than two ARGs in the trace file");
    }
    minState = getStateNumber(nameList.get(0));
    stepSize = getStateNumber(nameList.get(1)) - minState;
    // if (line.toUpperCase().startsWith("#NEXUS")) {
    // NexusImporter importer = new NexusImporter(reader);
    // Tree [] trees = importer.importTrees(null);
    // 
    // if (trees.length < 2) {
    // throw new Importer.ImportException("Less than two trees in the trace file");
    // }
    // 
    // String id1 = trees[0].getId();
    // String id2 = trees[1].getId();
    // 
    // minState = getStateNumber(id1);
    // stepSize = getStateNumber(id2) - minState;
    // 
    // for (int i = 0; i < trees.length; i++) {
    // args.add(args[i]);
    // }
    // } else {
    // NewickImporter importer = new NewickImporter(reader);
    // 
    // while (true) {
    // 
    // int state = 0;
    // Tree tree;
    // 
    // try {
    // state = importer.readInteger();
    // tree = importer.importTree(taxonList);
    // 
    // if (taxonList == null) {
    // // The first tree becomes the taxon list. This means
    // // that all subsequent trees will look up their taxa
    // // in that taxon list rather than creating their own
    // // duplicitous ones.
    // taxonList = tree;
    // }
    // } catch (Importer.ImportException ie) {
    // System.out.println("Error reading tree for state " + state);
    // throw ie;
    // } catch (EOFException e) {
    // break;
    // }
    // 
    // if (minState == -1) {
    // minState = state;
    // } else if (stepSize == 0) {
    // stepSize = state - minState;
    // }
    // 
    // trace.add(tree);
    // }
    // }
    trace.setMinimumState(minState);
    trace.setStepSize(stepSize);
    return trace;
}
Also used : StringTokenizer(java.util.StringTokenizer) BufferedReader(java.io.BufferedReader) ArrayList(java.util.ArrayList) TaxonList(dr.evolution.util.TaxonList)

Example 3 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 4 with TaxonList

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

the class GhostTreeModelParser 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);
    TaxonList ghostTaxa = (TaxonList) xo.getElementFirstChild(GHOST_TAXA);
    GhostTreeModel treeModel = new GhostTreeModel(xo.getId(), tree, ghostTaxa);
    Logger.getLogger("dr.evomodel").info("\nCreating the tree model, '" + xo.getId() + "'" + "\n\nwith " + ghostTaxa.getTaxonCount() + " ghost lineages.");
    Logger.getLogger("dr.evomodel").info("  taxon count = " + treeModel.getExternalNodeCount());
    Logger.getLogger("dr.evomodel").info("  tree height = " + treeModel.getNodeHeight(treeModel.getRoot()));
    return treeModel;
}
Also used : TaxonList(dr.evolution.util.TaxonList) Tree(dr.evolution.tree.Tree) GhostTreeModel(dr.evomodel.bigfasttree.ghosttree.GhostTreeModel)

Example 5 with TaxonList

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

the class BranchSpecificBranchModelParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    Logger.getLogger("dr.evomodel").info("\nUsing clade-specific branch model.");
    TreeModel tree = (TreeModel) xo.getChild(TreeModel.class);
    SubstitutionModel substitutionModel = (SubstitutionModel) xo.getChild(SubstitutionModel.class);
    BranchSpecificBranchModel branchModel = new BranchSpecificBranchModel(tree, substitutionModel);
    for (int i = 0; i < xo.getChildCount(); i++) {
        if (xo.getChild(i) instanceof XMLObject) {
            XMLObject xoc = (XMLObject) xo.getChild(i);
            if (xoc.getName().equals(CLADE)) {
                double stemWeight = xoc.getAttribute(STEM_WEIGHT, 0.0);
                substitutionModel = (SubstitutionModel) xoc.getChild(SubstitutionModel.class);
                TaxonList taxonList = (TaxonList) xoc.getChild(TaxonList.class);
                if (taxonList.getTaxonCount() == 1) {
                    throw new XMLParseException("A clade must be defined by at least two taxa");
                }
                try {
                    branchModel.addClade(taxonList, substitutionModel, stemWeight);
                } catch (TreeUtils.MissingTaxonException mte) {
                    throw new XMLParseException("Taxon, " + mte + ", in " + getParserName() + " was not found in the tree.");
                }
            } else if (xoc.getName().equals(EXTERNAL_BRANCHES)) {
                substitutionModel = (SubstitutionModel) xoc.getChild(SubstitutionModel.class);
                TaxonList taxonList = (TaxonList) xoc.getChild(TaxonList.class);
                try {
                    branchModel.addExternalBranches(taxonList, substitutionModel);
                } catch (TreeUtils.MissingTaxonException mte) {
                    throw new XMLParseException("Taxon, " + mte + ", in " + getParserName() + " was not found in the tree.");
                }
            } else if (xoc.getName().equals(BACKBONE)) {
                substitutionModel = (SubstitutionModel) xoc.getChild(SubstitutionModel.class);
                TaxonList taxonList = (TaxonList) xoc.getChild(TaxonList.class);
                try {
                    branchModel.addBackbone(taxonList, substitutionModel);
                } catch (TreeUtils.MissingTaxonException mte) {
                    throw new XMLParseException("Taxon, " + mte + ", in " + getParserName() + " was not found in the tree.");
                }
            }
        }
    }
    return branchModel;
}
Also used : TreeModel(dr.evomodel.tree.TreeModel) TaxonList(dr.evolution.util.TaxonList) BranchSpecificBranchModel(dr.evomodel.branchmodel.BranchSpecificBranchModel) SubstitutionModel(dr.evomodel.substmodel.SubstitutionModel) 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