Skip to content

Commit

Permalink
Refactor dynamic virtual study population
Browse files Browse the repository at this point in the history
Extract smaller functions
  • Loading branch information
forus committed Oct 28, 2024
1 parent cbab49a commit 1adca8c
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions src/main/java/org/cbioportal/web/SessionServiceController.java
Original file line number Diff line number Diff line change
Expand Up @@ -252,19 +252,36 @@ public ResponseEntity<Session> getSession(@PathVariable Session.SessionType type
*/
private void populateVirtualStudySamples(VirtualStudyData virtualStudyData) {
List<SampleIdentifier> sampleIdentifiers = studyViewFilterApplier.apply(virtualStudyData.getStudyViewFilter());
Set<VirtualStudySamples> virtualStudySamples = extractVirtualStudySamples(sampleIdentifiers);
virtualStudyData.setStudies(virtualStudySamples);
}

/**
* Transforms list of sample identifiers to set of virtual study samples
* @param sampleIdentifiers
*/
private Set<VirtualStudySamples> extractVirtualStudySamples(List<SampleIdentifier> sampleIdentifiers) {
Map<String, Set<String>> sampleIdsByStudyId = groupSampleIdsByStudyId(sampleIdentifiers);
return sampleIdsByStudyId.entrySet().stream().map(entry -> {
VirtualStudySamples vss = new VirtualStudySamples();
vss.setId(entry.getKey());
vss.setSamples(entry.getValue());
return vss;
}).collect(Collectors.toSet());
}

/**
* Groups sample IDs by their study ID
* @param sampleIdentifiers
*/
private Map<String, Set<String>> groupSampleIdsByStudyId(List<SampleIdentifier> sampleIdentifiers) {
Map<String, Set<String>> sampleIdsByStudyId = sampleIdentifiers
.stream()
.collect(
Collectors.groupingBy(
SampleIdentifier::getStudyId,
Collectors.mapping(SampleIdentifier::getSampleId, Collectors.toSet())));
Set<VirtualStudySamples> virtualStudySamples = sampleIdsByStudyId.entrySet().stream().map(entry -> {
VirtualStudySamples vss = new VirtualStudySamples();
vss.setId(entry.getKey());
vss.setSamples(entry.getValue());
return vss;
}).collect(Collectors.toSet());
virtualStudyData.setStudies(virtualStudySamples);
return sampleIdsByStudyId;
}

@RequestMapping(value = "/virtual_study", method = RequestMethod.GET)
Expand Down

0 comments on commit 1adca8c

Please sign in to comment.