Skip to content

Commit

Permalink
Merge pull request #9103 from n1zea144/demo-profiled-cases-counter-opt
Browse files Browse the repository at this point in the history
Optimization to ProfiledCasesCounter when counting profiled samples.
  • Loading branch information
dippindots authored Dec 9, 2021
2 parents d047ab8 + b14a81e commit aa7aec0
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,17 @@ public List<AlterationEnrichment> createAlterationEnrichments(Map<String, Pair<L
.stream()
.filter(gene -> {
// filter genes where number of altered cases in all groups is 0
// or where number of altered cases > number of profiled cases
// (the latter can happen in targeted studies when the gene is not on a panel,
// but it is a participant in a structural variant, e.g. fusion, with a gene
// that is on the panel
return groups.stream().filter(group -> {
AlterationCountByGene mutationCountByGene = mutationCountsbyEntrezGeneIdAndGroup
.getOrDefault(group, new HashMap<Integer, AlterationCountByGene>())
.get(gene.getEntrezGeneId());
return mutationCountByGene == null ? false : mutationCountByGene.getNumberOfAlteredCases() != 0;
return mutationCountByGene == null ? false : (mutationCountByGene.getNumberOfAlteredCases() != 0
&& mutationCountByGene.getNumberOfAlteredCases() <=
mutationCountByGene.getNumberOfProfiledCases());
}).count() > 0;
})
.map(gene -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,16 @@ public class ProfiledCasesCounter<T extends AlterationCountByGene> {
Function<GenePanelData, String> sampleUniqueIdentifier = sample -> sample.getStudyId() + sample.getSampleId();
Function<GenePanelData, String> patientUniqueIdentifier = sample -> sample.getStudyId() + sample.getPatientId();

private enum ProfiledCaseType {
SAMPLE, PATIENT;
}

public void calculate(List<T> alterationCounts,
List<GenePanelData> genePanelDataList,
boolean includeMissingAlterationsFromGenePanel,
Function<GenePanelData, String> caseUniqueIdentifier) {
ProfiledCaseType profiledCaseType = (caseUniqueIdentifier == patientUniqueIdentifier) ?
ProfiledCaseType.PATIENT : ProfiledCaseType.SAMPLE;
Map<String, Set<String>> casesWithDataInGenePanel = extractCasesWithDataInGenePanel(genePanelDataList, caseUniqueIdentifier);
List<GenePanel> genePanels = new ArrayList<>();
if (!casesWithDataInGenePanel.isEmpty()) {
Expand Down Expand Up @@ -64,7 +70,8 @@ public void calculate(List<T> alterationCounts,

for (AlterationCountByGene alterationCountByGene : alterationCounts) {
Integer entrezGeneId = alterationCountByGene.getEntrezGeneId();
Set<String> totalProfiledCases = new HashSet<String>();
Set<String> totalProfiledPatients = new HashSet<String>();
int totalProfiledSamples = 0;
Set<String> allMatchingGenePanelIds = new HashSet<String>();
Pair<Integer, String> key = new Pair<>(entrezGeneId,alterationCountByGene.getHugoGeneSymbol());
// different calculations depending on if gene is linked to gene panels
Expand All @@ -73,12 +80,21 @@ public void calculate(List<T> alterationCounts,
// as well as cases without panel data
for (GenePanel genePanel : geneGenePanelMap.get(key)) {
allMatchingGenePanelIds.add(genePanel.getStableId());
totalProfiledCases.addAll(casesWithDataInGenePanel.get(genePanel.getStableId()));
if (profiledCaseType == ProfiledCaseType.PATIENT) {
totalProfiledPatients.addAll(casesWithDataInGenePanel.get(genePanel.getStableId()));
} else {
totalProfiledSamples += casesWithDataInGenePanel.get(genePanel.getStableId()).size();
}
}
if (profiledCaseType == ProfiledCaseType.PATIENT) {
totalProfiledPatients.addAll(casesWithoutPanelData);
alterationCountByGene.setNumberOfProfiledCases(totalProfiledPatients.size());
} else {
totalProfiledSamples += casesWithoutPanelData.size();
alterationCountByGene.setNumberOfProfiledCases(totalProfiledSamples);
}
totalProfiledCases.addAll(casesWithoutPanelData);
alterationCountByGene.setNumberOfProfiledCases(totalProfiledCases.size());
} else {
alterationCountByGene.setNumberOfProfiledCases(profiledCasesCount);
alterationCountByGene.setNumberOfProfiledCases(casesWithoutPanelData.size());
}
alterationCountByGene.setMatchingGenePanelIds(allMatchingGenePanelIds);
}
Expand All @@ -96,17 +112,30 @@ public void calculate(List<T> alterationCounts,
if (!genesWithAlteration.containsKey(entrezGeneId)) {
AlterationCountByGene alterationCountByGene = new AlterationCountByGene();

Set<String> totalProfiledCases = new HashSet<String>();
Set<String> totalProfiledPatients = new HashSet<String>();
int totalProfiledSamples = 0;
Set<String> allMatchingGenePanelIds = new HashSet<String>();
for (GenePanel genePanel : geneGenePanelMap.get(key)) {
allMatchingGenePanelIds.add(genePanel.getStableId());
totalProfiledCases.addAll(casesWithDataInGenePanel.get(genePanel.getStableId()));
if (profiledCaseType == ProfiledCaseType.PATIENT) {
totalProfiledPatients.addAll(casesWithDataInGenePanel.get(genePanel.getStableId()));
} else {
totalProfiledSamples += casesWithDataInGenePanel.get(genePanel.getStableId()).size();
}
}
if (profiledCaseType == ProfiledCaseType.PATIENT) {
totalProfiledPatients.addAll(casesWithoutPanelData);
} else {
totalProfiledSamples += casesWithoutPanelData.size();
}
totalProfiledCases.addAll(casesWithoutPanelData);

alterationCountByGene.setEntrezGeneId(entrezGeneId);
alterationCountByGene.setMatchingGenePanelIds(allMatchingGenePanelIds);
alterationCountByGene.setNumberOfProfiledCases(totalProfiledCases.size());
if (profiledCaseType == ProfiledCaseType.PATIENT) {
alterationCountByGene.setNumberOfProfiledCases(totalProfiledPatients.size());
} else {
alterationCountByGene.setNumberOfProfiledCases(totalProfiledSamples);
}
alterationCountByGene.setNumberOfAlteredCases(0);
alterationCountByGene.setTotalCount(0);
alterationCountByGene.setHugoGeneSymbol(hugoGeneSymbol);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,21 +106,21 @@ public void calculate() throws Exception {

Assert.assertEquals(Integer.valueOf(3), alterationCounts.get(0).getNumberOfProfiledCases());
Assert.assertEquals(Integer.valueOf(2), alterationCounts.get(1).getNumberOfProfiledCases());
Assert.assertEquals(Integer.valueOf(3), alterationCounts.get(2).getNumberOfProfiledCases());
Assert.assertEquals(Integer.valueOf(1), alterationCounts.get(2).getNumberOfProfiledCases());


profiledSamplesCounter.calculate(alterationCounts, genePanelDataList, false, profiledSamplesCounter.patientUniqueIdentifier);

Assert.assertEquals(Integer.valueOf(2), alterationCounts.get(0).getNumberOfProfiledCases());
Assert.assertEquals(Integer.valueOf(2), alterationCounts.get(1).getNumberOfProfiledCases());
Assert.assertEquals(Integer.valueOf(2), alterationCounts.get(2).getNumberOfProfiledCases());
Assert.assertEquals(Integer.valueOf(1), alterationCounts.get(2).getNumberOfProfiledCases());

profiledSamplesCounter.calculate(alterationCounts, genePanelDataList, true, profiledSamplesCounter.patientUniqueIdentifier);

Assert.assertEquals(4, alterationCounts.size());
Assert.assertEquals(Integer.valueOf(2), alterationCounts.get(0).getNumberOfProfiledCases());
Assert.assertEquals(Integer.valueOf(2), alterationCounts.get(1).getNumberOfProfiledCases());
Assert.assertEquals(Integer.valueOf(2), alterationCounts.get(2).getNumberOfProfiledCases());
Assert.assertEquals(Integer.valueOf(1), alterationCounts.get(2).getNumberOfProfiledCases());
Assert.assertEquals(Integer.valueOf(2), alterationCounts.get(3).getNumberOfProfiledCases());
Assert.assertEquals(Integer.valueOf(4), alterationCounts.get(3).getEntrezGeneId());

Expand Down

0 comments on commit aa7aec0

Please sign in to comment.