inprogress
[jalview.git] / forester / java / src / org / forester / io / parsers / util / ParserUtils.java
index db628cf..92275ad 100644 (file)
@@ -55,18 +55,19 @@ import org.forester.util.ForesterUtil;
 
 public final class ParserUtils {
 
-    final public static Pattern TAXOMONY_CODE_PATTERN_1  = Pattern.compile( "[A-Z0-9]{5}|RAT|PIG|PEA|CAP" );
-    final private static Pattern TAXOMONY_CODE_PATTERN_2  = Pattern
-                                                                  .compile( "([A-Z0-9]{5}|RAT|PIG|PEA|CAP)[^A-Za-z].*" );
-    final private static Pattern TAXOMONY_CODE_PATTERN_PF = Pattern.compile( "([A-Z0-9]{5}|RAT|PIG|PEA|CAP)/\\d+-\\d+" );
-
-    
-    final private static Pattern TAXOMONY_UNIPROT_ID_PATTERN_1  = Pattern.compile( "\\d{1,7}" );
-    final private static Pattern TAXOMONY_UNIPROT_ID_PATTERN_2  = Pattern
-                                                                  .compile( "(\\d{1,7})[^A-Za-z].*" );
+    final public static String   TAX_CODE                       = "(?:[A-Z9][A-Z]{2}[A-Z0-9]{2})|RAT|PIG|PEA|CAP";
+    final public static Pattern  TAXOMONY_SN_PATTERN            = Pattern
+                                                                        .compile( "[A-Z0-9]{2,}_([A-Z][a-z]+_[a-z]{2,}(?:_[a-z][a-z0-9_]+)?)\\b" );
+    final public static Pattern  TAXOMONY_CODE_PATTERN_R1       = Pattern.compile( "[A-Z0-9]+_(" + TAX_CODE + ")\\b" );
+    final public static Pattern  TAXOMONY_CODE_PATTERN_R2       = Pattern.compile( "(?:\\b|_)(" + TAX_CODE + ")\\b" );
+    final private static Pattern TAXOMONY_CODE_PATTERN_PF       = Pattern.compile( "[A-Z0-9]{2,}_(" + TAX_CODE
+                                                                        + ")/\\d+-\\d+" );
+    final public static Pattern  TAXOMONY_CODE_PATTERN_4        = Pattern.compile( "\\[(" + TAX_CODE + ")\\]" );
+    final public static Pattern  TAXOMONY_CODE_PATTERN_6        = Pattern.compile( "\\[([A-Z9][A-Z]{2}[A-Z0-9]{3})\\]" );
+    final private static Pattern TAXOMONY_UNIPROT_ID_PATTERN_1  = Pattern.compile( "\\b\\d{1,7}\\b" );
+    final private static Pattern TAXOMONY_UNIPROT_ID_PATTERN_2  = Pattern.compile( "(\\d{1,7})[^0-9A-Za-z].*" );
     final private static Pattern TAXOMONY_UNIPROT_ID_PATTERN_PF = Pattern.compile( "(\\d{1,7})/\\d+-\\d+" );
 
-    
     final public static PhylogenyParser createParserDependingFileContents( final File file,
                                                                            final boolean phyloxml_validate_against_xsd )
             throws FileNotFoundException, IOException {
@@ -225,49 +226,81 @@ public final class ParserUtils {
 
     public final static String extractTaxonomyCodeFromNodeName( final String name,
                                                                 final TAXONOMY_EXTRACTION taxonomy_extraction ) {
-        if ( ( name.indexOf( "_" ) > 0 )
-                && ( ( taxonomy_extraction != TAXONOMY_EXTRACTION.PFAM_STYLE_ONLY ) || ( name.indexOf( "/" ) > 4 ) ) ) {
-            final String[] s = name.split( "[_\\s]" );
-            if ( s.length > 1 ) {
-                final String str = s[ 1 ];
-                if ( !ForesterUtil.isEmpty( str ) ) {
-                    if ( taxonomy_extraction == TAXONOMY_EXTRACTION.PFAM_STYLE_ONLY ) {
-                        final Matcher m = TAXOMONY_CODE_PATTERN_PF.matcher( str );
-                        if ( m.matches() ) {
-                            return m.group( 1 );
-                        }
-                    }
-                    else {
-                        final Matcher m1 = TAXOMONY_CODE_PATTERN_1.matcher( str );
-                        if ( m1.matches() ) {
-                            return m1.group();
-                        }
-                        final Matcher m2 = TAXOMONY_CODE_PATTERN_2.matcher( str );
-                        if ( m2.matches() ) {
-                            return m2.group( 1 );
-                        }
-                    }
-                }
+        if ( taxonomy_extraction == TAXONOMY_EXTRACTION.PFAM_STYLE_STRICT ) {
+            final Matcher m = TAXOMONY_CODE_PATTERN_PF.matcher( name );
+            if ( m.find() ) {
+                return m.group( 1 );
             }
         }
-        else if ( taxonomy_extraction == TAXONOMY_EXTRACTION.YES ) {
-            final Matcher m1 = TAXOMONY_CODE_PATTERN_1.matcher( name );
-            if ( m1.matches() ) {
-                return name;
+        else if ( ( taxonomy_extraction == TAXONOMY_EXTRACTION.PFAM_STYLE_RELAXED )
+                || ( taxonomy_extraction == TAXONOMY_EXTRACTION.AGGRESSIVE ) ) {
+            final Matcher m1 = TAXOMONY_CODE_PATTERN_R1.matcher( name );
+            if ( m1.find() ) {
+                return m1.group( 1 );
+            }
+            final Matcher m2 = TAXOMONY_CODE_PATTERN_R2.matcher( name );
+            if ( m2.find() ) {
+                return m2.group( 1 );
             }
         }
         return null;
     }
-    
+
+    public final static String extractScientificNameFromNodeName( final String name ) {
+        final Matcher m1 = TAXOMONY_SN_PATTERN.matcher( name );
+        if ( m1.find() ) {
+            return m1.group( 1 ).replace( '_', ' ' );
+        }
+        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 {
+            final String 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;
+            }
+            else if ( ( taxonomy_extraction == TAXONOMY_EXTRACTION.PFAM_STYLE_RELAXED || taxonomy_extraction == TAXONOMY_EXTRACTION.AGGRESSIVE ) ) {
+                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;
+                }
+            }
+        }
+        return null;
+    }
+
     public final static String extractUniprotTaxonomyIdFromNodeName( final String name,
-                                                                final TAXONOMY_EXTRACTION taxonomy_extraction ) {
+                                                                     final TAXONOMY_EXTRACTION taxonomy_extraction ) {
         if ( ( name.indexOf( "_" ) > 0 )
-                && ( ( taxonomy_extraction != TAXONOMY_EXTRACTION.PFAM_STYLE_ONLY ) || ( name.indexOf( "/" ) > 4 ) ) ) {
+                && ( ( ( taxonomy_extraction == TAXONOMY_EXTRACTION.PFAM_STYLE_RELAXED ) || ( taxonomy_extraction == TAXONOMY_EXTRACTION.AGGRESSIVE ) ) || ( ( ( name
+                        .indexOf( "/" ) > 4 ) && ( taxonomy_extraction == TAXONOMY_EXTRACTION.PFAM_STYLE_STRICT ) ) ) ) ) {
             final String[] s = name.split( "[_\\s]" );
             if ( s.length > 1 ) {
                 final String str = s[ 1 ];
                 if ( !ForesterUtil.isEmpty( str ) ) {
-                    if ( taxonomy_extraction == TAXONOMY_EXTRACTION.PFAM_STYLE_ONLY ) {
+                    if ( taxonomy_extraction == TAXONOMY_EXTRACTION.PFAM_STYLE_STRICT ) {
                         final Matcher m = TAXOMONY_UNIPROT_ID_PATTERN_PF.matcher( str );
                         if ( m.matches() ) {
                             return m.group( 1 );
@@ -286,7 +319,7 @@ public final class ParserUtils {
                 }
             }
         }
-        else if ( taxonomy_extraction == TAXONOMY_EXTRACTION.YES ) {
+        if ( taxonomy_extraction == TAXONOMY_EXTRACTION.AGGRESSIVE ) {
             final Matcher m1 = TAXOMONY_UNIPROT_ID_PATTERN_1.matcher( name );
             if ( m1.matches() ) {
                 return name;
@@ -302,25 +335,4 @@ public final class ParserUtils {
     public final static Phylogeny[] readPhylogenies( final String file_name ) throws FileNotFoundException, IOException {
         return readPhylogenies( new File( file_name ) );
     }
-
-    public final static void extractTaxonomyDataFromNodeName( final PhylogenyNode node,
-                                                              final NHXParser.TAXONOMY_EXTRACTION taxonomy_extraction )
-            throws PhyloXmlDataFormatException {
-        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" ) );
-        }
-        else {
-            final String code = extractTaxonomyCodeFromNodeName( node.getName(), taxonomy_extraction );
-            if ( !ForesterUtil.isEmpty( code ) ) {
-                if ( !node.getNodeData().isHasTaxonomy() ) {
-                    node.getNodeData().setTaxonomy( new Taxonomy() );
-                }
-                node.getNodeData().getTaxonomy().setTaxonomyCode( code );
-            }
-        }
-    }
 }