iterating nh parser
[jalview.git] / forester / java / src / org / forester / io / parsers / nhx / NHXParser.java
index 7725ee5..4711c8b 100644 (file)
@@ -21,7 +21,7 @@
 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 //
 // Contact: phylosoft @ gmail . com
-// WWW: www.phylosoft.org/forester
+// WWW: https://sites.google.com/site/cmzmasek/home/software/forester
 
 package org.forester.io.parsers.nhx;
 
@@ -39,20 +39,18 @@ import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
 import org.forester.io.parsers.PhylogenyParser;
+import org.forester.io.parsers.phyloxml.PhyloXmlDataFormatException;
 import org.forester.io.parsers.util.ParserUtils;
 import org.forester.io.parsers.util.PhylogenyParserException;
 import org.forester.phylogeny.Phylogeny;
 import org.forester.phylogeny.PhylogenyMethods;
 import org.forester.phylogeny.PhylogenyNode;
 import org.forester.phylogeny.data.Accession;
-import org.forester.phylogeny.data.Annotation;
 import org.forester.phylogeny.data.Confidence;
 import org.forester.phylogeny.data.DomainArchitecture;
 import org.forester.phylogeny.data.Event;
 import org.forester.phylogeny.data.Identifier;
 import org.forester.phylogeny.data.PhylogenyDataUtil;
-import org.forester.phylogeny.data.PropertiesMap;
-import org.forester.phylogeny.data.Property;
 import org.forester.phylogeny.data.Sequence;
 import org.forester.phylogeny.data.Taxonomy;
 import org.forester.phylogeny.iterators.PhylogenyNodeIterator;
