2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
19 package jalview.ws.ebi;
21 import java.io.BufferedInputStream;
22 import java.io.BufferedReader;
24 import java.io.FileOutputStream;
25 import java.io.InputStreamReader;
27 import java.util.ArrayList;
28 import java.util.StringTokenizer;
36 public class EBIFetchClient
38 String format = "default";
43 * Creates a new EBIFetchClient object.
45 public EBIFetchClient()
52 * @return DOCUMENT ME!
54 public String[] getSupportedDBs()
56 // TODO - implement rest call for dbfetch getSupportedDBs
57 throw new Error("Not yet implemented");
63 * @return DOCUMENT ME!
65 public String[] getSupportedFormats()
67 // TODO - implement rest call for dbfetch getSupportedFormats
68 throw new Error("Not yet implemented");
74 * @return DOCUMENT ME!
76 public String[] getSupportedStyles()
78 // TODO - implement rest call for dbfetch getSupportedStyles
79 throw new Error("Not yet implemented");
82 public File fetchDataAsFile(String ids, String f, String s)
83 throws OutOfMemoryError
88 outFile = File.createTempFile("jalview", ".xml");
89 outFile.deleteOnExit();
90 fetchData(ids, f, s, outFile);
91 if (outFile.length() == 0)
96 } catch (Exception ex)
103 * Single DB multiple record retrieval
106 * db:query1;query2;query3
112 * @return Raw string array result of query set
114 public String[] fetchData(String ids, String f, String s)
115 throws OutOfMemoryError
117 return fetchData(ids, f, s, null);
120 public String[] fetchData(String ids, String f, String s, File outFile)
121 throws OutOfMemoryError
124 // ids of the form uniprot:25KD_SARPE;ADHR_DROPS;
125 String[] rslts = new String[0];
126 StringTokenizer queries = new StringTokenizer(ids, ";");
128 StringBuffer querystring = null;
130 while (queries.hasMoreTokens())
132 String query = queries.nextToken();
134 if ((p = query.indexOf(':')) > -1)
136 db = query.substring(0, p);
137 query = query.substring(p + 1);
139 if (querystring == null)
141 querystring = new StringBuffer(query);
146 querystring.append("," + query);
152 System.err.println("Invalid Query string : '" + ids
153 + "'\nShould be of form 'dbname:q1;q2;q3;q4'");
156 String[] rslt = fetchBatch(querystring.toString(), db, f, s, outFile);
159 String[] nrslts = new String[rslt.length + rslts.length];
160 System.arraycopy(rslts, 0, nrslts, 0, rslts.length);
161 System.arraycopy(rslt, 0, nrslts, rslts.length, rslt.length);
165 return (rslts.length == 0 ? null : rslts);
168 public String[] fetchBatch(String ids, String db, String f, String s,
169 File outFile) throws OutOfMemoryError
171 long time = System.currentTimeMillis();
172 // max 200 ids can be added at one time
175 URL rcall = new URL("http://www.ebi.ac.uk/Tools/dbfetch/dbfetch/"
176 + db.toLowerCase() + "/" + ids.toLowerCase()
177 + (f != null ? "/" + f : ""));
179 BufferedInputStream is = new BufferedInputStream(rcall.openStream());
182 FileOutputStream fio = new FileOutputStream(outFile);
183 byte[] bb = new byte[32 * 1024];
185 while ((l = is.read(bb)) > 0)
194 BufferedReader br = new BufferedReader(new InputStreamReader(is));
196 ArrayList<String> arl = new ArrayList<String>();
197 while ((rtn = br.readLine()) != null)
201 return arl.toArray(new String[arl.size()]);
203 } catch (OutOfMemoryError er)
206 System.out.println("OUT OF MEMORY DOWNLOADING QUERY FROM " + db
209 } catch (Exception ex)
211 if (ex.getMessage().startsWith(
212 "uk.ac.ebi.jdbfetch.exceptions.DbfNoEntryFoundException"))
216 System.err.println("Unexpected exception when retrieving from " + db
217 + "\nQuery was : '" + ids + "'");
218 ex.printStackTrace(System.err);
222 //System.err.println("Took " + (System.currentTimeMillis() - time)
223 // / 1000 + " secs for one call.");