Skip to content

Commit

Permalink
Add tests for clinical data count merging
Browse files Browse the repository at this point in the history
  • Loading branch information
alisman committed Nov 11, 2024
1 parent 73b867f commit 43297f8
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,20 @@ public List<ClinicalDataCountItem> getClinicalDataCounts(StudyViewFilter studyVi
List<Sample> filteredSamples = getFilteredSamples(studyViewFilter);

var resultWithAllAttributes = StudyViewColumnarServiceUtil.addClinicalDataCountsForMissingAttributes(result, filteredAttributes, filteredSamples);

resultWithAllAttributes.stream().forEach(attr -> {
Map<String, List<ClinicalDataCount>> countsPerType = attr.getCounts().stream()
.collect(Collectors.groupingBy(ClinicalDataCount::getValue));
List<ClinicalDataCount> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,22 @@ public static List<ClinicalDataCountItem> addClinicalDataCountsForMissingAttribu

return result;
}

public static List<ClinicalDataCountItem> mergeClinicalDataCounts(List<ClinicalDataCountItem> items) {
items.forEach(attr -> {
Map<String, List<ClinicalDataCount>> countsPerType = attr.getCounts().stream()
.collect(Collectors.groupingBy(ClinicalDataCount::getValue));
List<ClinicalDataCount> 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;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;

Expand Down Expand Up @@ -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<ClinicalDataCountItem> items = Collections.singletonList(item);

// Call the method under test
List<ClinicalDataCountItem> mergedItems = StudyViewColumnarServiceUtil.mergeClinicalDataCounts(items);

// it merged three count items to 2
Optional<ClinicalDataCount> mergedCount=mergedItems.get(0).getCounts().stream()
.filter(count->count.getValue().equals("value1")).findFirst();
Assert.assertEquals(3, mergedCount.get().getCount().intValue());

Optional<ClinicalDataCount> mergedCount2=mergedItems.get(0).getCounts().stream()
.filter(count->count.getValue().equals("value2")).findFirst();
Assert.assertEquals(4, mergedCount2.get().getCount().intValue());

Optional<ClinicalDataCount> mergedCount3=mergedItems.get(0).getCounts().stream()
.filter(count->count.getValue().equals("value3")).findFirst();
Assert.assertEquals(10, mergedCount3.get().getCount().intValue());

}

}

0 comments on commit 43297f8

Please sign in to comment.