2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
21 package jalview.ext.ensembl;
23 import jalview.io.FileParse;
24 import jalview.util.StringUtils;
26 import java.io.BufferedReader;
27 import java.io.DataOutputStream;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.io.InputStreamReader;
31 import java.net.HttpURLConnection;
32 import java.net.MalformedURLException;
34 import java.util.HashMap;
35 import java.util.List;
38 import javax.ws.rs.HttpMethod;
40 import org.json.simple.JSONArray;
41 import org.json.simple.JSONObject;
42 import org.json.simple.parser.JSONParser;
44 import com.stevesoft.pat.Regex;
47 * Base class for Ensembl REST service clients
51 abstract class EnsemblRestClient extends EnsemblSequenceFetcher
54 * update these constants when Jalview has been checked / updated for
55 * changes to Ensembl REST API
56 * @see https://github.com/Ensembl/ensembl-rest/wiki/Change-log
57 * @see http://rest.ensembl.org/info/rest?content-type=application/json
59 private static final String LATEST_ENSEMBLGENOMES_REST_VERSION = "4.6";
61 private static final String LATEST_ENSEMBL_REST_VERSION = "4.7";
63 private static final String REST_CHANGE_LOG = "https://github.com/Ensembl/ensembl-rest/wiki/Change-log";
65 private static Map<String, EnsemblInfo> domainData;
67 // @see https://github.com/Ensembl/ensembl-rest/wiki/Output-formats
68 private static final String PING_URL = "http://rest.ensembl.org/info/ping.json";
70 private final static long AVAILABILITY_RETEST_INTERVAL = 10000L; // 10 seconds
72 private final static long VERSION_RETEST_INTERVAL = 1000L * 3600; // 1 hr
74 private static final Regex TRANSCRIPT_REGEX = new Regex(
75 "(ENS)([A-Z]{3}|)T[0-9]{11}$");
77 private static final Regex GENE_REGEX = new Regex(
78 "(ENS)([A-Z]{3}|)G[0-9]{11}$");
82 domainData = new HashMap<String, EnsemblInfo>();
83 domainData.put(ENSEMBL_REST, new EnsemblInfo(ENSEMBL_REST,
84 LATEST_ENSEMBL_REST_VERSION));
85 domainData.put(ENSEMBL_GENOMES_REST, new EnsemblInfo(
86 ENSEMBL_GENOMES_REST, LATEST_ENSEMBLGENOMES_REST_VERSION));
89 protected volatile boolean inProgress = false;
92 * Default constructor to use rest.ensembl.org
94 public EnsemblRestClient()
100 * Constructor given the target domain to fetch data from
104 public EnsemblRestClient(String d)
110 * Answers true if the query matches the regular expression pattern for an
111 * Ensembl transcript stable identifier
116 public boolean isTranscriptIdentifier(String query)
118 return query == null ? false : TRANSCRIPT_REGEX.search(query);
122 * Answers true if the query matches the regular expression pattern for an
123 * Ensembl gene stable identifier
128 public boolean isGeneIdentifier(String query)
130 return query == null ? false : GENE_REGEX.search(query);
134 public boolean queryInProgress()
140 public StringBuffer getRawRecords()
146 * Returns the URL for the client http request
150 * @throws MalformedURLException
152 protected abstract URL getUrl(List<String> ids)
153 throws MalformedURLException;
156 * Returns true if client uses GET method, false if it uses POST
160 protected abstract boolean useGetRequest();
163 * Return the desired value for the Content-Type request header
168 * @see https://github.com/Ensembl/ensembl-rest/wiki/HTTP-Headers
170 protected abstract String getRequestMimeType(boolean multipleIds);
173 * Return the desired value for the Accept request header
176 * @see https://github.com/Ensembl/ensembl-rest/wiki/HTTP-Headers
178 protected abstract String getResponseMimeType();
181 * Checks Ensembl's REST 'ping' endpoint, and returns true if response
182 * indicates available, else false
184 * @see http://rest.ensembl.org/documentation/info/ping
187 private boolean checkEnsembl()
189 HttpURLConnection conn = null;
192 // note this format works for both ensembl and ensemblgenomes
193 // info/ping.json works for ensembl only (March 2016)
194 URL ping = new URL(getDomain()
195 + "/info/ping?content-type=application/json");
198 * expect {"ping":1} if ok
200 BufferedReader br = getHttpResponse(ping, null);
201 JSONParser jp = new JSONParser();
202 JSONObject val = (JSONObject) jp.parse(br);
203 String pingString = val.get("ping").toString();
204 return pingString != null;
205 } catch (Throwable t)
207 System.err.println("Error connecting to " + PING_URL + ": "
220 * returns a reader to a Fasta response from the Ensembl sequence endpoint
224 * @throws IOException
226 protected FileParse getSequenceReader(List<String> ids)
229 URL url = getUrl(ids);
231 BufferedReader reader = getHttpResponse(url, ids);
237 FileParse fp = new FileParse(reader, url.toString(), "HTTP_POST");
242 * Writes the HTTP request and gets the response as a reader.
246 * written as Json POST body if more than one
248 * @throws IOException
249 * if response code was not 200, or other I/O error
251 protected BufferedReader getHttpResponse(URL url, List<String> ids)
254 // long now = System.currentTimeMillis();
255 HttpURLConnection connection = (HttpURLConnection) url.openConnection();
258 * POST method allows multiple queries in one request; it is supported for
259 * sequence queries, but not for overlap
261 boolean multipleIds = ids != null && ids.size() > 1;
262 connection.setRequestMethod(multipleIds ? HttpMethod.POST
264 connection.setRequestProperty("Content-Type",
265 getRequestMimeType(multipleIds));
266 connection.setRequestProperty("Accept", getResponseMimeType());
268 connection.setUseCaches(false);
269 connection.setDoInput(true);
270 connection.setDoOutput(multipleIds);
274 writePostBody(connection, ids);
277 int responseCode = connection.getResponseCode();
279 if (responseCode != 200)
282 * note: a GET request for an invalid id returns an error code e.g. 415
283 * but POST request returns 200 and an empty Fasta response
285 System.err.println("Response code " + responseCode + " for " + url);
289 InputStream response = connection.getInputStream();
291 // System.out.println(getClass().getName() + " took "
292 // + (System.currentTimeMillis() - now) + "ms to fetch");
294 checkRateLimits(connection);
296 BufferedReader reader = null;
297 reader = new BufferedReader(new InputStreamReader(response, "UTF-8"));
302 * Inspect response headers for any sign of server overload and respect any
303 * 'retry-after' directive
305 * @see https://github.com/Ensembl/ensembl-rest/wiki/Rate-Limits
308 void checkRateLimits(HttpURLConnection connection)
310 // number of requests allowed per time interval:
311 String limit = connection.getHeaderField("X-RateLimit-Limit");
312 // length of quota time interval in seconds:
313 // String period = connection.getHeaderField("X-RateLimit-Period");
314 // seconds remaining until usage quota is reset:
315 String reset = connection.getHeaderField("X-RateLimit-Reset");
316 // number of requests remaining from quota for current period:
317 String remaining = connection.getHeaderField("X-RateLimit-Remaining");
318 // number of seconds to wait before retrying (if remaining == 0)
319 String retryDelay = connection.getHeaderField("Retry-After");
324 EnsemblInfo info = domainData.get(getDomain());
325 if (retryDelay != null)
327 System.err.println("Ensembl REST service rate limit exceeded, wait "
328 + retryDelay + " seconds before retrying");
331 info.retryAfter = System.currentTimeMillis()
332 + (1000 * Integer.valueOf(retryDelay));
333 } catch (NumberFormatException e)
335 System.err.println("Unexpected value for Retry-After: "
343 // System.out.println(String.format(
344 // "%s Ensembl requests remaining of %s (reset in %ss)",
345 // remaining, limit, reset));
350 * Rechecks if Ensembl is responding, unless the last check was successful and
351 * the retest interval has not yet elapsed. Returns true if Ensembl is up,
352 * else false. Also retrieves and saves the current version of Ensembl data
353 * and REST services at intervals.
357 protected boolean isEnsemblAvailable()
359 EnsemblInfo info = domainData.get(getDomain());
361 long now = System.currentTimeMillis();
364 * check if we are waiting for 'Retry-After' to expire
366 if (info.retryAfter > now)
368 System.err.println("Still " + (1 + (info.retryAfter - now) / 1000)
369 + " secs to wait before retrying Ensembl");
378 * recheck if Ensembl is up if it was down, or the recheck period has elapsed
380 boolean retestAvailability = (now - info.lastAvailableCheckTime) > AVAILABILITY_RETEST_INTERVAL;
381 if (!info.restAvailable || retestAvailability)
383 info.restAvailable = checkEnsembl();
384 info.lastAvailableCheckTime = now;
388 * refetch Ensembl versions if the recheck period has elapsed
390 boolean refetchVersion = (now - info.lastVersionCheckTime) > VERSION_RETEST_INTERVAL;
393 checkEnsemblRestVersion();
394 checkEnsemblDataVersion();
395 info.lastVersionCheckTime = now;
398 return info.restAvailable;
402 * Constructs, writes and flushes the POST body of the request, containing the
403 * query ids in JSON format
407 * @throws IOException
409 protected void writePostBody(HttpURLConnection connection,
410 List<String> ids) throws IOException
413 StringBuilder postBody = new StringBuilder(64);
414 postBody.append("{\"ids\":[");
416 for (String id : ids)
420 postBody.append(",");
423 postBody.append("\"");
424 postBody.append(id.trim());
425 postBody.append("\"");
427 postBody.append("]}");
428 byte[] thepostbody = postBody.toString().getBytes();
429 connection.setRequestProperty("Content-Length",
430 Integer.toString(thepostbody.length));
431 DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
432 wr.write(thepostbody);
438 * Fetches and checks Ensembl's REST version number
442 private void checkEnsemblRestVersion()
444 EnsemblInfo info = domainData.get(getDomain());
446 JSONParser jp = new JSONParser();
450 url = new URL(getDomain()
451 + "/info/rest?content-type=application/json");
452 BufferedReader br = getHttpResponse(url, null);
453 JSONObject val = (JSONObject) jp.parse(br);
454 String version = val.get("release").toString();
455 String majorVersion = version.substring(0, version.indexOf("."));
456 String expected = info.expectedRestVersion;
457 String expectedMajorVersion = expected.substring(0,
458 expected.indexOf("."));
459 info.restMajorVersionMismatch = false;
463 * if actual REST major version is ahead of what we expect,
464 * record this in case we want to warn the user
466 if (Float.valueOf(majorVersion) > Float
467 .valueOf(expectedMajorVersion))
469 info.restMajorVersionMismatch = true;
471 } catch (NumberFormatException e)
473 System.err.println("Error in REST version: " + e.toString());
477 * check if REST version is later than what Jalview has tested against,
478 * if so warn; we don't worry if it is earlier (this indicates Jalview has
479 * been tested in advance against the next pending REST version)
481 boolean laterVersion = StringUtils.compareVersions(version, expected) == 1;
484 System.err.println(String.format(
485 "Expected %s REST version %s but found %s, see %s",
486 getDbSource(), expected, version, REST_CHANGE_LOG));
488 info.restVersion = version;
489 } catch (Throwable t)
491 System.err.println("Error checking Ensembl REST version: "
496 public boolean isRestMajorVersionMismatch()
498 return domainData.get(getDomain()).restMajorVersionMismatch;
502 * Fetches and checks Ensembl's data version number
506 private void checkEnsemblDataVersion()
508 JSONParser jp = new JSONParser();
512 url = new URL(getDomain()
513 + "/info/data?content-type=application/json");
514 BufferedReader br = getHttpResponse(url, null);
515 JSONObject val = (JSONObject) jp.parse(br);
516 JSONArray versions = (JSONArray) val.get("releases");
517 domainData.get(getDomain()).dataVersion = versions.get(0).toString();
518 } catch (Throwable t)
520 System.err.println("Error checking Ensembl data version: "
525 public String getEnsemblDataVersion()
527 return domainData.get(getDomain()).dataVersion;
531 public String getDbVersion()
533 return getEnsemblDataVersion();