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
101 public File fetchDataAsFile(String ids, String format, String s)
102 throws OutOfMemoryError
107 outFile = File.createTempFile("jalview", ".xml");
108 outFile.deleteOnExit();
109 fetchData(ids, format, s, outFile);
110 if (outFile.length() == 0)
115 } catch (Exception ex)
122 * Single DB multiple record retrieval
125 * db:query1;query2;query3
131 * @return Raw string array result of query set
133 public String[] fetchData(String ids, String format, String s)
134 throws OutOfMemoryError
136 return fetchData(ids, format, s, null);
139 String[] fetchData(String ids, String f, String s, File outFile)
140 throws OutOfMemoryError
143 // ids of the form uniprot:25KD_SARPE;ADHR_DROPS;
144 String[] rslts = new String[0];
145 StringTokenizer queries = new StringTokenizer(ids, ";");
147 StringBuffer querystring = null;
149 while (queries.hasMoreTokens())
151 String query = queries.nextToken();
153 if ((p = query.indexOf(':')) > -1)
155 db = query.substring(0, p);
156 query = query.substring(p + 1);
158 if (querystring == null)
160 querystring = new StringBuffer(query);
165 querystring.append("," + query);
171 System.err.println("Invalid Query string : '" + ids
172 + "'\nShould be of form 'dbname:q1;q2;q3;q4'");
175 String[] rslt = fetchBatch(querystring.toString(), db, f, s, outFile);
178 String[] nrslts = new String[rslt.length + rslts.length];
179 System.arraycopy(rslts, 0, nrslts, 0, rslts.length);
180 System.arraycopy(rslt, 0, nrslts, rslts.length, rslt.length);
184 return (rslts.length == 0 ? null : rslts);
187 public String[] fetchBatch(String ids, String dbPath, String format, String s,
188 File outFile) throws OutOfMemoryError
190 // long time = System.currentTimeMillis();
192 * JAL-1855 dbfetch from ena_sequence, ena_coding
194 if (dbPath.equalsIgnoreCase(DBRefSource.EMBL))
196 dbPath = "ena_sequence";
198 else if (dbPath.equalsIgnoreCase(DBRefSource.EMBLCDS))
200 dbPath = "ena_coding";
205 URL rcall = new URL("http://www.ebi.ac.uk/Tools/dbfetch/dbfetch/"
206 + dbPath.toLowerCase() + "/" + ids.toLowerCase()
207 + (format != null ? "/" + format : ""));
209 InputStream is = new BufferedInputStream(rcall.openStream());
212 FileOutputStream fio = new FileOutputStream(outFile);
213 byte[] bb = new byte[32 * 1024];
215 while ((l = is.read(bb)) > 0)
224 BufferedReader br = new BufferedReader(new InputStreamReader(is));
226 List<String> arl = new ArrayList<String>();
227 while ((rtn = br.readLine()) != null)
231 return arl.toArray(new String[arl.size()]);
233 } catch (OutOfMemoryError er)
236 System.out.println("OUT OF MEMORY DOWNLOADING QUERY FROM " + dbPath
239 } catch (Exception ex)
241 if (ex.getMessage().startsWith(
242 "uk.ac.ebi.jdbfetch.exceptions.DbfNoEntryFoundException"))
246 System.err.println("Unexpected exception when retrieving from "
248 + "\nQuery was : '" + ids + "'");
249 ex.printStackTrace(System.err);
253 // System.err.println("EBIFetch took " + (System.currentTimeMillis() -