diff --git a/src/main/java/org/cbioportal/web/SessionServiceController.java b/src/main/java/org/cbioportal/web/SessionServiceController.java index 286f8d9aa8b..6f5182b122d 100644 --- a/src/main/java/org/cbioportal/web/SessionServiceController.java +++ b/src/main/java/org/cbioportal/web/SessionServiceController.java @@ -252,19 +252,36 @@ public ResponseEntity getSession(@PathVariable Session.SessionType type */ private void populateVirtualStudySamples(VirtualStudyData virtualStudyData) { List sampleIdentifiers = studyViewFilterApplier.apply(virtualStudyData.getStudyViewFilter()); + Set virtualStudySamples = extractVirtualStudySamples(sampleIdentifiers); + virtualStudyData.setStudies(virtualStudySamples); + } + + /** + * Transforms list of sample identifiers to set of virtual study samples + * @param sampleIdentifiers + */ + private Set extractVirtualStudySamples(List sampleIdentifiers) { + Map> 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> groupSampleIdsByStudyId(List sampleIdentifiers) { Map> sampleIdsByStudyId = sampleIdentifiers .stream() .collect( Collectors.groupingBy( SampleIdentifier::getStudyId, Collectors.mapping(SampleIdentifier::getSampleId, Collectors.toSet()))); - Set 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)