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