Skip to content

Commit

Permalink
Convert from redis cache to in-memory cache
Browse files Browse the repository at this point in the history
  • Loading branch information
calvinlu3 committed Oct 30, 2024
1 parent 3837d87 commit 9bec7ac
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 47 deletions.
23 changes: 17 additions & 6 deletions core/src/main/java/org/mskcc/cbio/oncokb/cache/CacheFetcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@

@Component
public class CacheFetcher {
private static Map<ReferenceGenome, Map<String, Set<EnsemblGene>>> canonicalEnsemblGenesByChromosomeByReferenceGenome = new HashMap<ReferenceGenome, Map<String, Set<EnsemblGene>>>() {{
put(ReferenceGenome.GRCh37, new HashMap<>());
put(ReferenceGenome.GRCh38, new HashMap<>());
}};

OncokbTranscriptService oncokbTranscriptService = new OncokbTranscriptService();
NotationConverter notationConverter = new NotationConverter();

Expand Down Expand Up @@ -103,9 +108,13 @@ public Set<org.oncokb.oncokb_transcript.client.Gene> getAllTranscriptGenes() thr
return oncokbTranscriptService.findTranscriptGenesBySymbols(CacheUtils.getAllGenes().stream().filter(gene -> gene.getEntrezGeneId() > 0).map(gene -> gene.getEntrezGeneId().toString()).collect(Collectors.toList()));
}

