Search in sources :

Example 11 with SingleNucleotidePolymorphism

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

Example 12 with SingleNucleotidePolymorphism

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

the class SingleNucleotidePolymorphismQueryService method findByRsIdIgnoreCase.

@Transactional(readOnly = true)
public SingleNucleotidePolymorphism findByRsIdIgnoreCase(String rsId) {
    SingleNucleotidePolymorphism singleNucleotidePolymorphism = singleNucleotidePolymorphismRepository.findByRsIdIgnoreCase(rsId);
    loadAssociatedData(singleNucleotidePolymorphism);
    return singleNucleotidePolymorphism;
}
Also used : SingleNucleotidePolymorphism(uk.ac.ebi.spot.goci.model.SingleNucleotidePolymorphism) Transactional(org.springframework.transaction.annotation.Transactional)

Example 13 with SingleNucleotidePolymorphism

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

the class SnpLocationMappingService method storeSnpLocation.

public void storeSnpLocation(Map<String, Set<Location>> snpToLocations) {
    // Go through each rs_id and its associated locations returned from the mapping pipeline
    for (String snpRsId : snpToLocations.keySet()) {
        Set<Location> snpLocationsFromMapping = snpToLocations.get(snpRsId);
        // Check if the SNP exists
        SingleNucleotidePolymorphism snpInDatabase = singleNucleotidePolymorphismQueryService.findByRsIdIgnoreCase(snpRsId);
        if (snpInDatabase != null) {
            // Store all new location objects
            Collection<Location> newSnpLocations = new ArrayList<>();
            for (Location snpLocationFromMapping : snpLocationsFromMapping) {
                String chromosomeNameFromMapping = snpLocationFromMapping.getChromosomeName();
                if (chromosomeNameFromMapping != null) {
                    chromosomeNameFromMapping = chromosomeNameFromMapping.trim();
                }
                Integer chromosomePositionFromMapping = snpLocationFromMapping.getChromosomePosition();
                //                    if (chromosomePositionFromMapping != null) {
                //                        chromosomePositionFromMapping = chromosomePositionFromMapping.trim();
                //                    }
                Region regionFromMapping = snpLocationFromMapping.getRegion();
                String regionNameFromMapping = null;
                if (regionFromMapping != null) {
                    if (regionFromMapping.getName() != null) {
                        regionNameFromMapping = regionFromMapping.getName().trim();
                    }
                }
                // Check if location already exists
                Location existingLocation = locationRepository.findByChromosomeNameAndChromosomePositionAndRegionName(chromosomeNameFromMapping, chromosomePositionFromMapping, regionNameFromMapping);
                if (existingLocation != null) {
                    newSnpLocations.add(existingLocation);
                } else // Create new location
                {
                    Location newLocation = locationCreationService.createLocation(chromosomeNameFromMapping, chromosomePositionFromMapping, regionNameFromMapping);
                    newSnpLocations.add(newLocation);
                }
            }
            // If we have new locations then link to snp and save
            if (newSnpLocations.size() > 0) {
                // Set new location details
                snpInDatabase.setLocations(newSnpLocations);
                // Update the last update date
                snpInDatabase.setLastUpdateDate(new Date());
                singleNucleotidePolymorphismRepository.save(snpInDatabase);
            } else {
                getLog().warn("No new locations to add to " + snpRsId);
            }
        } else // SNP doesn't exist, this should be extremely rare as SNP value is a copy
        // of the variant entered by the curator which
        // by the time mapping is started should already have been saved
        {
            // TODO WHAT WILL HAPPEN FOR MERGED SNPS
            getLog().error("Adding location for SNP not found in database, RS_ID:" + snpRsId);
            throw new RuntimeException("Adding location for SNP not found in database, RS_ID: " + snpRsId);
        }
    }
}
Also used : SingleNucleotidePolymorphism(uk.ac.ebi.spot.goci.model.SingleNucleotidePolymorphism) ArrayList(java.util.ArrayList) Region(uk.ac.ebi.spot.goci.model.Region) Date(java.util.Date) Location(uk.ac.ebi.spot.goci.model.Location)

Example 14 with SingleNucleotidePolymorphism

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

the class DefaultGWASOWLPublisher method publishGWASData.

