Skip to content

Commit

Permalink
Fix for frequency in multi-study view (#11331)
Browse files Browse the repository at this point in the history
* Fix frequency issue for same genePanel
Rebase to original branch
  • Loading branch information
eugeniomazzone authored Feb 28, 2025
1 parent 74cf611 commit b4cade9
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,8 @@ private <S extends AlterationCountBase> Pair<List<S>, Long> getAlterationGeneCou
.collect(Collectors.toMap(MolecularProfile::getStableId, MolecularProfile::getCancerStudyIdentifier));

Map<String, S> totalResult = new HashMap<>();


// Initialize mutation info for study view
molecularProfileCaseIdentifiers
.stream()
.collect(Collectors
Expand All @@ -280,6 +281,22 @@ private <S extends AlterationCountBase> Pair<List<S>, Long> getAlterationGeneCou
}
AlterationCountServiceUtil.setupAlterationGeneCountsMap(studyAlterationCountByGenes, totalResult);
});

profiledCasesCount.set(0L);
// Update number of profiled case considering the whole selected sample cohort
molecularProfileCaseIdentifiers // the list of all cases in the cohort
.stream()
.collect(Collectors
.groupingBy(identifier -> molecularProfileIdStudyIdMap.get(identifier.getMolecularProfileId())))
.values()
.forEach(studyMolecularProfileCaseIdentifiers -> {
List<S> studyAlterationCountByGenes = dataFetcher.apply(studyMolecularProfileCaseIdentifiers); // the list of all genes with at least one mutation in the study
if (includeFrequency) {
Long studyProfiledCasesCount = includeFrequencyFunction.apply(studyMolecularProfileCaseIdentifiers, studyAlterationCountByGenes);
profiledCasesCount.updateAndGet(v -> v + studyProfiledCasesCount);
}
AlterationCountServiceUtil.updateAlterationGeneCountsMap(totalResult, profiledCasesCount.get()); // Get study identifiers and update TotalResult
});
alterationCountByGenes = new ArrayList<>(totalResult.values());
}
return new Pair<>(alterationCountByGenes, profiledCasesCount.get());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
import org.cbioportal.legacy.model.GisticToGene;
import org.cbioportal.legacy.model.MolecularProfile;
import org.cbioportal.legacy.model.MutSig;

import org.springframework.lang.NonNull;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.ArrayList;
import java.util.function.Function;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -148,7 +150,7 @@ public static <S extends AlterationCountBase> void setupAlterationGeneCountsMap(
S alterationCountByGene = totalResult.get(key);
alterationCountByGene.setTotalCount(alterationCountByGene.getTotalCount() + datum.getTotalCount());
alterationCountByGene.setNumberOfAlteredCases(alterationCountByGene.getNumberOfAlteredCases() + datum.getNumberOfAlteredCases());
alterationCountByGene.setNumberOfProfiledCases(alterationCountByGene.getNumberOfProfiledCases() + datum.getNumberOfProfiledCases());
alterationCountByGene.setNumberOfProfiledCases(0); //Set number of cases to zero
Set<String> matchingGenePanelIds = new HashSet<>();
if (!alterationCountByGene.getMatchingGenePanelIds().isEmpty()) {
matchingGenePanelIds.addAll(alterationCountByGene.getMatchingGenePanelIds());
Expand All @@ -159,10 +161,24 @@ public static <S extends AlterationCountBase> void setupAlterationGeneCountsMap(
alterationCountByGene.setMatchingGenePanelIds(matchingGenePanelIds);
totalResult.put(key, alterationCountByGene);
} else {
datum.setNumberOfProfiledCases(0); //Ensure number of cases is initialized to zero
totalResult.put(key, datum);
}
});
}

public static <S extends AlterationCountBase> void updateAlterationGeneCountsMap(
Map<String, S> totalResult, // the map of all genes with at least one mutation in the whole cohort
Long profiledCasesCount) { // the list of all cases in the study

List<S> allGene= new ArrayList<>(totalResult.values()); // get all genes with at least one mutation in the whole cohort
allGene.forEach(datum -> { // for each gene in the whole cohort
String key = datum.getUniqueEventKey(); // get the unique key of the gene
S alterationCountByGene = totalResult.get(key); // get the gene from the map
alterationCountByGene.setNumberOfProfiledCases(profiledCasesCount.intValue()); // the update the number of profiled cases for each study
totalResult.put(key, alterationCountByGene); // update the map
});
}


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,7 @@
import java.util.List;
import java.util.TreeSet;

