-
Notifications
You must be signed in to change notification settings - Fork 511
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add back NA counts when corresponding samples are filtered out of stu…
…dy view (#11185) * Fix counting of clinical data na when filtered out * Add caching and cleanup * Fix Sonar issues
- Loading branch information
Showing
8 changed files
with
266 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
src/main/java/org/cbioportal/service/util/StudyViewColumnarServiceUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package org.cbioportal.service.util; | ||
|
||
import org.cbioportal.model.ClinicalAttribute; | ||
import org.cbioportal.model.ClinicalDataCount; | ||
import org.cbioportal.model.ClinicalDataCountItem; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
public class StudyViewColumnarServiceUtil { | ||
|
||
private StudyViewColumnarServiceUtil() {} | ||
|
||
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; | ||
}).toList(); | ||
attr.setCounts(res); | ||
}); | ||
return items; | ||
} | ||
|
||
public static List<ClinicalDataCountItem> addClinicalDataCountsForMissingAttributes( | ||
List<ClinicalDataCountItem> counts, | ||
List<ClinicalAttribute> attributes, | ||
Integer filteredSampleCount, | ||
Integer filteredPatientCount | ||
) { | ||
Map<String, ClinicalDataCountItem> map = counts.stream() | ||
.collect(Collectors.toMap(ClinicalDataCountItem::getAttributeId, item -> item)); | ||
|
||
List<ClinicalDataCountItem> result = new ArrayList<>(counts); | ||
|
||
attributes.forEach(attr -> { | ||
Integer count = attr.getPatientAttribute().booleanValue() ? filteredPatientCount : filteredSampleCount; | ||
|
||
if (!map.containsKey(attr.getAttrId())) { | ||
ClinicalDataCountItem newItem = new ClinicalDataCountItem(); | ||
newItem.setAttributeId(attr.getAttrId()); | ||
ClinicalDataCount countObj = new ClinicalDataCount(); | ||
countObj.setCount(count); | ||
countObj.setValue("NA"); | ||
countObj.setAttributeId(attr.getAttrId()); | ||
newItem.setCounts(List.of(countObj)); | ||
result.add(newItem); | ||
} | ||
}); | ||
|
||
return result; | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
src/test/java/org/cbioportal/service/util/StudyViewColumnarServiceUtilTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
package org.cbioportal.service.util; | ||
|
||
import org.cbioportal.model.ClinicalAttribute; | ||
import org.cbioportal.model.ClinicalDataCount; | ||
import org.cbioportal.model.ClinicalDataCountItem; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public class StudyViewColumnarServiceUtilTest { | ||
|
||
@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()); | ||
|
||
} | ||
|
||
|
||
@Test | ||
public void testAddClinicalDataCountsForMissingAttributes() { | ||
ClinicalDataCountItem existingItem = new ClinicalDataCountItem(); | ||
existingItem.setAttributeId("attr1"); | ||
ClinicalDataCount existingCount = new ClinicalDataCount(); | ||
existingCount.setCount(5); | ||
existingCount.setValue("value1"); | ||
existingCount.setAttributeId("attr1"); | ||
existingItem.setCounts(Collections.singletonList(existingCount)); | ||
|
||
List<ClinicalDataCountItem> counts = Collections.singletonList(existingItem); | ||
|
||
// we're gonna create two attributes which will not be represented in the passed result set | ||
// test whether addClinicalDataCountsForMissingAttributes restores them | ||
|
||
ClinicalAttribute missingAttributeSample = new ClinicalAttribute(); | ||
missingAttributeSample.setAttrId("attr2"); | ||
missingAttributeSample.setPatientAttribute(false); | ||
|
||
ClinicalAttribute missingAttributePatient = new ClinicalAttribute(); | ||
missingAttributePatient.setAttrId("attr3"); | ||
missingAttributePatient.setPatientAttribute(true); | ||
|
||
List<ClinicalAttribute> attributes = Arrays.asList(missingAttributeSample, missingAttributePatient); | ||
|
||
List<ClinicalDataCountItem> result = StudyViewColumnarServiceUtil.addClinicalDataCountsForMissingAttributes( | ||
counts, attributes, 10, 20 | ||
); | ||
|
||
assertEquals(3, result.size()); | ||
|
||
Optional<ClinicalDataCountItem> addedItemSample = result.stream() | ||
.filter(item -> item.getAttributeId().equals("attr2")) | ||
.findFirst(); | ||
|
||
assertTrue(addedItemSample.isPresent()); | ||
assertEquals(1, addedItemSample.get().getCounts().size()); | ||
assertEquals("NA", addedItemSample.get().getCounts().get(0).getValue()); | ||
assertEquals(10, addedItemSample.get().getCounts().get(0).getCount().intValue()); | ||
|
||
Optional<ClinicalDataCountItem> addedItemPatient = result.stream() | ||
.filter(item -> item.getAttributeId().equals("attr3")) | ||
.findFirst(); | ||
|
||
assertTrue(addedItemPatient.isPresent()); | ||
assertEquals(1, addedItemPatient.get().getCounts().size()); | ||
assertEquals("NA", addedItemPatient.get().getCounts().get(0).getValue()); | ||
assertEquals(20, addedItemPatient.get().getCounts().get(0).getCount().intValue()); | ||
|
||
|
||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
} |