Clover icon

Coverage Report

  1. Project Clover database Thu Aug 13 2020 12:04:21 BST
  2. Package jalview.ext.ensembl

File EnsemblRestClient.java

 

Coverage histogram

../../../img/srcFileCovDistChart1.png
52% of files have more coverage

Code metrics

48
144
20
1
636
353
56
0.39
7.2
20
2.8

Classes

Class Line # Actions
EnsemblRestClient 47 144 56
0.0707547147.1%
 

Contributing tests

This file is covered by 26 tests. .

Source view

1    /*
2    * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3    * Copyright (C) $$Year-Rel$$ 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.ext.ensembl;
22   
23    import java.io.BufferedReader;
24    import java.io.DataOutputStream;
25    import java.io.IOException;
26    import java.io.InputStream;
27    import java.net.HttpURLConnection;
28    import java.net.MalformedURLException;
29    import java.net.ProtocolException;
30    import java.net.URL;
31    import java.util.HashMap;
32    import java.util.List;
33    import java.util.Map;
34   
35    import javax.ws.rs.HttpMethod;
36   
37    import org.json.simple.parser.ParseException;
38   
39    import jalview.util.Platform;
40    import jalview.util.StringUtils;
41   
42    /**
43    * Base class for Ensembl REST service clients
44    *
45    * @author gmcarstairs
46    */
 