public OWLOntology publishGWASData() throws OWLConversionException {
    // create new ontology
    OWLOntology conversion = getConverter().createConversionOntology();
    // grab all studies from the DAO
    getLog().debug("Fetching studies that require conversion to OWL using StudyRepository...");
    Collection<Study> studies = getStudyService().deepFindPublishedStudies();
    //TODO : check with Tony probably better to do it at the Repository/Service level
    //Discard studies which are not associated with a disease trait and those which haven't been published yet
    //by the GWAS catalog.
    Iterator<Study> iterator = studies.iterator();
    while (iterator.hasNext()) {
        Study study = iterator.next();
        //Remove study which have no diseaseTrait.
        if (study.getDiseaseTrait() == null) {
            iterator.remove();
            getLog().error("Study '" + study.getId() + "' has no disease trait");
        } else if (study.getHousekeeping().getCatalogPublishDate() == null) {
            iterator.remove();
            getLog().error("Study '" + study.getId() + "' has not yet been published");
        } else //Remove studies that have been unpublished
        if (study.getHousekeeping().getCatalogUnpublishDate() != null) {
            iterator.remove();
            getLog().error("Study '" + study.getId() + "' has been unpublished");
        }
    //            }else {
    //
    //                //Remove study which have no associations where pvalue is not null.
    //                Collection<Association> associations = study.getAssociations();
    //                Iterator<Association> associationIterator = associations.iterator();
    //                int associationCount = 0;
    //                while (associationIterator.hasNext()) {
    //                    Association association = associationIterator.next();
    //
    //                    if (association.getPvalueExponent() != null && association.getPvalueMantissa() != null) {
    //                        associationCount++;
    //                    }
    //                }
    //                if (associationCount == 0) {
    //                    iterator.remove();
    //                }
    //            }
    }
    getLog().debug("Query complete, got " + studies.size() + " studies");
    // if studies limit is not set, convert all data, else filter to first n studies and associated data
    if (getStudiesLimit() == -1 && FilterProperties.getDateFilter() == null && FilterProperties.getPvalueFilter() == null) {
        System.out.println("Converting all available data");
        // grab all other data from the DAO
        getLog().debug("Fetching traits that require conversion to OWL using AssociationRepository...");
        Collection<Association> traitAssociations = getAssociationService().findReallyAll();
        //TODO check with Tony how to do that in a better way from service or repository (how to not get associations linked to study with no trait.
        //Discard all the associations which are linked to study which are not linked to a disease trait or haven't
        //been published yet in the GWAS catalog.
        Iterator<Association> associationIterator = traitAssociations.iterator();
        while (associationIterator.hasNext()) {
            Association association = associationIterator.next();
            if (association.getStudy().getDiseaseTrait() == null) {
                associationIterator.remove();
            } else if (association.getStudy().getHousekeeping().getCatalogPublishDate() == null) {
                associationIterator.remove();
            } else if (association.getStudy().getHousekeeping().getCatalogUnpublishDate() != null) {
                associationIterator.remove();
            }
        }
        getLog().debug("Fetching SNPs that require conversion to OWL using SingleNucleotidePolymorphismRepository...");
        Collection<SingleNucleotidePolymorphism> snps = getSingleNucleotidePolymorphismService().findAll();
        getLog().debug("All data fetched");
        // convert this data, starting with SNPs (no dependencies) and working up to studies
        getLog().debug("Starting conversion to OWL...");
        getLog().debug("Converting SNPs...");
        getConverter().addSNPsToOntology(snps, conversion);
        getLog().debug("Converting Trait Associations...");
        getConverter().addAssociationsToOntology(traitAssociations, conversion);
        getLog().debug("Converting Studies...");
        getConverter().addStudiesToOntology(studies, conversion);
        getLog().debug("All conversion done!");
        return conversion;
    } else {
        System.out.println("Data conforming to the filter only");
        return filterAndPublishGWASData(conversion, studies);
    }
}
Also used : Study(uk.ac.ebi.spot.goci.model.Study) Association(uk.ac.ebi.spot.goci.model.Association) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) SingleNucleotidePolymorphism(uk.ac.ebi.spot.goci.model.SingleNucleotidePolymorphism)

Example 15 with SingleNucleotidePolymorphism

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

the class AssociationRowProcessorTest method testCreateAssociationFromUploadRow.

