1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
package jalview.ws.ebi; |
22 |
|
|
23 |
|
import java.io.BufferedReader; |
24 |
|
import java.io.File; |
25 |
|
import java.io.IOException; |
26 |
|
import java.io.InputStream; |
27 |
|
import java.io.InputStreamReader; |
28 |
|
import java.net.HttpURLConnection; |
29 |
|
import java.net.URL; |
30 |
|
import java.util.ArrayList; |
31 |
|
import java.util.List; |
32 |
|
import java.util.Locale; |
33 |
|
import java.util.StringTokenizer; |
34 |
|
|
35 |
|
import jalview.datamodel.DBRefSource; |
36 |
|
import jalview.util.HttpUtils; |
37 |
|
import jalview.util.MessageManager; |
38 |
|
import jalview.util.Platform; |
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
@author |
44 |
|
@version |
45 |
|
|
|
|
| 0% |
Uncovered Elements: 113 (113) |
Complexity: 33 |
Complexity Density: 0.45 |
|
46 |
|
public class EBIFetchClient |
47 |
|
{ |
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
52 |
0 |
public EBIFetchClient()... |
53 |
|
{ |
54 |
|
} |
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
@return |
60 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
61 |
0 |
public String[] getSupportedDBs()... |
62 |
|
{ |
63 |
|
|
64 |
0 |
throw new Error(MessageManager.getString("error.not_yet_implemented")); |
65 |
|
} |
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
@return |
71 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
72 |
0 |
public String[] getSupportedFormats()... |
73 |
|
{ |
74 |
|
|
75 |
0 |
throw new Error(MessageManager.getString("error.not_yet_implemented")); |
76 |
|
} |
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
@return |
82 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
83 |
0 |
public String[] getSupportedStyles()... |
84 |
|
{ |
85 |
|
|
86 |
0 |
throw new Error(MessageManager.getString("error.not_yet_implemented")); |
87 |
|
} |
88 |
|
|
89 |
|
|
90 |
|
|
91 |
|
|
92 |
|
@param |
93 |
|
|
94 |
|
@param |
95 |
|
|
96 |
|
@param |
97 |
|
|
98 |
|
@return |
99 |
|
@throws |
100 |
|
|
101 |
|
|
|
|
| 0% |
Uncovered Elements: 11 (11) |
Complexity: 3 |
Complexity Density: 0.33 |
|
102 |
0 |
public File fetchDataAsFile(String ids, String format, String ext)... |
103 |
|
throws OutOfMemoryError |
104 |
|
{ |
105 |
0 |
File outFile = null; |
106 |
0 |
try |
107 |
|
{ |
108 |
0 |
outFile = File.createTempFile("jalview", "." + ext); |
109 |
0 |
outFile.deleteOnExit(); |
110 |
0 |
fetchData(ids, format, outFile); |
111 |
0 |
if (outFile.length() == 0) |
112 |
|
{ |
113 |
0 |
outFile.delete(); |
114 |
0 |
return null; |
115 |
|
} |
116 |
|
} catch (Exception ex) |
117 |
|
{ |
118 |
|
} |
119 |
0 |
return outFile; |
120 |
|
} |
121 |
|
|
122 |
|
|
123 |
|
|
124 |
|
|
125 |
|
|
126 |
|
@param |
127 |
|
@param |
128 |
|
@param |
129 |
|
@return |
130 |
|
@throws |
131 |
|
|
|
|
| 0% |
Uncovered Elements: 12 (12) |
Complexity: 4 |
Complexity Density: 0.5 |
|
132 |
0 |
String[] fetchData(String ids, String format, File outFile)... |
133 |
|
throws OutOfMemoryError |
134 |
|
{ |
135 |
0 |
StringBuilder querystring = new StringBuilder(ids.length()); |
136 |
0 |
String database = parseIds(ids, querystring); |
137 |
0 |
if (database == null) |
138 |
|
{ |
139 |
0 |
jalview.bin.Console |
140 |
|
.errPrintln("Invalid Query string : '" + ids + "'"); |
141 |
0 |
jalview.bin.Console |
142 |
|
.errPrintln("Should be of form 'dbname:q1;q2;q3;q4'"); |
143 |
0 |
return null; |
144 |
|
} |
145 |
|
|
146 |
|
|
147 |
0 |
String[] rslt = fetchBatch(querystring.toString(), database, format, |
148 |
|
outFile); |
149 |
|
|
150 |
0 |
return (rslt != null && rslt.length > 0 ? rslt : null); |
151 |
|
} |
152 |
|
|
153 |
|
|
154 |
|
|
155 |
|
|
156 |
|
|
157 |
|
|
158 |
|
|
159 |
|
@param |
160 |
|
@param |
161 |
|
@return |
162 |
|
|
|
|
| 0% |
Uncovered Elements: 24 (24) |
Complexity: 6 |
Complexity Density: 0.38 |
|
163 |
0 |
static String parseIds(String ids, StringBuilder queryString)... |
164 |
|
{ |
165 |
0 |
String database = null; |
166 |
0 |
StringTokenizer queries = new StringTokenizer(ids, ";"); |
167 |
0 |
boolean appending = queryString.length() > 0; |
168 |
0 |
while (queries.hasMoreTokens()) |
169 |
|
{ |
170 |
0 |
String query = queries.nextToken(); |
171 |
0 |
int p = query.indexOf(':'); |
172 |
0 |
if (p > -1) |
173 |
|
{ |
174 |
0 |
String db = query.substring(0, p); |
175 |
0 |
if (database != null && !db.equalsIgnoreCase(database)) |
176 |
|
{ |
177 |
|
|
178 |
|
|
179 |
|
|
180 |
0 |
return null; |
181 |
|
} |
182 |
0 |
database = db; |
183 |
0 |
query = query.substring(p + 1); |
184 |
|
} |
185 |
0 |
queryString.append(appending ? "," : ""); |
186 |
0 |
queryString.append(query); |
187 |
0 |
appending = true; |
188 |
|
} |
189 |
0 |
return database; |
190 |
|
} |
191 |
|
|
192 |
|
|
193 |
|
|
194 |
|
|
195 |
|
|
196 |
|
@param |
197 |
|
@param |
198 |
|
@param |
199 |
|
@param |
200 |
|
@return |
201 |
|
|
202 |
|
@throws |
203 |
|
|
|
|
| 0% |
Uncovered Elements: 43 (43) |
Complexity: 11 |
Complexity Density: 0.35 |
|
204 |
0 |
String[] fetchBatch(String ids, String database, String format,... |
205 |
|
File outFile) throws OutOfMemoryError |
206 |
|
{ |
207 |
0 |
String url = buildUrl(ids, database, format); |
208 |
0 |
InputStream is = null; |
209 |
0 |
BufferedReader br = null; |
210 |
0 |
try |
211 |
|
{ |
212 |
0 |
URL rcall = new URL(url); |
213 |
0 |
HttpURLConnection conn = (HttpURLConnection) HttpUtils |
214 |
|
.openConnection(rcall); |
215 |
0 |
int responseCode = conn.getResponseCode(); |
216 |
0 |
if (responseCode == 200) |
217 |
|
{ |
218 |
0 |
is = conn.getInputStream(); |
219 |
0 |
if (outFile != null) |
220 |
|
{ |
221 |
0 |
Platform.streamToFile(is, outFile); |
222 |
0 |
return null; |
223 |
|
} |
224 |
0 |
br = new BufferedReader(new InputStreamReader(is)); |
225 |
0 |
String rtn; |
226 |
0 |
List<String> arl = new ArrayList<>(); |
227 |
0 |
while ((rtn = br.readLine()) != null) |
228 |
|
{ |
229 |
0 |
arl.add(rtn); |
230 |
|
} |
231 |
0 |
return (String[]) arl.toArray(); |
232 |
|
} |
233 |
0 |
jalview.bin.Console.errPrintln( |
234 |
|
"Warning: response code " + responseCode + " for " + url); |
235 |
|
} catch (OutOfMemoryError er) |
236 |
|
{ |
237 |
0 |
jalview.bin.Console.outPrintln("OUT OF MEMORY DOWNLOADING QUERY FROM " |
238 |
|
+ database + ":\n" + ids); |
239 |
0 |
throw er; |
240 |
|
} catch (Exception ex) |
241 |
|
{ |
242 |
0 |
if (!ex.getMessage().startsWith( |
243 |
|
"uk.ac.ebi.jdbfetch.exceptions.DbfNoEntryFoundException")) |
244 |
|
{ |
245 |
0 |
jalview.bin.Console |
246 |
|
.errPrintln("Unexpected exception when retrieving from " |
247 |
|
+ database + "\nQuery was : '" + ids + "'"); |
248 |
0 |
ex.printStackTrace(System.err); |
249 |
|
} |
250 |
|
} finally |
251 |
|
{ |
252 |
0 |
if (is != null) |
253 |
|
{ |
254 |
0 |
try |
255 |
|
{ |
256 |
0 |
is.close(); |
257 |
|
} catch (IOException e) |
258 |
|
{ |
259 |
|
} |
260 |
|
} |
261 |
0 |
if (br != null) |
262 |
|
{ |
263 |
0 |
try |
264 |
|
{ |
265 |
0 |
br.close(); |
266 |
|
} catch (IOException e) |
267 |
|
{ |
268 |
|
} |
269 |
|
} |
270 |
|
} |
271 |
0 |
return null; |
272 |
|
} |
273 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
274 |
0 |
static... |
275 |
|
{ |
276 |
0 |
Platform.addJ2SDirectDatabaseCall("https://www.ebi.ac.uk/"); |
277 |
|
} |
278 |
|
|
279 |
|
|
280 |
|
|
281 |
|
|
282 |
|
@param |
283 |
|
@param |
284 |
|
@param |
285 |
|
@return |
286 |
|
|
|
|
| 0% |
Uncovered Elements: 9 (9) |
Complexity: 4 |
Complexity Density: 0.8 |
|
287 |
0 |
static String buildUrl(String ids, String database, String format)... |
288 |
|
{ |
289 |
0 |
String url; |
290 |
0 |
if (database.equalsIgnoreCase(DBRefSource.EMBL) |
291 |
|
|| database.equalsIgnoreCase(DBRefSource.EMBLCDS)) |
292 |
|
{ |
293 |
0 |
url = "https://www.ebi.ac.uk/ena/browser/api/embl/" |
294 |
|
+ ids.toLowerCase(Locale.ROOT) + "?download=true&gzip=true"; |
295 |
|
} |
296 |
|
else |
297 |
|
{ |
298 |
0 |
url = "https://www.ebi.ac.uk/Tools/dbfetch/dbfetch/" |
299 |
|
+ database.toLowerCase(Locale.ROOT) + "/" |
300 |
|
+ ids.toLowerCase(Locale.ROOT) |
301 |
0 |
+ (format != null ? "/" + format : ""); |
302 |
|
} |
303 |
0 |
return url; |
304 |
|
} |
305 |
|
} |