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.util.MessageManager;
25 import java.io.BufferedInputStream;
26 import java.io.BufferedReader;
28 import java.io.FileOutputStream;
29 import java.io.InputStream;
30 import java.io.InputStreamReader;
32 import java.util.ArrayList;
33 import java.util.List;
34 import java.util.StringTokenizer;
42 public class EBIFetchClient
44 String format = "default";
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
97 * @return the file holding the response
98 * @throws OutOfMemoryError
100 public File fetchDataAsFile(String ids, String f, String s)
101 throws OutOfMemoryError
106 outFile = File.createTempFile("jalview", ".xml");
107 outFile.deleteOnExit();
108 fetchData(ids, f, s, outFile);
109 if (outFile.length() == 0)
114 } catch (Exception ex)
121 * Single DB multiple record retrieval
124 * db:query1;query2;query3
130 * @return Raw string array result of query set
132 public String[] fetchData(String ids, String f, String s)
133 throws OutOfMemoryError
135 return fetchData(ids, f, s, null);
138 public String[] fetchData(String ids, String f, String s, File outFile)
139 throws OutOfMemoryError
142 // ids of the form uniprot:25KD_SARPE;ADHR_DROPS;
143 String[] rslts = new String[0];
144 StringTokenizer queries = new StringTokenizer(ids, ";");
146 StringBuffer querystring = null;
148 while (queries.hasMoreTokens())
150 String query = queries.nextToken();
152 if ((p = query.indexOf(':')) > -1)
154 db = query.substring(0, p);
155 query = query.substring(p + 1);
157 if (querystring == null)
159 querystring = new StringBuffer(query);
164 querystring.append("," + query);
170 System.err.println("Invalid Query string : '" + ids
171 + "'\nShould be of form 'dbname:q1;q2;q3;q4'");
174 String[] rslt = fetchBatch(querystring.toString(), db, f, s, outFile);
177 String[] nrslts = new String[rslt.length + rslts.length];
178 System.arraycopy(rslts, 0, nrslts, 0, rslts.length);
179 System.arraycopy(rslt, 0, nrslts, rslts.length, rslt.length);
183 return (rslts.length == 0 ? null : rslts);
186 public String[] fetchBatch(String ids, String db, String f, String s,
187 File outFile) throws OutOfMemoryError
189 long time = System.currentTimeMillis();
190 // max 200 ids can be added at one time
193 URL rcall = new URL("http://www.ebi.ac.uk/Tools/dbfetch/dbfetch/"
194 + db.toLowerCase() + "/" + ids.toLowerCase()
195 + (f != null ? "/" + f : ""));
197 InputStream is = new BufferedInputStream(rcall.openStream());
200 FileOutputStream fio = new FileOutputStream(outFile);
201 byte[] bb = new byte[32 * 1024];
203 while ((l = is.read(bb)) > 0)
212 BufferedReader br = new BufferedReader(new InputStreamReader(is));
214 List<String> arl = new ArrayList<String>();
215 while ((rtn = br.readLine()) != null)
219 return arl.toArray(new String[arl.size()]);
221 } catch (OutOfMemoryError er)
224 System.out.println("OUT OF MEMORY DOWNLOADING QUERY FROM " + db
227 } catch (Exception ex)
229 if (ex.getMessage().startsWith(
230 "uk.ac.ebi.jdbfetch.exceptions.DbfNoEntryFoundException"))
234 System.err.println("Unexpected exception when retrieving from " + db
235 + "\nQuery was : '" + ids + "'");
236 ex.printStackTrace(System.err);
240 // System.err.println("Took " + (System.currentTimeMillis() - time)
241 // / 1000 + " secs for one call.");