import static org.mockito.Mockito.anyBoolean;
import static org.mockito.Mockito.anyList;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.*;

@RunWith(MockitoJUnitRunner.class)
public class AlterationCountServiceImplTest extends BaseServiceImplTest {
Expand Down Expand Up @@ -127,7 +123,7 @@ public void getSampleAlterationGeneCounts() {
includeMissingAlterationsFromGenePanel,
alterationFilter);

verify(alterationEnrichmentUtil, times(1)).includeFrequencyForSamples(anyList(), anyList(), anyBoolean());
verify(alterationEnrichmentUtil, times(2)).includeFrequencyForSamples(anyList(), anyList(), anyBoolean());

}

Expand All @@ -147,7 +143,7 @@ public void getPatientAlterationGeneCounts() {
includeMissingAlterationsFromGenePanel,
alterationFilter);

verify(alterationEnrichmentUtil, times(1)).includeFrequencyForPatients(anyList(), anyList(), anyBoolean());
verify(alterationEnrichmentUtil, times(2)).includeFrequencyForPatients(anyList(), anyList(), anyBoolean());
}


Expand Down Expand Up @@ -206,7 +202,7 @@ public void getSampleCnaGeneCounts() {
includeMissingAlterationsFromGenePanel,
alterationFilter);

verify(alterationEnrichmentUtilCna, times(1)).includeFrequencyForSamples(anyList(), anyList(), anyBoolean());
verify(alterationEnrichmentUtilCna, times(2)).includeFrequencyForSamples(anyList(), anyList(), anyBoolean());
Assert.assertEquals(expectedCnaCountByGeneList, result.getFirst());

}
Expand All @@ -228,7 +224,7 @@ public void getPatientCnaGeneCounts() {
includeMissingAlterationsFromGenePanel,
alterationFilter);

verify(alterationEnrichmentUtilCna, times(1)).includeFrequencyForPatients(anyList(), anyList(), anyBoolean());
verify(alterationEnrichmentUtilCna, times(2)).includeFrequencyForPatients(anyList(), anyList(), anyBoolean());
Assert.assertEquals(expectedCnaCountByGeneList, result.getFirst());
}

Expand All @@ -245,8 +241,8 @@ public void getSampleStructuralVariantCounts() {
includeMissingAlterationsFromGenePanel,
alterationFilter);

verify(alterationEnrichmentUtilStructVar, times(1)).includeFrequencyForSamples(anyList(), anyList(), anyBoolean());
verify(alterationEnrichmentUtilStructVar, times(2)).includeFrequencyForSamples(anyList(), anyList(), anyBoolean());
Assert.assertEquals(expectedStructuralVariantList, result.getFirst());

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,22 @@
import org.cbioportal.legacy.model.GisticToGene;
import org.cbioportal.legacy.model.MolecularProfile;
import org.cbioportal.legacy.model.MutSig;
import org.cbioportal.legacy.service.util.AlterationCountServiceUtil;
import org.cbioportal.legacy.model.MolecularProfileCaseIdentifier;
import org.junit.Test;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Arrays;
import java.util.ArrayList;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import static org.junit.Assert.*;

