JAL-1645 Version-Rel Version 2.9 Year-Rel 2015 Licensing glob
[jalview.git] / src / jalview / ws / ebi / EBIFetchClient.java
index 40a21c6..5c1dd91 100644 (file)
-/*\r
- * Jalview - A Sequence Alignment Editor and Viewer (Version 2.6)\r
- * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle\r
- * \r
- * This file is part of Jalview.\r
- * \r
- * Jalview is free software: you can redistribute it and/or\r
- * modify it under the terms of the GNU General Public License \r
- * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.\r
- * \r
- * Jalview is distributed in the hope that it will be useful, but \r
- * WITHOUT ANY WARRANTY; without even the implied warranty \r
- * of MERCHANTABILITY or FITNESS FOR A PARTICULAR \r
- * PURPOSE.  See the GNU General Public License for more details.\r
- * \r
- * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.\r
- */\r
-package jalview.ws.ebi;\r
-\r
-import java.io.*;\r
-import java.util.*;\r
-import javax.xml.namespace.*;\r
-import javax.xml.rpc.*;\r
-\r
-import org.apache.axis.client.Call;\r
-import org.apache.axis.client.Service;\r
-import org.apache.axis.encoding.*;\r
-\r
-/**\r
- * DOCUMENT ME!\r
- * \r
- * @author $author$\r
- * @version $Revision$\r
- */\r
-public class EBIFetchClient\r
-{\r
-  Call call;\r
-\r
-  String format = "default";\r
-\r
-  String style = "raw";\r
-\r
-  /**\r
-   * Creates a new EBIFetchClient object.\r
-   */\r
-  public EBIFetchClient()\r
-  {\r
-    try\r
-    {\r
-      call = (Call) new Service().createCall();\r
-      call.setTargetEndpointAddress(new java.net.URL(\r
-              "http://www.ebi.ac.uk/ws/services/Dbfetch"));\r
-    } catch (Exception ex)\r
-    {\r
-    }\r
-  }\r
-\r
-  /**\r
-   * DOCUMENT ME!\r
-   * \r
-   * @return DOCUMENT ME!\r
-   */\r
-  public String[] getSupportedDBs()\r
-  {\r
-    try\r
-    {\r
-      call.setOperationName(new QName("urn:Dbfetch", "getSupportedDBs"));\r
-      call.setReturnType(XMLType.SOAP_ARRAY);\r
-\r
-      return (String[]) call.invoke(new Object[]\r
-      {});\r
-    } catch (Exception ex)\r
-    {\r
-      return null;\r
-    }\r
-  }\r
-\r
-  /**\r
-   * DOCUMENT ME!\r
-   * \r
-   * @return DOCUMENT ME!\r
-   */\r
-  public String[] getSupportedFormats()\r
-  {\r
-    try\r
-    {\r
-      call\r
-              .setOperationName(new QName("urn:Dbfetch",\r
-                      "getSupportedFormats"));\r
-      call.setReturnType(XMLType.SOAP_ARRAY);\r
-\r
-      return (String[]) call.invoke(new Object[]\r
-      {});\r
-    } catch (Exception ex)\r
-    {\r
-      return null;\r
-    }\r
-  }\r
-\r
-  /**\r
-   * DOCUMENT ME!\r
-   * \r
-   * @return DOCUMENT ME!\r
-   */\r
-  public String[] getSupportedStyles()\r
-  {\r
-    try\r
-    {\r
-      call.setOperationName(new QName("urn:Dbfetch", "getSupportedStyles"));\r
-      call.setReturnType(XMLType.SOAP_ARRAY);\r
-\r
-      return (String[]) call.invoke(new Object[]\r
-      {});\r
-    } catch (Exception ex)\r
-    {\r
-      return null;\r
-    }\r
-  }\r
-\r
-  public File fetchDataAsFile(String ids, String f, String s)\r
-          throws OutOfMemoryError\r
-  {\r
-    String[] data = fetchData(ids, f, s);\r
-    // TODO: after JV 2.4 - test data==null and pass error(s) back up if\r
-    // possible (OutOfMemoryErrors are usual problem)\r
-    if (data == null)\r
-    {\r
-      return null;\r
-    }\r
-    File outFile = null;\r
-    try\r
-    {\r
-      outFile = File.createTempFile("jalview", ".xml");\r
-      outFile.deleteOnExit();\r
-      PrintWriter out = new PrintWriter(new FileOutputStream(outFile));\r
-      int index = 0;\r
-      while (index < data.length)\r
-      {\r
-        out.println(data[index]);\r
-        index++;\r
-      }\r
-      out.close();\r
-    } catch (Exception ex)\r
-    {\r
-    }\r
-    return outFile;\r
-  }\r
-\r
-  /**\r
-   * Single DB multiple record retrieval\r
-   * \r
-   * @param ids\r
-   *          db:query1;query2;query3\r
-   * @param f\r
-   *          raw/xml\r
-   * @param s\r
-   *          ?\r
-   * \r
-   * @return Raw string array result of query set\r
-   */\r
-  public String[] fetchData(String ids, String f, String s)\r
-          throws OutOfMemoryError\r
-  {\r
-    // Need to split\r
-    // ids of the form uniprot:25KD_SARPE;ADHR_DROPS;\r
-    StringTokenizer queries = new StringTokenizer(ids, ";");\r
-    String db = null;\r
-    StringBuffer querystring = null;\r
-    while (queries.hasMoreTokens())\r
-    {\r
-      String query = queries.nextToken();\r
-      int p;\r
-      if ((p = query.indexOf(':')) > -1)\r
-      {\r
-        db = query.substring(0, p);\r
-        query = query.substring(p + 1);\r
-      }\r
-      if (querystring == null)\r
-      {\r
-        querystring = new StringBuffer(query);\r
-      }\r
-      else\r
-      {\r
-        querystring.append("," + query);\r
-      }\r
-    }\r
-    if (db == null)\r
-    {\r
-      System.err.println("Invalid Query string : '" + ids\r
-              + "'\nShould be of form 'dbname:q1;q2;q3;q4'");\r
-    }\r
-    return fetchBatch(querystring.toString(), db, f, s);\r
-  }\r
-\r
-  public String[] fetchBatch(String ids, String db, String f, String s)\r
-          throws OutOfMemoryError\r
-  {\r
-    // max 50 ids can be added at one time\r
-    try\r
-    {\r
-      // call.setOperationName(new QName("urn:Dbfetch", "fetchData"));\r
-      call.setOperationName(new QName("urn:Dbfetch", "fetchBatch"));\r
-      call.addParameter("ids", XMLType.XSD_STRING, ParameterMode.IN);\r
-      call.addParameter("db", XMLType.XSD_STRING, ParameterMode.IN);\r
-      call.addParameter("format", XMLType.XSD_STRING, ParameterMode.IN);\r
-      call.addParameter("style", XMLType.XSD_STRING, ParameterMode.IN);\r
-      call.setReturnType(XMLType.SOAP_ARRAY);\r
-\r
-      if (f != null)\r
-      {\r
-        format = f;\r
-      }\r
-\r
-      if (s != null)\r
-      {\r
-        style = s;\r
-      }\r
-\r
-      try\r
-      {\r
-        return (String[]) call.invoke(new Object[]\r
-        { ids.toLowerCase(), db.toLowerCase(), format, style });\r
-      } catch (OutOfMemoryError er)\r
-      {\r
-\r
-        System.out.println("OUT OF MEMORY DOWNLOADING QUERY FROM " + db\r
-                + ":\n" + ids);\r
-        throw er;\r
-      }\r
-      // return null;\r
-    } catch (Exception ex)\r
-    {\r
-      if (ex.getMessage().startsWith(\r
-              "uk.ac.ebi.jdbfetch.exceptions.DbfNoEntryFoundException"))\r
-      {\r
-        return null;\r
-      }\r
-      System.err.println("Unexpected exception when retrieving from " + db\r
-              + "\nQuery was : '" + ids + "'");\r
-      ex.printStackTrace(System.err);\r
-      return null;\r
-    }\r
-  }\r
-}\r
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer (Version 2.9)
+ * Copyright (C) 2015 The Jalview Authors
+ * 
+ * This file is part of Jalview.
+ * 
+ * Jalview is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License 
+ * as published by the Free Software Foundation, either version 3
+ * of the License, or (at your option) any later version.
+ *  
+ * Jalview is distributed in the hope that it will be useful, but 
+ * WITHOUT ANY WARRANTY; without even the implied warranty 
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
+ * PURPOSE.  See the GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
+ */
+package jalview.ws.ebi;
+
+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;
+
+/**
+ * DOCUMENT ME!
+ * 
+ * @author $author$
+ * @version $Revision$
+ */
+public class EBIFetchClient
+{
+  String format = "default";
+
+  String style = "raw";
+
+  /**
+   * Creates a new EBIFetchClient object.
+   */
+  public EBIFetchClient()
+  {
+  }
+
+  /**
+   * DOCUMENT ME!
+   * 
+   * @return DOCUMENT ME!
+   */
+  public String[] getSupportedDBs()
+  {
+    // TODO - implement rest call for dbfetch getSupportedDBs
+    throw new Error(MessageManager.getString("error.not_yet_implemented"));
+  }
+
+  /**
+   * DOCUMENT ME!
+   * 
+   * @return DOCUMENT ME!
+   */
+  public String[] getSupportedFormats()
+  {
+    // TODO - implement rest call for dbfetch getSupportedFormats
+    throw new Error(MessageManager.getString("error.not_yet_implemented"));
+  }
+
+  /**
+   * DOCUMENT ME!
+   * 
+   * @return DOCUMENT ME!
+   */
+  public String[] getSupportedStyles()
+  {
+    // TODO - implement rest call for dbfetch getSupportedStyles
+    throw new Error(MessageManager.getString("error.not_yet_implemented"));
+  }
+
+  /**
+   * 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 f
+   *          the format wanted
+   * @param s
+   *          - unused parameter
+   * @return the file holding the response
+   * @throws OutOfMemoryError
+   */
+  public File fetchDataAsFile(String ids, String f, String s)
+          throws OutOfMemoryError
+  {
+    File outFile = null;
+    try
+    {
+      outFile = File.createTempFile("jalview", ".xml");
+      outFile.deleteOnExit();
+      fetchData(ids, f, s, outFile);
+      if (outFile.length() == 0)
+      {
+        outFile.delete();
+        return null;
+      }
+    } catch (Exception ex)
+    {
+    }
+    return outFile;
+  }
+
+  /**
+   * Single DB multiple record retrieval
+   * 
+   * @param ids
+   *          db:query1;query2;query3
+   * @param f
+   *          raw/xml
+   * @param s
+   *          not used - remove?
+   * 
+   * @return Raw string array result of query set
+   */
+  public String[] fetchData(String ids, String f, String s)
+          throws OutOfMemoryError
+  {
+    return fetchData(ids, f, s, null);
+  }
+
+  public String[] fetchData(String ids, String f, String s, File outFile)
+          throws OutOfMemoryError
+  {
+    // Need to split
+    // ids of the form uniprot:25KD_SARPE;ADHR_DROPS;
+    String[] rslts = new String[0];
+    StringTokenizer queries = new StringTokenizer(ids, ";");
+    String db = null;
+    StringBuffer querystring = null;
+    int nq = 0;
+    while (queries.hasMoreTokens())
+    {
+      String query = queries.nextToken();
+      int p;
+      if ((p = query.indexOf(':')) > -1)
+      {
+        db = query.substring(0, p);
+        query = query.substring(p + 1);
+      }
+      if (querystring == null)
+      {
+        querystring = new StringBuffer(query);
+        nq++;
+      }
+      else
+      {
+        querystring.append("," + query);
+        nq++;
+      }
+    }
+    if (db == null)
+    {
+      System.err.println("Invalid Query string : '" + ids
+              + "'\nShould be of form 'dbname:q1;q2;q3;q4'");
+      return null;
+    }
+    String[] rslt = fetchBatch(querystring.toString(), db, f, s, outFile);
+    if (rslt != null)
+    {
+      String[] nrslts = new String[rslt.length + rslts.length];
+      System.arraycopy(rslts, 0, nrslts, 0, rslts.length);
+      System.arraycopy(rslt, 0, nrslts, rslts.length, rslt.length);
+      rslts = nrslts;
+    }
+
+    return (rslts.length == 0 ? null : rslts);
+  }
+
+  public String[] fetchBatch(String ids, String db, String f, String s,
+          File outFile) throws OutOfMemoryError
+  {
+    long time = System.currentTimeMillis();
+    // max 200 ids can be added at one time
+    try
+    {
+      URL rcall = new URL("http://www.ebi.ac.uk/Tools/dbfetch/dbfetch/"
+              + db.toLowerCase() + "/" + ids.toLowerCase()
+              + (f != null ? "/" + f : ""));
+
+      InputStream is = new BufferedInputStream(rcall.openStream());
+      if (outFile != null)
+      {
+        FileOutputStream fio = new FileOutputStream(outFile);
+        byte[] bb = new byte[32 * 1024];
+        int l;
+        while ((l = is.read(bb)) > 0)
+        {
+          fio.write(bb, 0, l);
+        }
+        fio.close();
+        is.close();
+      }
+      else
+      {
+        BufferedReader br = new BufferedReader(new InputStreamReader(is));
+        String rtn;
+        List<String> arl = new ArrayList<String>();
+        while ((rtn = br.readLine()) != null)
+        {
+          arl.add(rtn);
+        }
+        return arl.toArray(new String[arl.size()]);
+      }
+    } catch (OutOfMemoryError er)
+    {
+
+      System.out.println("OUT OF MEMORY DOWNLOADING QUERY FROM " + db
+              + ":\n" + ids);
+      throw er;
+    } catch (Exception ex)
+    {
+      if (ex.getMessage().startsWith(
+              "uk.ac.ebi.jdbfetch.exceptions.DbfNoEntryFoundException"))
+      {
+        return null;
+      }
+      System.err.println("Unexpected exception when retrieving from " + db
+              + "\nQuery was : '" + ids + "'");
+      ex.printStackTrace(System.err);
+      return null;
+    } finally
+    {
+      // System.err.println("Took " + (System.currentTimeMillis() - time)
+      // / 1000 + " secs for one call.");
+    }
+    return null;
+  }
+}