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;
33 import java.util.ArrayList;
34 import java.util.List;
35 import java.util.StringTokenizer;
43 public class EBIFetchClient
47 * Creates a new EBIFetchClient object.
49 public EBIFetchClient()
56 * @return DOCUMENT ME!
58 public String[] getSupportedDBs()
60 // TODO - implement rest call for dbfetch getSupportedDBs
61 throw new Error(MessageManager.getString("error.not_yet_implemented"));
67 * @return DOCUMENT ME!
69 public String[] getSupportedFormats()
71 // TODO - implement rest call for dbfetch getSupportedFormats
72 throw new Error(MessageManager.getString("error.not_yet_implemented"));
78 * @return DOCUMENT ME!
80 public String[] getSupportedStyles()
82 // TODO - implement rest call for dbfetch getSupportedStyles
83 throw new Error(MessageManager.getString("error.not_yet_implemented"));
87 * Send an HTTP fetch request to EBI and save the reply in a temporary file.
90 * the query formatted as db:query1;query2;query3
94 * for the temporary file to hold response (without separator)
95 * @return the file holding the response
96 * @throws OutOfMemoryError
99 public File fetchDataAsFile(String ids, String format, String ext)
100 throws OutOfMemoryError
105 outFile = File.createTempFile("jalview", "." + ext);
106 outFile.deleteOnExit();
107 fetchData(ids, format, outFile);
108 if (outFile.length() == 0)
113 } catch (Exception ex)
120 * Fetches queries and either saves the response to a file or returns as
127 * @throws OutOfMemoryError
129 String[] fetchData(String ids, String format, File outFile)
130 throws OutOfMemoryError
132 StringBuilder querystring = new StringBuilder(ids.length());
133 String database = parseIds(ids, querystring);
134 if (database == null)
136 System.err.println("Invalid Query string : '" + ids + "'");
137 System.err.println("Should be of form 'dbname:q1;q2;q3;q4'");
141 // note: outFile is currently always specified, so return value is null
142 String[] rslt = fetchBatch(querystring.toString(), database, format,
145 return (rslt != null && rslt.length > 0 ? rslt : null);
149 * Parses ids formatted as dbname:q1;q2;q3, returns the dbname and adds
150 * queries as comma-separated items to the querystring. dbname must be
151 * specified for at least one queryId. Returns null if a mixture of different
152 * dbnames is found (ignoring case).
158 static String parseIds(String ids, StringBuilder queryString)
160 String database = null;
161 StringTokenizer queries = new StringTokenizer(ids, ";");
162 boolean appending = queryString.length() > 0;
163 while (queries.hasMoreTokens())
165 String query = queries.nextToken();
166 int p = query.indexOf(':');
169 String db = query.substring(0, p);
170 if (database != null && !db.equalsIgnoreCase(database))
173 * different databases mixed in together - invalid
178 query = query.substring(p + 1);
180 queryString.append(appending ? "," : "");
181 queryString.append(query);
188 * Fetches queries and either saves the response to a file or (if no file
189 * specified) returns as string data
196 * @throws OutOfMemoryError
198 String[] fetchBatch(String ids, String database, String format,
199 File outFile) throws OutOfMemoryError
201 // long time = System.currentTimeMillis();
202 String url = buildUrl(ids, database, format);
206 URL rcall = new URL(url);
208 InputStream is = new BufferedInputStream(rcall.openStream());
211 FileOutputStream fio = new FileOutputStream(outFile);
212 byte[] bb = new byte[32 * 1024];
214 while ((l = is.read(bb)) > 0)
223 BufferedReader br = new BufferedReader(new InputStreamReader(is));
225 List<String> arl = new ArrayList<String>();
226 while ((rtn = br.readLine()) != null)
230 return arl.toArray(new String[arl.size()]);
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"))
244 System.err.println("Unexpected exception when retrieving from "
245 + database + "\nQuery was : '" + ids + "'");
246 ex.printStackTrace(System.err);
250 // System.err.println("EBIFetch took " + (System.currentTimeMillis() -
257 * Constructs the URL to fetch from
264 static String buildUrl(String ids, String database, String format)
267 if (database.equalsIgnoreCase(DBRefSource.EMBL)
268 || database.equalsIgnoreCase(DBRefSource.EMBLCDS))
270 url = "http://www.ebi.ac.uk/ena/data/view/" + ids.toLowerCase()
271 + (format != null ? "&" + format : "");
275 url = "http://www.ebi.ac.uk/Tools/dbfetch/dbfetch/"
276 + database.toLowerCase() + "/" + ids.toLowerCase()
277 + (format != null ? "/" + format : "");