@@ -60,188 +58,43 @@ import org.forester.util.ForesterUtil;
 
 public final class NHXParser implements PhylogenyParser {
 
-    public static final boolean                              LIMIT_SPECIES_NAMES_TO_FIVE_CHARS = true;
-    public static final PhylogenyMethods.TAXONOMY_EXTRACTION TAXONOMY_EXTRACTION_DEFAULT       = PhylogenyMethods.TAXONOMY_EXTRACTION.NO;
-    final static private boolean                             GUESS_ROOTEDNESS_DEFAULT          = true;
-    final static private boolean                             GUESS_IF_SUPPORT_VALUES           = true;
-    final static private boolean                             IGNORE_QUOTES_DEFAULT             = false;
-    final static public boolean                              REPLACE_UNDERSCORES_DEFAULT       = false;
-    private boolean                                          _saw_closing_paren;
-    final static private byte                                STRING                            = 0;
-    final static private byte                                STRING_BUFFER                     = 1;
-    final static private byte                                CHAR_ARRAY                        = 2;
-    final static private byte                                BUFFERED_READER                   = 3;
-    private boolean                                          _guess_rootedness;
-    private boolean                                          _has_next;
-    private boolean                                          _ignore_quotes;
-    private byte                                             _input_type;
-    private int                                              _source_length;
-    private PhylogenyNode                                    _current_node;
-    private StringBuilder                                    _current_anotation;
-    private Object                                           _nhx_source;
-    private int                                              _clade_level;
-    private List<Phylogeny>                                  _phylogenies;
-    private Phylogeny                                        _current_phylogeny;
-    private PhylogenyMethods.TAXONOMY_EXTRACTION             _taxonomy_extraction;
-    private boolean                                          _replace_underscores;
-    public final static Pattern                              UC_LETTERS_NUMBERS_PATTERN        = Pattern
-                                                                                                       .compile( "^[A-Z0-9]+$" );
-    public final static Pattern                              NUMBERS_ONLY_PATTERN              = Pattern
-                                                                                                       .compile( "^[0-9\\.]+$" );
-    public final static Pattern                              MB_PROB_PATTERN                   = Pattern
-                                                                                                       .compile( "prob=([^,]+)" );
-    public final static Pattern                              MB_PROB_SD_PATTERN                = Pattern
-                                                                                                       .compile( "prob_stddev=([^,]+)" );
-    public final static Pattern                              MB_BL_PATTERN                     = Pattern
-                                                                                                       .compile( "length_median=([^,]+)" );
+    public static final TAXONOMY_EXTRACTION TAXONOMY_EXTRACTION_DEFAULT = TAXONOMY_EXTRACTION.NO;
+    final static private boolean            GUESS_ROOTEDNESS_DEFAULT    = true;
+    final static private boolean            GUESS_IF_SUPPORT_VALUES     = true;
+    final static private boolean            IGNORE_QUOTES_DEFAULT       = false;
+    final static public boolean             REPLACE_UNDERSCORES_DEFAULT = false;
+    private boolean                         _saw_closing_paren;
+    final static private byte               STRING                      = 0;
+    final static private byte               STRING_BUFFER               = 1;
+    final static private byte               CHAR_ARRAY                  = 2;
+    final static private byte               BUFFERED_READER             = 3;
+    final static private byte               STRING_BUILDER              = 4;
+    private boolean                         _guess_rootedness;
+    private boolean                         _ignore_quotes;
+    private byte                            _input_type;
+    private int                             _source_length;
+    private PhylogenyNode                   _current_node;
+    private StringBuilder                   _current_anotation;
+    private Object                          _nhx_source;
+    private int                             _clade_level;
+    private List<Phylogeny>                 _phylogenies;
+    private Phylogeny                       _current_phylogeny;
+    private TAXONOMY_EXTRACTION             _taxonomy_extraction;
+    private boolean                         _replace_underscores;
+    public final static Pattern             UC_LETTERS_NUMBERS_PATTERN  = Pattern.compile( "^[A-Z0-9]+$" );
+    public final static Pattern             NUMBERS_ONLY_PATTERN        = Pattern.compile( "^[0-9\\.]+$" );
+    public final static Pattern             MB_PROB_PATTERN             = Pattern.compile( "prob=([^,]+)" );
+    public final static Pattern             MB_PROB_SD_PATTERN          = Pattern.compile( "prob_stddev=([^,]+)" );
+    public final static Pattern             MB_BL_PATTERN               = Pattern.compile( "length_median=([^,]+)" );
 
     public NHXParser() {
         init();
     }
 
-    /**
-     * Decreases the clade level by one.
-     * 
-     * @throws PhylogenyParserException
-     *             if level goes below zero.
-     */
-    private void decreaseCladeLevel() throws PhylogenyParserException {
-        if ( getCladeLevel() < 0 ) {
-            throw new PhylogenyParserException( "error in NH (Newick)/NHX formatted data: most likely cause: number of close parens is larger than number of open parens" );
-        }
-        --_clade_level;
-    }
-
-    /**
-     * Finishes the current Phylogeny and adds it to the list of Phylogenies
-     * created.
-     * 
-     * @throws PhylogenyParserException
-     * @throws NHXFormatException
-     */
-    private void finishPhylogeny() throws PhylogenyParserException, NHXFormatException {
-        setCladeLevel( 0 );
-        if ( getCurrentPhylogeny() != null ) {
-            parseNHX( getCurrentAnotation().toString(),
-                      getCurrentPhylogeny().getRoot(),
-                      getTaxonomyExtraction(),
-                      isReplaceUnderscores() );
-            if ( NHXParser.GUESS_IF_SUPPORT_VALUES ) {
-                if ( NHXParser.isBranchLengthsLikeBootstrapValues( getCurrentPhylogeny() ) ) {
-                    NHXParser.moveBranchLengthsToBootstrapValues( getCurrentPhylogeny() );
-                }
-            }
-            if ( isGuessRootedness() ) {
-                final PhylogenyNode root = getCurrentPhylogeny().getRoot();
-                if ( ( root.getDistanceToParent() >= 0.0 ) || !ForesterUtil.isEmpty( root.getName() )
-                        || !ForesterUtil.isEmpty( PhylogenyMethods.getSpecies( root ) ) || root.isHasAssignedEvent() ) {
-                    getCurrentPhylogeny().setRooted( true );
-                }
-            }
-            getPhylogenies().add( getCurrentPhylogeny() );
-        }
-    }
-
-    private void finishSingleNodePhylogeny() throws PhylogenyParserException, NHXFormatException {
-        setCladeLevel( 0 );
-        final PhylogenyNode new_node = new PhylogenyNode();
-        parseNHX( getCurrentAnotation().toString(), new_node, getTaxonomyExtraction(), isReplaceUnderscores() );
-        setCurrentPhylogeny( new Phylogeny() );
-        getCurrentPhylogeny().setRoot( new_node );
-        getPhylogenies().add( getCurrentPhylogeny() );
-    }
-
-    private int getCladeLevel() {
-        return _clade_level;
-    }
-
-    private StringBuilder getCurrentAnotation() {
-        return _current_anotation;
-    }
-
-    private PhylogenyNode getCurrentNode() {
-        return _current_node;
-    }
-
-    private Phylogeny getCurrentPhylogeny() {
-        return _current_phylogeny;
-    }
-
-    private byte getInputType() {
-        return _input_type;
-    }
-
-    private Object getNhxSource() {
-        return _nhx_source;
-    }
-
-    private List<Phylogeny> getPhylogenies() {
-        return _phylogenies;
-    }
-
-    /**
-     * Returns the Phylogenies created as Array.
-     * 
-     * @return the Phylogenies created as Array
-     */
-    private Phylogeny[] getPhylogeniesAsArray() {
-        final Phylogeny[] p = new Phylogeny[ getPhylogenies().size() ];
-        for( int i = 0; i < getPhylogenies().size(); ++i ) {
-            p[ i ] = getPhylogenies().get( i );
-        }
-        return p;
-    }
-
-    private int getSourceLength() {
-        return _source_length;
-    }
-
-    public PhylogenyMethods.TAXONOMY_EXTRACTION getTaxonomyExtraction() {
+    public NHXParser.TAXONOMY_EXTRACTION getTaxonomyExtraction() {
         return _taxonomy_extraction;
     }
 
-    public boolean hasNext() {
-        return _has_next;
-    }
-
-    /**
-     * Increases the clade level by one.
-     */
-    private void increaseCladeLevel() {
-        ++_clade_level;
-    }
-
-    private void init() {
-        setTaxonomyExtraction( TAXONOMY_EXTRACTION_DEFAULT );
-        setReplaceUnderscores( REPLACE_UNDERSCORES_DEFAULT );
-        setGuessRootedness( GUESS_ROOTEDNESS_DEFAULT );
-        setIgnoreQuotes( IGNORE_QUOTES_DEFAULT );
-        setHasNext( false );
-    }
-
-    private boolean isGuessRootedness() {
-        return _guess_rootedness;
-    }
-
-    private boolean isIgnoreQuotes() {
-        return _ignore_quotes;
-    }
-
-    private boolean isReplaceUnderscores() {
-        return _replace_underscores;
-    }
-
-    private boolean isSawClosingParen() {
-        return _saw_closing_paren;
-    }
-
-    /**
-     * Replaces the current annotation with a new StringBuffer.
-     */
-    private void newCurrentAnotation() {
-        setCurrentAnotation( new StringBuilder() );
-    }
-
     /**
      * Parses the source set with setSource( final Object nhx_source ). Returns
      * the Phylogenies found in the source as Phylogeny[].
@@ -260,7 +113,6 @@ public final class NHXParser implements PhylogenyParser {
      */
     @Override
     public Phylogeny[] parse() throws IOException, NHXFormatException {
-        setHasNext( false );
         boolean in_comment = false;
         boolean saw_colon = false;
         boolean saw_open_bracket = false;
@@ -270,11 +122,37 @@ public final class NHXParser implements PhylogenyParser {
         setPhylogenies( new ArrayList<Phylogeny>() );
         setCladeLevel( 0 );
         newCurrentAnotation();
+        setCurrentPhylogeny( null );
+        setCurrentNode( null );
         int i = 0;
+        String my_source_str = null;
+        StringBuffer my_source_sbuff = null;
+        StringBuilder my_source_sbuil = null;
+        char[] my_source_charary = null;
+        BufferedReader my_source_br = null;
+        switch ( getInputType() ) {
+            case STRING:
+                my_source_str = ( String ) getNhxSource();
+                break;
+            case STRING_BUFFER:
+                my_source_sbuff = ( StringBuffer ) getNhxSource();
+                break;
+            case STRING_BUILDER:
+                my_source_sbuil = ( StringBuilder ) getNhxSource();
+                break;
+            case CHAR_ARRAY:
+                my_source_charary = ( char[] ) getNhxSource();
+                break;
+            case BUFFERED_READER:
+                my_source_br = ( BufferedReader ) getNhxSource();
+                break;
+            default:
+                throw new RuntimeException( "unknown input type" );
+        }
         while ( true ) {
             char c = '\b';
             if ( getInputType() == NHXParser.BUFFERED_READER ) {
-                final int ci = ( ( BufferedReader ) getNhxSource() ).read();
+                final int ci = my_source_br.read();
                 if ( ci >= 0 ) {
                     c = ( char ) ci;
                 }
@@ -289,13 +167,16 @@ public final class NHXParser implements PhylogenyParser {
                 else {
                     switch ( getInputType() ) {
                         case STRING:
-                            c = ( ( String ) getNhxSource() ).charAt( i );
+                            c = my_source_str.charAt( i );
                             break;
                         case STRING_BUFFER:
-                            c = ( ( StringBuffer ) getNhxSource() ).charAt( i );
+                            c = my_source_sbuff.charAt( i );
+                            break;
+                        case STRING_BUILDER:
+                            c = my_source_sbuil.charAt( i );
                             break;
                         case CHAR_ARRAY:
-                            c = ( ( char[] ) getNhxSource() )[ i ];
+                            c = my_source_charary[ i ];
                             break;
                     }
                 }
@@ -308,9 +189,9 @@ public final class NHXParser implements PhylogenyParser {
                         && ( ( c != '[' ) && ( c != '.' ) && ( ( c < 48 ) || ( c > 57 ) ) ) ) {
                     saw_colon = false;
                 }
-            }
-            if ( in_open_bracket && ( c == ']' ) ) {
-                in_open_bracket = false;
+                if ( in_open_bracket && ( c == ']' ) ) {
+                    in_open_bracket = false;
+                }
             }
             // \n\t is always ignored,
             // as is " (34) and ' (39) (space is 32):
@@ -399,8 +280,232 @@ public final class NHXParser implements PhylogenyParser {
         return getPhylogeniesAsArray();
     } // parse()
 
-    public Phylogeny parseNext() throws IOException, NHXFormatException {
-        return null;
+    public void setGuessRootedness( final boolean guess_rootedness ) {
+        _guess_rootedness = guess_rootedness;
+    }
+
+    public void setIgnoreQuotes( final boolean ignore_quotes ) {
+        _ignore_quotes = ignore_quotes;
+    }
+
+    public void setReplaceUnderscores( final boolean replace_underscores ) {
+        _replace_underscores = replace_underscores;
+    }
+
+    /**
+     * This sets the source to be parsed. The source can be: String,
+     * StringBuffer, char[], File, or InputStream. The source can contain more
+     * than one phylogenies in either New Hamphshire (NH) or New Hamphshire
+     * Extended (NHX) format. There is no need to separate phylogenies with any
+     * special character. White space is always ignored, as are semicolons
+     * inbetween phylogenies. Example of a source describing two phylogenies
+     * (source is a String, in this example): "(A,(B,(C,(D,E)de)cde)bcde)abcde
+     * ((((A,B)ab,C)abc,D)abcd,E)abcde". Everything between a '[' followed by any
+     * character other than '&' and ']' is considered a comment and ignored
+     * (example: "[this is a comment]"). NHX tags are surrounded by '[&&NHX' and
+     * ']' (example: "[&&NHX:S=Varanus_storri]"). A sequence like "[& some
+     * info]" is ignored, too (at the PhylogenyNode level, though).
+     * Exception: numbers only between [ and ] (e.g. [90]) are interpreted as support values.
+     * 
+     * @see #parse()
+     * @see org.forester.io.parsers.PhylogenyParser#setSource(java.lang.Object)
+     * @param nhx_source
+     *            the source to be parsed (String, StringBuffer, char[], File,
+     *            or InputStream)
+     * @throws IOException
+     * @throws PhylogenyParserException
+     */
+    @Override
+    public void setSource( final Object nhx_source ) throws PhylogenyParserException, IOException {
+        if ( nhx_source == null ) {
+            throw new PhylogenyParserException( getClass() + ": attempt to parse null object." );
+        }
+        else if ( nhx_source instanceof String ) {
+            setInputType( NHXParser.STRING );
+            setSourceLength( ( ( String ) nhx_source ).length() );
+            setNhxSource( nhx_source );
+        }
+        else if ( nhx_source instanceof StringBuilder ) {
+            setInputType( NHXParser.STRING_BUILDER );
+            setSourceLength( ( ( StringBuilder ) nhx_source ).length() );
+            setNhxSource( nhx_source );
+        }
+        else if ( nhx_source instanceof StringBuffer ) {
+            setInputType( NHXParser.STRING_BUFFER );
+            setSourceLength( ( ( StringBuffer ) nhx_source ).length() );
+            setNhxSource( nhx_source );
+        }
+        else if ( nhx_source instanceof StringBuilder ) {
+            setInputType( NHXParser.STRING_BUILDER );
+            setSourceLength( ( ( StringBuilder ) nhx_source ).length() );
+            setNhxSource( nhx_source );
+        }
+        else if ( nhx_source instanceof char[] ) {
+            setInputType( NHXParser.CHAR_ARRAY );
+            setSourceLength( ( ( char[] ) nhx_source ).length );
+            setNhxSource( nhx_source );
+        }
+        else if ( nhx_source instanceof File ) {
+            setInputType( NHXParser.BUFFERED_READER );
+            setSourceLength( 0 );
+            final File f = ( File ) nhx_source;
+            final String error = ForesterUtil.isReadableFile( f );
+            if ( !ForesterUtil.isEmpty( error ) ) {
+                throw new PhylogenyParserException( error );
+            }
+            setNhxSource( new BufferedReader( new FileReader( f ) ) );
+        }
+        else if ( nhx_source instanceof InputStream ) {
+            setInputType( NHXParser.BUFFERED_READER );
+            setSourceLength( 0 );
+            final InputStreamReader isr = new InputStreamReader( ( InputStream ) nhx_source );
+            setNhxSource( new BufferedReader( isr ) );
+        }
+        else {
+            throw new IllegalArgumentException( getClass() + " can only parse objects of type String,"
+                    + " StringBuffer, char[], File," + " or InputStream " + " [attempt to parse object of "
+                    + nhx_source.getClass() + "]." );
+        }
+    }
+
+    public void setTaxonomyExtraction( final NHXParser.TAXONOMY_EXTRACTION taxonomy_extraction ) {
+        _taxonomy_extraction = taxonomy_extraction;
+    }
+
+    /**
+     * Decreases the clade level by one.
+     * 
+     * @throws PhylogenyParserException
+     *             if level goes below zero.
+     */
+    private void decreaseCladeLevel() throws PhylogenyParserException {
+        if ( getCladeLevel() < 0 ) {
+            throw new PhylogenyParserException( "error in NH (Newick)/NHX formatted data: most likely cause: number of close parens is larger than number of open parens" );
+        }
+        --_clade_level;
+    }
+
+    /**
+     * Finishes the current Phylogeny and adds it to the list of Phylogenies
+     * created.
+     * 
+     * @throws PhylogenyParserException
+     * @throws NHXFormatException
+     * @throws PhyloXmlDataFormatException 
+     */
+    private void finishPhylogeny() throws PhylogenyParserException, NHXFormatException, PhyloXmlDataFormatException {
+        setCladeLevel( 0 );
+        if ( getCurrentPhylogeny() != null ) {
+            parseNHX( getCurrentAnotation().toString(),
+                      getCurrentPhylogeny().getRoot(),
+                      getTaxonomyExtraction(),
+                      isReplaceUnderscores() );
+            if ( NHXParser.GUESS_IF_SUPPORT_VALUES ) {
+                if ( NHXParser.isBranchLengthsLikeBootstrapValues( getCurrentPhylogeny() ) ) {
+                    NHXParser.moveBranchLengthsToConfidenceValues( getCurrentPhylogeny() );
+                }
+            }
+            if ( isGuessRootedness() ) {
+                final PhylogenyNode root = getCurrentPhylogeny().getRoot();
+                if ( ( root.getDistanceToParent() >= 0.0 ) || !ForesterUtil.isEmpty( root.getName() )
+                        || !ForesterUtil.isEmpty( PhylogenyMethods.getSpecies( root ) ) || root.isHasAssignedEvent() ) {
+                    getCurrentPhylogeny().setRooted( true );
+                }
+            }
+            getPhylogenies().add( getCurrentPhylogeny() );
+        }
+    }
+
+    private void finishSingleNodePhylogeny() throws PhylogenyParserException, NHXFormatException,
+            PhyloXmlDataFormatException {
+        setCladeLevel( 0 );
+        final PhylogenyNode new_node = new PhylogenyNode();
+        parseNHX( getCurrentAnotation().toString(), new_node, getTaxonomyExtraction(), isReplaceUnderscores() );
+        setCurrentPhylogeny( new Phylogeny() );
+        getCurrentPhylogeny().setRoot( new_node );
+        getPhylogenies().add( getCurrentPhylogeny() );
+    }
+
+    private int getCladeLevel() {
+        return _clade_level;
+    }
+
+    private StringBuilder getCurrentAnotation() {
+        return _current_anotation;
+    }
+
+    private PhylogenyNode getCurrentNode() {
+        return _current_node;
+    }
+
+    private Phylogeny getCurrentPhylogeny() {
+        return _current_phylogeny;
+    }
+
+    private byte getInputType() {
+        return _input_type;
+    }
+
+    private Object getNhxSource() {
+        return _nhx_source;
+    }
+
+    private List<Phylogeny> getPhylogenies() {
+        return _phylogenies;
+    }
+
+    /**
+     * Returns the Phylogenies created as Array.
+     * 
+     * @return the Phylogenies created as Array
+     */
+    private Phylogeny[] getPhylogeniesAsArray() {
+        final Phylogeny[] p = new Phylogeny[ getPhylogenies().size() ];
+        for( int i = 0; i < getPhylogenies().size(); ++i ) {
+            p[ i ] = getPhylogenies().get( i );
+        }
+        return p;
+    }
+
+    private int getSourceLength() {
+        return _source_length;
+    }
+
+    /**
+     * Increases the clade level by one.
+     */
+    private void increaseCladeLevel() {
+        ++_clade_level;
+    }
+
+    private void init() {
+        setTaxonomyExtraction( TAXONOMY_EXTRACTION_DEFAULT );
+        setReplaceUnderscores( REPLACE_UNDERSCORES_DEFAULT );
+        setGuessRootedness( GUESS_ROOTEDNESS_DEFAULT );
+        setIgnoreQuotes( IGNORE_QUOTES_DEFAULT );
+    }
+
+    private boolean isGuessRootedness() {
+        return _guess_rootedness;
+    }
+
+    private boolean isIgnoreQuotes() {
+        return _ignore_quotes;
+    }
+
+    private boolean isReplaceUnderscores() {
+        return _replace_underscores;
+    }
+
+    private boolean isSawClosingParen() {
+        return _saw_closing_paren;
+    }
+
+    /**
+     * Replaces the current annotation with a new StringBuffer.
+     */
+    private void newCurrentAnotation() {
+        setCurrentAnotation( new StringBuilder() );
     }
 
     /**
@@ -408,8 +513,9 @@ public final class NHXParser implements PhylogenyParser {
      * 
      * @throws PhylogenyParserException
      * @throws NHXFormatException
+     * @throws PhyloXmlDataFormatException 
      */
-    private void processCloseParen() throws PhylogenyParserException, NHXFormatException {
+    private void processCloseParen() throws PhylogenyParserException, NHXFormatException, PhyloXmlDataFormatException {
         decreaseCladeLevel();
         if ( !isSawClosingParen() ) {
             final PhylogenyNode new_node = new PhylogenyNode();
@@ -435,8 +541,9 @@ public final class NHXParser implements PhylogenyParser {
      * 
      * @throws PhylogenyParserException
      * @throws NHXFormatException
+     * @throws PhyloXmlDataFormatException 
      */
-    private void processComma() throws PhylogenyParserException, NHXFormatException {
+    private void processComma() throws PhylogenyParserException, NHXFormatException, PhyloXmlDataFormatException {
         if ( !isSawClosingParen() ) {
             final PhylogenyNode new_node = new PhylogenyNode();
             parseNHX( getCurrentAnotation().toString(), new_node, getTaxonomyExtraction(), isReplaceUnderscores() );
@@ -460,8 +567,9 @@ public final class NHXParser implements PhylogenyParser {
      * 
      * @throws PhylogenyParserException
      * @throws NHXFormatException
+     * @throws PhyloXmlDataFormatException 
      */
-    private void processOpenParen() throws PhylogenyParserException, NHXFormatException {
+    private void processOpenParen() throws PhylogenyParserException, NHXFormatException, PhyloXmlDataFormatException {
         final PhylogenyNode new_node = new PhylogenyNode();
         if ( getCladeLevel() == 0 ) {
             if ( getCurrentPhylogeny() != null ) {
@@ -499,18 +607,6 @@ public final class NHXParser implements PhylogenyParser {
         _current_phylogeny = current_phylogeny;
     }
 
-    public void setGuessRootedness( final boolean guess_rootedness ) {
-        _guess_rootedness = guess_rootedness;
-    }
-
-    private void setHasNext( final boolean has_next ) {
-        _has_next = has_next;
-    }
-
-    public void setIgnoreQuotes( final boolean ignore_quotes ) {
-        _ignore_quotes = ignore_quotes;
-    }
-
     private void setInputType( final byte input_type ) {
         _input_type = input_type;
     }
@@ -523,128 +619,20 @@ public final class NHXParser implements PhylogenyParser {
         _phylogenies = phylogenies;
     }
 
-    public void setReplaceUnderscores( final boolean replace_underscores ) {
-        _replace_underscores = replace_underscores;
-    }
-
     private void setSawClosingParen( final boolean saw_closing_paren ) {
         _saw_closing_paren = saw_closing_paren;
     }
 
-    /**
-     * This sets the source to be parsed. The source can be: String,
-     * StringBuffer, char[], File, or InputStream. The source can contain more
-     * than one phylogenies in either New Hamphshire (NH) or New Hamphshire
-     * Extended (NHX) format. There is no need to separate phylogenies with any
-     * special character. White space is always ignored, as are semicolons
-     * inbetween phylogenies. Example of a source describing two phylogenies
-     * (source is a String, in this example): "(A,(B,(C,(D,E)de)cde)bcde)abcde
-     * ((((A,B)ab,C)abc,D)abcd,E)abcde". Everything between a '[' followed by any
-     * character other than '&' and ']' is considered a comment and ignored
-     * (example: "[this is a comment]"). NHX tags are surrounded by '[&&NHX' and
-     * ']' (example: "[&&NHX:S=Varanus_storri]"). A sequence like "[& some
-     * info]" is ignored, too (at the PhylogenyNode level, though).
-     * Exception: numbers only between [ and ] (e.g. [90]) are interpreted as support values.
-     * 
-     * @see #parse()
-     * @see org.forester.io.parsers.PhylogenyParser#setSource(java.lang.Object)
-     * @param nhx_source
-     *            the source to be parsed (String, StringBuffer, char[], File,
-     *            or InputStream)
-     * @throws IOException
-     * @throws PhylogenyParserException
-     */
-    @Override
-    public void setSource( final Object nhx_source ) throws PhylogenyParserException, IOException {
-        if ( nhx_source == null ) {
-            throw new PhylogenyParserException( getClass() + ": attempt to parse null object." );
-        }
-        else if ( nhx_source instanceof String ) {
-            setInputType( NHXParser.STRING );
-            setSourceLength( ( ( String ) nhx_source ).length() );
-            setNhxSource( nhx_source );
-        }
-        else if ( nhx_source instanceof StringBuffer ) {
-            setInputType( NHXParser.STRING_BUFFER );
-            setSourceLength( ( ( StringBuffer ) nhx_source ).length() );
-            setNhxSource( nhx_source );
-        }
-        else if ( nhx_source instanceof char[] ) {
-            setInputType( NHXParser.CHAR_ARRAY );
-            setSourceLength( ( ( char[] ) nhx_source ).length );
-            setNhxSource( nhx_source );
-        }
-        else if ( nhx_source instanceof File ) {
-            setInputType( NHXParser.BUFFERED_READER );
-            setSourceLength( 0 );
-            final File f = ( File ) nhx_source;
-            final String error = ForesterUtil.isReadableFile( f );
-            if ( !ForesterUtil.isEmpty( error ) ) {
-                throw new PhylogenyParserException( error );
-            }
-            setNhxSource( new BufferedReader( new FileReader( f ) ) );
-        }
-        else if ( nhx_source instanceof InputStream ) {
-            setInputType( NHXParser.BUFFERED_READER );
-            setSourceLength( 0 );
-            final InputStreamReader isr = new InputStreamReader( ( InputStream ) nhx_source );
-            setNhxSource( new BufferedReader( isr ) );
-        }
-        else {
-            throw new IllegalArgumentException( getClass() + " can only parse objects of type String,"
-                    + " StringBuffer, char[], File," + " or InputStream " + " [attempt to parse object of "
-                    + nhx_source.getClass() + "]." );
-        }
-        setHasNext( true );
-    }
-
     private void setSourceLength( final int source_length ) {
         _source_length = source_length;
     }
 
-    public void setTaxonomyExtraction( final PhylogenyMethods.TAXONOMY_EXTRACTION taxonomy_extraction ) {
-        _taxonomy_extraction = taxonomy_extraction;
-    }
-
-    private static double doubleValue( final String str ) throws NHXFormatException {
-        try {
-            return Double.valueOf( str ).doubleValue();
-        }
-        catch ( final NumberFormatException ex ) {
-            throw new NHXFormatException( "error in NH/NHX formatted data: failed to parse number from :" + "\"" + str
-                    + "\"" );
-        }
-    }
-
-    private static boolean isBranchLengthsLikeBootstrapValues( final Phylogeny p ) {
-        final PhylogenyNodeIterator it = p.iteratorExternalForward();
-        final double d0 = it.next().getDistanceToParent();
-        if ( ( d0 < 10 ) || !it.hasNext() ) {
-            return false;
-        }
-        while ( it.hasNext() ) {
-            final double d = it.next().getDistanceToParent();
-            if ( ( d != d0 ) || ( d < 10 ) ) {
-                return false;
-            }
-        }
-        return true;
-    }
-
-    private static void moveBranchLengthsToBootstrapValues( final Phylogeny p ) {
-        final PhylogenyNodeIterator it = p.iteratorPostorder();
-        while ( it.hasNext() ) {
-            final PhylogenyNode n = it.next();
-            PhylogenyMethods.setBootstrapConfidence( n, n.getDistanceToParent() );
-            n.setDistanceToParent( PhylogenyDataUtil.BRANCH_LENGTH_DEFAULT );
-        }
-    }
-
     public static void parseNHX( String s,
                                  final PhylogenyNode node_to_annotate,
-                                 final PhylogenyMethods.TAXONOMY_EXTRACTION taxonomy_extraction,
-                                 final boolean replace_underscores ) throws NHXFormatException {
-        if ( ( taxonomy_extraction != PhylogenyMethods.TAXONOMY_EXTRACTION.NO ) && replace_underscores ) {
+                                 final TAXONOMY_EXTRACTION taxonomy_extraction,
+                                 final boolean replace_underscores ) throws NHXFormatException,
+            PhyloXmlDataFormatException {
+        if ( ( taxonomy_extraction != TAXONOMY_EXTRACTION.NO ) && replace_underscores ) {
             throw new IllegalArgumentException( "cannot extract taxonomies and replace under scores at the same time" );
         }
         if ( ( s != null ) && ( s.length() > 0 ) ) {
@@ -683,18 +671,8 @@ public final class NHXParser implements PhylogenyParser {
             if ( t.countTokens() > 0 ) {
                 if ( !s.startsWith( ":" ) ) {
                     node_to_annotate.setName( t.nextToken() );
-                    if ( !replace_underscores
-                            && ( !is_nhx && ( taxonomy_extraction != PhylogenyMethods.TAXONOMY_EXTRACTION.NO ) ) ) {
-                        final String tax = ParserUtils
-                                .extractTaxonomyCodeFromNodeName( node_to_annotate.getName(),
-                                                                  LIMIT_SPECIES_NAMES_TO_FIVE_CHARS,
-                                                                  taxonomy_extraction );
-                        if ( !ForesterUtil.isEmpty( tax ) ) {
-                            if ( !node_to_annotate.getNodeData().isHasTaxonomy() ) {
-                                node_to_annotate.getNodeData().setTaxonomy( new Taxonomy() );
-                            }
-                            node_to_annotate.getNodeData().getTaxonomy().setTaxonomyCode( tax );
-                        }
+                    if ( !replace_underscores && ( !is_nhx && ( taxonomy_extraction != TAXONOMY_EXTRACTION.NO ) ) ) {
+                        ParserUtils.extractTaxonomyDataFromNodeName( node_to_annotate, taxonomy_extraction );
                     }
                 }
                 while ( t.hasMoreTokens() ) {
@@ -705,14 +683,6 @@ public final class NHXParser implements PhylogenyParser {
                         }
                         node_to_annotate.getNodeData().getTaxonomy().setScientificName( s.substring( 2 ) );
                     }
-                    else if ( s.startsWith( org.forester.io.parsers.nhx.NHXtags.ANNOTATION ) ) {
-                        if ( !node_to_annotate.getNodeData().isHasSequence() ) {
-                            node_to_annotate.getNodeData().setSequence( new Sequence() );
-                        }
-                        final Annotation annotation = new Annotation( "_:_" );
-                        annotation.setDesc( s.substring( 3 ) );
-                        node_to_annotate.getNodeData().getSequence().addAnnotation( annotation );
-                    }
                     else if ( s.startsWith( org.forester.io.parsers.nhx.NHXtags.IS_DUPLICATION ) ) {
                         if ( ( s.charAt( 2 ) == 'Y' ) || ( s.charAt( 2 ) == 'T' ) ) {
                             node_to_annotate.getNodeData().setEvent( Event.createSingleDuplicationEvent() );
@@ -745,12 +715,6 @@ public final class NHXParser implements PhylogenyParser {
                             PhylogenyMethods.setBranchColorValue( node_to_annotate, c );
                         }
                     }
-                    else if ( s.startsWith( NHXtags.CUSTOM_DATA_ON_NODE ) ) {
-                        if ( !node_to_annotate.getNodeData().isHasProperties() ) {
-                            node_to_annotate.getNodeData().setProperties( new PropertiesMap() );
-                        }
-                        node_to_annotate.getNodeData().getProperties().addProperty( Property.createFromNhxString( s ) );
-                    }
                     else if ( s.startsWith( NHXtags.DOMAIN_STRUCTURE ) ) {
                         if ( !node_to_annotate.getNodeData().isHasSequence() ) {
                             node_to_annotate.getNodeData().setSequence( new Sequence() );
@@ -758,9 +722,6 @@ public final class NHXParser implements PhylogenyParser {
                         node_to_annotate.getNodeData().getSequence()
                                 .setDomainArchitecture( new DomainArchitecture( s.substring( 3 ) ) );
                     }
-                    else if ( s.startsWith( NHXtags.NODE_IDENTIFIER ) ) {
-                        node_to_annotate.getNodeData().setNodeIdentifier( new Identifier( s.substring( 3 ) ) );
-                    }
                     else if ( s.startsWith( NHXtags.SEQUENCE_ACCESSION ) ) {
                         if ( !node_to_annotate.getNodeData().isHasSequence() ) {
                             node_to_annotate.getNodeData().setSequence( new Sequence() );
@@ -774,12 +735,6 @@ public final class NHXParser implements PhylogenyParser {
                         }
                         node_to_annotate.getNodeData().getSequence().setName( s.substring( 3 ) );
                     }
-                    else if ( s.startsWith( NHXtags.GENE_NAME_SYNONYM ) ) {
-                        if ( !node_to_annotate.getNodeData().isHasSequence() ) {
-                            node_to_annotate.getNodeData().setSequence( new Sequence() );
-                        }
-                        node_to_annotate.getNodeData().getSequence().setName( s.substring( 2 ) );
-                    }
                     else if ( s.indexOf( '=' ) < 0 ) {
                         if ( node_to_annotate.getDistanceToParent() != PhylogenyDataUtil.BRANCH_LENGTH_DEFAULT ) {
                             throw new NHXFormatException( "error in NHX formatted data: more than one distance to parent:"
@@ -792,6 +747,40 @@ public final class NHXParser implements PhylogenyParser {
         }
     }
 
+    private static double doubleValue( final String str ) throws NHXFormatException {
+        try {
+            return Double.valueOf( str ).doubleValue();
+        }
+        catch ( final NumberFormatException ex ) {
+            throw new NHXFormatException( "error in NH/NHX formatted data: failed to parse number from " + "\"" + str
+                    + "\"" );
+        }
+    }
+
+    private static boolean isBranchLengthsLikeBootstrapValues( final Phylogeny p ) {
+        final PhylogenyNodeIterator it = p.iteratorExternalForward();
+        final double d0 = it.next().getDistanceToParent();
+        if ( ( d0 < 10 ) || !it.hasNext() ) {
+            return false;
+        }
+        while ( it.hasNext() ) {
+            final double d = it.next().getDistanceToParent();
+            if ( ( d != d0 ) || ( d < 10 ) ) {
+                return false;
+            }
+        }
+        return true;
+    }
+
+    private static void moveBranchLengthsToConfidenceValues( final Phylogeny p ) {
+        final PhylogenyNodeIterator it = p.iteratorPostorder();
+        while ( it.hasNext() ) {
+            final PhylogenyNode n = it.next();
+            PhylogenyMethods.setBootstrapConfidence( n, n.getDistanceToParent() );
+            n.setDistanceToParent( PhylogenyDataUtil.BRANCH_LENGTH_DEFAULT );
+        }
+    }
+
     private static void processMrBayes3Data( final String s, final PhylogenyNode node_to_annotate )
             throws NHXFormatException {
         double sd = -1;
@@ -854,4 +843,8 @@ public final class NHXParser implements PhylogenyParser {
         final int blu = ForesterUtil.limitRangeForColor( Integer.parseInt( st.nextToken() ) );
         return new Color( red, green, blu );
     }
+
+    public static enum TAXONOMY_EXTRACTION {
+        NO, YES, PFAM_STYLE_ONLY;
+    }
 }