2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.ws.ebi;
23 import jalview.datamodel.DBRefSource;
24 import jalview.util.MessageManager;
26 import java.io.BufferedInputStream;
27 import java.io.BufferedReader;
29 import java.io.FileOutputStream;
30 import java.io.InputStream;
31 import java.io.InputStreamReader;
32 import java.net.HttpURLConnection;
34 import java.util.ArrayList;
35 import java.util.List;
36 import java.util.StringTokenizer;
44 public class EBIFetchClient
48 * Creates a new EBIFetchClient object.
50 public EBIFetchClient()
57 * @return DOCUMENT ME!
59 public String[] getSupportedDBs()
61 // TODO - implement rest call for dbfetch getSupportedDBs
62 throw new Error(MessageManager.getString("error.not_yet_implemented"));
68 * @return DOCUMENT ME!
70 public String[] getSupportedFormats()
72 // TODO - implement rest call for dbfetch getSupportedFormats
73 throw new Error(MessageManager.getString("error.not_yet_implemented"));
79 * @return DOCUMENT ME!
81 public String[] getSupportedStyles()
83 // TODO - implement rest call for dbfetch getSupportedStyles
84 throw new Error(MessageManager.getString("error.not_yet_implemented"));
88 * Send an HTTP fetch request to EBI and save the reply in a temporary file.
91 * the query formatted as db:query1;query2;query3
95 * for the temporary file to hold response (without separator)
96 * @return the file holding the response
97 * @throws OutOfMemoryError
100 public File fetchDataAsFile(String ids, String format, String ext)
101 throws OutOfMemoryError
106 outFile = File.createTempFile("jalview", "." + ext);
107 outFile.deleteOnExit();
108 fetchData(ids, format, outFile);
109 if (outFile.length() == 0)
114 } catch (Exception ex)
121 * Fetches queries and either saves the response to a file or returns as
128 * @throws OutOfMemoryError
130 String[] fetchData(String ids, String format, File outFile)
131 throws OutOfMemoryError
133 StringBuilder querystring = new StringBuilder(ids.length());
134 String database = parseIds(ids, querystring);
135 if (database == null)
137 System.err.println("Invalid Query string : '" + ids + "'");
138 System.err.println("Should be of form 'dbname:q1;q2;q3;q4'");
142 // note: outFile is currently always specified, so return value is null
143 String[] rslt = fetchBatch(querystring.toString(), database, format,
146 return (rslt != null && rslt.length > 0 ? rslt : null);
150 * Parses ids formatted as dbname:q1;q2;q3, returns the dbname and adds
151 * queries as comma-separated items to the querystring. dbname must be
152 * specified for at least one queryId. Returns null if a mixture of different
153 * dbnames is found (ignoring case).
159 static String parseIds(String ids, StringBuilder queryString)
161 String database = null;
162 StringTokenizer queries = new StringTokenizer(ids, ";");
163 boolean appending = queryString.length() > 0;
164 while (queries.hasMoreTokens())
166 String query = queries.nextToken();
167 int p = query.indexOf(':');
170 String db = query.substring(0, p);
171 if (database != null && !db.equalsIgnoreCase(database))
174 * different databases mixed in together - invalid
179 query = query.substring(p + 1);
181 queryString.append(appending ? "," : "");
182 queryString.append(query);
189 * Fetches queries and either saves the response to a file or (if no file
190 * specified) returns as string data
197 * @throws OutOfMemoryError
199 String[] fetchBatch(String ids, String database, String format,
200 File outFile) throws OutOfMemoryError
202 // long time = System.currentTimeMillis();
203 String url = buildUrl(ids, database, format);
207 URL rcall = new URL(url);
209 HttpURLConnection conn = (HttpURLConnection) rcall.openConnection();
210 int responseCode = conn.getResponseCode();
211 if (responseCode != 200)
213 System.err.println("Warning: response code " + responseCode
216 InputStream is = new BufferedInputStream(conn.getInputStream());
219 FileOutputStream fio = new FileOutputStream(outFile);
220 byte[] bb = new byte[32 * 1024];
222 while ((l = is.read(bb)) > 0)
231 BufferedReader br = new BufferedReader(new InputStreamReader(is));
233 List<String> arl = new ArrayList<String>();
234 while ((rtn = br.readLine()) != null)
238 return arl.toArray(new String[arl.size()]);
240 } catch (OutOfMemoryError er)
242 System.out.println("OUT OF MEMORY DOWNLOADING QUERY FROM " + database
245 } catch (Exception ex)
247 if (ex.getMessage().startsWith(
248 "uk.ac.ebi.jdbfetch.exceptions.DbfNoEntryFoundException"))
252 System.err.println("Unexpected exception when retrieving from "
253 + database + "\nQuery was : '" + ids + "'");
254 ex.printStackTrace(System.err);
258 // System.err.println("EBIFetch took " + (System.currentTimeMillis() -
265 * Constructs the URL to fetch from
272 static String buildUrl(String ids, String database, String format)
275 if (database.equalsIgnoreCase(DBRefSource.EMBL)
276 || database.equalsIgnoreCase(DBRefSource.EMBLCDS))
278 url = "https://www.ebi.ac.uk/ena/data/view/" + ids.toLowerCase()
279 + (format != null ? "&" + format : "");
283 url = "https://www.ebi.ac.uk/Tools/dbfetch/dbfetch/"
284 + database.toLowerCase() + "/" + ids.toLowerCase()
285 + (format != null ? "/" + format : "");