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 java.io.BufferedInputStream;
24 import java.io.BufferedReader;
26 import java.io.FileOutputStream;
27 import java.io.InputStreamReader;
29 import java.util.ArrayList;
30 import java.util.StringTokenizer;
38 public class EBIFetchClient
40 String format = "default";
45 * Creates a new EBIFetchClient object.
47 public EBIFetchClient()
54 * @return DOCUMENT ME!
56 public String[] getSupportedDBs()
58 // TODO - implement rest call for dbfetch getSupportedDBs
59 throw new Error("Not yet implemented");
65 * @return DOCUMENT ME!
67 public String[] getSupportedFormats()
69 // TODO - implement rest call for dbfetch getSupportedFormats
70 throw new Error("Not yet implemented");
76 * @return DOCUMENT ME!
78 public String[] getSupportedStyles()
80 // TODO - implement rest call for dbfetch getSupportedStyles
81 throw new Error("Not yet implemented");
84 public File fetchDataAsFile(String ids, String f, String s)
85 throws OutOfMemoryError
90 outFile = File.createTempFile("jalview", ".xml");
91 outFile.deleteOnExit();
92 fetchData(ids, f, s, outFile);
93 if (outFile.length() == 0)
98 } catch (Exception ex)
105 * Single DB multiple record retrieval
108 * db:query1;query2;query3
114 * @return Raw string array result of query set
116 public String[] fetchData(String ids, String f, String s)
117 throws OutOfMemoryError
119 return fetchData(ids, f, s, null);
122 public String[] fetchData(String ids, String f, String s, File outFile)
123 throws OutOfMemoryError
126 // ids of the form uniprot:25KD_SARPE;ADHR_DROPS;
127 String[] rslts = new String[0];
128 StringTokenizer queries = new StringTokenizer(ids, ";");
130 StringBuffer querystring = null;
132 while (queries.hasMoreTokens())
134 String query = queries.nextToken();
136 if ((p = query.indexOf(':')) > -1)
138 db = query.substring(0, p);
139 query = query.substring(p + 1);
141 if (querystring == null)
143 querystring = new StringBuffer(query);
148 querystring.append("," + query);
154 System.err.println("Invalid Query string : '" + ids
155 + "'\nShould be of form 'dbname:q1;q2;q3;q4'");
158 String[] rslt = fetchBatch(querystring.toString(), db, f, s, outFile);
161 String[] nrslts = new String[rslt.length + rslts.length];
162 System.arraycopy(rslts, 0, nrslts, 0, rslts.length);
163 System.arraycopy(rslt, 0, nrslts, rslts.length, rslt.length);
167 return (rslts.length == 0 ? null : rslts);
170 public String[] fetchBatch(String ids, String db, String f, String s,
171 File outFile) throws OutOfMemoryError
173 long time = System.currentTimeMillis();
174 // max 200 ids can be added at one time
177 URL rcall = new URL("http://www.ebi.ac.uk/Tools/dbfetch/dbfetch/"
178 + db.toLowerCase() + "/" + ids.toLowerCase()
179 + (f != null ? "/" + f : ""));
181 BufferedInputStream is = new BufferedInputStream(rcall.openStream());
184 FileOutputStream fio = new FileOutputStream(outFile);
185 byte[] bb = new byte[32 * 1024];
187 while ((l = is.read(bb)) > 0)
196 BufferedReader br = new BufferedReader(new InputStreamReader(is));
198 ArrayList<String> arl = new ArrayList<String>();
199 while ((rtn = br.readLine()) != null)
203 return arl.toArray(new String[arl.size()]);
205 } catch (OutOfMemoryError er)
208 System.out.println("OUT OF MEMORY DOWNLOADING QUERY FROM " + db
211 } catch (Exception ex)
213 if (ex.getMessage().startsWith(
214 "uk.ac.ebi.jdbfetch.exceptions.DbfNoEntryFoundException"))
218 System.err.println("Unexpected exception when retrieving from " + db
219 + "\nQuery was : '" + ids + "'");
220 ex.printStackTrace(System.err);
224 // System.err.println("Took " + (System.currentTimeMillis() - time)
225 // / 1000 + " secs for one call.");