2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 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.InputStreamReader;
31 import java.util.ArrayList;
32 import java.util.StringTokenizer;
40 public class EBIFetchClient
42 String format = "default";
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"));
86 public File fetchDataAsFile(String ids, String f, String s)
87 throws OutOfMemoryError
92 outFile = File.createTempFile("jalview", ".xml");
93 outFile.deleteOnExit();
94 fetchData(ids, f, s, outFile);
95 if (outFile.length() == 0)
100 } catch (Exception ex)
107 * Single DB multiple record retrieval
110 * db:query1;query2;query3
116 * @return Raw string array result of query set
118 public String[] fetchData(String ids, String f, String s)
119 throws OutOfMemoryError
121 return fetchData(ids, f, s, null);
124 public String[] fetchData(String ids, String f, String s, File outFile)
125 throws OutOfMemoryError
128 // ids of the form uniprot:25KD_SARPE;ADHR_DROPS;
129 String[] rslts = new String[0];
130 StringTokenizer queries = new StringTokenizer(ids, ";");
132 StringBuffer querystring = null;
134 while (queries.hasMoreTokens())
136 String query = queries.nextToken();
138 if ((p = query.indexOf(':')) > -1)
140 db = query.substring(0, p);
141 query = query.substring(p + 1);
143 if (querystring == null)
145 querystring = new StringBuffer(query);
150 querystring.append("," + query);
156 System.err.println("Invalid Query string : '" + ids
157 + "'\nShould be of form 'dbname:q1;q2;q3;q4'");
160 String[] rslt = fetchBatch(querystring.toString(), db, f, s, outFile);
163 String[] nrslts = new String[rslt.length + rslts.length];
164 System.arraycopy(rslts, 0, nrslts, 0, rslts.length);
165 System.arraycopy(rslt, 0, nrslts, rslts.length, rslt.length);
169 return (rslts.length == 0 ? null : rslts);
172 public String[] fetchBatch(String ids, String db, String f, String s,
173 File outFile) throws OutOfMemoryError
175 long time = System.currentTimeMillis();
176 // max 200 ids can be added at one time
179 URL rcall = new URL("http://www.ebi.ac.uk/Tools/dbfetch/dbfetch/"
180 + db.toLowerCase() + "/" + ids.toLowerCase()
181 + (f != null ? "/" + f : ""));
183 BufferedInputStream is = new BufferedInputStream(rcall.openStream());
186 FileOutputStream fio = new FileOutputStream(outFile);
187 byte[] bb = new byte[32 * 1024];
189 while ((l = is.read(bb)) > 0)
198 BufferedReader br = new BufferedReader(new InputStreamReader(is));
200 ArrayList<String> arl = new ArrayList<String>();
201 while ((rtn = br.readLine()) != null)
205 return arl.toArray(new String[arl.size()]);
207 } catch (OutOfMemoryError er)
210 System.out.println("OUT OF MEMORY DOWNLOADING QUERY FROM " + db
213 } catch (Exception ex)
215 if (ex.getMessage().startsWith(
216 "uk.ac.ebi.jdbfetch.exceptions.DbfNoEntryFoundException"))
220 System.err.println("Unexpected exception when retrieving from " + db
221 + "\nQuery was : '" + ids + "'");
222 ex.printStackTrace(System.err);
226 // System.err.println("Took " + (System.currentTimeMillis() - time)
227 // / 1000 + " secs for one call.");