Class NexusImporter

  • All Implemented Interfaces:
    java.lang.Iterable<Tree>, AlignmentImporter, DistanceMatrixImporter, SequenceImporter, TreeImporter

    public class NexusImporter
    extends java.lang.Object
    implements AlignmentImporter, SequenceImporter, TreeImporter, DistanceMatrixImporter
    Class for importing NEXUS file format. This is a good starting point for documentation about the nexus file format: https://www.nescent.org/wg_phyloinformatics/NEXUS_Specification For a quick check if something is probably valid or not, consider this attempt at a nexus grammar (the above page says "Don't treat this as gospel, its just an attempt to get the syntax rules"): http://www.cs.nmsu.edu/~epontell/nexus/nexus_grammar
    Version:
    $Id: NexusImporter.java 1052 2010-05-10 00:45:35Z richardmoir $
    Author:
    Andrew Rambaut, Alexei Drummond
    • Constructor Summary

      Constructors 
      Constructor Description
      NexusImporter​(java.io.Reader reader)  
      NexusImporter​(java.io.Reader reader, boolean compactTrees)
      Deprecated.
      Use NexusImporter(Reader reader, boolean compactTrees, long expectedInputLength)
      NexusImporter​(java.io.Reader reader, boolean compactTrees, long expectedInputLength)  
      NexusImporter​(java.io.Reader reader, long expectedLength)  
    • Constructor Detail

      • NexusImporter

        public NexusImporter​(java.io.Reader reader,
                             long expectedLength)
        Parameters:
        reader -
        expectedLength - Expected length of the input in bytes, or 0 if unknown. Used for optimization and tracking progress.
      • NexusImporter

        public NexusImporter​(java.io.Reader reader)
      • NexusImporter

        public NexusImporter​(java.io.Reader reader,
                             boolean compactTrees,
                             long expectedInputLength)
        Parameters:
        reader -
        compactTrees - true to import trees as CompactRootedTree, false to use SimpleRootedTree.
        expectedInputLength - Expected length of the input in bytes, or 0 if unknown. Used for optimization and tracking progress.
      • NexusImporter

        @Deprecated
        public NexusImporter​(java.io.Reader reader,
                             boolean compactTrees)
        Deprecated.
        Use NexusImporter(Reader reader, boolean compactTrees, long expectedInputLength)
        Parameters:
        reader -
        compactTrees -
    • Method Detail

      • findNextBlock

        public NexusImporter.NexusBlock findNextBlock()
                                               throws java.io.IOException
        Read ahead to the next block in the input. This should be overridden to provide support for other blocks. Once the block is read in, nextBlock is automatically set to UNKNOWN by findEndBlock.
        Returns:
        the type of the next block
        Throws:
        java.io.IOException
      • getNextBlockName

        public java.lang.String getNextBlockName()
        Returns:
        the name of the next block which will be read. eg "TREES"
      • iterator

        public java.util.Iterator<Tree> iterator()
        Specified by:
        iterator in interface java.lang.Iterable<Tree>
        Returns:
        an Iterator over the trees in the nexus input.
      • parseTaxaBlock

        public java.util.List<Taxon> parseTaxaBlock()
                                             throws ImportException,
                                                    java.io.IOException
        Parse the next 'TAXA' block encountered in the input.
        Throws:
        ImportException
        java.io.IOException
      • parseCharactersBlock

        public java.util.List<Sequence> parseCharactersBlock​(java.util.List<Taxon> taxonList)
                                                      throws ImportException,
                                                             java.io.IOException
        Parse the next 'CHARACTERS' block encountered in the input.
        Throws:
        ImportException
        java.io.IOException
      • parseDataBlock

        public java.util.List<Sequence> parseDataBlock​(java.util.List<Taxon> taxonList)
                                                throws ImportException,
                                                       java.io.IOException
        Parse the next 'DATA' block encountered in the input.
        Throws:
        ImportException
        java.io.IOException
      • parseTreesBlock

        public java.util.List<Tree> parseTreesBlock​(java.util.List<Taxon> taxonList)
                                             throws ImportException,
                                                    java.io.IOException
        Parse the next 'TREES' block encountered in the input.
        Throws:
        ImportException
        java.io.IOException
      • parseDistancesBlock

        public DistanceMatrix parseDistancesBlock​(java.util.List<Taxon> taxonList)
                                           throws ImportException,
                                                  java.io.IOException
        Parse the next 'DISTANCES' block encountered in the input.
        Throws:
        ImportException
        java.io.IOException
      • hasTree

        public boolean hasTree()
                        throws java.io.IOException,
                               ImportException
        If not currently reading a TREES block then read ahead to the next TREES block, parsing TRANSLATE and TAXA blocks in the process if necessary.

        Then determine if the current (or next) TREES block contains any more trees. If true then importNextTree will return a non-null value.

        Specified by:
        hasTree in interface TreeImporter
        Returns:
        true if another tree is available, false otherwise.
        Throws:
        java.io.IOException
        ImportException
      • importNextTree

        public Tree importNextTree()
                            throws java.io.IOException,
                                   ImportException
        If not currently reading a TREES block then read ahead to the next TREES block, parsing TRANSLATE and TAXA blocks in the process if necessary.

        Then parse the next available tree.

        Specified by:
        importNextTree in interface TreeImporter
        Returns:
        the next available tree or null if no more trees are available
        Throws:
        java.io.IOException
        ImportException
      • importTrees

        public java.util.List<Tree> importTrees()
                                         throws java.io.IOException,
                                                ImportException
        Import all trees in the file from the current position. Will read ahead to the next TREES block if necessary
        Specified by:
        importTrees in interface TreeImporter
        Returns:
        list of trees
        Throws:
        java.io.IOException
        ImportException
      • startReadingTrees

        public boolean startReadingTrees()
                                  throws java.io.IOException,
                                         ImportException
        Read nexus blocks until the next TREES block (or the end of the input) is encountered.
        Returns:
        true if a TREES block was found, false otherwise
        Throws:
        java.io.IOException
        ImportException
      • findEndBlock

        public void findEndBlock()
                          throws java.io.IOException
        Read ahead to the end of the current block.
        Throws:
        java.io.IOException
      • makeIntoAllowableIdentifier

        public static java.lang.String makeIntoAllowableIdentifier​(java.lang.String identifier)