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

Resolve KM plot censored event bug and add caching #11027

Merged
merged 2 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -150,12 +150,13 @@ public List<ClinicalData> getSurvivalData(List<String> studyIds,
).toList();

List<ClinicalEvent> patientCensoredEvents = filterClinicalEvents(patientStartEvents, survivalRequest.getCensoredEventRequestIdentifier());
Map<String, ClinicalEvent> patientCensoredEventsById = patientCensoredEvents.stream().collect(Collectors.toMap(ClinicalEventServiceImpl::getKey, Function.identity()));

return patientStartEvents.stream()
.flatMap(event -> {
ClinicalData clinicalDataMonths = buildClinicalSurvivalMonths(attributeIdPrefix, event, survivalRequest, patientEndEvents, patientCensoredEvents);
ClinicalData clinicalDataMonths = buildClinicalSurvivalMonths(attributeIdPrefix, event, survivalRequest, patientEndEventsById, patientCensoredEventsById);
if (clinicalDataMonths == null) return Stream.empty();
ClinicalData clinicalDataStatus = buildClinicalSurvivalStatus(attributeIdPrefix, event, patientEndEvents);
ClinicalData clinicalDataStatus = buildClinicalSurvivalStatus(attributeIdPrefix, event, patientEndEventsById);

return Stream.of(clinicalDataMonths, clinicalDataStatus);
}).toList();
Expand Down Expand Up @@ -200,22 +201,19 @@ private List<ClinicalEvent> filterClinicalEvents(List<ClinicalEvent> patientEven

// only fetch end timeline events for patients that have endClinicalEventsMeta and start timeline events
List<ClinicalEvent> queriedPatientEvents = new ArrayList<>();
if (CollectionUtils.isNotEmpty(clinicalEventsMeta) && CollectionUtils.isNotEmpty(filteredStudyIds)) {
Copy link
Member

Choose a reason for hiding this comment

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

Is this line causing the issue?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes

if (CollectionUtils.isNotEmpty(filteredStudyIds)) {
queriedPatientEvents = clinicalEventRepository.getTimelineEvents(filteredStudyIds, filteredPatientIds, clinicalEventsMeta);
}
return queriedPatientEvents;
}

private ClinicalData buildClinicalSurvivalMonths(String attributeIdPrefix, ClinicalEvent event, SurvivalRequest survivalRequest, List<ClinicalEvent> patientEndEvents, List<ClinicalEvent> patientCensoredEvents) {
private ClinicalData buildClinicalSurvivalMonths(String attributeIdPrefix, ClinicalEvent event, SurvivalRequest survivalRequest, Map<String, ClinicalEvent> patientEndEventsById, Map<String, ClinicalEvent> patientCensoredEventsById) {
final String SURVIVAL_MONTH_ATTRIBUTE = attributeIdPrefix + "_MONTHS";
ClinicalData clinicalDataMonths = new ClinicalData();
clinicalDataMonths.setStudyId(event.getStudyId());
clinicalDataMonths.setPatientId(event.getPatientId());
clinicalDataMonths.setAttrId(SURVIVAL_MONTH_ATTRIBUTE);

Map<String, ClinicalEvent> patientEndEventsById = patientEndEvents.stream().collect(Collectors.toMap(ClinicalEventServiceImpl::getKey, Function.identity()));
Map<String, ClinicalEvent> patientCensoredEventsById = patientCensoredEvents.stream().collect(Collectors.toMap(ClinicalEventServiceImpl::getKey, Function.identity()));

ToIntFunction<ClinicalEvent> startPositionIdentifier = getPositionIdentifier(survivalRequest.getStartEventRequestIdentifier().getPosition());
ToIntFunction<ClinicalEvent> endPositionIdentifier = survivalRequest.getEndEventRequestIdentifier() == null ? ClinicalEvent::getStopDate : getPositionIdentifier(survivalRequest.getEndEventRequestIdentifier().getPosition());
ToIntFunction<ClinicalEvent> censoredPositionIdentifier = survivalRequest.getCensoredEventRequestIdentifier() == null ? ClinicalEvent::getStopDate : getPositionIdentifier(survivalRequest.getCensoredEventRequestIdentifier().getPosition());
Expand All @@ -241,8 +239,7 @@ private ClinicalData buildClinicalSurvivalMonths(String attributeIdPrefix, Clini
return clinicalDataMonths;
}

private ClinicalData buildClinicalSurvivalStatus(String attributeIdPrefix, ClinicalEvent event, List<ClinicalEvent> patientEndEvents) {
Map<String, ClinicalEvent> patientEndEventsById = patientEndEvents.stream().collect(Collectors.toMap(ClinicalEventServiceImpl::getKey, Function.identity()));
private ClinicalData buildClinicalSurvivalStatus(String attributeIdPrefix, ClinicalEvent event, Map<String, ClinicalEvent> patientEndEventsById) {

ClinicalData clinicalDataStatus = new ClinicalData();
clinicalDataStatus.setStudyId(event.getStudyId());
Expand Down
23 changes: 13 additions & 10 deletions src/main/java/org/cbioportal/web/ClinicalEventController.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,10 @@
import org.cbioportal.service.exception.StudyNotFoundException;
import org.cbioportal.web.config.InternalApiTags;
import org.cbioportal.web.config.annotation.InternalApi;
import org.cbioportal.web.parameter.ClinicalEventAttributeRequest;
import org.cbioportal.web.parameter.Direction;
import org.cbioportal.web.parameter.HeaderKeyConstants;
import org.cbioportal.web.parameter.PagingConstants;
import org.cbioportal.web.parameter.PatientIdentifier;
import org.cbioportal.web.parameter.Projection;
import org.cbioportal.web.parameter.*;
import org.cbioportal.web.parameter.sort.ClinicalEventSortBy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
Expand Down Expand Up @@ -144,6 +140,15 @@ public ResponseEntity<List<ClinicalEvent>> fetchClinicalEventsMeta(
@Parameter(hidden = true) // prevent reference to this attribute in the swagger-ui interface. This attribute is needed for the @PreAuthorize tag above.
@Valid @RequestAttribute(required = false, value = "interceptedClinicalEventAttributeRequest") ClinicalEventAttributeRequest interceptedClinicalEventAttributeRequest) {

return new ResponseEntity<>(cachedClinicalEventsMeta(interceptedClinicalEventAttributeRequest), HttpStatus.OK);

}

@Cacheable(
cacheResolver = "staticRepositoryCacheOneResolver",
condition = "@cacheEnabledConfig.getEnabled()"
)
public List<ClinicalEvent> cachedClinicalEventsMeta(ClinicalEventAttributeRequest interceptedClinicalEventAttributeRequest) {
List<String> studyIds = new ArrayList<>();
List<String> patientIds = new ArrayList<>();
for (PatientIdentifier patientIdentifier : interceptedClinicalEventAttributeRequest.getPatientIdentifiers()) {
Expand All @@ -160,9 +165,7 @@ public ResponseEntity<List<ClinicalEvent>> fetchClinicalEventsMeta(
return clinicalEvent;
})
.toList();

return new ResponseEntity<>(clinicalEventService.getClinicalEventsMeta(
studyIds, patientIds, clinicalEventsRequest), HttpStatus.OK);

return clinicalEventService.getClinicalEventsMeta(
studyIds, patientIds, clinicalEventsRequest);
}
}
22 changes: 15 additions & 7 deletions src/main/java/org/cbioportal/web/SurvivalController.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.cbioportal.web.parameter.PatientIdentifier;
import org.cbioportal.web.parameter.SurvivalRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
Expand All @@ -36,7 +37,7 @@
@Tag(name = "Survival", description = " ")
public class SurvivalController {
private final ClinicalEventService clinicalEventService;

@Autowired
public SurvivalController(ClinicalEventService clinicalEventService) {
this.clinicalEventService = clinicalEventService;
Expand All @@ -59,18 +60,25 @@ public ResponseEntity<List<ClinicalData>> fetchSurvivalData(
// prevent reference to this attribute in the swagger-ui interface. this attribute is needed for the @PreAuthorize tag above.
@Valid @RequestAttribute(required = false, value = "interceptedSurvivalRequest") SurvivalRequest interceptedSurvivalRequest) {

return new ResponseEntity<>(cachedSurvivalData(interceptedSurvivalRequest),
HttpStatus.OK);
}

@Cacheable(
cacheResolver = "staticRepositoryCacheOneResolver",
Copy link
Member

Choose a reason for hiding this comment

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

Wondering why we need to use staticRepositoryCacheOneResolver at here, I think for the case with lots of permutations like this (study_ids with sample_ids) we should use generalRepositoryCacheResolver

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure. will update

condition = "@cacheEnabledConfig.getEnabled()"
)
public List<ClinicalData> cachedSurvivalData(SurvivalRequest interceptedSurvivalRequest) {
List<String> studyIds = new ArrayList<>();
List<String> patientIds = new ArrayList<>();
for (PatientIdentifier patientIdentifier : interceptedSurvivalRequest.getPatientIdentifiers()) {
studyIds.add(patientIdentifier.getStudyId());
patientIds.add(patientIdentifier.getPatientId());
}

List<ClinicalData> result = clinicalEventService.getSurvivalData(studyIds,
patientIds,
interceptedSurvivalRequest.getAttributeIdPrefix(),
interceptedSurvivalRequest);

return new ResponseEntity<>(result, HttpStatus.OK);
return clinicalEventService.getSurvivalData(studyIds,
patientIds,
interceptedSurvivalRequest.getAttributeIdPrefix(),
interceptedSurvivalRequest);
}
}
Loading