X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fws%2Febi%2FEBIFetchClient.java;h=f6928c4aa7e60e92d65ed0630b205958e5429b18;hb=37de9310bec3501cbc6381e0c3dcb282fcaad812;hp=e9aff35f16991cf53f6c13a0707efac80a1d0d1a;hpb=0eda2ac2f334f55f6e5f137789453a9f4dfe4d73;p=jalview.git diff --git a/src/jalview/ws/ebi/EBIFetchClient.java b/src/jalview/ws/ebi/EBIFetchClient.java index e9aff35..f6928c4 100644 --- a/src/jalview/ws/ebi/EBIFetchClient.java +++ b/src/jalview/ws/ebi/EBIFetchClient.java @@ -1,235 +1,281 @@ /* - * Jalview - A Sequence Alignment Editor and Viewer - * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle - * - * This program 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 2 + * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$) + * Copyright (C) $$Year-Rel$$ 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. - * - * This program 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. - * + * + * 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 this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + * along with Jalview. If not, see . + * The Jalview Authors are detailed in the 'AUTHORS' file. */ package jalview.ws.ebi; -import java.io.*; -import java.util.*; -import javax.xml.namespace.*; -import javax.xml.rpc.*; +import jalview.datamodel.DBRefSource; +import jalview.util.MessageManager; -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.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 { - Call call; - String format = "default"; - String style = "raw"; /** * Creates a new EBIFetchClient object. */ 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) - { - } } /** * DOCUMENT ME! - * + * * @return DOCUMENT ME! */ 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(MessageManager.getString("error.not_yet_implemented")); } /** * DOCUMENT ME! - * + * * @return DOCUMENT ME! */ 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(MessageManager.getString("error.not_yet_implemented")); } /** * DOCUMENT ME! - * + * * @return DOCUMENT ME! */ 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(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 format + * the format wanted + * @param extension + * for the temporary file to hold response + * @return the file holding the response + * @throws OutOfMemoryError + */ - public File fetchDataAsFile(String ids, String f, String s) + public File fetchDataAsFile(String ids, String format, String ext) + throws OutOfMemoryError { - String[] data = fetchData(ids, f, s); File outFile = null; try { - outFile = File.createTempFile("jalview", ".xml"); + outFile = File.createTempFile("jalview", ext); outFile.deleteOnExit(); - PrintWriter out = new PrintWriter(new FileOutputStream(outFile)); - int index = 0; - while (index < data.length) + fetchData(ids, format, outFile); + if (outFile.length() == 0) { - out.println(data[index]); - index++; + outFile.delete(); + return null; } - out.close(); + } catch (Exception ex) + { } - catch (Exception ex) - {} return outFile; } /** - * Single DB multiple record retrieval - * - * @param ids db:query1;query2;query3 - * @param f raw/xml - * @param s ? - * - * @return Raw string array result of query set + * Fetches queries and either saves the response to a file or returns as + * string data + * + * @param ids + * @param format + * @param outFile + * @return + * @throws OutOfMemoryError */ - public String[] fetchData(String ids, String f, String s) + String[] fetchData(String ids, String format, File outFile) + throws OutOfMemoryError { - // Need to split - // ids of the form uniprot:25KD_SARPE;ADHR_DROPS; + StringBuilder querystring = new StringBuilder(ids.length()); + String database = parseIds(ids, querystring); + if (database == null) + { + System.err.println("Invalid Query string : '" + ids + "'"); + System.err.println("Should be of form 'dbname:q1;q2;q3;q4'"); + return null; + } + + // note: outFile is currently always specified, so return value is null + String[] rslt = fetchBatch(querystring.toString(), database, format, + outFile); + + return (rslt != null && rslt.length > 0 ? rslt : null); + } + + /** + * Parses ids formatted as dbname:q1;q2;q3, returns the dbname and adds + * queries as comma-separated items to the querystring. dbname must be + * specified for at least one queryId. Returns null if a mixture of different + * dbnames is found (ignoring case). + * + * @param ids + * @param queryString + * @return + */ + static String parseIds(String ids, StringBuilder queryString) + { + String database = null; StringTokenizer queries = new StringTokenizer(ids, ";"); - String db = null; - StringBuffer querystring = null; + boolean appending = queryString.length() > 0; while (queries.hasMoreTokens()) { String query = queries.nextToken(); - int p; - if ( (p = query.indexOf(':')) > -1) + int p = query.indexOf(':'); + if (p > -1) { - db = query.substring(0, p); + String db = query.substring(0, p); + if (database != null && !db.equalsIgnoreCase(database)) + { + /* + * different databases mixed in together - invalid + */ + return null; + } + database = db; query = query.substring(p + 1); } - if (querystring == null) - { - querystring = new StringBuffer(query); - } - else - { - querystring.append("," + query); - } - } - if (db == null) - { - System.err.println("Invalid Query string : '" + ids + - "'\nShould be of form 'dbname:q1;q2;q3;q4'"); + queryString.append(appending ? "," : ""); + queryString.append(query); + appending = true; } - return fetchBatch(querystring.toString(), db, f, s); + return database; } - public String[] fetchBatch(String ids, String db, String f, String s) + /** + * Fetches queries and either saves the response to a file or (if no file + * specified) returns as string data + * + * @param ids + * @param database + * @param format + * @param outFile + * @return + * @throws OutOfMemoryError + */ + String[] fetchBatch(String ids, String database, String format, + File outFile) throws OutOfMemoryError { - // max 50 ids can be added at one time + // long time = System.currentTimeMillis(); + String url = buildUrl(ids, database, format); + 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); - - if (f != null) - { - format = f; - } + URL rcall = new URL(url); - if (s != null) + InputStream is = new BufferedInputStream(rcall.openStream()); + if (outFile != null) { - style = s; + 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(); } - - try - { - return (String[]) call.invoke(new Object[] - {ids.toLowerCase(), db.toLowerCase(), - format, style}); - } - catch (OutOfMemoryError er) + else { - System.out.println("OUT OF MEMORY DOWNLOADING QUERY FROM " + db + ":\n" + - ids); + BufferedReader br = new BufferedReader(new InputStreamReader(is)); + String rtn; + List arl = new ArrayList(); + while ((rtn = br.readLine()) != null) + { + arl.add(rtn); + } + return arl.toArray(new String[arl.size()]); } - return null; - } - catch (Exception ex) + } catch (OutOfMemoryError er) + { + System.out.println("OUT OF MEMORY DOWNLOADING QUERY FROM " + database + + ":\n" + ids); + throw er; + } catch (Exception ex) { if (ex.getMessage().startsWith( - "uk.ac.ebi.jdbfetch.exceptions.DbfNoEntryFoundException")) + "uk.ac.ebi.jdbfetch.exceptions.DbfNoEntryFoundException")) { return null; } - System.err.println("Unexpected exception when retrieving from " + db + - "\nQuery was : '" + ids + "'"); + System.err.println("Unexpected exception when retrieving from " + + database + "\nQuery was : '" + ids + "'"); ex.printStackTrace(System.err); return null; + } finally + { + // System.err.println("EBIFetch took " + (System.currentTimeMillis() - + // time) + " ms"); + } + return null; + } + + /** + * Constructs the URL to fetch from + * + * @param ids + * @param database + * @param format + * @return + */ + static String buildUrl(String ids, String database, String format) + { + String url; + if (database.equalsIgnoreCase(DBRefSource.EMBL) + || database.equalsIgnoreCase(DBRefSource.EMBLCDS)) + { + url = "http://www.ebi.ac.uk/ena/data/view/" + ids.toLowerCase() + + (format != null ? "&" + format : ""); + } + else + { + url = "http://www.ebi.ac.uk/Tools/dbfetch/dbfetch/" + + database.toLowerCase() + "/" + ids.toLowerCase() + + (format != null ? "/" + format : ""); } + return url; } }