Search in sources :

Example 11 with RestResponseResult

use of uk.ac.ebi.spot.goci.model.RestResponseResult 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 12 with RestResponseResult

use of uk.ac.ebi.spot.goci.model.RestResponseResult in project goci by EBISPOT.

the class EnsemblMappingPipeline method getOverlapRegionCalls.

/**
     * Ensembl REST API call for the overlap region endpoint
     *
     * @param chromosome the chromosome name
     * @param position1  the 5' position of the region
     * @param position2  the 3' position of the region
     * @param rest_opt   the extra parameters to add at the end of the REST call url (inherited from other methods)
     * @return A JSONArray object containing a list of JSONObjects corresponding to the genes overlapping the region
     */
private JSONArray getOverlapRegionCalls(String chromosome, Integer position1, Integer position2, String rest_opt, String eRelease) throws EnsemblRestIOException {
    String data = chromosome + ":" + position1 + "-" + position2;
    String param = data.concat("?").concat(rest_opt);
    RestResponseResult restResponseResult = ensemblRestcallHistoryService.getEnsemblRestCallByTypeAndParamAndVersion("overlap_region", param, eRelease);
    if (restResponseResult == null) {
        restResponseResult = ensemblRestTemplateService.getRestCall("overlap_region", data, rest_opt);
        ensemblRestcallHistoryService.create(restResponseResult, "overlap_region", param, eRelease);
    }
    JsonNode result = restResponseResult.getRestResult();
    JSONArray overlap_result = new JSONArray();
    if (result.isArray()) {
        overlap_result = result.getArray();
    } else {
        // Errors
        getEnsemblMappingResult().addPipelineErrors(restResponseResult.getError());
        overlap_result = new JSONArray("[{\"overlap_error\":\"1\"}]");
    }
    return overlap_result;
}
Also used : JSONArray(org.json.JSONArray) RestResponseResult(uk.ac.ebi.spot.goci.model.RestResponseResult) JsonNode(com.mashape.unirest.http.JsonNode)

Example 13 with RestResponseResult

use of uk.ac.ebi.spot.goci.model.RestResponseResult in project goci by EBISPOT.

the class EnsemblMappingPipeline method getChromosomeEnd.

/**
     * Get the end position of a given chromosome, using an Ensembl REST API call
     *
     * @param chromosome the chromosome name
     * @return the position of the end of the chromosome
     */
private int getChromosomeEnd(String chromosome, String eRelease) throws EnsemblRestIOException {
    int chr_end = 0;
    String webservice = "info_assembly";
    RestResponseResult restResponseResult = ensemblRestcallHistoryService.getEnsemblRestCallByTypeAndParamAndVersion("info_assembly", chromosome, eRelease);
    if (restResponseResult == null) {
        restResponseResult = ensemblRestTemplateService.getRestCall(webservice, chromosome, "");
        ensemblRestcallHistoryService.create(restResponseResult, "info_assembly", chromosome, eRelease);
    }
    JSONObject info_result = restResponseResult.getRestResult().getObject();
    if (info_result.length() > 0) {
        if (info_result.has("length")) {
            chr_end = info_result.getInt("length");
        }
    }
    return chr_end;
}
Also used : JSONObject(org.json.JSONObject) RestResponseResult(uk.ac.ebi.spot.goci.model.RestResponseResult)

Example 14 with RestResponseResult

use of uk.ac.ebi.spot.goci.model.RestResponseResult in project goci by EBISPOT.

the class EnsemblRestcallHistoryService method getEnsemblRestCallByTypeAndParamAndVersion.

// BEWARE:If the Ensembl release is valid, the system can try to retrieve the data from the table
public RestResponseResult getEnsemblRestCallByTypeAndParamAndVersion(String type, String param, String eRelease) {
    RestResponseResult restResponseResult = null;
    // Without release it is pointless stores the info.
    if (eRelease != null) {
        if (!(eRelease.isEmpty())) {
            try {
                Collection<EnsemblRestcallHistory> urls = ensemblRestcallHistoryRepository.findByRequestTypeAndEnsemblParamAndEnsemblVersion(type, param, eRelease);
                if (urls.size() > 0) {
                    EnsemblRestcallHistory result = urls.iterator().next();
                    restResponseResult = new RestResponseResult();
                    restResponseResult.setUrl(result.getEnsemblUrl());
                    String restApiError = result.getEnsemblError();
                    if (restApiError != null && !restApiError.isEmpty()) {
                        restResponseResult.setError(restApiError);
                    } else {
                        ObjectMapper mapper = new ObjectMapper();
                        JsonNode jsonNode = mapper.convertValue(result.getEnsemblResponse().toString(), JsonNode.class);
                        restResponseResult.setRestResult(jsonNode);
                    }
                }
            } catch (Exception e) {
            // BEWARE: the following code MUST NOT block Ensembl Rest API Call
            }
        }
    }
    return restResponseResult;
}
Also used : RestResponseResult(uk.ac.ebi.spot.goci.model.RestResponseResult) JsonNode(com.mashape.unirest.http.JsonNode) EnsemblRestcallHistory(uk.ac.ebi.spot.goci.model.EnsemblRestcallHistory) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

RestResponseResult (uk.ac.ebi.spot.goci.model.RestResponseResult)14 JSONObject (org.json.JSONObject)6 JsonNode (com.mashape.unirest.http.JsonNode)4 MalformedURLException (java.net.MalformedURLException)3 JSONArray (org.json.JSONArray)3 UnirestException (com.mashape.unirest.http.exceptions.UnirestException)2 URL (java.net.URL)2 Matcher (java.util.regex.Matcher)2 EnsemblRestIOException (uk.ac.ebi.spot.goci.exception.EnsemblRestIOException)2 Location (uk.ac.ebi.spot.goci.model.Location)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 RestTemplate (org.springframework.web.client.RestTemplate)1 EnsemblRestClientException (uk.ac.ebi.spot.goci.exception.EnsemblRestClientException)1 EnsemblMappingResult (uk.ac.ebi.spot.goci.model.EnsemblMappingResult)1 EnsemblRestcallHistory (uk.ac.ebi.spot.goci.model.EnsemblRestcallHistory)1 SnpLookupJson (uk.ac.ebi.spot.goci.model.SnpLookupJson)1