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)
139 System.err.println("Invalid Query string : '" + ids + "'");
140 System.err.println("Should be of form 'dbname:q1;q2;q3;q4'");
144 // note: outFile is currently always specified, so return value is null
145 String[] rslt = fetchBatch(querystring.toString(), database, format,
148 return (rslt != null && rslt.length > 0 ? rslt : null);
152 * Parses ids formatted as dbname:q1;q2;q3, returns the dbname and adds
153 * queries as comma-separated items to the querystring. dbname must be
154 * specified for at least one queryId. Returns null if a mixture of different
155 * dbnames is found (ignoring case).
161 static String parseIds(String ids, StringBuilder queryString)
163 String database = null;
164 StringTokenizer queries = new StringTokenizer(ids, ";");
165 boolean appending = queryString.length() > 0;
166 while (queries.hasMoreTokens())
168 String query = queries.nextToken();
169 int p = query.indexOf(':');
172 String db = query.substring(0, p);
173 if (database != null && !db.equalsIgnoreCase(database))
176 * different databases mixed in together - invalid
181 query = query.substring(p + 1);
183 queryString.append(appending ? "," : "");
184 queryString.append(query);
191 * Fetches queries and either saves the response to a file or (if no file
192 * specified) returns as string data
198 * @return array of lines from EBI only if outFile is null (which it will not
200 * @throws OutOfMemoryError
202 String[] fetchBatch(String ids, String database, String format,
203 File outFile) throws OutOfMemoryError
205 String url = buildUrl(ids, database, format);
206 InputStream is = null;
207 BufferedReader br = null;
210 URL rcall = new URL(url);
211 HttpURLConnection conn = (HttpURLConnection) rcall.openConnection();
212 int responseCode = conn.getResponseCode();
213 if (responseCode == 200)
215 is = conn.getInputStream();
218 Platform.streamToFile(is, outFile);
221 br = new BufferedReader(new InputStreamReader(is));
223 List<String> arl = new ArrayList<>();
224 while ((rtn = br.readLine()) != null)
228 return (String[]) arl.toArray();
231 "Warning: response code " + responseCode + " for " + url);
232 } catch (OutOfMemoryError er)
234 System.out.println("OUT OF MEMORY DOWNLOADING QUERY FROM " + database
237 } catch (Exception ex)
239 if (!ex.getMessage().startsWith(
240 "uk.ac.ebi.jdbfetch.exceptions.DbfNoEntryFoundException"))
242 System.err.println("Unexpected exception when retrieving from "
243 + database + "\nQuery was : '" + ids + "'");
244 ex.printStackTrace(System.err);
253 } catch (IOException e)
262 } catch (IOException e)
271 * Constructs the URL to fetch from
278 static String buildUrl(String ids, String database, String format)
281 if (database.equalsIgnoreCase(DBRefSource.EMBL)
282 || database.equalsIgnoreCase(DBRefSource.EMBLCDS))
284 url = "https://www.ebi.ac.uk/ena/browser/api/embl/"
285 + ids.toLowerCase(Locale.ROOT) + "?download=true&gzip=true";
289 url = "https://www.ebi.ac.uk/Tools/dbfetch/dbfetch/"
290 + database.toLowerCase(Locale.ROOT) + "/" + ids.toLowerCase(Locale.ROOT)
291 + (format != null ? "/" + format : "");