Search in sources :

Example 1 with EnsemblMappingResult

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

the class EnsemblMappingPipeline method run_pipeline.

// Run the pipeline for a given SNP
public synchronized EnsemblMappingResult run_pipeline(String rsId, Collection<String> reportedGenes, String eRelease) throws EnsemblRestIOException {
    // Create our result object
    setEnsemblMappingResult(new EnsemblMappingResult());
    getEnsemblMappingResult().setRsId(rsId);
    // Variation call
    RestResponseResult variationDataApiResult = ensemblRestcallHistoryService.getEnsemblRestCallByTypeAndParamAndVersion("snp", rsId, eRelease);
    if (variationDataApiResult == null) {
        variationDataApiResult = ensemblRestTemplateService.getRestCall("variation", rsId, "");
        ensemblRestcallHistoryService.create(variationDataApiResult, "snp", rsId, eRelease);
    }
    String restApiError = variationDataApiResult.getError();
    // Check for any errors
    if (restApiError != null && !restApiError.isEmpty()) {
        getEnsemblMappingResult().addPipelineErrors(restApiError);
    }
    if (variationDataApiResult.getRestResult() != null) {
        JSONObject variationResult = variationDataApiResult.getRestResult().getObject();
        if (variationResult.has("error")) {
            checkError(variationResult, "variation", "Variant " + rsId + " is not found in Ensembl");
        } else if (variationResult.length() > 0) {
            // Merged SNP
            String currentRsId = variationResult.getString("name");
            getEnsemblMappingResult().setMerged((currentRsId.equals(rsId)) ? 0 : 1);
            if (getEnsemblMappingResult().getMerged() == 1) {
                getEnsemblMappingResult().setCurrentSnpId(currentRsId);
            }
            // Mapping errors
            if (variationResult.has("failed")) {
                getEnsemblMappingResult().addPipelineErrors(variationResult.getString("failed"));
            }
            // Mapping and genomic context calls
            JSONArray mappings = variationResult.getJSONArray("mappings");
            Collection<Location> locations = getMappings(mappings, eRelease);
            getEnsemblMappingResult().setLocations(locations);
            // Add genomic context
            if (locations.size() > 0) {
                // This implies there is at least one variant location.
                if (variationResult.has("most_severe_consequence")) {
                    getEnsemblMappingResult().setFunctionalClass(variationResult.getString("most_severe_consequence"));
                }
                // Genomic context (loop over the "locations" object)
                for (Location snp_location : locations) {
                    getAllGenomicContexts(snp_location, eRelease);
                }
            }
        }
    } else {
        getLog().error("Variation call for SNP " + rsId + " returned no result");
    }
    // Reported genes checks
    if (reportedGenes.size() > 0) {
        checkReportedGenes(reportedGenes, getEnsemblMappingResult().getLocations(), eRelease);
    }
    return getEnsemblMappingResult();
}
Also used : EnsemblMappingResult(uk.ac.ebi.spot.goci.model.EnsemblMappingResult) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) Collection(java.util.Collection) RestResponseResult(uk.ac.ebi.spot.goci.model.RestResponseResult) Location(uk.ac.ebi.spot.goci.model.Location)

Example 2 with EnsemblMappingResult

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

the class MappingService method doMapping.

