Search in sources :

Example 6 with ImportException

use of dr.evolution.io.Importer.ImportException in project beast-mcmc by beast-dev.

the class BEAUTiImporter method importMicroSatFile.

// micro-sat
private void importMicroSatFile(File file) throws IOException, ImportException {
    try {
        Reader reader = new FileReader(file);
        BufferedReader bufferedReader = new BufferedReader(reader);
        MicroSatImporter importer = new MicroSatImporter(bufferedReader);
        List<Patterns> microsatPatList = importer.importPatterns();
        Taxa unionSetTaxonList = importer.getUnionSetTaxonList();
        Microsatellite microsatellite = importer.getMicrosatellite();
        //            options.allowDifferentTaxa = importer.isHasDifferentTaxon();
        bufferedReader.close();
        PartitionSubstitutionModel substModel = new PartitionSubstitutionModel(options, microsatPatList.get(0).getId());
        substModel.setMicrosatellite(microsatellite);
        for (Patterns patterns : microsatPatList) {
            setData(file.getName(), unionSetTaxonList, patterns, substModel, null);
        }
        // has to call after data is imported
        options.microsatelliteOptions.initModelParametersAndOpererators();
    } catch (ImportException e) {
        throw new ImportException(e.getMessage());
    } catch (IOException e) {
        throw new IOException(e.getMessage());
    }
}
Also used : ImportException(dr.evolution.io.Importer.ImportException) Taxa(dr.evolution.util.Taxa) Microsatellite(dr.evolution.datatype.Microsatellite) MicroSatImporter(dr.evolution.io.MicroSatImporter) Patterns(dr.evolution.alignment.Patterns)

Example 7 with ImportException

use of dr.evolution.io.Importer.ImportException in project beast-mcmc by beast-dev.

the class BEAUTiImporter method checkTaxonList.

private void checkTaxonList(TaxonList taxonList) throws ImportException {
    // check the taxon names for invalid characters
    boolean foundAmp = false;
    for (Taxon taxon : taxonList) {
        String name = taxon.getId();
        if (name.indexOf('&') >= 0) {
            foundAmp = true;
        }
    }
    if (foundAmp) {
        throw new ImportException("One or more taxon names include an illegal character ('&').\n" + "These characters will prevent BEAST from reading the resulting XML file.\n\n" + "Please edit the taxon name(s) before reloading the data file.");
    }
    // make sure they all have dates...
    for (int i = 0; i < taxonList.getTaxonCount(); i++) {
        if (taxonList.getTaxonAttribute(i, "date") == null) {
            Date origin = new Date(0);
            dr.evolution.util.Date date = dr.evolution.util.Date.createTimeSinceOrigin(0.0, Units.Type.YEARS, origin);
            taxonList.getTaxon(i).setAttribute("date", date);
        }
    }
}
Also used : ImportException(dr.evolution.io.Importer.ImportException) Taxon(dr.evolution.util.Taxon) Date(java.util.Date)

Example 8 with ImportException

use of dr.evolution.io.Importer.ImportException 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 9 with ImportException

use of dr.evolution.io.Importer.ImportException in project beast-mcmc by beast-dev.

the class BEAUTiImporter method importFromFile.

public void importFromFile(File file) throws IOException, ImportException, JDOMException {
    try {
        Reader reader = new FileReader(file);
        BufferedReader bufferedReader = new BufferedReader(reader);
        String line = bufferedReader.readLine();
        while (line != null && line.length() == 0) {
            line = bufferedReader.readLine();
        }
        if ((line != null && line.toUpperCase().contains("#NEXUS"))) {
            // is a NEXUS file
            importNexusFile(file);
        } else if ((line != null && line.trim().startsWith("" + FastaImporter.FASTA_FIRST_CHAR))) {
            // is a FASTA file
            importFastaFile(file);
        } else if ((line != null && (line.toUpperCase().contains("<?XML") || line.toUpperCase().contains("<BEAST")))) {
            // assume it is a BEAST XML file and see if that works...
            importBEASTFile(file);
        //            } else {
        //                // assume it is a tab-delimited traits file and see if that works...
        //                importTraits(file);
        } else if ((line != null && line.toUpperCase().contains("#MICROSAT"))) {
            // MicroSatellite
            importMicroSatFile(file);
        } else {
            throw new ImportException("Unrecognized format for imported file.");
        }
        bufferedReader.close();
    } catch (IOException e) {
        throw new IOException(e.getMessage());
    }
}
Also used : ImportException(dr.evolution.io.Importer.ImportException)

Example 10 with ImportException

use of dr.evolution.io.Importer.ImportException 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)

Aggregations

ImportException (dr.evolution.io.Importer.ImportException)14 SimpleAlignment (dr.evolution.alignment.SimpleAlignment)7 Tree (dr.evolution.tree.Tree)5 Alignment (dr.evolution.alignment.Alignment)4 TaxonList (dr.evolution.util.TaxonList)4 ArrayList (java.util.ArrayList)4 MissingBlockException (dr.evolution.io.NexusImporter.MissingBlockException)3 Taxa (dr.evolution.util.Taxa)3 JDOMException (org.jdom.JDOMException)3 FastaImporter (dr.evolution.io.FastaImporter)2 NewickImporter (dr.evolution.io.NewickImporter)2 NexusBlock (dr.evolution.io.NexusImporter.NexusBlock)2 Taxon (dr.evolution.util.Taxon)2 IOException (java.io.IOException)2 BeagleSequenceSimulator (dr.app.beagle.tools.BeagleSequenceSimulator)1 Partition (dr.app.beagle.tools.Partition)1 BEAUTiImporter (dr.app.beauti.util.BEAUTiImporter)1 ArgumentException (dr.app.util.Arguments.ArgumentException)1 Patterns (dr.evolution.alignment.Patterns)1 DataType (dr.evolution.datatype.DataType)1