@Test
public void testCreateAssociationFromUploadRow() throws Exception {
    // Stubbing mock object behaviour
    when(associationAttributeService.createLocusGenes(STANDARD_ROW.getAuthorReportedGene(), ",")).thenReturn(Arrays.asList(GENE_01, GENE_02));
    when(associationAttributeService.createSnp(STANDARD_ROW.getSnp())).thenReturn(SNP_01);
    when(associationAttributeService.createRiskAllele(STANDARD_ROW.getStrongestAllele(), SNP_01)).thenReturn(RA_01);
    when(associationAttributeService.createSnp(STANDARD_ROW.getProxy())).thenReturn(PROXY);
    Association association = associationRowProcessor.createAssociationFromUploadRow(STANDARD_ROW);
    verify(associationCalculationService, never()).reverseCI(STANDARD_ROW.getRange());
    verify(associationCalculationService, never()).setRange(STANDARD_ROW.getStandardError(), STANDARD_ROW.getOrPerCopyNum());
    verify(associationAttributeService, never()).getEfoTraitsFromRepository(Collections.EMPTY_LIST);
    verify(associationAttributeService, times(1)).createLocusGenes(STANDARD_ROW.getAuthorReportedGene(), ",");
    verify(associationAttributeService, times(1)).createSnp(STANDARD_ROW.getSnp());
    verify(associationAttributeService, times(1)).createSnp(STANDARD_ROW.getProxy());
    verify(associationAttributeService, times(1)).createRiskAllele(STANDARD_ROW.getStrongestAllele(), SNP_01);
    assertThat(association).extracting("id", "riskFrequency", "pvalueDescription", "pvalueMantissa", "pvalueExponent", "multiSnpHaplotype", "snpInteraction", "snpApproved", "snpType", "standardError", "range", "description", "orPerCopyNum", "orPerCopyRecip", "orPerCopyRecipRange", "betaNum", "betaUnit", "betaDirection", "study", "associationReport", "lastMappingDate", "lastMappingPerformedBy", "lastUpdateDate").containsExactly(null, "0.52", "(some pvalue description)", 2, -7, false, false, false, null, (float) 0.6, "[0.82-0.92]", null, (float) 1.22, null, null, null, null, null, null, null, null, null, null);
    assertThat(association.getEfoTraits()).isEmpty();
    assertThat(association.getEvents()).isEmpty();
    assertThat(association.getStudy()).isNull();
    assertThat(association.getLoci()).hasSize(1);
    // Check locus attributes
    Collection<Gene> locusGenes = new ArrayList<>();
    association.getLoci().stream().forEach(locus -> {
        locusGenes.addAll(locus.getAuthorReportedGenes());
    });
    Collection<RiskAllele> locusRiskAlleles = new ArrayList<>();
    association.getLoci().stream().forEach(locus -> {
        locusRiskAlleles.addAll(locus.getStrongestRiskAlleles());
    });
    Collection<SingleNucleotidePolymorphism> proxies = new ArrayList<>();
    locusRiskAlleles.stream().forEach(riskAllele -> {
        proxies.addAll(riskAllele.getProxySnps());
    });
    assertThat(association.getLoci()).extracting(Locus::getDescription).containsOnly("Single variant");
    assertThat(locusGenes).hasSize(2).contains(GENE_01, GENE_02);
    assertThat(locusRiskAlleles).hasSize(1).contains(RA_01);
    assertThat(locusRiskAlleles).extracting("riskAlleleName", "riskFrequency", "snp.rsId").contains(tuple("rs123-?", "0.52", "rs123"));
    assertThat(locusRiskAlleles).extracting(RiskAllele::getSnp).contains(SNP_01);
    assertThat(proxies).contains(PROXY);
    assertThat(proxies).extracting(SingleNucleotidePolymorphism::getRsId).containsExactly("rs99");
}
Also used : Association(uk.ac.ebi.spot.goci.model.Association) Gene(uk.ac.ebi.spot.goci.model.Gene) RiskAllele(uk.ac.ebi.spot.goci.model.RiskAllele) ArrayList(java.util.ArrayList) SingleNucleotidePolymorphism(uk.ac.ebi.spot.goci.model.SingleNucleotidePolymorphism) Test(org.junit.Test)

Aggregations

SingleNucleotidePolymorphism (uk.ac.ebi.spot.goci.model.SingleNucleotidePolymorphism)24 ArrayList (java.util.ArrayList)15 RiskAllele (uk.ac.ebi.spot.goci.model.RiskAllele)12 Association (uk.ac.ebi.spot.goci.model.Association)9 Gene (uk.ac.ebi.spot.goci.model.Gene)9 Locus (uk.ac.ebi.spot.goci.model.Locus)8 Location (uk.ac.ebi.spot.goci.model.Location)6 GenomicContext (uk.ac.ebi.spot.goci.model.GenomicContext)5 Study (uk.ac.ebi.spot.goci.model.Study)4 Date (java.util.Date)3 EfoTrait (uk.ac.ebi.spot.goci.model.EfoTrait)3 Region (uk.ac.ebi.spot.goci.model.Region)3 DateFormat (java.text.DateFormat)2 SimpleDateFormat (java.text.SimpleDateFormat)2 Collection (java.util.Collection)2 Collectors (java.util.stream.Collectors)2 Test (org.junit.Test)2 Autowired (org.springframework.beans.factory.annotation.Autowired)2 Service (org.springframework.stereotype.Service)2 SnpMappingForm (uk.ac.ebi.spot.goci.curation.model.SnpMappingForm)2