Search in sources :

Example 1 with MissingBlockException

use of dr.evolution.io.NexusImporter.MissingBlockException in project beast-mcmc by beast-dev.

the class BeautiFrame method importFiles.

private void importFiles(File[] files) {
    for (File file : files) {
        if (file == null || file.getName().equals("")) {
            JOptionPane.showMessageDialog(this, "Invalid file name", "Invalid file name", JOptionPane.ERROR_MESSAGE);
        } else {
            try {
                BEAUTiImporter beautiImporter = new BEAUTiImporter(this, options);
                beautiImporter.importFromFile(file);
                setDirty();
            // } catch (FileNotFoundException fnfe) {
            // JOptionPane.showMessageDialog(this, "Unable to open file: File not found",
            // "Unable to open file", JOptionPane.ERROR_MESSAGE);
            } catch (IOException ioe) {
                JOptionPane.showMessageDialog(this, "File I/O Error unable to read file: " + ioe.getMessage(), "Unable to read file", JOptionPane.ERROR_MESSAGE);
                ioe.printStackTrace();
            // there may be other files in the list so don't return
            // return;
            } catch (MissingBlockException ex) {
                JOptionPane.showMessageDialog(this, "TAXON, DATA or CHARACTERS block is missing in Nexus file: " + ex, "Missing Block in Nexus File", JOptionPane.ERROR_MESSAGE);
                ex.printStackTrace();
            } catch (ImportException ime) {
                JOptionPane.showMessageDialog(this, "Error parsing imported file: " + ime, "Error reading file", JOptionPane.ERROR_MESSAGE);
                ime.printStackTrace();
            } catch (JDOMException jde) {
                JOptionPane.showMessageDialog(this, "Error parsing imported file: " + jde, "Error reading file", JOptionPane.ERROR_MESSAGE);
                jde.printStackTrace();
            }
        }
    }
    if (!options.hasIdenticalTaxa()) {
        // need this to refresh panels otherwise it will throw exception
        setAllOptions();
        dataPanel.selectAll();
        dataPanel.unlinkTreeModels();
    }
    setAllOptions();
}
Also used : ImportException(dr.evolution.io.Importer.ImportException) JDOMException(org.jdom.JDOMException) BEAUTiImporter(dr.app.beauti.util.BEAUTiImporter) MissingBlockException(dr.evolution.io.NexusImporter.MissingBlockException)

Example 2 with MissingBlockException

use of dr.evolution.io.NexusImporter.MissingBlockException in project beast-mcmc by beast-dev.

the class DataModelImporter method importNexusFile.

// nexus
private void importNexusFile(File file, DateGuesser guesser, Map dataModel) throws IOException, ImportException {
    TaxonList taxa = null;
    SimpleAlignment alignment = null;
    List<Tree> trees = new ArrayList<Tree>();
    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();
                    dataModel.put("taxa", createTaxonList(taxa));
                } 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.ASSUMPTIONS_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(dataModel, guesser, file.getName(), taxa, null, alignment, charSets, null, trees);
}
Also used : TaxonList(dr.evolution.util.TaxonList) 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 3 with MissingBlockException

use of dr.evolution.io.NexusImporter.MissingBlockException 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<CharSet> charSets = new ArrayList<CharSet>();
    List<NexusApplicationImporter.TaxSet> taxSets = new ArrayList<NexusApplicationImporter.TaxSet>();
    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, taxSets);
                } 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, taxSets, model, null, trees);
}
Also used : TaxonList(dr.evolution.util.TaxonList) 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)

Aggregations

ImportException (dr.evolution.io.Importer.ImportException)3 MissingBlockException (dr.evolution.io.NexusImporter.MissingBlockException)3 SimpleAlignment (dr.evolution.alignment.SimpleAlignment)2 NexusBlock (dr.evolution.io.NexusImporter.NexusBlock)2 Tree (dr.evolution.tree.Tree)2 TaxonList (dr.evolution.util.TaxonList)2 BEAUTiImporter (dr.app.beauti.util.BEAUTiImporter)1 JDOMException (org.jdom.JDOMException)1