diff --git a/src/main/java/org/cbioportal/service/impl/StudyViewColumnarServiceImpl.java b/src/main/java/org/cbioportal/service/impl/StudyViewColumnarServiceImpl.java index 49cafd324ed..0f82f0e6fa6 100644 --- a/src/main/java/org/cbioportal/service/impl/StudyViewColumnarServiceImpl.java +++ b/src/main/java/org/cbioportal/service/impl/StudyViewColumnarServiceImpl.java @@ -161,6 +161,20 @@ public List getClinicalDataCounts(StudyViewFilter studyVi List filteredSamples = getFilteredSamples(studyViewFilter); var resultWithAllAttributes = StudyViewColumnarServiceUtil.addClinicalDataCountsForMissingAttributes(result, filteredAttributes, filteredSamples); + + resultWithAllAttributes.stream().forEach(attr -> { + Map> countsPerType = attr.getCounts().stream() + .collect(Collectors.groupingBy(ClinicalDataCount::getValue)); + List res = countsPerType.entrySet().stream().map((entry)->{ + ClinicalDataCount mergedCount = new ClinicalDataCount(); + mergedCount.setAttributeId(attr.getAttributeId()); + mergedCount.setValue(entry.getKey()); + mergedCount.setCount(entry.getValue().stream().mapToInt(ClinicalDataCount::getCount).sum()); + return mergedCount; + }).collect(Collectors.toList()); + attr.setCounts(res); + }); + return resultWithAllAttributes; } diff --git a/src/main/java/org/cbioportal/service/util/StudyViewColumnarServiceUtil.java b/src/main/java/org/cbioportal/service/util/StudyViewColumnarServiceUtil.java index e4210ebbcb4..0a8018a7e16 100644 --- a/src/main/java/org/cbioportal/service/util/StudyViewColumnarServiceUtil.java +++ b/src/main/java/org/cbioportal/service/util/StudyViewColumnarServiceUtil.java @@ -32,4 +32,22 @@ public static List addClinicalDataCountsForMissingAttribu return result; } + + public static List mergeClinicalDataCounts(List items) { + items.forEach(attr -> { + Map> countsPerType = attr.getCounts().stream() + .collect(Collectors.groupingBy(ClinicalDataCount::getValue)); + List res = countsPerType.entrySet().stream().map((entry) -> { + ClinicalDataCount mergedCount = new ClinicalDataCount(); + mergedCount.setAttributeId(attr.getAttributeId()); + mergedCount.setValue(entry.getKey()); + mergedCount.setCount(entry.getValue().stream().mapToInt(ClinicalDataCount::getCount).sum()); + return mergedCount; + }).collect(Collectors.toList()); + attr.setCounts(res); + }); + return items; + } + + } \ No newline at end of file diff --git a/src/test/java/org/cbioportal/service/util/StudyViewColumnarServiceUtilTest.java b/src/test/java/org/cbioportal/service/util/StudyViewColumnarServiceUtilTest.java index 18894827ac1..64567c59309 100644 --- a/src/test/java/org/cbioportal/service/util/StudyViewColumnarServiceUtilTest.java +++ b/src/test/java/org/cbioportal/service/util/StudyViewColumnarServiceUtilTest.java @@ -9,6 +9,7 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; +import java.util.Optional; import static org.junit.Assert.*; @@ -55,4 +56,59 @@ public void testAddClinicalDataCountsForMissingAttributes() { Assert.assertEquals(2, item3.getCounts().get(0).getCount().intValue()); } + @Test + public void testMergeClinicalDataCounts() { + + // first two counts are for same value (value1) and so should be + // merged + + ClinicalDataCount count1 = new ClinicalDataCount(); + count1.setAttributeId("attr1"); + count1.setValue("value1"); + count1.setCount(1); + + ClinicalDataCount count2 = new ClinicalDataCount(); + count2.setAttributeId("attr1"); + count2.setValue("value1"); + count2.setCount(2); + + ClinicalDataCount count3 = new ClinicalDataCount(); + count3.setAttributeId("attr1"); + count3.setValue("value3"); + count3.setCount(6); + + ClinicalDataCount count4 = new ClinicalDataCount(); + count4.setAttributeId("attr1"); + count4.setValue("value3"); + count4.setCount(4); + + ClinicalDataCount count5 = new ClinicalDataCount(); + count5.setAttributeId("attr1"); + count5.setValue("value2"); + count5.setCount(4); + + ClinicalDataCountItem item = new ClinicalDataCountItem(); + item.setAttributeId("attr1"); + item.setCounts(Arrays.asList(count1, count2, count3, count4, count5)); + + List items = Collections.singletonList(item); + + // Call the method under test + List mergedItems = StudyViewColumnarServiceUtil.mergeClinicalDataCounts(items); + + // it merged three count items to 2 + Optional mergedCount=mergedItems.get(0).getCounts().stream() + .filter(count->count.getValue().equals("value1")).findFirst(); + Assert.assertEquals(3, mergedCount.get().getCount().intValue()); + + Optional mergedCount2=mergedItems.get(0).getCounts().stream() + .filter(count->count.getValue().equals("value2")).findFirst(); + Assert.assertEquals(4, mergedCount2.get().getCount().intValue()); + + Optional mergedCount3=mergedItems.get(0).getCounts().stream() + .filter(count->count.getValue().equals("value3")).findFirst(); + Assert.assertEquals(10, mergedCount3.get().getCount().intValue()); + + } + } \ No newline at end of file