Search in sources :

Example 66 with Tree

use of dr.evolution.tree.Tree in project beast-mcmc by beast-dev.

the class BEAUTiImporter method importNexusFile.

// nexus
private void importNexusFile(File file) throws IOException, ImportException {
    TaxonList taxa = null;
    SimpleAlignment alignment = null;
    List<Tree> trees = new ArrayList<Tree>();
    PartitionSubstitutionModel model = null;
    List<NexusApplicationImporter.CharSet> charSets = new ArrayList<NexusApplicationImporter.CharSet>();
    try {
        FileReader reader = new FileReader(file);
        NexusApplicationImporter importer = new NexusApplicationImporter(reader);
        boolean done = false;
        while (!done) {
            try {
                NexusBlock block = importer.findNextBlock();
                if (block == NexusImporter.TAXA_BLOCK) {
                    if (taxa != null) {
                        throw new MissingBlockException("TAXA block already defined");
                    }
                    taxa = importer.parseTaxaBlock();
                } else if (block == NexusImporter.CALIBRATION_BLOCK) {
                    if (taxa == null) {
                        throw new MissingBlockException("TAXA or DATA block must be defined before a CALIBRATION block");
                    }
                    importer.parseCalibrationBlock(taxa);
                } else if (block == NexusImporter.CHARACTERS_BLOCK) {
                    if (taxa == null) {
                        throw new MissingBlockException("TAXA block must be defined before a CHARACTERS block");
                    }
                    if (alignment != null) {
                        throw new MissingBlockException("CHARACTERS or DATA block already defined");
                    }
                    alignment = (SimpleAlignment) importer.parseCharactersBlock(taxa);
                } else if (block == NexusImporter.DATA_BLOCK) {
                    if (alignment != null) {
                        throw new MissingBlockException("CHARACTERS or DATA block already defined");
                    }
                    // A data block doesn't need a taxon block before it
                    // but if one exists then it will use it.
                    alignment = (SimpleAlignment) importer.parseDataBlock(taxa);
                    if (taxa == null) {
                        taxa = alignment;
                    }
                } else if (block == NexusImporter.TREES_BLOCK) {
                    // I guess there is no reason not to allow multiple trees blocks
                    //                        if (trees.size() > 0) {
                    //                            throw new MissingBlockException("TREES block already defined");
                    //                        }
                    Tree[] treeArray = importer.parseTreesBlock(taxa);
                    trees.addAll(Arrays.asList(treeArray));
                    if (taxa == null && trees.size() > 0) {
                        taxa = trees.get(0);
                    }
                } else if (block == NexusApplicationImporter.PAUP_BLOCK) {
                    model = importer.parsePAUPBlock(options, charSets);
                } else if (block == NexusApplicationImporter.MRBAYES_BLOCK) {
                    model = importer.parseMrBayesBlock(options, charSets);
                } else if (block == NexusApplicationImporter.ASSUMPTIONS_BLOCK || block == NexusApplicationImporter.SETS_BLOCK) {
                    importer.parseAssumptionsBlock(charSets);
                } else {
                // Ignore the block..
                }
            } catch (EOFException ex) {
                done = true;
            }
        }
        reader.close();
        // Allow the user to load taxa only (perhaps from a tree file) so that they can sample from a prior...
        if (alignment == null && taxa == null) {
            throw new MissingBlockException("TAXON, DATA or CHARACTERS block is missing");
        }
    } catch (IOException e) {
        throw new IOException(e.getMessage());
    } catch (ImportException e) {
        throw new ImportException(e.getMessage());
    //        } catch (Exception e) {
    //            throw new Exception(e.getMessage());
    }
    setData(file.getName(), taxa, alignment, charSets, model, null, trees);
}
Also used : TaxonList(dr.evolution.util.TaxonList) ArrayList(java.util.ArrayList) ImportException(dr.evolution.io.Importer.ImportException) SimpleAlignment(dr.evolution.alignment.SimpleAlignment) NexusBlock(dr.evolution.io.NexusImporter.NexusBlock) Tree(dr.evolution.tree.Tree) MissingBlockException(dr.evolution.io.NexusImporter.MissingBlockException)

Example 67 with Tree

use of dr.evolution.tree.Tree in project beast-mcmc by beast-dev.

the class BEAUTiImporter method addTrees.

private void addTrees(List<Tree> trees) {
    if (trees != null && trees.size() > 0) {
        for (Tree tree : trees) {
            String id = tree.getId();
            if (id == null || id.trim().length() == 0) {
                tree.setId("tree_" + (options.userTrees.size() + 1));
            } else {
                String newId = id;
                int count = 1;
                for (Tree tree1 : options.userTrees) {
                    if (tree1.getId().equals(newId)) {
                        newId = id + "_" + count;
                        count++;
                    }
                }
                tree.setId(newId);
            }
            options.userTrees.add(tree);
        }
    }
}
Also used : Tree(dr.evolution.tree.Tree)

Example 68 with Tree

use of dr.evolution.tree.Tree in project beast-mcmc by beast-dev.

the class MapperFrame method readFromFile.

