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;
}
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;
}
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;
}
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;
}
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");
}
}
Aggregations