Search in sources :

Example 1 with EnsemblRestIOException

use of uk.ac.ebi.spot.goci.exception.EnsemblRestIOException in project goci by EBISPOT.

the class EnsemblRelease method getReleaseVersion.

/**
     * Getter for the release version
     *
     * @return the numeric release version
     */
public int getReleaseVersion() throws EnsemblRestIOException {
    RestTemplate restTemplate = new RestTemplate();
    String url = getServer() + getEndpoint();
    int currentEnsemblRelease = 0;
    try {
        EnsemblReleaseJson ensemblReleaseJson = restTemplate.getForObject(url, EnsemblReleaseJson.class);
        getLog().info("Querying " + url);
        int[] releases = ensemblReleaseJson.getReleases();
        if (releases != null) {
            if (releases.length == 1) {
                currentEnsemblRelease = releases[0];
            } else {
                throw new EnsemblRestIOException("Unable to determine Ensembl release");
            }
        } else {
            throw new EnsemblRestIOException("No Ensembl release information returned from API");
        }
    } catch (Exception e) {
        throw new EnsemblRestIOException("Problem querying Ensembl API for release");
    }
    return currentEnsemblRelease;
}
Also used : RestTemplate(org.springframework.web.client.RestTemplate) EnsemblRestIOException(uk.ac.ebi.spot.goci.exception.EnsemblRestIOException) EnsemblRestIOException(uk.ac.ebi.spot.goci.exception.EnsemblRestIOException) EnsemblReleaseJson(uk.ac.ebi.spot.goci.model.EnsemblReleaseJson)

Example 2 with EnsemblRestIOException

use of uk.ac.ebi.spot.goci.exception.EnsemblRestIOException in project goci by EBISPOT.

the class EnsemblRestService method fetchJson.

/**
     * Fetch response from API
     *
     * @param url URL to query
     * @return the corresponding result
     */
private RestResponseResult fetchJson(String url) throws UnirestException, InterruptedException, EnsemblRestIOException {
    RestResponseResult restResponseResult = new RestResponseResult();
    restResponseResult.setUrl(url);
    // Parameters to monitor API call success
    int maxTries = 5;
    int tries = 0;
    int wait = 1;
    boolean success = false;
    while (!success && tries < maxTries) {
        tries++;
        try {
            getLog().trace("Querying URL: " + url);
            HttpResponse<JsonNode> response = Unirest.get(url).header("Content-Type", "application/json").asJson();
            String retryHeader = response.getHeaders().getFirst("Retry-After");
            getLog().trace("URL response: " + response.getStatus());
            if (response.getStatus() == 200) {
                // Success
                success = true;
            //restResponseResult.setRestResult(response.getBody());
            } else if (response.getStatus() == 429 && retryHeader != null) {
                // Too Many Requests
                Long waitSeconds = Long.valueOf(retryHeader);
                Thread.sleep(waitSeconds * 1000);
            } else {
                if (response.getStatus() == 503) {
                    // Service unavailable
                    restResponseResult.setError("No server is available to handle this request (Error 503: service unavailable) at url: " + url);
                    getLog().error("No server is available to handle this request (Error 503: service unavailable) at url: " + url);
                    throw new EnsemblRestIOException("No server is available to handle this request (Error 503: service unavailable) at url: " + url);
                } else if (response.getStatus() == 400) {
                    // Bad request (no result found)
                    JSONObject json_obj = response.getBody().getObject();
                    if (json_obj.has("error")) {
                        restResponseResult.setError(json_obj.getString("error"));
                    }
                    getLog().error(url + " is generating an invalid request. (Error 400: bad request)");
                    // Success is set to true here as the API call was successful
                    // but the gene or snp does not exist in Ensembl
                    success = true;
                } else {
                    // Other issue
                    restResponseResult.setError("No data available at url " + url);
                    getLog().error("No data at " + url);
                    throw new EnsemblRestIOException("No data available at url " + url);
                }
            }
        } catch (UnirestException e) {
            getLog().error("Caught exception from Ensembl Rest call, this call will be retried after " + wait + "s.", e);
            Thread.sleep(wait * 1000);
        }
    }
    if (success) {
        return restResponseResult;
    } else {
        getLog().error("Failed to obtain a result from from '" + url + "' after after " + maxTries + " attempts");
        throw new EnsemblRestIOException("Failed to obtain a result from '" + url + "' after " + maxTries + " attempts");
    }
}
Also used : JSONObject(org.json.JSONObject) UnirestException(com.mashape.unirest.http.exceptions.UnirestException) RestResponseResult(uk.ac.ebi.spot.goci.model.RestResponseResult) JsonNode(com.mashape.unirest.http.JsonNode) EnsemblRestIOException(uk.ac.ebi.spot.goci.exception.EnsemblRestIOException)

Example 3 with EnsemblRestIOException

use of uk.ac.ebi.spot.goci.exception.EnsemblRestIOException in project goci by EBISPOT.

the class EnsemblRestService method getRestCall.

/**
     * Simple generic Ensembl REST API call method.
     *
     * @param endpoint_type   the endpoint name
     * @param data            the data/id/symbol we want to query
     * @param rest_parameters rest parameters
     * @return the corresponding result
     */
