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