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
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, outFile);
144 return (rslt != null && rslt.length > 0 ? rslt : null);
148 * Parses ids formatted as dbname:q1;q2;q3, returns the dbname and adds
149 * queries as comma-separated items to the querystring. dbname must be
150 * specified for at least one queryId. Returns null if a mixture of different
151 * dbnames is found (ignoring case).
157 static String parseIds(String ids, StringBuilder queryString)
159 String database = null;
160 StringTokenizer queries = new StringTokenizer(ids, ";");
161 boolean appending = queryString.length() > 0;
162 while (queries.hasMoreTokens())
164 String query = queries.nextToken();
165 int p = query.indexOf(':');
168 String db = query.substring(0, p);
169 if (database != null && !db.equalsIgnoreCase(database))
172 * different databases mixed in together - invalid
177 query = query.substring(p + 1);
179 queryString.append(appending ? "," : "");
180 queryString.append(query);
187 * Fetches queries and either saves the response to a file or (if no file
188 * specified) returns as string data
195 * @throws OutOfMemoryError
197 String[] fetchBatch(String ids, String database, String format,
198 File outFile) throws OutOfMemoryError
200 // long time = System.currentTimeMillis();
201 String url = buildUrl(ids, database, format);
205 URL rcall = new URL(url);
207 InputStream is = new BufferedInputStream(rcall.openStream());
210 FileOutputStream fio = new FileOutputStream(outFile);
211 byte[] bb = new byte[32 * 1024];
213 while ((l = is.read(bb)) > 0)
222 BufferedReader br = new BufferedReader(new InputStreamReader(is));
224 List<String> arl = new ArrayList<String>();
225 while ((rtn = br.readLine()) != null)
229 return arl.toArray(new String[arl.size()]);
231 } catch (OutOfMemoryError er)
233 System.out.println("OUT OF MEMORY DOWNLOADING QUERY FROM " + database
236 } catch (Exception ex)
238 if (ex.getMessage().startsWith(
239 "uk.ac.ebi.jdbfetch.exceptions.DbfNoEntryFoundException"))
243 System.err.println("Unexpected exception when retrieving from "
245 + "\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 : "");