Search in sources :

Example 6 with EnsemblGene

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

the class SnpGenomicContextMappingService method storeGenes.

/**
     * Create/update genes with latest mapping information
     *
     * @param geneToExternalIdMap map of a gene name and all external database IDs from current mapping run
     * @param source              the source of mapping, either Ensembl or Entrez
     */
private void storeGenes(Map<String, Set<String>> geneToExternalIdMap, String source) {
    for (String geneName : geneToExternalIdMap.keySet()) {
        Set<String> externalIds = geneToExternalIdMap.get(geneName);
        // Find any existing database genes that match the gene name
        // IgnoreCase query is not used here as we want
        // the exact gene name returned from mapping
        Gene existingGeneInDatabase = geneQueryService.findByGeneName(geneName);
        // If gene is not found in database then create one
        if (existingGeneInDatabase == null) {
            createGene(geneName, externalIds, source);
        } else // Update gene
        {
            if (source.equalsIgnoreCase("Ensembl")) {
                // Get a list of current Ensembl IDs linked to existing gene
                Collection<EnsemblGene> oldEnsemblGenesLinkedToGene = existingGeneInDatabase.getEnsemblGeneIds();
                Collection<Long> oldEnsemblIdsLinkedToGene = new ArrayList<>();
                for (EnsemblGene oldEnsemblGeneLinkedToGene : oldEnsemblGenesLinkedToGene) {
                    oldEnsemblIdsLinkedToGene.add(oldEnsemblGeneLinkedToGene.getId());
                }
                Collection<EnsemblGene> newEnsemblGenes = new ArrayList<>();
                for (String id : externalIds) {
                    EnsemblGene ensemblGene = createOrRetrieveEnsemblExternalId(id, geneName);
                    newEnsemblGenes.add(ensemblGene);
                }
                // Set latest IDs from mapping run
                existingGeneInDatabase.setEnsemblGeneIds(newEnsemblGenes);
                // Save changes
                geneRepository.save(existingGeneInDatabase);
                // Clean-up any Ensembl IDs that may now be left without a gene linked
                for (Long oldEnsemblIdLinkedToGene : oldEnsemblIdsLinkedToGene) {
                    cleanUpEnsemblGenes(oldEnsemblIdLinkedToGene);
                }
            }
            if (source.equalsIgnoreCase("Entrez")) {
                // Get a list of of current Entrez IDs linked to existing gene
                Collection<EntrezGene> oldEntrezGenesLinkedToGene = existingGeneInDatabase.getEntrezGeneIds();
                Collection<Long> oldEntrezGenesIdsLinkedToGene = new ArrayList<>();
                for (EntrezGene oldEntrezGeneLinkedToGene : oldEntrezGenesLinkedToGene) {
                    oldEntrezGenesIdsLinkedToGene.add(oldEntrezGeneLinkedToGene.getId());
                }
                Collection<EntrezGene> newEntrezGenes = new ArrayList<>();
                for (String id : externalIds) {
                    EntrezGene entrezGene = createOrRetrieveEntrezExternalId(id, geneName);
                    newEntrezGenes.add(entrezGene);
                }
                // Set latest IDs from mapping run
                existingGeneInDatabase.setEntrezGeneIds(newEntrezGenes);
                // Save changes
                geneRepository.save(existingGeneInDatabase);
                // Clean-up any Entrez IDs that may now be left without a gene linked
                for (Long oldEntrezGenesIdLinkedToGene : oldEntrezGenesIdsLinkedToGene) {
                    cleanUpEntrezGenes(oldEntrezGenesIdLinkedToGene);
                }
            }
        }
    }
}
Also used : EntrezGene(uk.ac.ebi.spot.goci.model.EntrezGene) EnsemblGene(uk.ac.ebi.spot.goci.model.EnsemblGene) Gene(uk.ac.ebi.spot.goci.model.Gene) EntrezGene(uk.ac.ebi.spot.goci.model.EntrezGene) ArrayList(java.util.ArrayList) EnsemblGene(uk.ac.ebi.spot.goci.model.EnsemblGene)

Aggregations

EnsemblGene (uk.ac.ebi.spot.goci.model.EnsemblGene)6 EntrezGene (uk.ac.ebi.spot.goci.model.EntrezGene)6 Gene (uk.ac.ebi.spot.goci.model.Gene)5 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2 GenomicContext (uk.ac.ebi.spot.goci.model.GenomicContext)2 SingleNucleotidePolymorphism (uk.ac.ebi.spot.goci.model.SingleNucleotidePolymorphism)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 JSONObject (org.json.JSONObject)1 Location (uk.ac.ebi.spot.goci.model.Location)1 Region (uk.ac.ebi.spot.goci.model.Region)1 RiskAllele (uk.ac.ebi.spot.goci.model.RiskAllele)1 Study (uk.ac.ebi.spot.goci.model.Study)1