JAL-1620 version bump and release notes
[jalview.git] / src / jalview / ws / ebi / EBIFetchClient.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2b1)
3  * Copyright (C) 2014 The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
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.
11  *  
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.
16  * 
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.
20  */
21 package jalview.ws.ebi;
22
23 import jalview.util.MessageManager;
24
25 import java.io.BufferedInputStream;
26 import java.io.BufferedReader;
27 import java.io.File;
28 import java.io.FileOutputStream;
29 import java.io.InputStreamReader;
30 import java.net.URL;
31 import java.util.ArrayList;
32 import java.util.StringTokenizer;
33
34 /**
35  * DOCUMENT ME!
36  * 
37  * @author $author$
38  * @version $Revision$
39  */
40 public class EBIFetchClient
41 {
42   String format = "default";
43
44   String style = "raw";
45
46   /**
47    * Creates a new EBIFetchClient object.
48    */
49   public EBIFetchClient()
50   {
51   }
52
53   /**
54    * DOCUMENT ME!
55    * 
56    * @return DOCUMENT ME!
57    */
58   public String[] getSupportedDBs()
59   {
60     // TODO - implement rest call for dbfetch getSupportedDBs
61     throw new Error(MessageManager.getString("error.not_yet_implemented"));
62   }
63
64   /**
65    * DOCUMENT ME!
66    * 
67    * @return DOCUMENT ME!
68    */
69   public String[] getSupportedFormats()
70   {
71     // TODO - implement rest call for dbfetch getSupportedFormats
72     throw new Error(MessageManager.getString("error.not_yet_implemented"));
73   }
74
75   /**
76    * DOCUMENT ME!
77    * 
78    * @return DOCUMENT ME!
79    */
80   public String[] getSupportedStyles()
81   {
82     // TODO - implement rest call for dbfetch getSupportedStyles
83     throw new Error(MessageManager.getString("error.not_yet_implemented"));
84   }
85
86   public File fetchDataAsFile(String ids, String f, String s)
87           throws OutOfMemoryError
88   {
89     File outFile = null;
90     try
91     {
92       outFile = File.createTempFile("jalview", ".xml");
93       outFile.deleteOnExit();
94       fetchData(ids, f, s, outFile);
95       if (outFile.length() == 0)
96       {
97         outFile.delete();
98         return null;
99       }
100     } catch (Exception ex)
101     {
102     }
103     return outFile;
104   }
105
106   /**
107    * Single DB multiple record retrieval
108    * 
109    * @param ids
110    *          db:query1;query2;query3
111    * @param f
112    *          raw/xml
113    * @param s
114    *          ?
115    * 
116    * @return Raw string array result of query set
117    */
118   public String[] fetchData(String ids, String f, String s)
119           throws OutOfMemoryError
120   {
121     return fetchData(ids, f, s, null);
122   }
123
124   public String[] fetchData(String ids, String f, String s, File outFile)
125           throws OutOfMemoryError
126   {
127     // Need to split
128     // ids of the form uniprot:25KD_SARPE;ADHR_DROPS;
129     String[] rslts = new String[0];
130     StringTokenizer queries = new StringTokenizer(ids, ";");
131     String db = null;
132     StringBuffer querystring = null;
133     int nq = 0;
134     while (queries.hasMoreTokens())
135     {
136       String query = queries.nextToken();
137       int p;
138       if ((p = query.indexOf(':')) > -1)
139       {
140         db = query.substring(0, p);
141         query = query.substring(p + 1);
142       }
143       if (querystring == null)
144       {
145         querystring = new StringBuffer(query);
146         nq++;
147       }
148       else
149       {
150         querystring.append("," + query);
151         nq++;
152       }
153     }
154     if (db == null)
155     {
156       System.err.println("Invalid Query string : '" + ids
157               + "'\nShould be of form 'dbname:q1;q2;q3;q4'");
158       return null;
159     }
160     String[] rslt = fetchBatch(querystring.toString(), db, f, s, outFile);
161     if (rslt != null)
162     {
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);
166       rslts = nrslts;
167     }
168
169     return (rslts.length == 0 ? null : rslts);
170   }
171
172   public String[] fetchBatch(String ids, String db, String f, String s,
173           File outFile) throws OutOfMemoryError
174   {
175     long time = System.currentTimeMillis();
176     // max 200 ids can be added at one time
177     try
178     {
179       URL rcall = new URL("http://www.ebi.ac.uk/Tools/dbfetch/dbfetch/"
180               + db.toLowerCase() + "/" + ids.toLowerCase()
181               + (f != null ? "/" + f : ""));
182
183       BufferedInputStream is = new BufferedInputStream(rcall.openStream());
184       if (outFile != null)
185       {
186         FileOutputStream fio = new FileOutputStream(outFile);
187         byte[] bb = new byte[32 * 1024];
188         int l;
189         while ((l = is.read(bb)) > 0)
190         {
191           fio.write(bb, 0, l);
192         }
193         fio.close();
194         is.close();
195       }
196       else
197       {
198         BufferedReader br = new BufferedReader(new InputStreamReader(is));
199         String rtn;
200         ArrayList<String> arl = new ArrayList<String>();
201         while ((rtn = br.readLine()) != null)
202         {
203           arl.add(rtn);
204         }
205         return arl.toArray(new String[arl.size()]);
206       }
207     } catch (OutOfMemoryError er)
208     {
209
210       System.out.println("OUT OF MEMORY DOWNLOADING QUERY FROM " + db
211               + ":\n" + ids);
212       throw er;
213     } catch (Exception ex)
214     {
215       if (ex.getMessage().startsWith(
216               "uk.ac.ebi.jdbfetch.exceptions.DbfNoEntryFoundException"))
217       {
218         return null;
219       }
220       System.err.println("Unexpected exception when retrieving from " + db
221               + "\nQuery was : '" + ids + "'");
222       ex.printStackTrace(System.err);
223       return null;
224     } finally
225     {
226       // System.err.println("Took " + (System.currentTimeMillis() - time)
227       // / 1000 + " secs for one call.");
228     }
229     return null;
230   }
231 }