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)
119 String[] fetchData(String ids, String f, File outFile)
120 throws OutOfMemoryError
122 String[] rslts = new String[0];
123 StringBuilder querystring = new StringBuilder(ids.length());
124 String db = parseIds(ids, querystring);
127 System.err.println("Invalid Query string : '" + ids + "'");
128 System.err.println("Should be of form 'dbname:q1;q2;q3;q4'");
131 String[] rslt = fetchBatch(querystring.toString(), db, f, outFile);
134 String[] nrslts = new String[rslt.length + rslts.length];
135 System.arraycopy(rslts, 0, nrslts, 0, rslts.length);
136 System.arraycopy(rslt, 0, nrslts, rslts.length, rslt.length);
140 return (rslts.length == 0 ? null : rslts);
144 * Parses ids formatted as dbname:q1;q2;q3, returns the dbname and adds
145 * queries as comma-separated items to the querystring. dbname must be
146 * specified for at least one queryId. Returns null if a mixture of different
147 * dbnames is found (ignoring case).
153 static String parseIds(String ids, StringBuilder queryString)
155 String database = null;
156 StringTokenizer queries = new StringTokenizer(ids, ";");
157 boolean appending = queryString.length() > 0;
158 while (queries.hasMoreTokens())
160 String query = queries.nextToken();
161 int p = query.indexOf(':');
164 String db = query.substring(0, p);
165 if (database != null && !db.equalsIgnoreCase(database))
168 * different databases mixed in together - invalid
173 query = query.substring(p + 1);
175 queryString.append(appending ? "," : "");
176 queryString.append(query);
182 String[] fetchBatch(String ids, String dbPath, String format,
183 File outFile) throws OutOfMemoryError
185 // long time = System.currentTimeMillis();
186 String url = buildUrl(ids, dbPath, format);
190 URL rcall = new URL(url);
192 InputStream is = new BufferedInputStream(rcall.openStream());
195 FileOutputStream fio = new FileOutputStream(outFile);
196 byte[] bb = new byte[32 * 1024];
198 while ((l = is.read(bb)) > 0)
207 BufferedReader br = new BufferedReader(new InputStreamReader(is));
209 List<String> arl = new ArrayList<String>();
210 while ((rtn = br.readLine()) != null)
214 return arl.toArray(new String[arl.size()]);
216 } catch (OutOfMemoryError er)
219 System.out.println("OUT OF MEMORY DOWNLOADING QUERY FROM " + dbPath
222 } catch (Exception ex)
224 if (ex.getMessage().startsWith(
225 "uk.ac.ebi.jdbfetch.exceptions.DbfNoEntryFoundException"))
229 System.err.println("Unexpected exception when retrieving from "
231 + "\nQuery was : '" + ids + "'");
232 ex.printStackTrace(System.err);
236 // System.err.println("EBIFetch took " + (System.currentTimeMillis() -
243 * Constructs the URL to fetch from
250 static String buildUrl(String ids, String database, String format)
253 if (database.equalsIgnoreCase(DBRefSource.EMBL)
254 || database.equalsIgnoreCase(DBRefSource.EMBLCDS))
256 url = "http://www.ebi.ac.uk/ena/data/view/" + ids.toLowerCase()
257 + (format != null ? "&" + format : "");
261 url = "http://www.ebi.ac.uk/Tools/dbfetch/dbfetch/"
262 + database.toLowerCase() + "/" + ids.toLowerCase()
263 + (format != null ? "/" + format : "");