diff --git a/src/main/java/org/cbioportal/service/impl/ClinicalDataDensityPlotServiceImpl.java b/src/main/java/org/cbioportal/service/impl/ClinicalDataDensityPlotServiceImpl.java index e4abf2e631c..940828bab10 100644 --- a/src/main/java/org/cbioportal/service/impl/ClinicalDataDensityPlotServiceImpl.java +++ b/src/main/java/org/cbioportal/service/impl/ClinicalDataDensityPlotServiceImpl.java @@ -34,7 +34,7 @@ public DensityPlotData getDensityPlotData(List sampleClinicalData, result.setBins(new ArrayList<>()); Map> clinicalDataGroupedBySampleId = sampleClinicalData.stream(). - collect(Collectors.groupingBy(ClinicalData::getSampleId)); + collect(Collectors.groupingBy(c -> c.getStudyId() + "_" + c.getSampleId())); List extractedXYClinicalData = clinicalDataGroupedBySampleId.entrySet().stream() .filter(entry -> entry.getValue().size() == 2 && diff --git a/src/main/java/org/cbioportal/service/impl/ViolinPlotServiceImpl.java b/src/main/java/org/cbioportal/service/impl/ViolinPlotServiceImpl.java index e3da1e50019..3b7527f8ca7 100644 --- a/src/main/java/org/cbioportal/service/impl/ViolinPlotServiceImpl.java +++ b/src/main/java/org/cbioportal/service/impl/ViolinPlotServiceImpl.java @@ -39,9 +39,11 @@ public ClinicalViolinPlotData getClinicalViolinPlotData( result.setRows(new ArrayList<>()); // collect filtered samples into a set for quick lookup - Set samplesForSampleCountsIds = + Set samplesForSampleCountsIds = samplesForSampleCounts.stream() - .map(Sample::getInternalId) + .map(s -> s.getInternalId() == null ? + s.getCancerStudyIdentifier() + "_" + s.getStableId(): s.getInternalId().toString() + ) .collect(Collectors.toSet()); // clinicalDataMap is a map sampleId->studyId->data @@ -214,12 +216,14 @@ public ClinicalViolinPlotData getClinicalViolinPlotData( @SafeVarargs private static int countFilteredSamples( - Set filteredSampleIds, + Set filteredSampleIds, List... dataLists ) { return (int) Arrays.stream(dataLists) .flatMap(Collection::stream) - .map(ClinicalData::getInternalId) + .map(c -> c.getInternalId() == null ? + c.getStudyId() + "_" + c.getSampleId() : c.getInternalId().toString() + ) .filter(filteredSampleIds::contains) .count(); } diff --git a/src/main/java/org/cbioportal/web/columnar/StudyViewColumnStoreController.java b/src/main/java/org/cbioportal/web/columnar/StudyViewColumnStoreController.java index d74af6613b9..c2aa05fa076 100644 --- a/src/main/java/org/cbioportal/web/columnar/StudyViewColumnStoreController.java +++ b/src/main/java/org/cbioportal/web/columnar/StudyViewColumnStoreController.java @@ -343,9 +343,12 @@ public ResponseEntity fetchClinicalDataViolinPlots( @Parameter(required = true, description = "Study view filter") @Valid @RequestBody(required = false) StudyViewFilter studyViewFilter) { + // fetch the samples by using the provided study view filter List filteredSamples = studyViewColumnarService.getFilteredSamples(interceptedStudyViewFilter); - // get samples that are filtered without the numerical filter - this is violin plot data + // remove the numerical clinical data filter from the study view filter. + // this new modified filter is used to fetch sample and patient clinical data. + // this is required to get the complete violin plot data. if (interceptedStudyViewFilter.getClinicalDataFilters() != null) { interceptedStudyViewFilter.getClinicalDataFilters().stream() .filter(f->f.getAttributeId().equals(numericalAttributeId)) @@ -362,11 +365,22 @@ public ResponseEntity fetchClinicalDataViolinPlots( List patientClinicalDataList = filterNonEmptyClinicalData( studyViewColumnarService.getPatientClinicalData(interceptedStudyViewFilter, attributeIds) ); - - List combinedClinicalDataList = Stream.concat( - sampleClinicalDataList.stream(), - convertPatientClinicalDataToSampleClinicalData(patientClinicalDataList, filteredSamples).stream() - ).toList(); + + List combinedClinicalDataList; + if (patientClinicalDataList.isEmpty()) { + combinedClinicalDataList = sampleClinicalDataList; + } else { + // we previously fetched sample and patient clinical data with the modified study view filter, + // however filteredSamples reflects only the original unmodified study view filter. + // we need to fetch samples again to get the samples corresponding to this modified filter, + // otherwise patient to sample mapping may be incomplete. + List samplesWithoutNumericalFilter = studyViewColumnarService.getFilteredSamples(interceptedStudyViewFilter); + + combinedClinicalDataList = Stream.concat( + sampleClinicalDataList.stream(), + convertPatientClinicalDataToSampleClinicalData(patientClinicalDataList, samplesWithoutNumericalFilter).stream() + ).toList(); + } // Only mutation count can use log scale boolean useLogScale = logScale && numericalAttributeId.equals("MUTATION_COUNT"); diff --git a/src/main/java/org/cbioportal/web/columnar/util/ClinicalDataViolinPlotUtil.java b/src/main/java/org/cbioportal/web/columnar/util/ClinicalDataViolinPlotUtil.java index 3041a1a498f..597845db2ef 100644 --- a/src/main/java/org/cbioportal/web/columnar/util/ClinicalDataViolinPlotUtil.java +++ b/src/main/java/org/cbioportal/web/columnar/util/ClinicalDataViolinPlotUtil.java @@ -18,14 +18,14 @@ public static List filterNonEmptyClinicalData(List c public static List convertPatientClinicalDataToSampleClinicalData( List patientClinicalDataList, - List filteredSamples + List samplesWithoutNumericalFilter ) { List sampleClinicalDataList = new ArrayList<>(); - Map>> patientToSamples = filteredSamples + Map>> patientToSamples = samplesWithoutNumericalFilter .stream() .collect(Collectors.groupingBy( - s -> s.getCancerStudyIdentifier() + "_" + s.getPatientStableId(), + Sample::getPatientStableId, Collectors.groupingBy(Sample::getCancerStudyIdentifier) )); @@ -41,7 +41,7 @@ public static List convertPatientClinicalDataToSampleClinicalData( newData.setPatientId(d.getPatientId()); newData.setStudyId(d.getStudyId()); newData.setAttrValue(d.getAttrValue()); - newData.setSampleId(s.getCancerStudyIdentifier() + "_" + s.getStableId()); + newData.setSampleId(s.getStableId()); sampleClinicalDataList.add(newData); } diff --git a/src/main/resources/org/cbioportal/persistence/mybatisclickhouse/StudyViewMapper.xml b/src/main/resources/org/cbioportal/persistence/mybatisclickhouse/StudyViewMapper.xml index 7ed1c05f8d3..fd7d37b6d6f 100644 --- a/src/main/resources/org/cbioportal/persistence/mybatisclickhouse/StudyViewMapper.xml +++ b/src/main/resources/org/cbioportal/persistence/mybatisclickhouse/StudyViewMapper.xml @@ -91,8 +91,8 @@ SELECT - patient_unique_id as patientId, + replaceOne(patient_unique_id, concat(cancer_study_identifier, '_'), '') as patientId, attribute_name as attrId, attribute_value as attrValue, cancer_study_identifier as studyId