Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for frequency in multi-study view #11331

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@
import org.cbioportal.model.util.Select;
import org.cbioportal.persistence.AlterationRepository;
import org.cbioportal.persistence.MolecularProfileRepository;
import org.cbioportal.persistence.StudyViewRepository;
import org.cbioportal.service.AlterationCountService;
import org.cbioportal.service.SignificantCopyNumberRegionService;
import org.cbioportal.service.SignificantlyMutatedGeneService;

import org.cbioportal.service.util.AlterationCountServiceUtil;
import org.cbioportal.service.util.AlterationEnrichmentUtil;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -266,7 +264,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 +279,21 @@ private <S extends AlterationCountBase> Pair<List<S>, Long> getAlterationGeneCou
}
AlterationCountServiceUtil.setupAlterationGeneCountsMap(studyAlterationCountByGenes, totalResult);
});

// Update number of profiled case considering the whole selected sample cohort
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this second part it looks mostly duplicated from above old code. Is this intended? Could you elaborate?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function's structure is mostly inspired from setupAlterationGeneCountsMap, although updateAlterationGeneCountsMap use similar arguments in a different way. In the update function, TotalResults is now used in the place of studyAlterationCountByGenes and updated with identifiers information (studyMolecularProfileCaseIdentifiers).
Additionally, I've deleted a part of the function that was duplicated and was re-updating the gene panel information.

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, studyMolecularProfileCaseIdentifiers); // 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 @@ -7,6 +7,7 @@
import org.cbioportal.model.Gistic;
import org.cbioportal.model.GisticToGene;
import org.cbioportal.model.MolecularProfile;
import org.cbioportal.model.MolecularProfileCaseIdentifier;
import org.cbioportal.model.MutSig;
import org.springframework.lang.NonNull;

Expand All @@ -15,6 +16,7 @@
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(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also some comments explaining this function would be great. We're implementing new coding guidelines so it'll help us to add JavaDocs in the future. Thanks

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added some comments int he code for clarity.

Map<String, S> totalResult, // the map of all genes with at least one mutation in the whole cohort
List<MolecularProfileCaseIdentifier> studyMolecularProfileCaseIdentifiers) { // 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(alterationCountByGene.getNumberOfProfiledCases() + studyMolecularProfileCaseIdentifiers.size()); // 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 @@ -12,8 +12,6 @@
import org.cbioportal.model.util.Select;
import org.cbioportal.persistence.AlterationRepository;
import org.cbioportal.persistence.MolecularProfileRepository;
import org.cbioportal.persistence.StudyViewRepository;
import org.cbioportal.service.SignificantlyMutatedGeneService;
import org.cbioportal.service.exception.MolecularProfileNotFoundException;
import org.cbioportal.service.util.AlterationEnrichmentUtil;
import org.cbioportal.service.util.MolecularProfileUtil;
Expand All @@ -33,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 @@ -129,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 @@ -149,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 @@ -208,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 @@ -230,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 @@ -247,7 +241,7 @@ 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());

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,22 @@
import org.apache.commons.math3.util.Pair;
import org.cbioportal.model.AlterationCountByGene;
import org.cbioportal.model.AlterationCountByStructuralVariant;
import org.cbioportal.model.CopyNumberCountByGene;
import org.cbioportal.model.MutSig;
import org.cbioportal.model.Gistic;
import org.cbioportal.model.GisticToGene;
import org.cbioportal.model.CopyNumberCountByGene;
import org.cbioportal.model.MolecularProfile;
import org.cbioportal.model.MutSig;
import org.cbioportal.model.MolecularProfileCaseIdentifier;
import org.cbioportal.model.GisticToGene;
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.*;

Expand Down Expand Up @@ -272,6 +274,24 @@ 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);

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

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

assertEquals(2, totalResult.size());
assertTrue(totalResult.containsKey("hugo1"));
Expand All @@ -318,6 +339,24 @@ 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);

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

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

assertEquals(2, totalResult.size());
assertTrue(totalResult.containsKey("hugo1::hugo2"));
Expand All @@ -367,6 +407,24 @@ 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);

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

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

assertEquals(2, totalResult.size());
assertTrue(totalResult.containsKey("12"));
Expand Down