inprogress
[jalview.git] / forester / java / src / org / forester / ws / seqdb / SequenceDbWsTools.java
index 05efea9..209d284 100644 (file)
@@ -43,6 +43,7 @@ import org.forester.io.parsers.phyloxml.PhyloXmlDataFormatException;
 import org.forester.phylogeny.Phylogeny;
 import org.forester.phylogeny.PhylogenyNode;
 import org.forester.phylogeny.data.Accession;
+import org.forester.phylogeny.data.Accession.Source;
 import org.forester.phylogeny.data.Annotation;
 import org.forester.phylogeny.data.Identifier;
 import org.forester.phylogeny.data.Sequence;
@@ -53,13 +54,35 @@ import org.forester.util.SequenceAccessionTools;
 
 public final class SequenceDbWsTools {
 
-    public final static String   BASE_EMBL_DB_URL  = "http://www.ebi.ac.uk/Tools/dbfetch/dbfetch/";
-    public final static String   BASE_UNIPROT_URL  = "http://www.uniprot.org/";
-    public final static String   EMBL_DBS_EMBL     = "embl";
-    public final static String   EMBL_DBS_REFSEQ_N = "refseqn";
-    public final static String   EMBL_DBS_REFSEQ_P = "refseqp";
-    private final static boolean DEBUG             = true;
-    private final static String  URL_ENC           = "UTF-8";
+    public final static String   EMBL_REFSEQ             = "http://www.ebi.ac.uk/Tools/dbfetch/dbfetch?db=REFSEQ&style=raw&id=";
+    public final static String   EMBL_GENBANK            = "http://www.ebi.ac.uk/Tools/dbfetch/dbfetch?db=GENBANK&style=raw&id=";
+    public final static String   BASE_UNIPROT_URL        = "http://www.uniprot.org/";
+    //public final static String   EMBL_DBS_EMBL           = "embl";
+    public final static String   EMBL_DBS_REFSEQ_N       = "refseqn";
+    public final static String   EMBL_DBS_REFSEQ_P       = "refseqp";
+    private final static boolean DEBUG                   = true;
+    private final static String  URL_ENC                 = "UTF-8";
+    public final static int      DEFAULT_LINES_TO_RETURN = 4000;
+
+    final static String extractFrom( final String target, final String a ) {
+        final int i_a = target.indexOf( a );
+        return target.substring( i_a + a.length() ).trim();
+    }
+
+    final static String extractFromTo( final String target, final String a, final String b ) {
+        final int i_a = target.indexOf( a );
+        final int i_b = target.indexOf( b );
+        if ( ( i_a < 0 ) || ( i_b < i_a ) ) {
+            throw new IllegalArgumentException( "attempt to extract from \"" + target + "\" between \"" + a
+                    + "\" and \"" + b + "\"" );
+        }
+        return target.substring( i_a + a.length(), i_b ).trim();
+    }
+
+    final static String extractTo( final String target, final String b ) {
+        final int i_b = target.indexOf( b );
+        return target.substring( 0, i_b ).trim();
+    }
 
     public static List<UniProtTaxonomy> getTaxonomiesFromCommonNameStrict( final String cn,
                                                                            final int max_taxonomies_return )
@@ -119,13 +142,17 @@ public final class SequenceDbWsTools {
         return null;
     }
 
-    public static SequenceDatabaseEntry obtainEmblEntry( final Accession id, final int max_lines_to_return )
+    public static SequenceDatabaseEntry obtainEmblEntry( final Accession acc, final int max_lines_to_return )
             throws IOException {
-        final List<String> lines = queryEmblDb( id, max_lines_to_return );
-        return EbiDbEntry.createInstanceFromPlainText( lines );
+        final List<String> lines = queryEmblDb( acc, max_lines_to_return );
+        return EbiDbEntry.createInstanceFromPlainTextForRefSeq( lines );
     }
 
-    public final static Accession obtainFromSeqAccession( final PhylogenyNode node ) {
+    public static SequenceDatabaseEntry obtainEmblEntry( final Accession acc ) throws IOException {
+        return obtainEmblEntry( acc, DEFAULT_LINES_TO_RETURN );
+    }
+
+    public final static Accession obtainSeqAccession( final PhylogenyNode node ) {
         Accession acc = SequenceAccessionTools.obtainFromSeqAccession( node );
         if ( !isAccessionAcceptable( acc ) ) {
             acc = SequenceAccessionTools.obtainAccessorFromDataFields( node );
@@ -133,17 +160,21 @@ public final class SequenceDbWsTools {
         return acc;
     }
 
-    public static SequenceDatabaseEntry obtainRefSeqEntryFromEmbl( final Accession id, final int max_lines_to_return )
+    public static SequenceDatabaseEntry obtainRefSeqEntryFromEmbl( final Accession acc, final int max_lines_to_return )
             throws IOException {
-        final List<String> lines = queryEmblDb( id, max_lines_to_return );
+        final List<String> lines = queryEmblDbForRefSeqEntry( acc, max_lines_to_return );
         return EbiDbEntry.createInstanceFromPlainTextForRefSeq( lines );
     }
 
+    public static SequenceDatabaseEntry obtainRefSeqEntryFromEmbl( final Accession acc ) throws IOException {
+        return obtainRefSeqEntryFromEmbl( acc, DEFAULT_LINES_TO_RETURN );
+    }
+
     public final static void obtainSeqInformation( final boolean allow_to_set_taxonomic_data,
                                                    final int lines_to_return,
                                                    final SortedSet<String> not_found,
                                                    final PhylogenyNode node ) throws IOException {
-        final Accession acc = obtainFromSeqAccession( node );
+        final Accession acc = obtainSeqAccession( node );
         if ( !isAccessionAcceptable( acc ) ) {
             if ( node.isExternal() || !node.isEmpty() ) {
                 not_found.add( node.toString() );
@@ -154,6 +185,16 @@ public final class SequenceDbWsTools {
         }
     }
 
+    public final static void obtainSeqInformation( final boolean allow_to_set_taxonomic_data,
+                                                   final SortedSet<String> not_found,
+                                                   final PhylogenyNode node ) throws IOException {
+        obtainSeqInformation( allow_to_set_taxonomic_data, DEFAULT_LINES_TO_RETURN, not_found, node );
+    }
+
+    public final static void obtainSeqInformation( final PhylogenyNode node ) throws IOException {
+        obtainSeqInformation( true, DEFAULT_LINES_TO_RETURN, new TreeSet<String>(), node );
+    }
+
     public final static SortedSet<String> obtainSeqInformation( final Phylogeny phy,
                                                                 final boolean ext_nodes_only,
                                                                 final boolean allow_to_set_taxonomic_data,
@@ -174,6 +215,10 @@ public final class SequenceDbWsTools {
         return UniProtEntry.createInstanceFromPlainText( lines );
     }
 
+    public static SequenceDatabaseEntry obtainUniProtEntry( final String query ) throws IOException {
+        return obtainUniProtEntry( query, DEFAULT_LINES_TO_RETURN );
+    }
+
     public static List<String> queryDb( final String query, int max_lines_to_return, final String base_url )
             throws IOException {
         if ( ForesterUtil.isEmpty( query ) ) {
@@ -210,22 +255,33 @@ public final class SequenceDbWsTools {
         return result;
     }
 
+    public static List<String> queryEmblDbForRefSeqEntry( final Accession id, final int max_lines_to_return )
+            throws IOException {
+        final StringBuilder url_sb = new StringBuilder();
+        url_sb.append( EMBL_REFSEQ );
+        return queryDb( id.getValue(), max_lines_to_return, url_sb.toString() );
+    }
+
     public static List<String> queryEmblDb( final Accession id, final int max_lines_to_return ) throws IOException {
         final StringBuilder url_sb = new StringBuilder();
-        url_sb.append( BASE_EMBL_DB_URL );
-        if ( ForesterUtil.isEmpty( id.getSource() ) || ( id.getSource() == Accession.NCBI ) ) {
-            url_sb.append( SequenceDbWsTools.EMBL_DBS_EMBL );
-            url_sb.append( '/' );
+        //  url_sb.append( BASE_EMBL_DB_URL );
+        if ( id.getSource().equals( Source.NCBI.toString() ) ) {
+            url_sb.append( EMBL_GENBANK );
+            //url_sb.append( '/' );
         }
-        else if ( id.getSource() == Accession.REFSEQ ) {
-            if ( id.getValue().toUpperCase().indexOf( 'P' ) == 1 ) {
-                url_sb.append( SequenceDbWsTools.EMBL_DBS_REFSEQ_P );
-                url_sb.append( '/' );
-            }
-            else {
-                url_sb.append( SequenceDbWsTools.EMBL_DBS_REFSEQ_N );
-                url_sb.append( '/' );
-            }
+        else if ( id.getSource().equals( Source.REFSEQ.toString() ) ) {
+            url_sb.append( EMBL_REFSEQ );
+            //            if ( id.getValue().toUpperCase().indexOf( 'P' ) == 1 ) {
+            //                url_sb.append( SequenceDbWsTools.EMBL_DBS_REFSEQ_P );
+            //                url_sb.append( '/' );
+            //            }
+            //            else {
+            //                url_sb.append( SequenceDbWsTools.EMBL_DBS_REFSEQ_N );
+            //                url_sb.append( '/' );
+            //            }
+        }
+        else {
+            throw new IllegalArgumentException( "unable to handle source: " + id.getSource() );
         }
         return queryDb( id.getValue(), max_lines_to_return, url_sb.toString() );
     }
@@ -241,7 +297,7 @@ public final class SequenceDbWsTools {
                                              final Accession acc ) throws IOException {
         SequenceDatabaseEntry db_entry = null;
         final String query = acc.getValue();
-        if ( acc.getSource() == Accession.UNIPROT ) {
+        if ( acc.getSource().equals( Source.UNIPROT.toString() ) ) {
             if ( DEBUG ) {
                 System.out.println( "uniprot: " + query );
             }
@@ -252,7 +308,7 @@ public final class SequenceDbWsTools {
                 // Eat this, and move to next.
             }
         }
-        else if ( acc.getSource() == Accession.EMBL ) {
+        else if ( acc.getSource().equals( Source.EMBL.toString() ) ) {
             if ( DEBUG ) {
                 System.out.println( "embl: " + query );
             }
@@ -263,7 +319,7 @@ public final class SequenceDbWsTools {
                 // Eat this, and move to next.
             }
         }
-        else if ( acc.getSource() == Accession.REFSEQ ) {
+        else if ( acc.getSource().equals( Source.REFSEQ.toString() ) ) {
             if ( DEBUG ) {
                 System.out.println( "refseq: " + query );
             }
@@ -372,7 +428,9 @@ public final class SequenceDbWsTools {
 
     private final static boolean isAccessionAcceptable( final Accession acc ) {
         return ( !( ( acc == null ) || ForesterUtil.isEmpty( acc.getSource() ) || ForesterUtil.isEmpty( acc.getValue() ) || ( ( acc
-                .getSource() != Accession.UNIPROT ) && ( acc.getSource() != Accession.EMBL ) && ( acc.getSource() != Accession.REFSEQ ) ) ) );
+                .getSource().equals( Source.UNIPROT.toString() ) )
+                && ( acc.getSource().toString().equals( Source.EMBL.toString() ) ) && ( acc.getSource().toString()
+                .equals( Source.REFSEQ.toString() ) ) ) ) );
     }
 
     private static List<UniProtTaxonomy> parseUniProtTaxonomy( final List<String> result ) throws IOException {