public class AlterationCountServiceUtilTest {

Expand Down Expand Up @@ -273,6 +277,26 @@ public void testSetupGisticMap() {

@Test
public void testSetupAlterationGeneCountsMapWithAlterationCountByGene() {

MolecularProfileCaseIdentifier sample1 = new MolecularProfileCaseIdentifier();
sample1.setCaseId("sample1");
sample1.setMolecularProfileId("test1");

MolecularProfileCaseIdentifier sample2 = new MolecularProfileCaseIdentifier();
sample2.setCaseId("sample2");
sample2.setMolecularProfileId("test1");

MolecularProfileCaseIdentifier sample3 = new MolecularProfileCaseIdentifier();
sample3.setCaseId("sample3");
sample3.setMolecularProfileId("test1");

List<MolecularProfileCaseIdentifier> studyMolecularProfileCaseIdentifiers = new ArrayList<MolecularProfileCaseIdentifier> ();
studyMolecularProfileCaseIdentifiers.add(sample1);
studyMolecularProfileCaseIdentifiers.add(sample2);
studyMolecularProfileCaseIdentifiers.add(sample3);

Long profiledCasesCount = (long)studyMolecularProfileCaseIdentifiers.size();

AlterationCountByGene datum1 = new AlterationCountByGene();
datum1.setHugoGeneSymbol("hugo1");
datum1.setTotalCount(1);
Expand All @@ -298,6 +322,7 @@ public void testSetupAlterationGeneCountsMapWithAlterationCountByGene() {
Map<String, AlterationCountByGene> totalResult = new HashMap<>();

AlterationCountServiceUtil.setupAlterationGeneCountsMap(studyAlterationCountByGenes, totalResult);
AlterationCountServiceUtil.updateAlterationGeneCountsMap(totalResult, profiledCasesCount);

assertEquals(2, totalResult.size());
assertTrue(totalResult.containsKey("hugo1"));
Expand All @@ -319,6 +344,26 @@ public void testSetupAlterationGeneCountsMapWithAlterationCountByGene() {

@Test
public void testSetupAlterationGeneCountsMapWithAlterationCountByStructuralVariant() {

MolecularProfileCaseIdentifier sample1 = new MolecularProfileCaseIdentifier();
sample1.setCaseId("sample1");
sample1.setMolecularProfileId("test1");

MolecularProfileCaseIdentifier sample2 = new MolecularProfileCaseIdentifier();
sample2.setCaseId("sample2");
sample2.setMolecularProfileId("test1");

MolecularProfileCaseIdentifier sample3 = new MolecularProfileCaseIdentifier();
sample3.setCaseId("sample3");
sample3.setMolecularProfileId("test1");

List<MolecularProfileCaseIdentifier> studyMolecularProfileCaseIdentifiers = new ArrayList<MolecularProfileCaseIdentifier> ();
studyMolecularProfileCaseIdentifiers.add(sample1);
studyMolecularProfileCaseIdentifiers.add(sample2);
studyMolecularProfileCaseIdentifiers.add(sample3);

Long profiledCasesCount = (long)studyMolecularProfileCaseIdentifiers.size();

AlterationCountByStructuralVariant datum1 = new AlterationCountByStructuralVariant();
datum1.setGene1HugoGeneSymbol("hugo1");
datum1.setGene2HugoGeneSymbol("hugo2");
Expand Down Expand Up @@ -347,6 +392,7 @@ public void testSetupAlterationGeneCountsMapWithAlterationCountByStructuralVaria
Map<String, AlterationCountByStructuralVariant> totalResult = new HashMap<>();

AlterationCountServiceUtil.setupAlterationGeneCountsMap(studyAlterationCountByGenes, totalResult);
AlterationCountServiceUtil.updateAlterationGeneCountsMap(totalResult, profiledCasesCount);

assertEquals(2, totalResult.size());
assertTrue(totalResult.containsKey("hugo1::hugo2"));
Expand All @@ -368,6 +414,26 @@ public void testSetupAlterationGeneCountsMapWithAlterationCountByStructuralVaria

@Test
public void testSetupAlterationGeneCountsMapWithCopyNumberCountByGene() {

MolecularProfileCaseIdentifier sample1 = new MolecularProfileCaseIdentifier();
sample1.setCaseId("sample1");
sample1.setMolecularProfileId("test1");

MolecularProfileCaseIdentifier sample2 = new MolecularProfileCaseIdentifier();
sample2.setCaseId("sample2");
sample2.setMolecularProfileId("test1");

MolecularProfileCaseIdentifier sample3 = new MolecularProfileCaseIdentifier();
sample3.setCaseId("sample3");
sample3.setMolecularProfileId("test1");

List<MolecularProfileCaseIdentifier> studyMolecularProfileCaseIdentifiers = new ArrayList<MolecularProfileCaseIdentifier> ();
studyMolecularProfileCaseIdentifiers.add(sample1);
studyMolecularProfileCaseIdentifiers.add(sample2);
studyMolecularProfileCaseIdentifiers.add(sample3);

Long profiledCasesCount = (long)studyMolecularProfileCaseIdentifiers.size();

CopyNumberCountByGene datum1 = new CopyNumberCountByGene();
datum1.setEntrezGeneId(1);
datum1.setAlteration(2);
Expand Down Expand Up @@ -396,6 +462,7 @@ public void testSetupAlterationGeneCountsMapWithCopyNumberCountByGene() {
Map<String, CopyNumberCountByGene> totalResult = new HashMap<>();

AlterationCountServiceUtil.setupAlterationGeneCountsMap(studyAlterationCountByGenes, totalResult);
AlterationCountServiceUtil.updateAlterationGeneCountsMap(totalResult, profiledCasesCount);

assertEquals(2, totalResult.size());
assertTrue(totalResult.containsKey("12"));
Expand All @@ -416,4 +483,4 @@ public void testSetupAlterationGeneCountsMapWithCopyNumberCountByGene() {
}


}
}

0 comments on commit b4cade9

Please sign in to comment.