public RestResponseResult getRestCall(String endpoint_type, String data, String rest_parameters) throws EnsemblRestIOException {
    String endpoint = getEndpoints().get(endpoint_type);
    URL url = null;
    RestResponseResult restResponseResult = new RestResponseResult();
    try {
        rateLimit();
        // Build URL
        if (!Objects.equals(rest_parameters, "")) {
            Matcher matcher = Pattern.compile("^\\?").matcher(rest_parameters);
            if (!matcher.matches()) {
                rest_parameters = '?' + rest_parameters;
            }
        }
        url = new URL(getServer() + endpoint + data + rest_parameters);
        restResponseResult = fetchJson(url.toString());
    } catch (InterruptedException | MalformedURLException | UnirestException e) {
        getLog().error("Encountered a " + e.getClass().getSimpleName() + " whilst trying to run mapping of SNP", e);
        throw new EnsemblRestIOException("Encountered a " + e.getClass().getSimpleName() + " whilst trying to run mapping of SNP", e);
    }
    return restResponseResult;
}
Also used : MalformedURLException(java.net.MalformedURLException) Matcher(java.util.regex.Matcher) UnirestException(com.mashape.unirest.http.exceptions.UnirestException) RestResponseResult(uk.ac.ebi.spot.goci.model.RestResponseResult) EnsemblRestIOException(uk.ac.ebi.spot.goci.exception.EnsemblRestIOException) URL(java.net.URL)

Example 4 with EnsemblRestIOException

use of uk.ac.ebi.spot.goci.exception.EnsemblRestIOException in project goci by EBISPOT.

the class EnsemblDbsnpVersion method getDbsnpVersion.

/**
     * Getter for the dbSNP version
     *
     * @return the dbSNP version
     */
public int getDbsnpVersion() throws EnsemblRestIOException {
    RestTemplate restTemplate = new RestTemplate();
    String url = getServer() + getEndpoint();
    String version = "";
    try {
        getLog().info("Querying " + url);
        //Ensembl returns an array data structure for this call
        // Have to do some wrangling to get dbSNP version
        EnsemblDbsnpVersionJson[] response = restTemplate.getForObject(url, EnsemblDbsnpVersionJson[].class);
        version = response[0].getVersion();
        if (version.isEmpty()) {
            throw new EnsemblRestIOException("Unable to determine Ensembl dbSNP version");
        }
    } catch (Exception e) {
        throw new EnsemblRestIOException("Problem querying Ensembl API for dbSNP version");
    }
    return Integer.parseInt(version);
}
Also used : RestTemplate(org.springframework.web.client.RestTemplate) EnsemblDbsnpVersionJson(uk.ac.ebi.spot.goci.model.EnsemblDbsnpVersionJson) EnsemblRestIOException(uk.ac.ebi.spot.goci.exception.EnsemblRestIOException) EnsemblRestIOException(uk.ac.ebi.spot.goci.exception.EnsemblRestIOException)

Example 5 with EnsemblRestIOException

use of uk.ac.ebi.spot.goci.exception.EnsemblRestIOException in project goci by EBISPOT.

the class EnsemblGenomeBuildVersion method getGenomeBuildVersion.

/**
     * Getter for the genome build version
     *
     * @return the genome build version
     */
public String getGenomeBuildVersion() throws EnsemblRestIOException {
    RestTemplate restTemplate = new RestTemplate();
    String url = getServer() + getEndpoint();
    String assemblyName = "";
    try {
        EnsemblGenomeBuildVersionJson ensemblGenomeBuildVersionJson = restTemplate.getForObject(url, EnsemblGenomeBuildVersionJson.class);
        getLog().info("Querying " + url);
        assemblyName = ensemblGenomeBuildVersionJson.getAssembly_name();
        if (assemblyName.isEmpty()) {
            throw new EnsemblRestIOException("Unable to determine Ensembl genome build version");
        }
    } catch (Exception e) {
        throw new EnsemblRestIOException("Problem querying Ensembl API for genome build version");
    }
    return assemblyName;
}
Also used : EnsemblGenomeBuildVersionJson(uk.ac.ebi.spot.goci.model.EnsemblGenomeBuildVersionJson) RestTemplate(org.springframework.web.client.RestTemplate) EnsemblRestIOException(uk.ac.ebi.spot.goci.exception.EnsemblRestIOException) EnsemblRestIOException(uk.ac.ebi.spot.goci.exception.EnsemblRestIOException)

Aggregations

EnsemblRestIOException (uk.ac.ebi.spot.goci.exception.EnsemblRestIOException)5 RestTemplate (org.springframework.web.client.RestTemplate)3 UnirestException (com.mashape.unirest.http.exceptions.UnirestException)2 RestResponseResult (uk.ac.ebi.spot.goci.model.RestResponseResult)2 JsonNode (com.mashape.unirest.http.JsonNode)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 Matcher (java.util.regex.Matcher)1 JSONObject (org.json.JSONObject)1 EnsemblDbsnpVersionJson (uk.ac.ebi.spot.goci.model.EnsemblDbsnpVersionJson)1 EnsemblGenomeBuildVersionJson (uk.ac.ebi.spot.goci.model.EnsemblGenomeBuildVersionJson)1 EnsemblReleaseJson (uk.ac.ebi.spot.goci.model.EnsemblReleaseJson)1