Skip to content

Commit

Permalink
fix clinical data NA counts for multiple studies
Browse files Browse the repository at this point in the history
  • Loading branch information
onursumer committed Sep 5, 2024
1 parent b36bffb commit d791618
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public interface StudyViewRepository {

int getFilteredSamplesCount(StudyViewFilterContext studyViewFilterContext);

int getFilteredPatientCount(StudyViewFilterContext studyViewFilterContext);

Map<String, Set<String>> getMatchingGenePanelIds(StudyViewFilterContext studyViewFilterContext, String alterationType);

int getTotalProfiledCountsByAlterationType(StudyViewFilterContext studyViewFilterContext, String alterationType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public interface StudyViewMapper {

int getFilteredSamplesCount(@Param("studyViewFilterHelper") StudyViewFilterHelper studyViewFilterHelper);

int getFilteredPatientsCount(@Param("studyViewFilterHelper") StudyViewFilterHelper studyViewFilterHelper);

List<GenePanelToGene> getMatchingGenePanelIds(StudyViewFilterHelper studyViewFilterHelper, String alterationType);

int getTotalProfiledCountByAlterationType(StudyViewFilterHelper studyViewFilterHelper, String alterationType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,11 @@ public int getFilteredSamplesCount(StudyViewFilterContext studyViewFilterContext
return mapper.getFilteredSamplesCount(createStudyViewFilterHelper(studyViewFilterContext));
}

@Override
public int getFilteredPatientCount(StudyViewFilterContext studyViewFilterContext) {
return mapper.getFilteredPatientsCount(createStudyViewFilterHelper(studyViewFilterContext));
}

@Override
public Map<String, Set<String>> getMatchingGenePanelIds(StudyViewFilterContext studyViewFilterContext, String alterationType) {
return mapper.getMatchingGenePanelIds(createStudyViewFilterHelper(studyViewFilterContext), alterationType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,60 @@ public Map<String, ClinicalDataType> getClinicalAttributeDatatypeMap() {

@Override
public List<ClinicalDataCountItem> getClinicalDataCounts(StudyViewFilter studyViewFilter, List<String> filteredAttributes) {
return studyViewRepository.getClinicalDataCounts(createContext(studyViewFilter), filteredAttributes)
Map<String, ClinicalDataType> clinicalAttributeDatatypeMap = this.getClinicalAttributeDatatypeMap();
StudyViewFilterContext studyViewFilterContext = createContext(studyViewFilter);

int filteredSamplesCount = studyViewRepository.getFilteredSamplesCount(studyViewFilterContext);
int filteredPatientsCount = studyViewRepository.getFilteredPatientCount(studyViewFilterContext);

List<ClinicalDataCountItem> clinicalDataCountItems = studyViewRepository
.getClinicalDataCounts(createContext(studyViewFilter), filteredAttributes)
.stream().collect(Collectors.groupingBy(ClinicalDataCount::getAttributeId))
.entrySet().parallelStream().map(e -> {
ClinicalDataCountItem item = new ClinicalDataCountItem();
item.setAttributeId(e.getKey());
item.setCounts(e.getValue());
return item;
}).collect(Collectors.toList());
}).toList();

// This is postprocessing to adjust NA counts for multiple studies
for (ClinicalDataCountItem clinicalDataCountItem: clinicalDataCountItems) {
Integer totalClinicalDataCount = clinicalDataCountItem
.getCounts()
.stream()
.map(ClinicalDataCount::getCount)
.reduce(0, Integer::sum);
// depending on clinical data type we either use filtered sample count or filtered patient count
int filteredCount = clinicalAttributeDatatypeMap.get(clinicalDataCountItem.getAttributeId()) == ClinicalDataType.SAMPLE ?
filteredSamplesCount: filteredPatientsCount;
int casesWithoutClinicalData = filteredCount - totalClinicalDataCount;

if (casesWithoutClinicalData > 0) {
// find "NA"
ClinicalDataCount naClinicalDataCount = clinicalDataCountItem
.getCounts()
.stream()
.filter(c -> c.getValue().equals("NA"))
.findFirst()
.orElse(null);

if (naClinicalDataCount != null) {
// if there is already "NA" count add cases without clinical data to the existing count
naClinicalDataCount.setCount(naClinicalDataCount.getCount() + casesWithoutClinicalData);
}
else {
// if no NA count, create a new one
// this should only happen when there are multiple studies
naClinicalDataCount = new ClinicalDataCount();
naClinicalDataCount.setAttributeId(clinicalDataCountItem.getAttributeId());
naClinicalDataCount.setValue("NA");
naClinicalDataCount.setCount(casesWithoutClinicalData);
clinicalDataCountItem.getCounts().add(naClinicalDataCount);
}
}
}

return clinicalDataCountItems;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,12 @@
WHERE sample_unique_id IN (<include refid="sampleUniqueIdsFromStudyViewFilter"/>)
</sql>

<sql id="getTotalPatientCount">
SELECT count(distinct patient_unique_id) as count
FROM sample_derived sd
WHERE sample_unique_id IN (<include refid="sampleUniqueIdsFromStudyViewFilter"/>)
</sql>

<!-- for /genomic-data-counts/fetch - (returns GenomicDataCountItem objects) -->
<select id="getCNACounts" resultMap="GenomicDataCountItemResultMap">
<bind name="profileType" value="genomicDataFilters[0].profileType" />
Expand Down Expand Up @@ -397,6 +403,10 @@
<include refid="getTotalSampleCount"/>
</select>

<select id="getFilteredPatientsCount" resultType="int">
<include refid="getTotalPatientCount"/>
</select>

<!-- Get Matching Gene Panel Ids for all Samples after study view filter -->
<select id="getMatchingGenePanelIds" resultType="org.cbioportal.model.GenePanelToGene">
SELECT
Expand Down

0 comments on commit d791618

Please sign in to comment.