Search in sources :

Example 6 with RestResponseResult

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

the class GeneCheckingRestService method checkGeneSymbolIsValid.

/**
     * Check gene name returns a response
     *
     * @param gene Gene name to check
     * @return Error message
     */
public String checkGeneSymbolIsValid(String gene, String eRelease) {
    String error = null;
    try {
        RestResponseResult geneDataApiResult = ensemblRestcallHistoryService.getEnsemblRestCallByTypeAndParamAndVersion("lookup_symbol", gene, eRelease);
        if (geneDataApiResult == null) {
            geneDataApiResult = ensemblRestTemplateService.getRestCall(getEndpoint(), gene, "");
            ensemblRestcallHistoryService.create(geneDataApiResult, "lookup_symbol", gene, eRelease);
        }
        if (geneDataApiResult.hasErorr()) {
            error = geneDataApiResult.getError();
        } else {
            if (geneDataApiResult.getRestResult().getObject().has("object_type")) {
                String objectType = geneDataApiResult.getRestResult().getObject().get("object_type").toString();
                if (!(objectType.compareToIgnoreCase("gene") == 0)) {
                    error = "Gene symbol ".concat(gene).concat(" is not valid");
                }
            }
        }
    }// The query returns a 400 error if response returns an error
     catch (Exception e) {
        error = "Gene symbol ".concat(gene).concat(" was not retrieved by Ensembl Mapping. Contact Admin.");
        getLog().error("Gene Symbol".concat(gene).concat(" : was not retrieved. (exception)"), e);
    }
    return error;
}
Also used : RestResponseResult(uk.ac.ebi.spot.goci.model.RestResponseResult)

Example 7 with RestResponseResult

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

the class GeneCheckingRestService method getGeneLocation.

/**
     * Get the chromosome a SNP resides on
     *
     * @param gene Gene name/symbol
     * @return The name of the chromosome the gene is located on
     */
public String getGeneLocation(String gene, String eRelease) {
    String geneChromosome = null;
    try {
        RestResponseResult geneDataApiResult = ensemblRestcallHistoryService.getEnsemblRestCallByTypeAndParamAndVersion("lookup_symbol", gene, eRelease);
        if (geneDataApiResult == null) {
            geneDataApiResult = ensemblRestTemplateService.getRestCall(getEndpoint(), gene, "");
            ensemblRestcallHistoryService.create(geneDataApiResult, "lookup_symbol", gene, eRelease);
        }
        if (!(geneDataApiResult.hasErorr())) {
            if (geneDataApiResult.getRestResult().getObject().has("seq_region_name")) {
                geneChromosome = geneDataApiResult.getRestResult().getObject().getString("seq_region_name");
            }
        }
        if (geneChromosome == null) {
            getLog().error("Getting locations for gene ".concat(gene).concat("failed"));
        }
    }// The query returns a 400 error if response returns an error
     catch (Exception e) {
        getLog().error("Getting locations for gene ".concat(gene).concat("failed"), e);
    }
    return geneChromosome;
}
Also used : RestResponseResult(uk.ac.ebi.spot.goci.model.RestResponseResult)

Example 8 with RestResponseResult

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

the class SnpCheckingRestService method checkSnpIdentifierIsValid.

/**
     * Check gene name returns a response
     *
     * @param snp Snp identifier to check
     * @return Error message
     */
public String checkSnpIdentifierIsValid(String snp, String eRelease) {
    String error = null;
    try {
        RestResponseResult snpDataApiResult = ensemblRestcallHistoryService.getEnsemblRestCallByTypeAndParamAndVersion("snp", snp, eRelease);
        if (snpDataApiResult == null) {
            snpDataApiResult = ensemblRestTemplateService.getRestCall(getEndpoint(), snp, "");
            ensemblRestcallHistoryService.create(snpDataApiResult, "snp", snp, eRelease);
        }
        if ((snpDataApiResult.hasErorr())) {
            error = "SNP identifier ".concat(snpDataApiResult.getError());
        } else {
            if (snpDataApiResult.getRestResult().getObject().has("error")) {
                error = "SNP identifier ".concat(snp).concat(" is not valid");
            }
        }
    }// The query returns a 400 error if response returns an error
     catch (Exception e) {
        error = "SNP identifier impossible to retrieve";
        getLog().error("The SNP impossible to retrieve: : ".concat(snp), e);
    }
    return error;
}
Also used : RestResponseResult(uk.ac.ebi.spot.goci.model.RestResponseResult)

Example 9 with RestResponseResult

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

the class SnpCheckingRestService method getSnpLocations.

/**
     * Get the chromosome a SNP resides on
     *
     * @param snp Snp identifier to check
     * @return Set of all SNP chromosome names
     */
public Set<String> getSnpLocations(String snp, String eRelease) {
    Set<String> snpChromosomeNames = new HashSet<>();
    SnpLookupJson snpLookupJson = new SnpLookupJson();
    try {
        RestResponseResult snpDataApiResult = ensemblRestcallHistoryService.getEnsemblRestCallByTypeAndParamAndVersion("snp", snp, eRelease);
        if (snpDataApiResult == null) {
            snpDataApiResult = ensemblRestTemplateService.getRestCall(getEndpoint(), snp, "");
            ensemblRestcallHistoryService.create(snpDataApiResult, "snp", snp, eRelease);
        }
        if (!(snpDataApiResult.hasErorr())) {
            JSONObject snpResult = snpDataApiResult.getRestResult().getObject();
            JSONArray mappings = snpResult.getJSONArray("mappings");
            for (int i = 0; i < mappings.length(); ++i) {
                JSONObject mapping = mappings.getJSONObject(i);
                if (!mapping.has("seq_region_name")) {
                    continue;
                }
                String chromosome = mapping.getString("seq_region_name");
                //Integer position = Integer.valueOf(mapping.getInt("start"));
                snpChromosomeNames.add(chromosome);
            //System.out.println("Snp chromosome: ".concat(chromosome));
            }
        }
    }// The query returns a 400 error if response returns an error
     catch (Exception e) {
        //error = "Imnpossible retrieve SNP Mapping info."
        getLog().error("Getting locations for SNP ".concat(snp).concat(" failed"), e);
    }
    return snpChromosomeNames;
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) RestResponseResult(uk.ac.ebi.spot.goci.model.RestResponseResult) SnpLookupJson(uk.ac.ebi.spot.goci.model.SnpLookupJson) HashSet(java.util.HashSet)

Example 10 with RestResponseResult

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

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