From 0adb44b91b95c023bad382ad414dcb02a24cb890 Mon Sep 17 00:00:00 2001 From: jprocter Date: Thu, 4 Oct 2012 16:56:41 +0100 Subject: [PATCH] JAL-636 switch to simple REST interface to dbfetch (//http://www.ebi.ac.uk/Tools/webservices/services/dbfetch_rest) --- src/jalview/ws/ebi/EBIFetchClient.java | 168 +++++++++++++++----------------- 1 file changed, 76 insertions(+), 92 deletions(-) diff --git a/src/jalview/ws/ebi/EBIFetchClient.java b/src/jalview/ws/ebi/EBIFetchClient.java index b837354..48895ae 100644 --- a/src/jalview/ws/ebi/EBIFetchClient.java +++ b/src/jalview/ws/ebi/EBIFetchClient.java @@ -17,14 +17,14 @@ */ package jalview.ws.ebi; -import java.io.*; -import java.util.*; -import javax.xml.namespace.*; -import javax.xml.rpc.*; - -import org.apache.axis.client.Call; -import org.apache.axis.client.Service; -import org.apache.axis.encoding.*; +import java.io.BufferedInputStream; +import java.io.BufferedReader; +import java.io.File; +import java.io.FileOutputStream; +import java.io.InputStreamReader; +import java.net.URL; +import java.util.ArrayList; +import java.util.StringTokenizer; /** * DOCUMENT ME! @@ -34,8 +34,6 @@ import org.apache.axis.encoding.*; */ public class EBIFetchClient { - Call call; - String format = "default"; String style = "raw"; @@ -45,14 +43,6 @@ public class EBIFetchClient */ public EBIFetchClient() { - try - { - call = (Call) new Service().createCall(); - call.setTargetEndpointAddress(new java.net.URL( - "http://www.ebi.ac.uk/ws/services/Dbfetch")); - } catch (Exception ex) - { - } } /** @@ -62,17 +52,8 @@ public class EBIFetchClient */ public String[] getSupportedDBs() { - try - { - call.setOperationName(new QName("urn:Dbfetch", "getSupportedDBs")); - call.setReturnType(XMLType.SOAP_ARRAY); - - return (String[]) call.invoke(new Object[] - {}); - } catch (Exception ex) - { - return null; - } + // TODO - implement rest call for dbfetch getSupportedDBs + throw new Error("Not yet implemented"); } /** @@ -82,17 +63,8 @@ public class EBIFetchClient */ public String[] getSupportedFormats() { - try - { - call.setOperationName(new QName("urn:Dbfetch", "getSupportedFormats")); - call.setReturnType(XMLType.SOAP_ARRAY); - - return (String[]) call.invoke(new Object[] - {}); - } catch (Exception ex) - { - return null; - } + // TODO - implement rest call for dbfetch getSupportedFormats + throw new Error("Not yet implemented"); } /** @@ -102,42 +74,24 @@ public class EBIFetchClient */ public String[] getSupportedStyles() { - try - { - call.setOperationName(new QName("urn:Dbfetch", "getSupportedStyles")); - call.setReturnType(XMLType.SOAP_ARRAY); - - return (String[]) call.invoke(new Object[] - {}); - } catch (Exception ex) - { - return null; - } + // TODO - implement rest call for dbfetch getSupportedStyles + throw new Error("Not yet implemented"); } public File fetchDataAsFile(String ids, String f, String s) throws OutOfMemoryError { - String[] data = fetchData(ids, f, s); - // TODO: after JV 2.4 - test data==null and pass error(s) back up if - // possible (OutOfMemoryErrors are usual problem) - if (data == null) - { - return null; - } File outFile = null; try { outFile = File.createTempFile("jalview", ".xml"); outFile.deleteOnExit(); - PrintWriter out = new PrintWriter(new FileOutputStream(outFile)); - int index = 0; - while (index < data.length) + fetchData(ids, f, s, outFile); + if (outFile.length() == 0) { - out.println(data[index]); - index++; + outFile.delete(); + return null; } - out.close(); } catch (Exception ex) { } @@ -159,11 +113,19 @@ public class EBIFetchClient 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(); @@ -176,56 +138,73 @@ public class EBIFetchClient 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; } - return fetchBatch(querystring.toString(), db, f, s); + 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) - throws OutOfMemoryError + public String[] fetchBatch(String ids, String db, String f, String s, + File outFile) throws OutOfMemoryError { - // max 50 ids can be added at one time + long time = System.currentTimeMillis(); + // max 200 ids can be added at one time try { - // call.setOperationName(new QName("urn:Dbfetch", "fetchData")); - call.setOperationName(new QName("urn:Dbfetch", "fetchBatch")); - call.addParameter("ids", XMLType.XSD_STRING, ParameterMode.IN); - call.addParameter("db", XMLType.XSD_STRING, ParameterMode.IN); - call.addParameter("format", XMLType.XSD_STRING, ParameterMode.IN); - call.addParameter("style", XMLType.XSD_STRING, ParameterMode.IN); - call.setReturnType(XMLType.SOAP_ARRAY); + URL rcall = new URL("http://www.ebi.ac.uk/Tools/dbfetch/dbfetch/" + + db.toLowerCase() + "/" + ids.toLowerCase() + + (f != null ? "/" + f : "")); - if (f != null) + BufferedInputStream is = new BufferedInputStream(rcall.openStream()); + if (outFile != null) { - format = f; + 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(); } - - if (s != null) + else { - style = s; + BufferedReader br = new BufferedReader(new InputStreamReader(is)); + String rtn; + ArrayList arl = new ArrayList(); + while ((rtn = br.readLine()) != null) + { + arl.add(rtn); + } + return arl.toArray(new String[arl.size()]); } + } catch (OutOfMemoryError er) + { - try - { - return (String[]) call.invoke(new Object[] - { ids.toLowerCase(), db.toLowerCase(), format, style }); - } catch (OutOfMemoryError er) - { - - System.out.println("OUT OF MEMORY DOWNLOADING QUERY FROM " + db - + ":\n" + ids); - throw er; - } - // return null; + System.out.println("OUT OF MEMORY DOWNLOADING QUERY FROM " + db + + ":\n" + ids); + throw er; } catch (Exception ex) { if (ex.getMessage().startsWith( @@ -237,6 +216,11 @@ public class EBIFetchClient + "\nQuery was : '" + ids + "'"); ex.printStackTrace(System.err); return null; + } finally + { + //System.err.println("Took " + (System.currentTimeMillis() - time) + // / 1000 + " secs for one call."); } + return null; } } -- 1.7.10.2