+import jalview.datamodel.DBRefSource;
+import jalview.util.MessageManager;
+
+import java.io.BufferedInputStream;
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.StringTokenizer;
+
+/**
+ * DOCUMENT ME!
+ *
+ * @author $author$
+ * @version $Revision$
+ */
+public class EBIFetchClient
+{
+ String format = "default";
+
+ String style = "raw";
+
+ /**
+ * Creates a new EBIFetchClient object.
+ */
+ public EBIFetchClient()
+ {
+ }
+
+ /**
+ * DOCUMENT ME!
+ *
+ * @return DOCUMENT ME!
+ */
+ public String[] getSupportedDBs()
+ {
+ // TODO - implement rest call for dbfetch getSupportedDBs
+ throw new Error(MessageManager.getString("error.not_yet_implemented"));
+ }
+
+ /**
+ * DOCUMENT ME!
+ *
+ * @return DOCUMENT ME!
+ */
+ public String[] getSupportedFormats()
+ {
+ // TODO - implement rest call for dbfetch getSupportedFormats
+ throw new Error(MessageManager.getString("error.not_yet_implemented"));
+ }
+
+ /**
+ * DOCUMENT ME!
+ *
+ * @return DOCUMENT ME!
+ */
+ public String[] getSupportedStyles()
+ {
+ // TODO - implement rest call for dbfetch getSupportedStyles
+ throw new Error(MessageManager.getString("error.not_yet_implemented"));
+ }
+
+ /**
+ * Send an HTTP fetch request to EBI and save the reply in a temporary file.
+ *
+ * @param ids
+ * the query formatted as db:query1;query2;query3
+ * @param format
+ * the format wanted
+ * @param s
+ * - unused parameter
+ * @return the file holding the response
+ * @throws OutOfMemoryError
+ */
+
+ public File fetchDataAsFile(String ids, String format, String s,
+ String ext)
+ throws OutOfMemoryError
+ {
+ File outFile = null;
+ try
+ {
+ outFile = File.createTempFile("jalview", ext);
+ outFile.deleteOnExit();
+ fetchData(ids, format, s, outFile);
+ if (outFile.length() == 0)
+ {
+ outFile.delete();
+ return null;
+ }
+ } catch (Exception ex)
+ {
+ }
+ return outFile;
+ }
+
+ /**
+ * Single DB multiple record retrieval
+ *
+ * @param ids
+ * db:query1;query2;query3
+ * @param format
+ * raw/xml
+ * @param s
+ * not used - remove?
+ *
+ * @return Raw string array result of query set
+ */
+ public String[] fetchData(String ids, String format, String s)
+ throws OutOfMemoryError
+ {
+ return fetchData(ids, format, s, null);
+ }
+
+ String[] fetchData(String ids, String f, String s, File outFile)
+ throws OutOfMemoryError
+ {
+ // Need to split
+ // ids of the form uniprot:25KD_SARPE;ADHR_DROPS;
+ String[] rslts = new String[0];
+ StringTokenizer queries = new StringTokenizer(ids, ";");
+ String db = null;
+ StringBuffer querystring = null;
+ int nq = 0;
+ while (queries.hasMoreTokens())
+ {
+ String query = queries.nextToken();
+ int p;
+ if ((p = query.indexOf(':')) > -1)
+ {
+ db = query.substring(0, p);
+ query = query.substring(p + 1);
+ }
+ if (querystring == null)
+ {
+ querystring = new StringBuffer(query);
+ nq++;
+ }
+ else
+ {
+ querystring.append("," + query);
+ nq++;
+ }
+ }
+ if (db == null)
+ {
+ System.err.println("Invalid Query string : '" + ids
+ + "'\nShould be of form 'dbname:q1;q2;q3;q4'");
+ return null;
+ }
+ String[] rslt = fetchBatch(querystring.toString(), db, f, s, outFile);
+ if (rslt != null)
+ {
+ String[] nrslts = new String[rslt.length + rslts.length];
+ System.arraycopy(rslts, 0, nrslts, 0, rslts.length);
+ System.arraycopy(rslt, 0, nrslts, rslts.length, rslt.length);
+ rslts = nrslts;
+ }
+
+ return (rslts.length == 0 ? null : rslts);
+ }
+
+ public String[] fetchBatch(String ids, String dbPath, String format, String s,
+ File outFile) throws OutOfMemoryError
+ {
+ // long time = System.currentTimeMillis();
+ /*
+ * JAL-1855 dbfetch from ena_sequence, ena_coding
+ */
+ if (dbPath.equalsIgnoreCase(DBRefSource.EMBL))
+ {
+ dbPath = "ena_sequence";
+ }
+ else if (dbPath.equalsIgnoreCase(DBRefSource.EMBLCDS))
+ {
+ dbPath = "ena_coding";
+ }
+
+ try
+ {
+ URL rcall = new URL("http://www.ebi.ac.uk/Tools/dbfetch/dbfetch/"
+ + dbPath.toLowerCase() + "/" + ids.toLowerCase()
+ + (format != null ? "/" + format : ""));
+
+ InputStream is = new BufferedInputStream(rcall.openStream());
+ if (outFile != null)
+ {
+ FileOutputStream fio = new FileOutputStream(outFile);
+ byte[] bb = new byte[32 * 1024];
+ int l;
+ while ((l = is.read(bb)) > 0)
+ {
+ fio.write(bb, 0, l);
+ }
+ fio.close();
+ is.close();
+ }
+ else
+ {
+ BufferedReader br = new BufferedReader(new InputStreamReader(is));
+ String rtn;
+ List<String> arl = new ArrayList<String>();
+ while ((rtn = br.readLine()) != null)
+ {
+ arl.add(rtn);
+ }
+ return arl.toArray(new String[arl.size()]);
+ }
+ } catch (OutOfMemoryError er)
+ {
+
+ System.out.println("OUT OF MEMORY DOWNLOADING QUERY FROM " + dbPath
+ + ":\n" + ids);
+ throw er;
+ } catch (Exception ex)
+ {
+ if (ex.getMessage().startsWith(
+ "uk.ac.ebi.jdbfetch.exceptions.DbfNoEntryFoundException"))
+ {
+ return null;
+ }
+ System.err.println("Unexpected exception when retrieving from "
+ + dbPath
+ + "\nQuery was : '" + ids + "'");
+ ex.printStackTrace(System.err);
+ return null;
+ } finally
+ {
+ // System.err.println("EBIFetch took " + (System.currentTimeMillis() -
+ // time) + " ms");
+ }
+ return null;
+ }
+}