47    abstract class EnsemblRestClient extends EnsemblSequenceFetcher
48    {
49   
 
50  1 toggle static
51    {
52  1 Platform.addJ2SDirectDatabaseCall("http://rest.ensembl");
53  1 Platform.addJ2SDirectDatabaseCall("https://rest.ensembl");
54    }
55   
56    private static final int DEFAULT_READ_TIMEOUT = 5 * 60 * 1000; // 5 minutes
57   
58    private static final int CONNECT_TIMEOUT_MS = 10 * 1000; // 10 seconds
59   
60    private static final int MAX_RETRIES = 3;
61   
62    private static final int HTTP_OK = 200;
63   
64    private static final int HTTP_OVERLOAD = 429;
65   
66    /*
67    * update these constants when Jalview has been checked / updated for
68    * changes to Ensembl REST API, and updated JAL-3018
69    * @see https://github.com/Ensembl/ensembl-rest/wiki/Change-log
70    * @see http://rest.ensembl.org/info/rest?content-type=application/json
71    */
72    private static final String LATEST_ENSEMBLGENOMES_REST_VERSION = "10.0";
73   
74    private static final String LATEST_ENSEMBL_REST_VERSION = "10.0";
75   
76    private static final String REST_CHANGE_LOG = "https://github.com/Ensembl/ensembl-rest/wiki/Change-log";
77   
78    private static Map<String, EnsemblData> domainData;
79   
80    private final static long AVAILABILITY_RETEST_INTERVAL = 10000L; // 10 seconds
81   
82    private final static long VERSION_RETEST_INTERVAL = 1000L * 3600; // 1 hr
83   
84    protected static final String CONTENT_TYPE_JSON = "?content-type=application/json";
85   
 
86  1 toggle static
87    {
88  1 domainData = new HashMap<>();
89  1 domainData.put(DEFAULT_ENSEMBL_BASEURL, new EnsemblData(
90    DEFAULT_ENSEMBL_BASEURL, LATEST_ENSEMBL_REST_VERSION));
91  1 domainData.put(DEFAULT_ENSEMBL_GENOMES_BASEURL,
92    new EnsemblData(DEFAULT_ENSEMBL_GENOMES_BASEURL,
93    LATEST_ENSEMBLGENOMES_REST_VERSION));
94    }
95   
96    protected volatile boolean inProgress = false;
97   
98    /**
99    * Default constructor to use rest.ensembl.org
100    */
 
101  32 toggle public EnsemblRestClient()
102    {
103  32 super();
104   
105    /*
106    * initialise domain info lazily
107    */
108  32 if (!domainData.containsKey(ensemblDomain))
109    {
110  0 domainData.put(ensemblDomain,
111    new EnsemblData(ensemblDomain, LATEST_ENSEMBL_REST_VERSION));
112    }
113  32 if (!domainData.containsKey(ensemblGenomesDomain))
114    {
115  0 domainData.put(ensemblGenomesDomain, new EnsemblData(
116    ensemblGenomesDomain, LATEST_ENSEMBLGENOMES_REST_VERSION));
117    }
118    }
119   
120    /**
121    * Constructor given the target domain to fetch data from
122    *
123    * @param d
124    */
 
125  1 toggle public EnsemblRestClient(String d)
126    {
127  1 setDomain(d);
128    }
129   
 
130  0 toggle @Override
131    public boolean queryInProgress()
132    {
133  0 return inProgress;
134    }
135   
 
136  0 toggle @Override
137    public StringBuffer getRawRecords()
138    {
139  0 return null;
140    }
141   
142    /**
143    * Returns the URL for the client http request
144    *
145    * @param ids
146    * @return
147    * @throws MalformedURLException
148    */
149    protected abstract URL getUrl(List<String> ids)
150    throws MalformedURLException;
151   
152    /**
153    * Returns true if client uses GET method, false if it uses POST
154    *
155    * @return
156    */
157    protected abstract boolean useGetRequest();
158   
159    /**
160    * Returns the desired value for the Content-Type request header. Default is
161    * application/json, override if required to vary this.
162    *
163    * @return
164    * @see https://github.com/Ensembl/ensembl-rest/wiki/HTTP-Headers
165    */
 
166  0 toggle protected String getRequestMimeType()
167    {
168  0 return "application/json";
169    }
170   
171    /**
172    * Return the desired value for the Accept request header. Default is
173    * application/json, override if required to vary this.
174    *
175    * @return
176    * @see https://github.com/Ensembl/ensembl-rest/wiki/HTTP-Headers
177    */
 
178  0 toggle protected String getResponseMimeType()
179    {
180  0 return "application/json";
181    }
182   
183    /**
184    * Checks Ensembl's REST 'ping' endpoint, and returns true if response
185    * indicates available, else false
186    *
187    * @see http://rest.ensembl.org/documentation/info/ping
188    * @return
189    */
 
190  0 toggle @SuppressWarnings("unchecked")
191    boolean checkEnsembl()
192    {
193  0 BufferedReader br = null;
194  0 String pingUrl = getDomain() + "/info/ping" + CONTENT_TYPE_JSON;
195  0 try
196    {
197    // note this format works for both ensembl and ensemblgenomes
198    // info/ping.json works for ensembl only (March 2016)
199   
200    /*
201    * expect {"ping":1} if ok
202    * if ping takes more than 2 seconds to respond, treat as if unavailable
203    */
204  0 Map<String, Object> val = (Map<String, Object>) getJSON(
205    new URL(pingUrl), null, 2 * 1000, MODE_MAP, null);
206  0 if (val == null)
207    {
208  0 return false;
209    }
210  0 String pingString = val.get("ping").toString();
211  0 return pingString != null;
212    } catch (Throwable t)
213    {
214  0 System.err.println(
215    "Error connecting to " + pingUrl + ": " + t.getMessage());
216    } finally
217    {
218  0 if (br != null)
219    {
220  0 try
221    {
222  0 br.close();
223    } catch (IOException e)
224    {
225    // ignore
226    }
227    }
228    }
229  0 return false;
230    }
231   
232    protected final static int MODE_ARRAY = 0;
233   
234    protected final static int MODE_MAP = 1;
235   
236    protected final static int MODE_ITERATOR = 2;
237   
238    // /**
239    // * Returns a reader to a (Json) response from the Ensembl sequence endpoint.
240    // * If the request failed the return value may be null.
241    // *
242    // * @param ids
243    // * @return
244    // * @throws IOException
245    // * @throws ParseException
246    // */
247    // protected Object getSequenceJSON(List<String> ids, int mode)
248    // throws IOException, ParseException
249    // {
250    // URL url = getUrl(ids);
251    // return getJSON(url, ids, -1, mode);
252    // }
253    //
254    // /**
255    // * Gets a reader to the HTTP response, using the default read timeout of 5
256    // * minutes
257    // *
258    // * @param url
259    // * @param ids
260    // * @return
261    // * @throws IOException
262    // */
263    // protected BufferedReader getHttpResponse(URL url, List<String> ids)
264    // throws IOException
265    // {
266    // return getHttpResponse(url, ids, DEFAULT_READ_TIMEOUT);
267    // }
268   
269    /**
270    * Sends the HTTP request and gets the response as a reader. Returns null if
271    * the HTTP response code was not 200.
272    *
273    * @param url
274    * @param ids
275    * written as Json POST body if more than one
276    * @param readTimeout
277    * in milliseconds
278    * @return
279    * @throws IOException
280    * @throws ParseException
281    */
 
282  0 toggle private Object getJSON(URL url, List<String> ids, int readTimeout)
283    throws IOException, ParseException
284    {
285   
286  0 if (readTimeout < 0)
287    {
288  0 readTimeout = DEFAULT_READ_TIMEOUT;
289    }
290  0 int retriesLeft = MAX_RETRIES;
291  0 HttpURLConnection connection = null;
292  0 int responseCode = 0;
293   
294  0 Platform.setAjaxJSON(url);
295   
296  0 while (retriesLeft > 0)
297    {
298  0 connection = tryConnection(url, ids, readTimeout);
299  0 responseCode = connection.getResponseCode();
300  0 if (responseCode == HTTP_OVERLOAD) // 429
301    {
302  0 retriesLeft--;
303  0 checkRetryAfter(connection);
304    }
305    else
306    {
307  0 retriesLeft = 0;
308    }
309    }
310  0 if (responseCode != HTTP_OK) // 200
311    {
312    /*
313    * note: a GET request for an invalid id returns an error code e.g. 415
314    * but POST request returns 200 and an empty Fasta response
315    */
316  0 System.err.println("Response code " + responseCode);// + " for " + url);
317  0 return null;
318    }
319   
320  0 InputStream response = connection.getInputStream();
321   
322    // Platform.timeCheck(null, Platform.TIME_MARK);
323  0 Object ret = Platform.parseJSON(response);
324    // Platform.timeCheck("EnsemblRestClient.getJSON " + url,
325    // Platform.TIME_MARK);
326   
327  0 return ret;
328    }
329   
330    /**
331    * @param url
332    * @param ids
333    * @param readTimeout
334    * @return
335    * @throws IOException
336    * @throws ProtocolException
337    */
 
338  0 toggle protected HttpURLConnection tryConnection(URL url, List<String> ids,
339    int readTimeout) throws IOException, ProtocolException
340    {
341    // System.out.println(System.currentTimeMillis() + " " + url);
342   
343  0 HttpURLConnection connection = (HttpURLConnection) url.openConnection();
344   
345    /*
346    * POST method allows multiple queries in one request; it is supported for
347    * sequence queries, but not for overlap
348    */
349  0 boolean multipleIds = ids != null && ids.size() > 1;
350  0 connection.setRequestMethod(
351  0 multipleIds ? HttpMethod.POST : HttpMethod.GET);
352  0 connection.setRequestProperty("Content-Type", getRequestMimeType());
353  0 connection.setRequestProperty("Accept", getResponseMimeType());
354   
355  0 connection.setDoInput(true);
356  0 connection.setDoOutput(multipleIds);
357   
358  0 connection.setUseCaches(false);
359  0 connection.setConnectTimeout(CONNECT_TIMEOUT_MS);
360  0 connection.setReadTimeout(readTimeout);
361   
362  0 if (multipleIds)
363    {
364  0 writePostBody(connection, ids);
365    }
366  0 return connection;
367    }
368   
369    /**
370    * Inspects response headers for a 'retry-after' directive, and waits for the
371    * directed period (if less than 10 seconds)
372    *
373    * @see https://github.com/Ensembl/ensembl-rest/wiki/Rate-Limits
374    * @param connection
375    */
 
376  0 toggle void checkRetryAfter(HttpURLConnection connection)
377    {
378  0 String retryDelay = connection.getHeaderField("Retry-After");
379   
380    // to test:
381    // retryDelay = "5";
382   
383  0 if (retryDelay != null)
384    {
385  0 try
386    {
387  0 int retrySecs = Integer.valueOf(retryDelay);
388  0 if (retrySecs > 0 && retrySecs < 10)
389    {
390  0 System.err.println(
391    "Ensembl REST service rate limit exceeded, waiting "
392    + retryDelay + " seconds before retrying");
393  0 Thread.sleep(1000 * retrySecs);
394    }
395    } catch (NumberFormatException | InterruptedException e)
396    {
397  0 System.err.println("Error handling Retry-After: " + e.getMessage());
398    }
399    }
400    }
401   
402    /**
403    * Rechecks if Ensembl is responding, unless the last check was successful and
404    * the retest interval has not yet elapsed. Returns true if Ensembl is up,
405    * else false. Also retrieves and saves the current version of Ensembl data
406    * and REST services at intervals.
407    *
408    * @return
409    */
 
410  0 toggle protected boolean isEnsemblAvailable()
411    {
412  0 EnsemblData info = domainData.get(getDomain());
413   
414  0 long now = System.currentTimeMillis();
415   
416    /*
417    * recheck if Ensembl is up if it was down, or the recheck period has elapsed
418    */
419  0 boolean retestAvailability = (now
420    - info.lastAvailableCheckTime) > AVAILABILITY_RETEST_INTERVAL;
421  0 if (!info.restAvailable || retestAvailability)
422    {
423  0 info.restAvailable = checkEnsembl();
424  0 info.lastAvailableCheckTime = now;
425    }
426   
427    /*
428    * refetch Ensembl versions if the recheck period has elapsed
429    */
430  0 boolean refetchVersion = (now
431    - info.lastVersionCheckTime) > VERSION_RETEST_INTERVAL;
432  0 if (refetchVersion)
433    {
434  0 checkEnsemblRestVersion();
435  0 checkEnsemblDataVersion();
436  0 info.lastVersionCheckTime = now;
437    }
438   
439  0 return info.restAvailable;
440    }
441   
442    /**
443    * Constructs, writes and flushes the POST body of the request, containing the
444    * query ids in JSON format
445    *
446    * @param connection
447    * @param ids
448    * @throws IOException
449    */
 
450  0 toggle protected void writePostBody(HttpURLConnection connection,
451    List<String> ids) throws IOException
452    {
453  0 boolean first;
454  0 StringBuilder postBody = new StringBuilder(64);
455  0 postBody.append("{\"ids\":[");
456  0 first = true;
457  0 for (int i = 0, n = ids.size(); i < n; i++)
458    {
459  0 String id = ids.get(i);
460  0 if (!first)
461    {
462  0 postBody.append(",");
463    }
464  0 first = false;
465  0 postBody.append("\"");
466  0 postBody.append(id.trim());
467  0 postBody.append("\"");
468    }
469  0 postBody.append("]}");
470  0 byte[] thepostbody = postBody.toString().getBytes();
471  0 connection.setRequestProperty("Content-Length",
472    Integer.toString(thepostbody.length));
473  0 DataOutputStream wr = new DataOutputStream(
474    connection.getOutputStream());
475  0 wr.write(thepostbody);
476  0 wr.flush();
477  0 wr.close();
478    }
479   
480    /**
481    * Primary access point to parsed JSON data, including the call to retrieve
482    * and parsing.
483    *
484    * @param url
485    * request url; if null, getUrl(ids) will be used
486    * @param ids
487    * optional; may be null
488    * @param msDelay
489    * -1 for default delay
490    * @param mode
491    * map, array, or array iterator
492    * @param mapKey
493    * an optional key for an outer map
494    * @return a Map, List, Iterator, or null
495    * @throws IOException
496    * @throws ParseException
497    *
498    * @author Bob Hanson 2019
499    */
 
500  0 toggle @SuppressWarnings("unchecked")
501    protected Object getJSON(URL url, List<String> ids, int msDelay, int mode,
502    String mapKey) throws IOException, ParseException
503    {
504  0 if (url == null)
505    {
506  0 url = getUrl(ids);
507    }
508   
509  0 Object json = (url == null ? null : getJSON(url, ids, msDelay));
510   
511  0 if (json != null && mapKey != null)
512    {
513  0 json = ((Map<String, Object>) json).get(mapKey);
514    }
515  0 if (json == null)
516    {
517  0 return null;
518    }
519  0 switch (mode)
520    {
521  0 case MODE_ARRAY:
522  0 case MODE_MAP:
523  0 break;
524  0 case MODE_ITERATOR:
525  0 json = ((List<Object>) json).iterator();
526  0 break;
527    }
528  0 return json;
529    }
530   
531    /**
532    * Fetches and checks Ensembl's REST version number
533    *
534    * @return
535    */
 
536  0 toggle @SuppressWarnings("unchecked")
537    private void checkEnsemblRestVersion()
538    {
539  0 EnsemblData info = domainData.get(getDomain());
540   
541  0 try
542    {
543  0 Map<String, Object> val = (Map<String, Object>) getJSON(
544    new URL(getDomain() + "/info/rest" + CONTENT_TYPE_JSON), null,
545    -1, MODE_MAP, null);
546  0 if (val == null)
547    {
548  0 return;
549    }
550  0 String version = val.get("release").toString();
551  0 String majorVersion = version.substring(0, version.indexOf("."));
552  0 String expected = info.expectedRestVersion;
553  0 String expectedMajorVersion = expected.substring(0,
554    expected.indexOf("."));
555  0 info.restMajorVersionMismatch = false;
556  0 try
557    {
558    /*
559    * if actual REST major version is ahead of what we expect,
560    * record this in case we want to warn the user
561    */
562  0 if (Float.valueOf(majorVersion) > Float
563    .valueOf(expectedMajorVersion))
564    {
565  0 info.restMajorVersionMismatch = true;
566    }
567    } catch (NumberFormatException e)
568    {
569  0 System.err.println("Error in REST version: " + e.toString());
570    }
571   
572    /*
573    * check if REST version is later than what Jalview has tested against,
574    * if so warn; we don't worry if it is earlier (this indicates Jalview has
575    * been tested in advance against the next pending REST version)
576    */
577  0 boolean laterVersion = StringUtils.compareVersions(version,
578    expected) == 1;
579  0 if (laterVersion)
580    {
581  0 System.err.println(String.format(
582    "EnsemblRestClient expected %s REST version %s but found %s, see %s",
583    getDbSource(), expected, version, REST_CHANGE_LOG));
584    }
585  0 info.restVersion = version;
586    } catch (Throwable t)
587    {
588  0 System.err.println(
589    "Error checking Ensembl REST version: " + t.getMessage());
590    }
591    }
592   
 
593  0 toggle public boolean isRestMajorVersionMismatch()
594    {
595  0 return domainData.get(getDomain()).restMajorVersionMismatch;
596    }
597   
598    /**
599    * Fetches and checks Ensembl's data version number
600    *
601    * @return
602    */
 
603  0 toggle @SuppressWarnings("unchecked")
604    private void checkEnsemblDataVersion()
605    {
606  0 Map<String, Object> val;
607  0 try
608    {
609  0 val = (Map<String, Object>) getJSON(
610    new URL(getDomain() + "/info/data" + CONTENT_TYPE_JSON), null,
611    -1, MODE_MAP, null);
612  0 if (val == null)
613    {
614  0 return;
615    }
616  0 List<Object> versions = (List<Object>) val.get("releases");
617  0 domainData.get(getDomain()).dataVersion = versions.get(0).toString();
618    } catch (Throwable e)
619    {// could be IOException | ParseException e) {
620  0 System.err.println(
621    "Error checking Ensembl data version: " + e.getMessage());
622    }
623    }
624   
 
625  0 toggle public String getEnsemblDataVersion()
626    {
627  0 return domainData.get(getDomain()).dataVersion;
628    }
629   
 
630  0 toggle @Override
631    public String getDbVersion()
632    {
633  0 return getEnsemblDataVersion();
634    }
635   
636    }