private void doMapping(Association association, String eRelease) throws EnsemblMappingException {
    getLog().info("Mapping association: " + association.getId());
    // Map to store returned location data, this is used in
    // snpLocationMappingService to process all locations linked
    // to a single snp in one go
    Map<String, Set<Location>> snpToLocationsMap = new HashMap<>();
    // Collection to store all genomic contexts
    Collection<GenomicContext> allGenomicContexts = new ArrayList<>();
    // Collection to store all errors for one association
    Collection<String> associationPipelineErrors = new ArrayList<>();
    // For each loci get the SNP and author reported genes
    Collection<Locus> studyAssociationLoci = association.getLoci();
    for (Locus associationLocus : studyAssociationLoci) {
        Long locusId = associationLocus.getId();
        Collection<SingleNucleotidePolymorphism> snpsLinkedToLocus = singleNucleotidePolymorphismQueryService.findByRiskAllelesLociId(locusId);
        Collection<Gene> authorReportedGenesLinkedToSnp = associationLocus.getAuthorReportedGenes();
        // Get gene names
        Collection<String> authorReportedGeneNamesLinkedToSnp = new ArrayList<>();
        for (Gene authorReportedGeneLinkedToSnp : authorReportedGenesLinkedToSnp) {
            authorReportedGeneNamesLinkedToSnp.add(authorReportedGeneLinkedToSnp.getGeneName().trim());
        }
        // Pass rs_id and author reported genes to mapping component
        for (SingleNucleotidePolymorphism snpLinkedToLocus : snpsLinkedToLocus) {
            String snpRsId = snpLinkedToLocus.getRsId();
            EnsemblMappingResult ensemblMappingResult = new EnsemblMappingResult();
            // Try to map supplied data
            try {
                getLog().debug("Running mapping....");
                ensemblMappingResult = ensemblMappingPipeline.run_pipeline(snpRsId, authorReportedGeneNamesLinkedToSnp, eRelease);
            } catch (Exception e) {
                getLog().error("Encountered a " + e.getClass().getSimpleName() + " whilst trying to run mapping of SNP " + snpRsId + ", found in association: " + association.getId(), e);
                throw new EnsemblMappingException();
            }
            getLog().debug("Mapping complete");
            // First remove old locations and genomic contexts
            snpLocationMappingService.removeExistingSnpLocations(snpLinkedToLocus);
            snpGenomicContextMappingService.removeExistingGenomicContexts(snpLinkedToLocus);
            Collection<Location> locations = ensemblMappingResult.getLocations();
            Collection<GenomicContext> snpGenomicContexts = ensemblMappingResult.getGenomicContexts();
            ArrayList<String> pipelineErrors = ensemblMappingResult.getPipelineErrors();
            // Update functional class
            snpLinkedToLocus.setFunctionalClass(ensemblMappingResult.getFunctionalClass());
            snpLinkedToLocus.setLastUpdateDate(new Date());
            snpLinkedToLocus.setMerged(Long.valueOf(ensemblMappingResult.getMerged()));
            // Update the merge table
            if (ensemblMappingResult.getMerged() == 1) {
                String currentSnpId = ensemblMappingResult.getCurrentSnpId();
                SingleNucleotidePolymorphism currentSnp = singleNucleotidePolymorphismRepository.findByRsId(currentSnpId);
                // Add the current SingleNucleotidePolymorphism to the "merged" rsID
                if (currentSnp == null) {
                    currentSnp = new SingleNucleotidePolymorphism();
                    currentSnp.setRsId(currentSnpId);
                    currentSnp.setFunctionalClass(snpLinkedToLocus.getFunctionalClass());
                    singleNucleotidePolymorphismRepository.save(currentSnp);
                    currentSnp = singleNucleotidePolymorphismRepository.findByRsId(currentSnpId);
                }
                snpLinkedToLocus.setCurrentSnp(currentSnp);
            }
            singleNucleotidePolymorphismRepository.save(snpLinkedToLocus);
            // Store location information for SNP
            if (!locations.isEmpty()) {
                for (Location location : locations) {
                    // This would only occur is SNP has multiple locations
                    if (snpToLocationsMap.containsKey(snpRsId)) {
                        snpToLocationsMap.get(snpRsId).add(location);
                    } else // First time we see a SNP store the location
                    {
                        Set<Location> snpLocation = new HashSet<>();
                        snpLocation.add(location);
                        snpToLocationsMap.put(snpRsId, snpLocation);
                    }
                }
            } else {
                getLog().warn("Attempt to map SNP: " + snpRsId + " returned no location details");
                pipelineErrors.add("Attempt to map SNP: " + snpRsId + " returned no location details");
            }
            // Store genomic context data for snp
            if (!snpGenomicContexts.isEmpty()) {
                allGenomicContexts.addAll(snpGenomicContexts);
            } else {
                getLog().warn("Attempt to map SNP: " + snpRsId + " returned no mapped genes");
                pipelineErrors.add("Attempt to map SNP: " + snpRsId + " returned no mapped genes");
            }
            if (!pipelineErrors.isEmpty()) {
                associationPipelineErrors.addAll(pipelineErrors);
            }
        }
    }
    // Create association report based on whether there is errors or not
    if (!associationPipelineErrors.isEmpty()) {
        associationReportService.processAssociationErrors(association, associationPipelineErrors);
    } else {
        associationReportService.updateAssociationReportDetails(association);
    }
    // Save data
    if (!snpToLocationsMap.isEmpty()) {
        getLog().debug("Updating location details ...");
        snpLocationMappingService.storeSnpLocation(snpToLocationsMap);
        getLog().debug("Updating location details complete");
    }
    if (!allGenomicContexts.isEmpty()) {
        getLog().debug("Updating genomic context details ...");
        snpGenomicContextMappingService.processGenomicContext(allGenomicContexts);
        getLog().debug("Updating genomic context details complete");
    }
}
Also used : GenomicContext(uk.ac.ebi.spot.goci.model.GenomicContext) EnsemblMappingException(uk.ac.ebi.spot.goci.exception.EnsemblMappingException) EnsemblMappingResult(uk.ac.ebi.spot.goci.model.EnsemblMappingResult) Gene(uk.ac.ebi.spot.goci.model.Gene) SingleNucleotidePolymorphism(uk.ac.ebi.spot.goci.model.SingleNucleotidePolymorphism) Locus(uk.ac.ebi.spot.goci.model.Locus) EnsemblMappingException(uk.ac.ebi.spot.goci.exception.EnsemblMappingException) Location(uk.ac.ebi.spot.goci.model.Location)

Aggregations

EnsemblMappingResult (uk.ac.ebi.spot.goci.model.EnsemblMappingResult)2 Location (uk.ac.ebi.spot.goci.model.Location)2 Collection (java.util.Collection)1 JSONArray (org.json.JSONArray)1 JSONObject (org.json.JSONObject)1 EnsemblMappingException (uk.ac.ebi.spot.goci.exception.EnsemblMappingException)1 Gene (uk.ac.ebi.spot.goci.model.Gene)1 GenomicContext (uk.ac.ebi.spot.goci.model.GenomicContext)1 Locus (uk.ac.ebi.spot.goci.model.Locus)1 RestResponseResult (uk.ac.ebi.spot.goci.model.RestResponseResult)1 SingleNucleotidePolymorphism (uk.ac.ebi.spot.goci.model.SingleNucleotidePolymorphism)1