@Cacheable(cacheResolver = "generalCacheResolver", keyGenerator = "concatKeyGenerator")
public Map<String, Set<EnsemblGene>> getCanonicalEnsemblGenesByChromosome(ReferenceGenome referenceGenome) throws ApiException {
Map<String, Set<EnsemblGene>> chromosomeEnsemblGenesMap = new HashMap<>();
// Use cached data if available
Map<String, Set<EnsemblGene>> chromosomeEnsemblGenesMap = canonicalEnsemblGenesByChromosomeByReferenceGenome.get(referenceGenome);
if(!chromosomeEnsemblGenesMap.isEmpty()) {
return chromosomeEnsemblGenesMap;
}
// Reach out to transcript service
Set<org.oncokb.oncokb_transcript.client.Gene> allTranscriptGenes = getAllTranscriptGenes();
for (org.oncokb.oncokb_transcript.client.Gene gene : allTranscriptGenes) {
for (EnsemblGene ensemblGene : gene.getEnsemblGenes()) {
Expand All @@ -121,6 +130,7 @@ public Map<String, Set<EnsemblGene>> getCanonicalEnsemblGenesByChromosome(Refere
chromosomeEnsemblGenesMap.get(chromosome).add(ensemblGene);
}
}
canonicalEnsemblGenesByChromosomeByReferenceGenome.put(referenceGenome, chromosomeEnsemblGenesMap);
return chromosomeEnsemblGenesMap;
}

Expand Down Expand Up @@ -321,7 +331,7 @@ public List<TranscriptDTO> getAllGeneEnsemblTranscript(ReferenceGenome reference
return oncokbTranscriptService.findEnsemblTranscriptsByIds(ids, referenceGenome);
}

public boolean genomicLocationShouldBeAnnotated(GNVariantAnnotationType gnVariantAnnotationType, String query, ReferenceGenome referenceGenome,Map<String, Set<EnsemblGene>> chromosomeCanonicalEnsembleGeneMap) throws ApiException {
public boolean genomicLocationShouldBeAnnotated(GNVariantAnnotationType gnVariantAnnotationType, String query, ReferenceGenome referenceGenome) throws ApiException {
if (StringUtils.isEmpty(query)) {
return false;
}else{
Expand All @@ -332,8 +342,9 @@ public boolean genomicLocationShouldBeAnnotated(GNVariantAnnotationType gnVarian
return false;
}
}
Map<String, Set<EnsemblGene>> chromosomeCanonicalEnsemblGeneMap = canonicalEnsemblGenesByChromosomeByReferenceGenome.get(referenceGenome);
// when the transcript info is not available, we should always annotate the genomic location
if (chromosomeCanonicalEnsembleGeneMap == null || chromosomeCanonicalEnsembleGeneMap.isEmpty()) {
if (chromosomeCanonicalEnsemblGeneMap == null || chromosomeCanonicalEnsemblGeneMap.isEmpty()) {
return true;
}
GenomicLocation gl = null;
Expand All @@ -357,8 +368,8 @@ public boolean genomicLocationShouldBeAnnotated(GNVariantAnnotationType gnVarian
}
GenomicLocation finalGl = gl;

if (chromosomeCanonicalEnsembleGeneMap.containsKey(finalGl.getChromosome())){
return chromosomeCanonicalEnsembleGeneMap.get(finalGl.getChromosome()).stream().anyMatch(ensemblGene -> withinBuffer(ensemblGene, finalGl));
if (chromosomeCanonicalEnsemblGeneMap.containsKey(finalGl.getChromosome())){
return chromosomeCanonicalEnsemblGeneMap.get(finalGl.getChromosome()).stream().anyMatch(ensemblGene -> withinBuffer(ensemblGene, finalGl));
}

return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import org.apache.commons.collections.map.HashedMap;
import org.mskcc.cbio.oncokb.apiModels.download.DownloadAvailability;
import org.mskcc.cbio.oncokb.model.*;
import org.mskcc.cbio.oncokb.model.TumorType;

import java.io.IOException;
import java.util.*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,7 @@ public ResponseEntity<IndicatorQueryResp> annotateMutationsByGenomicChangeGet(
matchedRG,
genomicLocation,
tumorType,
new HashSet<>(MainUtils.stringToEvidenceTypes(evidenceTypes, ",")),
this.cacheFetcher.getCanonicalEnsemblGenesByChromosome(matchedRG)
new HashSet<>(MainUtils.stringToEvidenceTypes(evidenceTypes, ","))
);
return new ResponseEntity<>(indicatorQueryResp, HttpStatus.OK);
}
Expand Down Expand Up @@ -190,14 +189,8 @@ public ResponseEntity<List<IndicatorQueryResp>> annotateMutationsByGenomicChange
if (body == null) {
throw new ApiHttpErrorException("The request body is missing.", HttpStatus.BAD_REQUEST);
} else {
Map<String, Set<org.oncokb.oncokb_transcript.client.EnsemblGene>> chromosomeEnsemblGenesGrch37 = this.cacheFetcher.getCanonicalEnsemblGenesByChromosome(ReferenceGenome.GRCh37);
Map<String, Set<org.oncokb.oncokb_transcript.client.EnsemblGene>> chromosomeEnsemblGenesGrch38 = this.cacheFetcher.getCanonicalEnsemblGenesByChromosome(ReferenceGenome.GRCh38);
for (AnnotateMutationByGenomicChangeQuery query : body) {
Map<String, Set<org.oncokb.oncokb_transcript.client.EnsemblGene>> selectedEnsemblGenesMap = chromosomeEnsemblGenesGrch37;
if (ReferenceGenome.GRCh38.equals(query.getReferenceGenome())) {
selectedEnsemblGenesMap = chromosomeEnsemblGenesGrch38;
}
IndicatorQueryResp resp = this.getIndicatorQueryFromGenomicLocation(query.getReferenceGenome(), query.getGenomicLocation(), query.getTumorType(), query.getEvidenceTypes(), selectedEnsemblGenesMap);
IndicatorQueryResp resp = this.getIndicatorQueryFromGenomicLocation(query.getReferenceGenome(), query.getGenomicLocation(), query.getTumorType(), query.getEvidenceTypes());
resp.getQuery().setId(query.getId());
result.add(resp);
}
Expand Down Expand Up @@ -236,8 +229,7 @@ public ResponseEntity<IndicatorQueryResp> annotateMutationsByHGVSgGet(
matchedRG,
hgvsg,
tumorType,
new HashSet<>(MainUtils.stringToEvidenceTypes(evidenceTypes, ",")),
this.cacheFetcher.getCanonicalEnsemblGenesByChromosome(matchedRG)
new HashSet<>(MainUtils.stringToEvidenceTypes(evidenceTypes, ","))
);
}
return new ResponseEntity<>(indicatorQueryResp, HttpStatus.OK);
Expand All @@ -261,19 +253,12 @@ public ResponseEntity<List<IndicatorQueryResp>> annotateMutationsByHGVSgPost(
if (body == null) {
throw new ApiHttpErrorException("The request body is missing.", HttpStatus.BAD_REQUEST);
} else {
Map<String, Set<org.oncokb.oncokb_transcript.client.EnsemblGene>> chromosomeEnsemblGenesGrch37 = this.cacheFetcher.getCanonicalEnsemblGenesByChromosome(ReferenceGenome.GRCh37);
Map<String, Set<org.oncokb.oncokb_transcript.client.EnsemblGene>> chromosomeEnsemblGenesGrch38 = this.cacheFetcher.getCanonicalEnsemblGenesByChromosome(ReferenceGenome.GRCh38);
for (AnnotateMutationByHGVSgQuery query : body) {
Map<String, Set<org.oncokb.oncokb_transcript.client.EnsemblGene>> selectedEnsemblGenesMap = chromosomeEnsemblGenesGrch37;
if (ReferenceGenome.GRCh38.equals(query.getReferenceGenome())) {
selectedEnsemblGenesMap = chromosomeEnsemblGenesGrch38;
}
IndicatorQueryResp resp = this.getIndicatorQueryFromHGVSg(
query.getReferenceGenome(),
query.getHgvsg(),
query.getTumorType(),
query.getEvidenceTypes(),
selectedEnsemblGenesMap
query.getEvidenceTypes()
);
resp.getQuery().setId(query.getId());
result.add(resp);
Expand Down Expand Up @@ -548,11 +533,10 @@ private IndicatorQueryResp getIndicatorQueryFromGenomicLocation(
ReferenceGenome referenceGenome,
String genomicLocation,
String tumorType,
Set<EvidenceType> evidenceTypes,
Map<String, Set<org.oncokb.oncokb_transcript.client.EnsemblGene>> chromsomeEnsemblGeneMap
Set<EvidenceType> evidenceTypes
) throws ApiException, org.genome_nexus.ApiException {
Alteration alteration;
if (!this.cacheFetcher.genomicLocationShouldBeAnnotated(GNVariantAnnotationType.GENOMIC_LOCATION, genomicLocation, referenceGenome, chromsomeEnsemblGeneMap)) {
if (!this.cacheFetcher.genomicLocationShouldBeAnnotated(GNVariantAnnotationType.GENOMIC_LOCATION, genomicLocation, referenceGenome)) {
alteration = new Alteration();
} else {
alteration = this.cacheFetcher.getAlterationFromGenomeNexus(GNVariantAnnotationType.GENOMIC_LOCATION, referenceGenome, genomicLocation);
Expand Down Expand Up @@ -581,11 +565,10 @@ private IndicatorQueryResp getIndicatorQueryFromHGVSg(
ReferenceGenome referenceGenome,
String hgvsg,
String tumorType,
Set<EvidenceType> evidenceTypes,
Map<String, Set<org.oncokb.oncokb_transcript.client.EnsemblGene>> chromosomeEnsemblGeneMap
Set<EvidenceType> evidenceTypes
) throws ApiException, org.genome_nexus.ApiException {
Alteration alteration;
if (!this.cacheFetcher.genomicLocationShouldBeAnnotated(GNVariantAnnotationType.HGVS_G, hgvsg, referenceGenome, chromosomeEnsemblGeneMap)) {
if (!this.cacheFetcher.genomicLocationShouldBeAnnotated(GNVariantAnnotationType.HGVS_G, hgvsg, referenceGenome)) {
alteration = new Alteration();
} else {
alteration = this.cacheFetcher.getAlterationFromGenomeNexus(GNVariantAnnotationType.HGVS_G, referenceGenome, hgvsg);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ public ResponseEntity<VariantAnnotation> utilVariantAnnotationGet(
String genomicQuery = StringUtils.isNullOrEmpty(hgvsg) ? genomicChange : hgvsg;
GNVariantAnnotationType type = StringUtils.isNullOrEmpty(hgvsg) ? GNVariantAnnotationType.GENOMIC_LOCATION : GNVariantAnnotationType.HGVS_G;
Alteration alterationModel;
if (!this.cacheFetcher.genomicLocationShouldBeAnnotated(type, genomicQuery, matchedRG, this.cacheFetcher.getCanonicalEnsemblGenesByChromosome(matchedRG))) {
if (!this.cacheFetcher.genomicLocationShouldBeAnnotated(type, genomicQuery, matchedRG)) {
alterationModel = new Alteration();
} else {
alterationModel = this.cacheFetcher.getAlterationFromGenomeNexus(type, matchedRG, genomicQuery);
Expand Down Expand Up @@ -592,14 +592,8 @@ public ResponseEntity<List<TranscriptCoverageFilterResult>> utilFilterHgvsgBased
if (body == null) {
throw new ApiHttpErrorException("The request body is missing.", HttpStatus.BAD_REQUEST);
} else {
Map<String, Set<org.oncokb.oncokb_transcript.client.EnsemblGene>> chromosomeEnsemblGenesGrch37 = this.cacheFetcher.getCanonicalEnsemblGenesByChromosome(ReferenceGenome.GRCh37);
Map<String, Set<org.oncokb.oncokb_transcript.client.EnsemblGene>> chromosomeEnsemblGenesGrch38 = this.cacheFetcher.getCanonicalEnsemblGenesByChromosome(ReferenceGenome.GRCh38);
for (AnnotateMutationByHGVSgQuery query : body) {
Map<String, Set<org.oncokb.oncokb_transcript.client.EnsemblGene>> selectedEnsemblGenesMap = chromosomeEnsemblGenesGrch37;
if (ReferenceGenome.GRCh38.equals(query.getReferenceGenome())) {
selectedEnsemblGenesMap = chromosomeEnsemblGenesGrch38;
}
boolean shouldAnnotate = this.cacheFetcher.genomicLocationShouldBeAnnotated(GNVariantAnnotationType.HGVS_G, query.getHgvsg(), query.getReferenceGenome(),selectedEnsemblGenesMap);
boolean shouldAnnotate = this.cacheFetcher.genomicLocationShouldBeAnnotated(GNVariantAnnotationType.HGVS_G, query.getHgvsg(), query.getReferenceGenome());
result.add(new TranscriptCoverageFilterResult(query.getHgvsg(), shouldAnnotate));
}
}
Expand All @@ -615,14 +609,8 @@ public ResponseEntity<List<TranscriptCoverageFilterResult>> utilFilterGenomicCha
if (body == null) {
throw new ApiHttpErrorException("The request body is missing.", HttpStatus.BAD_REQUEST);
} else {
Map<String, Set<org.oncokb.oncokb_transcript.client.EnsemblGene>> chromosomeEnsemblGenesGrch37 = this.cacheFetcher.getCanonicalEnsemblGenesByChromosome(ReferenceGenome.GRCh37);
Map<String, Set<org.oncokb.oncokb_transcript.client.EnsemblGene>> chromosomeEnsemblGenesGrch38 = this.cacheFetcher.getCanonicalEnsemblGenesByChromosome(ReferenceGenome.GRCh38);
for (AnnotateMutationByGenomicChangeQuery query : body) {
Map<String, Set<org.oncokb.oncokb_transcript.client.EnsemblGene>> selectedEnsemblGenesMap = chromosomeEnsemblGenesGrch37;
if (ReferenceGenome.GRCh38.equals(query.getReferenceGenome())) {
selectedEnsemblGenesMap = chromosomeEnsemblGenesGrch38;
}
boolean shouldAnnotate = this.cacheFetcher.genomicLocationShouldBeAnnotated(GNVariantAnnotationType.GENOMIC_LOCATION, query.getGenomicLocation(), query.getReferenceGenome(), selectedEnsemblGenesMap);
boolean shouldAnnotate = this.cacheFetcher.genomicLocationShouldBeAnnotated(GNVariantAnnotationType.GENOMIC_LOCATION, query.getGenomicLocation(), query.getReferenceGenome());
result.add(new TranscriptCoverageFilterResult(query.getGenomicLocation(), shouldAnnotate));
}
}
Expand Down

0 comments on commit 9bec7ac

Please sign in to comment.