reordered
[jalview.git] / forester / java / src / org / forester / io / parsers / util / ParserUtils.java
index b8dd42e..9168c9d 100644 (file)
@@ -36,74 +36,71 @@ import java.io.InputStreamReader;
 import java.io.StringReader;
 import java.net.URL;
 import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 import org.forester.io.parsers.PhylogenyParser;
 import org.forester.io.parsers.nexus.NexusPhylogeniesParser;
 import org.forester.io.parsers.nhx.NHXParser;
+import org.forester.io.parsers.nhx.NHXParser.TAXONOMY_EXTRACTION;
+import org.forester.io.parsers.phyloxml.PhyloXmlDataFormatException;
 import org.forester.io.parsers.phyloxml.PhyloXmlParser;
 import org.forester.io.parsers.tol.TolParser;
+import org.forester.phylogeny.Phylogeny;
 import org.forester.phylogeny.PhylogenyMethods;
+import org.forester.phylogeny.PhylogenyNode;
+import org.forester.phylogeny.data.Identifier;
+import org.forester.phylogeny.data.Taxonomy;
 import org.forester.util.ForesterConstants;
 import org.forester.util.ForesterUtil;
 
 public final class ParserUtils {
 
-    final public static PhylogenyParser createParserDependingOnUrlContents( final URL url,
-                                                                            final boolean phyloxml_validate_against_xsd )
-            throws FileNotFoundException, IOException {
-        final String lc_filename = url.getFile().toString().toLowerCase();
-        PhylogenyParser parser = createParserDependingOnSuffix( lc_filename, phyloxml_validate_against_xsd );
-        if ( ( parser != null ) && lc_filename.endsWith( ".zip" ) ) {
-            if ( parser instanceof PhyloXmlParser ) {
-                ( ( PhyloXmlParser ) parser ).setZippedInputstream( true );
-            }
-            else if ( parser instanceof TolParser ) {
-                ( ( TolParser ) parser ).setZippedInputstream( true );
-            }
-        }
-        if ( parser == null ) {
-            final String first_line = ForesterUtil.getFirstLine( url ).trim().toLowerCase();
-            if ( first_line.startsWith( "<" ) ) {
-                parser = new PhyloXmlParser();
-                if ( phyloxml_validate_against_xsd ) {
-                    final ClassLoader cl = PhyloXmlParser.class.getClassLoader();
-                    final URL xsd_url = cl.getResource( ForesterConstants.LOCAL_PHYLOXML_XSD_RESOURCE );
-                    if ( xsd_url != null ) {
-                        ( ( PhyloXmlParser ) parser ).setValidateAgainstSchema( xsd_url.toString() );
-                    }
-                    else {
-                        throw new RuntimeException( "failed to get URL for phyloXML XSD from jar file from ["
-                                + ForesterConstants.LOCAL_PHYLOXML_XSD_RESOURCE + "]" );
-                    }
-                }
-            }
-            else if ( ( first_line.startsWith( "nexus" ) ) || ( first_line.startsWith( "#nexus" ) )
-                    || ( first_line.startsWith( "# nexus" ) ) || ( first_line.startsWith( "begin" ) ) ) {
-                parser = new NexusPhylogeniesParser();
-            }
-            else {
-                parser = new NHXParser();
-            }
-        }
-        return parser;
-    }
+    final private static String  SN_BN                                = "[A-Z][a-z]{2,30}[_ ][a-z]{3,30}";
+    final public static String   TAX_CODE                             = "(?:[A-Z9][A-Z]{2}[A-Z0-9]{2})|RAT|PIG|PEA";
+    final public static String   TAX_CODE_LO                          = "(?:[A-Z]{5})|RAT|PIG|PEA";
+    final public static Pattern  TAXOMONY_CODE_PATTERN_A              = Pattern.compile( "(?:\\b|_)(" + TAX_CODE
+                                                                              + ")(?:\\b|_)" );
+    final public static Pattern  TAXOMONY_CODE_PATTERN_A_LO           = Pattern.compile( "(?:\\b|_)(" + TAX_CODE_LO
+                                                                              + ")(?:\\b|_)" );
+    final public static Pattern  TAXOMONY_CODE_PATTERN_BRACKETED      = Pattern.compile( "\\[(" + TAX_CODE + ")\\]" );
+    final public static Pattern  TAXOMONY_CODE_PATTERN_PFR            = Pattern.compile( "(?:\\b|_)[a-zA-Z0-9]{3,}_("
+                                                                              + TAX_CODE + ")\\b" );
+    final public static Pattern  TAXOMONY_SN_PATTERN_GENUS            = Pattern.compile( "([A-Z][a-z]{2,30})" );
+    final public static Pattern  TAXOMONY_SN_PATTERN_SN               = Pattern.compile( "(?:\\b|_)(" + SN_BN
+                                                                              + ")(?:(\\s*$)|([_ ][a-z]*[A-Z0-9]))" );
+    final public static Pattern  TAXOMONY_SN_PATTERN_SNS              = Pattern.compile( "(?:\\b|_)(" + SN_BN
+                                                                              + "[_ ][a-z]{3,30}"
+                                                                              + ")[_ ][a-z]*[A-Z0-9]" );
+    final public static Pattern  TAXOMONY_SN_PATTERN_SNS2             = Pattern.compile( "[A-Z0-9][a-z]*[_ ](" + SN_BN
+                                                                              + "[_ ][a-z]{3,30}" + ")\\s*$" );
+    final public static Pattern  TAXOMONY_SN_PATTERN_SP               = Pattern
+                                                                              .compile( "(?:\\b|_)([A-Z][a-z]{2,30}[_ ]sp\\.?)(?:\\b|_)?" );
+    final public static Pattern  TAXOMONY_SN_PATTERN_STRAIN_1         = Pattern
+                                                                              .compile( "(?:\\b|_)("
+                                                                                      + SN_BN
+                                                                                      + "[_ ](?:str|subsp|ssp|var)[a-z]{0,5}\\.?[_ ]\\S{1,60})(?:\\b|_)" );
+    final public static Pattern  TAXOMONY_SN_PATTERN_STRAIN_2         = Pattern
+                                                                              .compile( "(?:\\b|_)("
+                                                                                      + SN_BN
+                                                                                      + "[_ ]\\((?:str|subsp|ssp|var)[a-z]{0,5}\\.?[_ ]\\S{1,60}\\))" );
+    final public static Pattern  TAXOMONY_SN_PATTERN_STRAIN_SUBSTRAIN = Pattern
+                                                                              .compile( "(?:\\b|_)("
+                                                                                      + SN_BN
+                                                                                      + "[_ ]str[a-z]{0,3}\\.?[_ ]\\S{1,60}[_ ]substr[a-z]{0,3}\\.?[_ ]\\S{1,60})(?:\\b|_)" );
+    final private static Pattern TAXOMONY_CODE_PATTERN_PFS            = Pattern.compile( "(?:\\b|_)[A-Z0-9]{4,}_("
+                                                                              + TAX_CODE + ")/\\d+-\\d+\\b" );
+    final private static Pattern TAXOMONY_UNIPROT_ID_PATTERN_PFR      = Pattern
+                                                                              .compile( "(?:\\b|_)[A-Z0-9]{1,}_(\\d{1,7})\\b" );
+    final private static Pattern TAXOMONY_UNIPROT_ID_PATTERN_PFS      = Pattern
+                                                                              .compile( "(?:\\b|_)[A-Z0-9]{4,}_(\\d{1,7})/\\d+-\\d+\\b" );
 
-    /**
-     * Return null if it can not guess the parser to use based on name suffix.
-     * 
-     * @param filename
-     * @return
-     */
-    final public static PhylogenyParser createParserDependingOnSuffix( final String filename,
-                                                                       final boolean phyloxml_validate_against_xsd ) {
+    final public static PhylogenyParser createParserDependingFileContents( final File file,
+                                                                           final boolean phyloxml_validate_against_xsd )
+            throws FileNotFoundException, IOException {
         PhylogenyParser parser = null;
-        final String filename_lc = filename.toLowerCase();
-        if ( filename_lc.endsWith( ".tol" ) || filename_lc.endsWith( ".tolxml" ) || filename_lc.endsWith( ".tol.zip" ) ) {
-            parser = new TolParser();
-        }
-        else if ( filename_lc.endsWith( ".xml" ) || filename_lc.endsWith( ".px" ) || filename_lc.endsWith( "phyloxml" )
-                || filename_lc.endsWith( ".zip" ) ) {
-            parser = new PhyloXmlParser();
+        final String first_line = ForesterUtil.getFirstLine( file ).trim().toLowerCase();
+        if ( first_line.startsWith( "<" ) ) {
+            parser = PhyloXmlParser.createPhyloXmlParser();
             if ( phyloxml_validate_against_xsd ) {
                 final ClassLoader cl = PhyloXmlParser.class.getClassLoader();
                 final URL xsd_url = cl.getResource( ForesterConstants.LOCAL_PHYLOXML_XSD_RESOURCE );
@@ -118,10 +115,11 @@ public final class ParserUtils {
                 }
             }
         }
-        else if ( filename_lc.endsWith( ".nexus" ) || filename_lc.endsWith( ".nex" ) || filename_lc.endsWith( ".nx" ) ) {
+        else if ( ( first_line.startsWith( "nexus" ) ) || ( first_line.startsWith( "#nexus" ) )
+                || ( first_line.startsWith( "# nexus" ) ) || ( first_line.startsWith( "begin" ) ) ) {
             parser = new NexusPhylogeniesParser();
         }
-        else if ( filename_lc.endsWith( ".nhx" ) || filename_lc.endsWith( ".nh" ) || filename_lc.endsWith( ".newick" ) ) {
+        else {
             parser = new NHXParser();
         }
         return parser;
@@ -135,90 +133,55 @@ public final class ParserUtils {
         if ( parser == null ) {
             parser = createParserDependingFileContents( file, phyloxml_validate_against_xsd );
         }
+        if ( ( parser != null ) && file.toString().toLowerCase().endsWith( ".zip" ) ) {
+            if ( parser instanceof PhyloXmlParser ) {
+                ( ( PhyloXmlParser ) parser ).setZippedInputstream( true );
+            }
+            else if ( parser instanceof TolParser ) {
+                ( ( TolParser ) parser ).setZippedInputstream( true );
+            }
+        }
         return parser;
     }
 
-    final public static PhylogenyParser createParserDependingFileContents( final File file,
-                                                                           final boolean phyloxml_validate_against_xsd )
+    final public static PhylogenyParser createParserDependingOnUrlContents( final URL url,
+                                                                            final boolean phyloxml_validate_against_xsd )
             throws FileNotFoundException, IOException {
-        PhylogenyParser parser = null;
-        final String first_line = ForesterUtil.getFirstLine( file ).trim().toLowerCase();
-        if ( first_line.startsWith( "<" ) ) {
-            parser = new PhyloXmlParser();
-            if ( phyloxml_validate_against_xsd ) {
-                final ClassLoader cl = PhyloXmlParser.class.getClassLoader();
-                final URL xsd_url = cl.getResource( ForesterConstants.LOCAL_PHYLOXML_XSD_RESOURCE );
-                if ( xsd_url != null ) {
-                    ( ( PhyloXmlParser ) parser ).setValidateAgainstSchema( xsd_url.toString() );
-                }
-                else {
-                    if ( ForesterConstants.RELEASE ) {
+        final String lc_filename = url.getFile().toString().toLowerCase();
+        PhylogenyParser parser = createParserDependingOnSuffix( lc_filename, phyloxml_validate_against_xsd );
+        if ( parser == null ) {
+            final String first_line = ForesterUtil.getFirstLine( url ).trim().toLowerCase();
+            if ( first_line.startsWith( "<" ) ) {
+                parser = PhyloXmlParser.createPhyloXmlParser();
+                if ( phyloxml_validate_against_xsd ) {
+                    final ClassLoader cl = PhyloXmlParser.class.getClassLoader();
+                    final URL xsd_url = cl.getResource( ForesterConstants.LOCAL_PHYLOXML_XSD_RESOURCE );
+                    if ( xsd_url != null ) {
+                        ( ( PhyloXmlParser ) parser ).setValidateAgainstSchema( xsd_url.toString() );
+                    }
+                    else {
                         throw new RuntimeException( "failed to get URL for phyloXML XSD from jar file from ["
                                 + ForesterConstants.LOCAL_PHYLOXML_XSD_RESOURCE + "]" );
                     }
                 }
             }
+            else if ( ( first_line.startsWith( "nexus" ) ) || ( first_line.startsWith( "#nexus" ) )
+                    || ( first_line.startsWith( "# nexus" ) ) || ( first_line.startsWith( "begin" ) ) ) {
+                parser = new NexusPhylogeniesParser();
+            }
+            else {
+                parser = new NHXParser();
+            }
         }
-        else if ( ( first_line.startsWith( "nexus" ) ) || ( first_line.startsWith( "#nexus" ) )
-                || ( first_line.startsWith( "# nexus" ) ) || ( first_line.startsWith( "begin" ) ) ) {
-            parser = new NexusPhylogeniesParser();
-        }
-        else {
-            parser = new NHXParser();
-        }
-        return parser;
-    }
-
-    /**
-     * Extracts a code if and only if:
-     * one and only one _, 
-     * shorter than 25, 
-     * no |, 
-     * no ., 
-     * if / present it has to be after the _, 
-     * if PFAM_STYLE_ONLY: / must be present,
-     * tax code can only contain uppercase letters and numbers,
-     * and must contain at least one uppercase letter.
-     * Return null if no code extractable.
-     * 
-     * @param name
-     * @param limit_to_five
-     * @return
-     */
-    public static String extractTaxonomyCodeFromNodeName( final String name,
-                                                          final boolean limit_to_five,
-                                                          final PhylogenyMethods.TAXONOMY_EXTRACTION taxonomy_extraction ) {
-        if ( ( name.indexOf( "_" ) > 0 )
-                && ( name.length() < 25 )
-                && ( name.lastIndexOf( "_" ) == name.indexOf( "_" ) )
-                && ( name.indexOf( "|" ) < 0 )
-                && ( name.indexOf( "." ) < 0 )
-                && ( ( taxonomy_extraction != PhylogenyMethods.TAXONOMY_EXTRACTION.PFAM_STYLE_ONLY ) || ( name
-                        .indexOf( "/" ) >= 0 ) )
-                && ( ( ( name.indexOf( "/" ) ) < 0 ) || ( name.indexOf( "/" ) > name.indexOf( "_" ) ) ) ) {
-            final String[] s = name.split( "[_/]" );
-            if ( s.length > 1 ) {
-                String str = s[ 1 ];
-                if ( limit_to_five ) {
-                    if ( str.length() > 5 ) {
-                        str = str.substring( 0, 5 );
-                    }
-                    else if ( ( str.length() < 5 ) && ( str.startsWith( "RAT" ) || str.startsWith( "PIG" ) ) ) {
-                        str = str.substring( 0, 3 );
-                    }
-                }
-                final Matcher letters_and_numbers = NHXParser.UC_LETTERS_NUMBERS_PATTERN.matcher( str );
-                if ( !letters_and_numbers.matches() ) {
-                    return null;
-                }
-                final Matcher numbers_only = NHXParser.NUMBERS_ONLY_PATTERN.matcher( str );
-                if ( numbers_only.matches() ) {
-                    return null;
-                }
-                return str;
+        if ( ( parser != null ) && lc_filename.endsWith( ".zip" ) ) {
+            if ( parser instanceof PhyloXmlParser ) {
+                ( ( PhyloXmlParser ) parser ).setZippedInputstream( true );
+            }
+            else if ( parser instanceof TolParser ) {
+                ( ( TolParser ) parser ).setZippedInputstream( true );
             }
         }
-        return null;
+        return parser;
     }
 
     public static BufferedReader createReader( final Object source ) throws IOException, FileNotFoundException {
@@ -254,4 +217,215 @@ public final class ParserUtils {
         }
         return reader;
     }
+
+    public final static String extractScientificNameFromNodeName( final String name ) {
+        final Matcher m_ss = TAXOMONY_SN_PATTERN_STRAIN_SUBSTRAIN.matcher( name );
+        if ( m_ss.find() ) {
+            String s = m_ss.group( 1 ).replace( '_', ' ' );
+            if ( s.indexOf( " str " ) > 4 ) {
+                s = s.replaceFirst( " str ", " str. " );
+            }
+            if ( s.indexOf( " substr " ) > 4 ) {
+                s = s.replaceFirst( " substr ", " substr. " );
+            }
+            return s;
+        }
+        final Matcher m_str1 = TAXOMONY_SN_PATTERN_STRAIN_1.matcher( name );
+        if ( m_str1.find() ) {
+            String s = m_str1.group( 1 ).replace( '_', ' ' );
+            if ( s.indexOf( " str " ) > 4 ) {
+                s = s.replaceFirst( " str ", " str. " );
+            }
+            else if ( s.indexOf( " subsp " ) > 4 ) {
+                s = s.replaceFirst( " subsp ", " subsp. " );
+            }
+            else if ( s.indexOf( " ssp " ) > 4 ) {
+                s = s.replaceFirst( " ssp ", " subsp. " );
+            }
+            else if ( s.indexOf( " ssp. " ) > 4 ) {
+                s = s.replaceFirst( " ssp. ", " subsp. " );
+            }
+            else if ( s.indexOf( " var " ) > 4 ) {
+                s = s.replaceFirst( " var ", " var. " );
+            }
+            return s;
+        }
+        final Matcher m_str2 = TAXOMONY_SN_PATTERN_STRAIN_2.matcher( name );
+        if ( m_str2.find() ) {
+            String s = m_str2.group( 1 ).replace( '_', ' ' );
+            if ( s.indexOf( " (str " ) > 4 ) {
+                s = s.replaceFirst( " \\(str ", " (str. " );
+            }
+            else if ( s.indexOf( " (subsp " ) > 4 ) {
+                s = s.replaceFirst( " \\(subsp ", " (subsp. " );
+            }
+            else if ( s.indexOf( " (ssp " ) > 4 ) {
+                s = s.replaceFirst( " \\(ssp ", " (subsp. " );
+            }
+            else if ( s.indexOf( " (ssp. " ) > 4 ) {
+                s = s.replaceFirst( " \\(ssp. ", " (subsp. " );
+            }
+            else if ( s.indexOf( " (var " ) > 4 ) {
+                s = s.replaceFirst( " \\(var ", " (var. " );
+            }
+            return s;
+        }
+        final Matcher m_sns = TAXOMONY_SN_PATTERN_SNS.matcher( name );
+        if ( m_sns.find() ) {
+            return m_sns.group( 1 ).replace( '_', ' ' );
+        }
+        final Matcher m_sns2 = TAXOMONY_SN_PATTERN_SNS2.matcher( name );
+        if ( m_sns2.find() ) {
+            return m_sns2.group( 1 ).replace( '_', ' ' );
+        }
+        final Matcher m_sn = TAXOMONY_SN_PATTERN_SN.matcher( name );
+        if ( m_sn.find() ) {
+            return m_sn.group( 1 ).replace( '_', ' ' );
+        }
+        final Matcher m_sp = TAXOMONY_SN_PATTERN_SP.matcher( name );
+        if ( m_sp.find() ) {
+            String s = m_sp.group( 1 ).replace( '_', ' ' );
+            if ( s.endsWith( " sp" ) ) {
+                s = s + ".";
+            }
+            return s;
+        }
+        return null;
+    }
+
+    public final static String extractTaxonomyCodeFromNodeName( final String name,
+                                                                final TAXONOMY_EXTRACTION taxonomy_extraction ) {
+        Matcher m = TAXOMONY_CODE_PATTERN_PFS.matcher( name );
+        if ( m.find() ) {
+            return m.group( 1 );
+        }
+        else if ( ( taxonomy_extraction == TAXONOMY_EXTRACTION.PFAM_STYLE_RELAXED )
+                || ( taxonomy_extraction == TAXONOMY_EXTRACTION.AGGRESSIVE ) ) {
+            m = TAXOMONY_CODE_PATTERN_PFR.matcher( name );
+            if ( m.find() ) {
+                return m.group( 1 );
+            }
+            else if ( taxonomy_extraction == TAXONOMY_EXTRACTION.AGGRESSIVE ) {
+                m = TAXOMONY_CODE_PATTERN_A.matcher( name );
+                if ( m.find() ) {
+                    return m.group( 1 );
+                }
+            }
+        }
+        return null;
+    }
+
+    public final static String extractTaxonomyDataFromNodeName( final PhylogenyNode node,
+                                                                final NHXParser.TAXONOMY_EXTRACTION taxonomy_extraction )
+            throws PhyloXmlDataFormatException {
+        if ( taxonomy_extraction == TAXONOMY_EXTRACTION.NO ) {
+            throw new IllegalArgumentException();
+        }
+        final String id = extractUniprotTaxonomyIdFromNodeName( node.getName(), taxonomy_extraction );
+        if ( !ForesterUtil.isEmpty( id ) ) {
+            if ( !node.getNodeData().isHasTaxonomy() ) {
+                node.getNodeData().setTaxonomy( new Taxonomy() );
+            }
+            node.getNodeData().getTaxonomy().setIdentifier( new Identifier( id, "uniprot" ) );
+            return id;
+        }
+        else {
+            String code = null;
+            if ( taxonomy_extraction == TAXONOMY_EXTRACTION.AGGRESSIVE ) {
+                code = extractTaxonomyCodeFromNodeNameLettersOnly( node.getName() );
+                if ( ForesterUtil.isEmpty( code ) ) {
+                    final String sn = extractScientificNameFromNodeName( node.getName() );
+                    if ( !ForesterUtil.isEmpty( sn ) ) {
+                        if ( !node.getNodeData().isHasTaxonomy() ) {
+                            node.getNodeData().setTaxonomy( new Taxonomy() );
+                        }
+                        node.getNodeData().getTaxonomy().setScientificName( sn );
+                        return sn;
+                    }
+                }
+            }
+            if ( ForesterUtil.isEmpty( code ) ) {
+                code = extractTaxonomyCodeFromNodeName( node.getName(), taxonomy_extraction );
+            }
+            if ( !ForesterUtil.isEmpty( code ) ) {
+                if ( !node.getNodeData().isHasTaxonomy() ) {
+                    node.getNodeData().setTaxonomy( new Taxonomy() );
+                }
+                node.getNodeData().getTaxonomy().setTaxonomyCode( code );
+                return code;
+            }
+        }
+        return null;
+    }
+
+    public final static String extractUniprotTaxonomyIdFromNodeName( final String name,
+                                                                     final TAXONOMY_EXTRACTION taxonomy_extraction ) {
+        Matcher m = TAXOMONY_UNIPROT_ID_PATTERN_PFS.matcher( name );
+        if ( m.find() ) {
+            return m.group( 1 );
+        }
+        else if ( ( taxonomy_extraction == TAXONOMY_EXTRACTION.PFAM_STYLE_RELAXED )
+                || ( taxonomy_extraction == TAXONOMY_EXTRACTION.AGGRESSIVE ) ) {
+            m = TAXOMONY_UNIPROT_ID_PATTERN_PFR.matcher( name );
+            if ( m.find() ) {
+                return m.group( 1 );
+            }
+        }
+        return null;
+    }
+
+    public final static Phylogeny[] readPhylogenies( final File file ) throws FileNotFoundException, IOException {
+        return PhylogenyMethods.readPhylogenies( ParserUtils.createParserDependingOnFileType( file, true ), file );
+    }
+
+    public final static Phylogeny[] readPhylogenies( final String file_name ) throws FileNotFoundException, IOException {
+        return readPhylogenies( new File( file_name ) );
+    }
+
+    /**
+     * Return null if it can not guess the parser to use based on name suffix.
+     * 
+     * @param filename
+     * @return
+     */
+    final private static PhylogenyParser createParserDependingOnSuffix( final String filename,
+                                                                        final boolean phyloxml_validate_against_xsd ) {
+        PhylogenyParser parser = null;
+        final String filename_lc = filename.toLowerCase();
+        if ( filename_lc.endsWith( ".tol" ) || filename_lc.endsWith( ".tolxml" ) || filename_lc.endsWith( ".tol.zip" ) ) {
+            parser = new TolParser();
+        }
+        else if ( filename_lc.endsWith( ".xml" ) || filename_lc.endsWith( "phyloxml" ) || filename_lc.endsWith( ".zip" ) ) {
+            parser = PhyloXmlParser.createPhyloXmlParser();
+            if ( phyloxml_validate_against_xsd ) {
+                final ClassLoader cl = PhyloXmlParser.class.getClassLoader();
+                final URL xsd_url = cl.getResource( ForesterConstants.LOCAL_PHYLOXML_XSD_RESOURCE );
+                if ( xsd_url != null ) {
+                    ( ( PhyloXmlParser ) parser ).setValidateAgainstSchema( xsd_url.toString() );
+                }
+                else {
+                    if ( ForesterConstants.RELEASE ) {
+                        throw new RuntimeException( "failed to get URL for phyloXML XSD from jar file from ["
+                                + ForesterConstants.LOCAL_PHYLOXML_XSD_RESOURCE + "]" );
+                    }
+                }
+            }
+        }
+        else if ( filename_lc.endsWith( ".nexus" ) || filename_lc.endsWith( ".nex" ) || filename_lc.endsWith( ".nx" ) ) {
+            parser = new NexusPhylogeniesParser();
+        }
+        else if ( filename_lc.endsWith( ".nhx" ) || filename_lc.endsWith( ".nh" ) || filename_lc.endsWith( ".newick" )
+                || filename_lc.endsWith( ".nwk" ) ) {
+            parser = new NHXParser();
+        }
+        return parser;
+    }
+
+    private final static String extractTaxonomyCodeFromNodeNameLettersOnly( final String name ) {
+        final Matcher m = TAXOMONY_CODE_PATTERN_A_LO.matcher( name );
+        if ( m.find() ) {
+            return m.group( 1 );
+        }
+        return null;
+    }
 }