protected boolean readFromFile(File file) throws IOException {
    Reader reader = new FileReader(file);
    BufferedReader bufferedReader = new BufferedReader(reader);
    String line = bufferedReader.readLine();
    while (line != null && line.length() == 0) {
        line = bufferedReader.readLine();
    }
    boolean isNexus = (line != null && line.toUpperCase().contains("#NEXUS"));
    reader = new FileReader(file);
    Tree tree = null;
    try {
    //            if (isNexus) {
    //                NexusImporter importer = new NexusImporter(reader);
    //                tree = importer.importTree(taxa);
    //            } else {
    //                NewickImporter importer = new NewickImporter(reader);
    //                tree = importer.importTree(taxa);
    //            }
    //        } catch (Importer.ImportException ime) {
    //            JOptionPane.showMessageDialog(this, "Error parsing imported file: " + ime,
    //                    "Error reading file",
    //                    JOptionPane.ERROR_MESSAGE);
    //            ime.printStackTrace();
    //            return false;
    //        } catch (IOException ioex) {
    //            JOptionPane.showMessageDialog(this, "File I/O Error: " + ioex,
    //                    "File I/O Error",
    //                    JOptionPane.ERROR_MESSAGE);
    //            ioex.printStackTrace();
    //            return false;
    } catch (Exception ex) {
        JOptionPane.showMessageDialog(this, "Fatal exception: " + ex, "Error reading file", JOptionPane.ERROR_MESSAGE);
        ex.printStackTrace();
        return false;
    }
    if (tree == null) {
        JOptionPane.showMessageDialog(this, "The file is not in a suitable format or contains no trees.", "Error reading file", JOptionPane.ERROR_MESSAGE);
        return false;
    }
    FlexibleTree binaryTree = new FlexibleTree(tree, true);
    binaryTree.resolveTree();
    trees.add(binaryTree);
    //        if (taxa == null) {
    //            taxa = binaryTree;
    //        }
    getExportDataAction().setEnabled(true);
    return true;
}
Also used : FlexibleTree(dr.evolution.tree.FlexibleTree) FlexibleTree(dr.evolution.tree.FlexibleTree) Tree(dr.evolution.tree.Tree) TraceException(dr.inference.trace.TraceException)

Example 69 with Tree

use of dr.evolution.tree.Tree in project beast-mcmc by beast-dev.

the class Coevolve method readTreeFile.

/**
     * @param fileName
     * @throws java.io.IOException
     */
private Tree readTreeFile(String fileName) throws java.io.IOException {
    Alignment alignment = null;
    Tree[] trees = 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) {
                    trees = importer.parseTreesBlock(taxonList);
                    if (taxonList == null) {
                        taxonList = alignment;
                    }
                } else {
                // Ignore the block..
                }
            } catch (EOFException ex) {
                done = true;
            }
        }
    } catch (Importer.ImportException ime) {
        System.err.println("Error reading alignment: " + ime);
    }
    return trees[0];
}
Also used : NexusImporter(dr.evolution.io.NexusImporter) TaxonList(dr.evolution.util.TaxonList) Tree(dr.evolution.tree.Tree) NexusImporter(dr.evolution.io.NexusImporter) Importer(dr.evolution.io.Importer)

Example 70 with Tree

use of dr.evolution.tree.Tree in project beast-mcmc by beast-dev.

the class PathogenFrame method writeTreeFile.

protected void writeTreeFile(PrintStream ps, boolean newickFormat) throws IOException {
    Tree tree = pathogenPanel.getTreeAsViewed();
    //        if (newickFormat) {
    //            NewickExporter newickExporter = new NewickExporter(ps);
    //            newickExporter.exportTree(tree);
    //        } else {
    NexusExporter nexusExporter = new NexusExporter(new PrintStream(ps));
    nexusExporter.exportTree(tree);
//        }
}
Also used : NexusExporter(dr.app.tools.NexusExporter) FlexibleTree(dr.evolution.tree.FlexibleTree) Tree(dr.evolution.tree.Tree)

Aggregations

Tree (dr.evolution.tree.Tree)128 NewickImporter (dr.evolution.io.NewickImporter)32 ArrayList (java.util.ArrayList)31 TreeModel (dr.evomodel.tree.TreeModel)26 Parameter (dr.inference.model.Parameter)26 NexusImporter (dr.evolution.io.NexusImporter)18 TaxonList (dr.evolution.util.TaxonList)18 Taxa (dr.evolution.util.Taxa)17 FlexibleTree (dr.evolution.tree.FlexibleTree)16 Taxon (dr.evolution.util.Taxon)15 BranchRateModel (dr.evomodel.branchratemodel.BranchRateModel)15 NodeRef (dr.evolution.tree.NodeRef)14 SimpleTree (dr.evolution.tree.SimpleTree)13 ImportException (dr.evolution.io.Importer.ImportException)12 Importer (dr.evolution.io.Importer)11 DefaultBranchRateModel (dr.evomodel.branchratemodel.DefaultBranchRateModel)11 BeagleSequenceSimulator (dr.app.beagle.tools.BeagleSequenceSimulator)10 Partition (dr.app.beagle.tools.Partition)10 GammaSiteRateModel (dr.evomodel.siteratemodel.GammaSiteRateModel)10 FrequencyModel (dr.evomodel.substmodel.FrequencyModel)9