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 java.io.BufferedReader;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.InputStreamReader;
28 import java.net.HttpURLConnection;
30 import java.util.ArrayList;
31 import java.util.List;
32 import java.util.Locale;
33 import java.util.StringTokenizer;
35 import jalview.datamodel.DBRefSource;
36 import jalview.util.MessageManager;
37 import jalview.util.Platform;
45 public class EBIFetchClient
49 Platform.addJ2SDirectDatabaseCall("https://www.ebi.ac.uk/");
53 * Creates a new EBIFetchClient object.
55 public EBIFetchClient()
62 * @return DOCUMENT ME!
64 public String[] getSupportedDBs()
66 // TODO - implement rest call for dbfetch getSupportedDBs
67 throw new Error(MessageManager.getString("error.not_yet_implemented"));
73 * @return DOCUMENT ME!
75 public String[] getSupportedFormats()
77 // TODO - implement rest call for dbfetch getSupportedFormats
78 throw new Error(MessageManager.getString("error.not_yet_implemented"));
84 * @return DOCUMENT ME!
86 public String[] getSupportedStyles()
88 // TODO - implement rest call for dbfetch getSupportedStyles
89 throw new Error(MessageManager.getString("error.not_yet_implemented"));
93 * Send an HTTP fetch request to EBI and save the reply in a temporary file.
96 * the query formatted as db:query1;query2;query3
100 * for the temporary file to hold response (without separator)
101 * @return the file holding the response
102 * @throws OutOfMemoryError
105 public File fetchDataAsFile(String ids, String format, String ext)
106 throws OutOfMemoryError
111 outFile = File.createTempFile("jalview", "." + ext);
112 outFile.deleteOnExit();
113 fetchData(ids, format, outFile);
114 if (outFile.length() == 0)
119 } catch (Exception ex)
126 * Fetches queries and either saves the response to a file or returns as
133 * @throws OutOfMemoryError
135 String[] fetchData(String ids, String format, File outFile)
136 throws OutOfMemoryError
138 StringBuilder querystring = new StringBuilder(ids.length());
139 String database = parseIds(ids, querystring);
140 if (database == null)
143 .errPrintln("Invalid Query string : '" + ids + "'");
145 .errPrintln("Should be of form 'dbname:q1;q2;q3;q4'");
149 // note: outFile is currently always specified, so return value is null
150 String[] rslt = fetchBatch(querystring.toString(), database, format,
153 return (rslt != null && rslt.length > 0 ? rslt : null);
157 * Parses ids formatted as dbname:q1;q2;q3, returns the dbname and adds
158 * queries as comma-separated items to the querystring. dbname must be
159 * specified for at least one queryId. Returns null if a mixture of different
160 * dbnames is found (ignoring case).
166 static String parseIds(String ids, StringBuilder queryString)
168 String database = null;
169 StringTokenizer queries = new StringTokenizer(ids, ";");
170 boolean appending = queryString.length() > 0;
171 while (queries.hasMoreTokens())
173 String query = queries.nextToken();
174 int p = query.indexOf(':');
177 String db = query.substring(0, p);
178 if (database != null && !db.equalsIgnoreCase(database))
181 * different databases mixed in together - invalid
186 query = query.substring(p + 1);
188 queryString.append(appending ? "," : "");
189 queryString.append(query);
196 * Fetches queries and either saves the response to a file or (if no file
197 * specified) returns as string data
203 * @return array of lines from EBI only if outFile is null (which it will not
205 * @throws OutOfMemoryError
207 String[] fetchBatch(String ids, String database, String format,
208 File outFile) throws OutOfMemoryError
210 String url = buildUrl(ids, database, format);
211 InputStream is = null;
212 BufferedReader br = null;
215 URL rcall = new URL(url);
216 HttpURLConnection conn = (HttpURLConnection) rcall.openConnection();
217 int responseCode = conn.getResponseCode();
218 if (responseCode == 200)
220 is = conn.getInputStream();
223 Platform.streamToFile(is, outFile);
226 br = new BufferedReader(new InputStreamReader(is));
228 List<String> arl = new ArrayList<>();
229 while ((rtn = br.readLine()) != null)
233 return (String[]) arl.toArray();
235 jalview.bin.Console.errPrintln(
236 "Warning: response code " + responseCode + " for " + url);
237 } catch (OutOfMemoryError er)
239 jalview.bin.Console.outPrintln("OUT OF MEMORY DOWNLOADING QUERY FROM "
240 + database + ":\n" + ids);
242 } catch (Exception ex)
244 if (!ex.getMessage().startsWith(
245 "uk.ac.ebi.jdbfetch.exceptions.DbfNoEntryFoundException"))
248 .errPrintln("Unexpected exception when retrieving from "
249 + database + "\nQuery was : '" + ids + "'");
250 ex.printStackTrace(System.err);
259 } catch (IOException e)
268 } catch (IOException e)
277 * Constructs the URL to fetch from
284 static String buildUrl(String ids, String database, String format)
287 if (database.equalsIgnoreCase(DBRefSource.EMBL)
288 || database.equalsIgnoreCase(DBRefSource.EMBLCDS))
290 url = "https://www.ebi.ac.uk/ena/browser/api/embl/"
291 + ids.toLowerCase(Locale.ROOT) + "?download=true&gzip=true";
295 url = "https://www.ebi.ac.uk/Tools/dbfetch/dbfetch/"
296 + database.toLowerCase(Locale.ROOT) + "/"
297 + ids.toLowerCase(Locale.ROOT)
298 + (format != null ? "/" + format : "");