use of uk.ac.ebi.spot.goci.model.EntrezGene 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) {
List<Gene> genesToUpdate = new ArrayList<>();
List<Gene> genesToCreate = new ArrayList<>();
List<EntrezGene> entrezGenesToDelete = new ArrayList<>();
List<EnsemblGene> ensemblGenesToDelete = new ArrayList<>();
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) {
Gene newGene = createGene(geneName, externalIds, source);
genesToCreate.add(newGene);
} 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);
genesToUpdate.add(existingGeneInDatabase);
// Clean-up any Ensembl IDs that may now be left without a gene linked
for (Long oldEnsemblIdLinkedToGene : oldEnsemblIdsLinkedToGene) {
cleanUpEnsemblGenes(oldEnsemblIdLinkedToGene, ensemblGenesToDelete);
}
}
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);
genesToUpdate.add(existingGeneInDatabase);
// Clean-up any Entrez IDs that may now be left without a gene linked
for (Long oldEntrezGenesIdLinkedToGene : oldEntrezGenesIdsLinkedToGene) {
cleanUpEntrezGenes(oldEntrezGenesIdLinkedToGene, entrezGenesToDelete);
}
}
}
}
ensemblGeneRepository.delete(ensemblGenesToDelete);
entrezGeneRepository.delete(entrezGenesToDelete);
geneRepository.save(genesToCreate);
geneRepository.save(genesToUpdate);
}
use of uk.ac.ebi.spot.goci.model.EntrezGene in project goci by EBISPOT.
the class SnpGenomicContextMappingService method createOrRetrieveEntrezExternalId.
/**
* Method to create an Entrez gene, this database table holds entrez gene IDs
*
* @param id Entrez gene ID
* @param geneName Gene name allows method to check if this id is actually already linked to another gene
*/
private EntrezGene createOrRetrieveEntrezExternalId(String id, String geneName) {
EntrezGene entrezGene = entrezGeneQueryService.findByEntrezGeneId(id);
// Create new entry in ENTREZ_GENE table for this ID
if (entrezGene == null) {
entrezGene = new EntrezGene();
entrezGene.setEntrezGeneId(id);
entrezGeneRepository.save(entrezGene);
} else // Check this ID is not linked to a gene with a different name
{
Gene existingGeneLinkedToId = entrezGene.getGene();
if (existingGeneLinkedToId != null) {
if (!Objects.equals(existingGeneLinkedToId.getGeneName(), geneName)) {
getLog().warn("Entrez ID: " + id + ", is already used in database by a different gene(s): " + existingGeneLinkedToId.getGeneName() + ". Will update so links to " + geneName);
// For gene already linked to this entrez ID remove the entrez ID
existingGeneLinkedToId.getEntrezGeneIds().remove(entrezGene);
geneRepository.save(existingGeneLinkedToId);
}
}
}
return entrezGene;
}
Aggregations