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
45 String format = "default";
50 * Creates a new EBIFetchClient object.
52 public EBIFetchClient()
59 * @return DOCUMENT ME!
61 public String[] getSupportedDBs()
63 // TODO - implement rest call for dbfetch getSupportedDBs
64 throw new Error(MessageManager.getString("error.not_yet_implemented"));
70 * @return DOCUMENT ME!
72 public String[] getSupportedFormats()
74 // TODO - implement rest call for dbfetch getSupportedFormats
75 throw new Error(MessageManager.getString("error.not_yet_implemented"));
81 * @return DOCUMENT ME!
83 public String[] getSupportedStyles()
85 // TODO - implement rest call for dbfetch getSupportedStyles
86 throw new Error(MessageManager.getString("error.not_yet_implemented"));
90 * Send an HTTP fetch request to EBI and save the reply in a temporary file.
93 * the query formatted as db:query1;query2;query3
98 * @return the file holding the response
99 * @throws OutOfMemoryError
102 public File fetchDataAsFile(String ids, String format, String s,
104 throws OutOfMemoryError
109 outFile = File.createTempFile("jalview", ext);
110 outFile.deleteOnExit();
111 fetchData(ids, format, s, outFile);
112 if (outFile.length() == 0)
117 } catch (Exception ex)
124 * Single DB multiple record retrieval
127 * db:query1;query2;query3
133 * @return Raw string array result of query set
135 public String[] fetchData(String ids, String format, String s)
136 throws OutOfMemoryError
138 return fetchData(ids, format, s, null);
141 String[] fetchData(String ids, String f, String s, File outFile)
142 throws OutOfMemoryError
145 // ids of the form uniprot:25KD_SARPE;ADHR_DROPS;
146 String[] rslts = new String[0];
147 StringTokenizer queries = new StringTokenizer(ids, ";");
149 StringBuffer querystring = null;
151 while (queries.hasMoreTokens())
153 String query = queries.nextToken();
155 if ((p = query.indexOf(':')) > -1)
157 db = query.substring(0, p);
158 query = query.substring(p + 1);
160 if (querystring == null)
162 querystring = new StringBuffer(query);
167 querystring.append("," + query);
173 System.err.println("Invalid Query string : '" + ids
174 + "'\nShould be of form 'dbname:q1;q2;q3;q4'");
177 String[] rslt = fetchBatch(querystring.toString(), db, f, s, outFile);
180 String[] nrslts = new String[rslt.length + rslts.length];
181 System.arraycopy(rslts, 0, nrslts, 0, rslts.length);
182 System.arraycopy(rslt, 0, nrslts, rslts.length, rslt.length);
186 return (rslts.length == 0 ? null : rslts);
189 public String[] fetchBatch(String ids, String dbPath, String format, String s,
190 File outFile) throws OutOfMemoryError
192 // long time = System.currentTimeMillis();
194 * JAL-1855 dbfetch from ena_sequence, ena_coding
196 if (dbPath.equalsIgnoreCase(DBRefSource.EMBL))
198 dbPath = "ena_sequence";
200 else if (dbPath.equalsIgnoreCase(DBRefSource.EMBLCDS))
202 dbPath = "ena_coding";
207 URL rcall = new URL("http://www.ebi.ac.uk/Tools/dbfetch/dbfetch/"
208 + dbPath.toLowerCase() + "/" + ids.toLowerCase()
209 + (format != null ? "/" + format : ""));
211 InputStream is = new BufferedInputStream(rcall.openStream());
214 FileOutputStream fio = new FileOutputStream(outFile);
215 byte[] bb = new byte[32 * 1024];
217 while ((l = is.read(bb)) > 0)
226 BufferedReader br = new BufferedReader(new InputStreamReader(is));
228 List<String> arl = new ArrayList<String>();
229 while ((rtn = br.readLine()) != null)
233 return arl.toArray(new String[arl.size()]);
235 } catch (OutOfMemoryError er)
238 System.out.println("OUT OF MEMORY DOWNLOADING QUERY FROM " + dbPath
241 } catch (Exception ex)
243 if (ex.getMessage().startsWith(
244 "uk.ac.ebi.jdbfetch.exceptions.DbfNoEntryFoundException"))
248 System.err.println("Unexpected exception when retrieving from "
250 + "\nQuery was : '" + ids + "'");
251 ex.printStackTrace(System.err);
255 // System.err.println("EBIFetch took " + (System.currentTimeMillis() -