X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fws%2Febi%2FEBIFetchClient.java;h=1f833d046e03f53e78083b0fa43ecc0f48284c41;hb=1724567f3dfc3b4637aac7bdeb6633628e6a8335;hp=468b7f0306acc3bd5626f54d5a23c8bf9fa7680a;hpb=86e1bfc3ed99bee91069b3238eb291c3955338d3;p=jalview.git diff --git a/src/jalview/ws/ebi/EBIFetchClient.java b/src/jalview/ws/ebi/EBIFetchClient.java index 468b7f0..1f833d0 100644 --- a/src/jalview/ws/ebi/EBIFetchClient.java +++ b/src/jalview/ws/ebi/EBIFetchClient.java @@ -20,14 +20,18 @@ */ package jalview.ws.ebi; +import java.util.Locale; + +import jalview.datamodel.DBRefSource; import jalview.util.MessageManager; +import jalview.util.Platform; -import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.File; -import java.io.FileOutputStream; +import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.net.HttpURLConnection; import java.net.URL; import java.util.ArrayList; import java.util.List; @@ -41,9 +45,6 @@ import java.util.StringTokenizer; */ public class EBIFetchClient { - String format = "default"; - - String style = "raw"; /** * Creates a new EBIFetchClient object. @@ -90,22 +91,23 @@ public class EBIFetchClient * * @param ids * the query formatted as db:query1;query2;query3 - * @param f + * @param format * the format wanted - * @param s - * - unused parameter + * @param ext + * for the temporary file to hold response (without separator) * @return the file holding the response * @throws OutOfMemoryError */ - public File fetchDataAsFile(String ids, String f, String s, String ext) + + public File fetchDataAsFile(String ids, String format, String ext) throws OutOfMemoryError { File outFile = null; try { - outFile = File.createTempFile("jalview", ext); + outFile = File.createTempFile("jalview", "." + ext); outFile.deleteOnExit(); - fetchData(ids, f, s, outFile); + fetchData(ids, format, outFile); if (outFile.length() == 0) { outFile.delete(); @@ -118,128 +120,178 @@ public class EBIFetchClient } /** - * Single DB multiple record retrieval + * Fetches queries and either saves the response to a file or returns as + * string data * * @param ids - * db:query1;query2;query3 - * @param f - * raw/xml - * @param s - * not used - remove? - * - * @return Raw string array result of query set + * @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 { - return fetchData(ids, f, s, null); + 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); } - public String[] fetchData(String ids, String f, String s, File outFile) - throws OutOfMemoryError + /** + * 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) { - // Need to split - // ids of the form uniprot:25KD_SARPE;ADHR_DROPS; - String[] rslts = new String[0]; + String database = null; StringTokenizer queries = new StringTokenizer(ids, ";"); - String db = null; - StringBuffer querystring = null; - int nq = 0; + 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); - nq++; - } - else - { - querystring.append("," + query); - nq++; - } + queryString.append(appending ? "," : ""); + queryString.append(query); + appending = true; } - 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); + 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 array of lines from EBI only if outFile is null (which it will not + * be) + * @throws OutOfMemoryError + */ + String[] fetchBatch(String ids, String database, String format, File outFile) throws OutOfMemoryError { - long time = System.currentTimeMillis(); - // max 200 ids can be added at one time + String url = buildUrl(ids, database, format); + InputStream is = null; + BufferedReader br = null; 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) + URL rcall = new URL(url); + HttpURLConnection conn = (HttpURLConnection) rcall.openConnection(); + int responseCode = conn.getResponseCode(); + if (responseCode == 200) { - FileOutputStream fio = new FileOutputStream(outFile); - byte[] bb = new byte[32 * 1024]; - int l; - while ((l = is.read(bb)) > 0) + is = conn.getInputStream(); + if (outFile != null) { - fio.write(bb, 0, l); + Platform.streamToFile(is, outFile); + return null; } - fio.close(); - is.close(); - } - else - { - BufferedReader br = new BufferedReader(new InputStreamReader(is)); + br = new BufferedReader(new InputStreamReader(is)); String rtn; - List arl = new ArrayList(); + List arl = new ArrayList<>(); while ((rtn = br.readLine()) != null) { arl.add(rtn); } - return arl.toArray(new String[arl.size()]); + return (String[]) arl.toArray(); } + System.err.println( + "Warning: response code " + responseCode + " for " + url); } catch (OutOfMemoryError er) { - - System.out.println("OUT OF MEMORY DOWNLOADING QUERY FROM " + db + System.out.println("OUT OF MEMORY DOWNLOADING QUERY FROM " + database + ":\n" + ids); throw er; } catch (Exception ex) { - if (ex.getMessage().startsWith( + if (!ex.getMessage().startsWith( "uk.ac.ebi.jdbfetch.exceptions.DbfNoEntryFoundException")) { - return null; + System.err.println("Unexpected exception when retrieving from " + + database + "\nQuery was : '" + ids + "'"); + ex.printStackTrace(System.err); } - 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."); + if (is != null) + { + try + { + is.close(); + } catch (IOException e) + { + } + } + if (br != null) + { + try + { + br.close(); + } catch (IOException e) + { + } + } } return null; } + static { + Platform.addJ2SDirectDatabaseCall("https://www.ebi.ac.uk/"); + } + /** + * 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 = "https://www.ebi.ac.uk/ena/browser/api/embl/" + + ids.toLowerCase(Locale.ROOT) + "?download=true&gzip=true"; + } + else + { + url = "https://www.ebi.ac.uk/Tools/dbfetch/dbfetch/" + + database.toLowerCase(Locale.ROOT) + "/" + ids.toLowerCase(Locale.ROOT) + + (format != null ? "/" + format : ""); + } + return url; + } }