From be8648cbb6a8d06d10a8402cff8be3f3c52876ac Mon Sep 17 00:00:00 2001 From: Ruslan Forostianov Date: Wed, 2 Oct 2024 21:15:52 +0200 Subject: [PATCH] Query sample IDs using Virtual Study View Filters is it's of dynamic type --- .../web/SessionServiceController.java | 37 ++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/cbioportal/web/SessionServiceController.java b/src/main/java/org/cbioportal/web/SessionServiceController.java index 83cf95c932a..286f8d9aa8b 100644 --- a/src/main/java/org/cbioportal/web/SessionServiceController.java +++ b/src/main/java/org/cbioportal/web/SessionServiceController.java @@ -23,10 +23,13 @@ import org.cbioportal.web.parameter.PageSettingsIdentifier; import org.cbioportal.web.parameter.PagingConstants; import org.cbioportal.web.parameter.ResultsPageSettings; +import org.cbioportal.web.parameter.SampleIdentifier; import org.cbioportal.web.parameter.SessionPage; import org.cbioportal.web.parameter.StudyPageSettings; import org.cbioportal.web.parameter.VirtualStudy; import org.cbioportal.web.parameter.VirtualStudyData; +import org.cbioportal.web.parameter.VirtualStudySamples; +import org.cbioportal.web.util.StudyViewFilterApplier; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; @@ -57,6 +60,7 @@ import java.util.Optional; import java.util.Set; import java.util.regex.Pattern; +import java.util.stream.Collectors; import static org.cbioportal.web.PublicVirtualStudiesController.ALL_USERS; @@ -76,6 +80,9 @@ public class SessionServiceController { @Autowired private ObjectMapper sessionServiceObjectMapper; + @Autowired + private StudyViewFilterApplier studyViewFilterApplier; + @Value("${session.service.url:}") private String sessionServiceURL; @@ -211,7 +218,12 @@ public ResponseEntity getSession(@PathVariable Session.SessionType type Session session; switch (type) { case virtual_study: - session = sessionServiceObjectMapper.readValue(sessionDataJson, VirtualStudy.class); + VirtualStudy virtualStudy = sessionServiceObjectMapper.readValue(sessionDataJson, VirtualStudy.class); + VirtualStudyData virtualStudyData = virtualStudy.getData(); + if (Boolean.TRUE.equals(virtualStudyData.getDynamic())) { + populateVirtualStudySamples(virtualStudyData); + } + session = virtualStudy; break; case settings: session = sessionServiceObjectMapper.readValue(sessionDataJson, PageSettings.class); @@ -232,6 +244,29 @@ public ResponseEntity getSession(@PathVariable Session.SessionType type } } + /** + * This method populates the `virtualStudyData` object with a new set of sample IDs retrieved as the result of executing a query based on virtual study view filters. + * It first applies the filters defined within the study view, runs the query to fetch the relevant sample IDs, and then updates the virtualStudyData to reflect these fresh results. + * This ensures that the virtual study contains the latest sample IDs. + * @param virtualStudyData + */ + private void populateVirtualStudySamples(VirtualStudyData virtualStudyData) { + List sampleIdentifiers = studyViewFilterApplier.apply(virtualStudyData.getStudyViewFilter()); + 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); + } + @RequestMapping(value = "/virtual_study", method = RequestMethod.GET) @ApiResponse(responseCode = "200", description = "OK", content = @Content(array = @ArraySchema(schema = @Schema(implementation = VirtualStudy.class))))