JAL-1855 dbfetch from /ena_sequence/, /ena_coding/
[jalview.git] / src / jalview / ws / ebi / EBIFetchClient.java
index e363750..b09cbaa 100644 (file)
@@ -1,6 +1,6 @@
 /*
- * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
- * Copyright (C) 2014 The Jalview Authors
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
  * 
  * This file is part of Jalview.
  * 
  */
 package jalview.ws.ebi;
 
+import jalview.datamodel.DBRefSource;
+import jalview.util.MessageManager;
+
 import java.io.BufferedInputStream;
 import java.io.BufferedReader;
 import java.io.File;
 import java.io.FileOutputStream;
+import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.net.URL;
 import java.util.ArrayList;
+import java.util.List;
 import java.util.StringTokenizer;
 
 /**
@@ -56,7 +61,7 @@ public class EBIFetchClient
   public String[] getSupportedDBs()
   {
     // TODO - implement rest call for dbfetch getSupportedDBs
-    throw new Error("Not yet implemented");
+    throw new Error(MessageManager.getString("error.not_yet_implemented"));
   }
 
   /**
@@ -67,7 +72,7 @@ public class EBIFetchClient
   public String[] getSupportedFormats()
   {
     // TODO - implement rest call for dbfetch getSupportedFormats
-    throw new Error("Not yet implemented");
+    throw new Error(MessageManager.getString("error.not_yet_implemented"));
   }
 
   /**
@@ -78,10 +83,22 @@ public class EBIFetchClient
   public String[] getSupportedStyles()
   {
     // TODO - implement rest call for dbfetch getSupportedStyles
-    throw new Error("Not yet implemented");
+    throw new Error(MessageManager.getString("error.not_yet_implemented"));
   }
 
-  public File fetchDataAsFile(String ids, String f, String s)
+  /**
+   * Send an HTTP fetch request to EBI and save the reply in a temporary file.
+   * 
+   * @param ids
+   *          the query formatted as db:query1;query2;query3
+   * @param format
+   *          the format wanted
+   * @param s
+   *          - unused parameter
+   * @return the file holding the response
+   * @throws OutOfMemoryError
+   */
+  public File fetchDataAsFile(String ids, String format, String s)
           throws OutOfMemoryError
   {
     File outFile = null;
@@ -89,7 +106,7 @@ public class EBIFetchClient
     {
       outFile = File.createTempFile("jalview", ".xml");
       outFile.deleteOnExit();
-      fetchData(ids, f, s, outFile);
+      fetchData(ids, format, s, outFile);
       if (outFile.length() == 0)
       {
         outFile.delete();
@@ -106,20 +123,20 @@ public class EBIFetchClient
    * 
    * @param ids
    *          db:query1;query2;query3
-   * @param f
+   * @param format
    *          raw/xml
    * @param s
-   *          ?
+   *          not used - remove?
    * 
    * @return Raw string array result of query set
    */
-  public String[] fetchData(String ids, String f, String s)
+  public String[] fetchData(String ids, String format, String s)
           throws OutOfMemoryError
   {
-    return fetchData(ids, f, s, null);
+    return fetchData(ids, format, s, null);
   }
 
-  public String[] fetchData(String ids, String f, String s, File outFile)
+  String[] fetchData(String ids, String f, String s, File outFile)
           throws OutOfMemoryError
   {
     // Need to split
@@ -167,18 +184,29 @@ public class EBIFetchClient
     return (rslts.length == 0 ? null : rslts);
   }
 
-  public String[] fetchBatch(String ids, String db, String f, String s,
+  public String[] fetchBatch(String ids, String dbPath, String format, String s,
           File outFile) throws OutOfMemoryError
   {
-    long time = System.currentTimeMillis();
-    // max 200 ids can be added at one time
+    // long time = System.currentTimeMillis();
+    /*
+     * JAL-1855 dbfetch from ena_sequence, ena_coding
+     */
+    if (dbPath.equalsIgnoreCase(DBRefSource.EMBL))
+    {
+      dbPath = "ena_sequence";
+    }
+    else if (dbPath.equalsIgnoreCase(DBRefSource.EMBLCDS))
+    {
+      dbPath = "ena_coding";
+    }
+
     try
     {
       URL rcall = new URL("http://www.ebi.ac.uk/Tools/dbfetch/dbfetch/"
-              + db.toLowerCase() + "/" + ids.toLowerCase()
-              + (f != null ? "/" + f : ""));
+              + dbPath.toLowerCase() + "/" + ids.toLowerCase()
+              + (format != null ? "/" + format : ""));
 
-      BufferedInputStream is = new BufferedInputStream(rcall.openStream());
+      InputStream is = new BufferedInputStream(rcall.openStream());
       if (outFile != null)
       {
         FileOutputStream fio = new FileOutputStream(outFile);
@@ -195,7 +223,7 @@ public class EBIFetchClient
       {
         BufferedReader br = new BufferedReader(new InputStreamReader(is));
         String rtn;
-        ArrayList<String> arl = new ArrayList<String>();
+        List<String> arl = new ArrayList<String>();
         while ((rtn = br.readLine()) != null)
         {
           arl.add(rtn);
@@ -205,7 +233,7 @@ public class EBIFetchClient
     } catch (OutOfMemoryError er)
     {
 
-      System.out.println("OUT OF MEMORY DOWNLOADING QUERY FROM " + db
+      System.out.println("OUT OF MEMORY DOWNLOADING QUERY FROM " + dbPath
               + ":\n" + ids);
       throw er;
     } catch (Exception ex)
@@ -215,16 +243,16 @@ public class EBIFetchClient
       {
         return null;
       }
-      System.err.println("Unexpected exception when retrieving from " + db
+      System.err.println("Unexpected exception when retrieving from "
+              + dbPath
               + "\nQuery was : '" + ids + "'");
       ex.printStackTrace(System.err);
       return null;
     } finally
     {
-      //System.err.println("Took " + (System.currentTimeMillis() - time)
-      //        / 1000 + " secs for one call.");
+      // System.err.println("EBIFetch took " + (System.currentTimeMillis() -
+      // time) + " ms");
     }
     return null;
   }
 }
-