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.util.Locale;
25 import jalview.datamodel.DBRefSource;
26 import jalview.util.MessageManager;
27 import jalview.util.Platform;
29 import java.io.BufferedReader;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.io.InputStreamReader;
34 import java.net.HttpURLConnection;
36 import java.util.ArrayList;
37 import java.util.List;
38 import java.util.StringTokenizer;
46 public class EBIFetchClient
50 * Creates a new EBIFetchClient object.
52 public EBIFetchClient()
59 * @return DOCUMENT ME!
61 public String[] getSupportedDBs()
63 // TODO - implement rest call for dbfetch getSupportedDBs
64 throw new Error(MessageManager.getString("error.not_yet_implemented"));
70 * @return DOCUMENT ME!
72 public String[] getSupportedFormats()
74 // TODO - implement rest call for dbfetch getSupportedFormats
75 throw new Error(MessageManager.getString("error.not_yet_implemented"));
81 * @return DOCUMENT ME!
83 public String[] getSupportedStyles()
85 // TODO - implement rest call for dbfetch getSupportedStyles
86 throw new Error(MessageManager.getString("error.not_yet_implemented"));
90 * Send an HTTP fetch request to EBI and save the reply in a temporary file.
93 * the query formatted as db:query1;query2;query3
97 * for the temporary file to hold response (without separator)
98 * @return the file holding the response
99 * @throws OutOfMemoryError
102 public File fetchDataAsFile(String ids, String format, String ext)
103 throws OutOfMemoryError
108 outFile = File.createTempFile("jalview", "." + ext);
109 outFile.deleteOnExit();
110 fetchData(ids, format, outFile);
111 if (outFile.length() == 0)
116 } catch (Exception ex)
123 * Fetches queries and either saves the response to a file or returns as
130 * @throws OutOfMemoryError
132 String[] fetchData(String ids, String format, File outFile)
133 throws OutOfMemoryError
135 StringBuilder querystring = new StringBuilder(ids.length());
136 String database = parseIds(ids, querystring);
137 if (database == null)
140 .errPrintln("Invalid Query string : '" + ids + "'");
142 .errPrintln("Should be of form 'dbname:q1;q2;q3;q4'");
146 // note: outFile is currently always specified, so return value is null
147 String[] rslt = fetchBatch(querystring.toString(), database, format,
150 return (rslt != null && rslt.length > 0 ? rslt : null);
154 * Parses ids formatted as dbname:q1;q2;q3, returns the dbname and adds
155 * queries as comma-separated items to the querystring. dbname must be
156 * specified for at least one queryId. Returns null if a mixture of different
157 * dbnames is found (ignoring case).
163 static String parseIds(String ids, StringBuilder queryString)
165 String database = null;
166 StringTokenizer queries = new StringTokenizer(ids, ";");
167 boolean appending = queryString.length() > 0;
168 while (queries.hasMoreTokens())
170 String query = queries.nextToken();
171 int p = query.indexOf(':');
174 String db = query.substring(0, p);
175 if (database != null && !db.equalsIgnoreCase(database))
178 * different databases mixed in together - invalid
183 query = query.substring(p + 1);
185 queryString.append(appending ? "," : "");
186 queryString.append(query);
193 * Fetches queries and either saves the response to a file or (if no file
194 * specified) returns as string data
200 * @return array of lines from EBI only if outFile is null (which it will not
202 * @throws OutOfMemoryError
204 String[] fetchBatch(String ids, String database, String format,
205 File outFile) throws OutOfMemoryError
207 String url = buildUrl(ids, database, format);
208 InputStream is = null;
209 BufferedReader br = null;
212 URL rcall = new URL(url);
213 HttpURLConnection conn = (HttpURLConnection) rcall.openConnection();
214 int responseCode = conn.getResponseCode();
215 if (responseCode == 200)
217 is = conn.getInputStream();
220 Platform.streamToFile(is, outFile);
223 br = new BufferedReader(new InputStreamReader(is));
225 List<String> arl = new ArrayList<>();
226 while ((rtn = br.readLine()) != null)
230 return (String[]) arl.toArray();
232 jalview.bin.Console.errPrintln(
233 "Warning: response code " + responseCode + " for " + url);
234 } catch (OutOfMemoryError er)
236 jalview.bin.Console.outPrintln("OUT OF MEMORY DOWNLOADING QUERY FROM "
237 + database + ":\n" + ids);
239 } catch (Exception ex)
241 if (!ex.getMessage().startsWith(
242 "uk.ac.ebi.jdbfetch.exceptions.DbfNoEntryFoundException"))
245 .errPrintln("Unexpected exception when retrieving from "
246 + database + "\nQuery was : '" + ids + "'");
247 ex.printStackTrace(System.err);
256 } catch (IOException e)
265 } catch (IOException e)
275 Platform.addJ2SDirectDatabaseCall("https://www.ebi.ac.uk/");
279 * Constructs the URL to fetch from
286 static String buildUrl(String ids, String database, String format)
289 if (database.equalsIgnoreCase(DBRefSource.EMBL)
290 || database.equalsIgnoreCase(DBRefSource.EMBLCDS))
292 url = "https://www.ebi.ac.uk/ena/browser/api/embl/"
293 + ids.toLowerCase(Locale.ROOT) + "?download=true&gzip=true";
297 url = "https://www.ebi.ac.uk/Tools/dbfetch/dbfetch/"
298 + database.toLowerCase(Locale.ROOT) + "/"
299 + ids.toLowerCase(Locale.ROOT)
300 + (format != null ? "/" + format : "");