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.IOException;
31 import java.io.InputStream;
32 import java.io.InputStreamReader;
33 import java.net.HttpURLConnection;
35 import java.util.ArrayList;
36 import java.util.List;
37 import java.util.StringTokenizer;
45 public class EBIFetchClient
49 * Creates a new EBIFetchClient object.
51 public EBIFetchClient()
58 * @return DOCUMENT ME!
60 public String[] getSupportedDBs()
62 // TODO - implement rest call for dbfetch getSupportedDBs
63 throw new Error(MessageManager.getString("error.not_yet_implemented"));
69 * @return DOCUMENT ME!
71 public String[] getSupportedFormats()
73 // TODO - implement rest call for dbfetch getSupportedFormats
74 throw new Error(MessageManager.getString("error.not_yet_implemented"));
80 * @return DOCUMENT ME!
82 public String[] getSupportedStyles()
84 // TODO - implement rest call for dbfetch getSupportedStyles
85 throw new Error(MessageManager.getString("error.not_yet_implemented"));
89 * Send an HTTP fetch request to EBI and save the reply in a temporary file.
92 * the query formatted as db:query1;query2;query3
96 * for the temporary file to hold response (without separator)
97 * @return the file holding the response
98 * @throws OutOfMemoryError
101 public File fetchDataAsFile(String ids, String format, String ext)
102 throws OutOfMemoryError
107 outFile = File.createTempFile("jalview", "." + ext);
108 outFile.deleteOnExit();
109 fetchData(ids, format, outFile);
110 if (outFile.length() == 0)
115 } catch (Exception ex)
122 * Fetches queries and either saves the response to a file or returns as
129 * @throws OutOfMemoryError
131 String[] fetchData(String ids, String format, File outFile)
132 throws OutOfMemoryError
134 StringBuilder querystring = new StringBuilder(ids.length());
135 String database = parseIds(ids, querystring);
136 if (database == null)
138 System.err.println("Invalid Query string : '" + ids + "'");
139 System.err.println("Should be of form 'dbname:q1;q2;q3;q4'");
143 // note: outFile is currently always specified, so return value is null
144 String[] rslt = fetchBatch(querystring.toString(), database, format,
147 return (rslt != null && rslt.length > 0 ? rslt : null);
151 * Parses ids formatted as dbname:q1;q2;q3, returns the dbname and adds
152 * queries as comma-separated items to the querystring. dbname must be
153 * specified for at least one queryId. Returns null if a mixture of different
154 * dbnames is found (ignoring case).
160 static String parseIds(String ids, StringBuilder queryString)
162 String database = null;
163 StringTokenizer queries = new StringTokenizer(ids, ";");
164 boolean appending = queryString.length() > 0;
165 while (queries.hasMoreTokens())
167 String query = queries.nextToken();
168 int p = query.indexOf(':');
171 String db = query.substring(0, p);
172 if (database != null && !db.equalsIgnoreCase(database))
175 * different databases mixed in together - invalid
180 query = query.substring(p + 1);
182 queryString.append(appending ? "," : "");
183 queryString.append(query);
190 * Fetches queries and either saves the response to a file or (if no file
191 * specified) returns as string data
198 * @throws OutOfMemoryError
200 String[] fetchBatch(String ids, String database, String format,
201 File outFile) throws OutOfMemoryError
203 // long time = System.currentTimeMillis();
204 String url = buildUrl(ids, database, format);
205 InputStream is = null;
206 BufferedReader br = null;
209 URL rcall = new URL(url);
211 HttpURLConnection conn = (HttpURLConnection) rcall.openConnection();
212 int responseCode = conn.getResponseCode();
213 if (responseCode != 200)
215 System.err.println("Warning: response code " + responseCode
218 is = new BufferedInputStream(conn.getInputStream());
221 FileOutputStream fio = new FileOutputStream(outFile);
222 byte[] bb = new byte[32 * 1024];
224 while ((l = is.read(bb)) > 0)
233 br = new BufferedReader(new InputStreamReader(is));
235 List<String> arl = new ArrayList<String>();
236 while ((rtn = br.readLine()) != null)
240 return arl.toArray(new String[arl.size()]);
242 } catch (OutOfMemoryError er)
244 System.out.println("OUT OF MEMORY DOWNLOADING QUERY FROM " + database
247 } catch (Exception ex)
249 if (ex.getMessage().startsWith(
250 "uk.ac.ebi.jdbfetch.exceptions.DbfNoEntryFoundException"))
254 System.err.println("Unexpected exception when retrieving from "
255 + database + "\nQuery was : '" + ids + "'");
256 ex.printStackTrace(System.err);
260 // System.err.println("EBIFetch took " + (System.currentTimeMillis() -
267 } catch (IOException e)
276 } catch (IOException e)
285 * Constructs the URL to fetch from
292 static String buildUrl(String ids, String database, String format)
295 if (database.equalsIgnoreCase(DBRefSource.EMBL)
296 || database.equalsIgnoreCase(DBRefSource.EMBLCDS))
298 url = "https://www.ebi.ac.uk/ena/browser/api/embl/"
299 + ids.toLowerCase() + "?download=true&gzip=true";
303 url = "https://www.ebi.ac.uk/Tools/dbfetch/dbfetch/"
304 + database.toLowerCase() + "/" + ids.toLowerCase()
305 + (format != null ? "/" + format : "");