From fbd3653d86ce04c4685fa9915b4c4658f99095e6 Mon Sep 17 00:00:00 2001 From: "Zhaoyuan (Ryan) Fu" Date: Wed, 4 Dec 2024 11:09:49 -0500 Subject: [PATCH] Cover edge cases & generic --- .../util/StudyViewColumnarServiceUtil.java | 48 +++++++---- .../StudyViewColumnarServiceUtilTest.java | 83 ++++++++++++++++++- 2 files changed, 113 insertions(+), 18 deletions(-) diff --git a/src/main/java/org/cbioportal/service/util/StudyViewColumnarServiceUtil.java b/src/main/java/org/cbioportal/service/util/StudyViewColumnarServiceUtil.java index ecd1c02f4f7..d4c81ad3630 100644 --- a/src/main/java/org/cbioportal/service/util/StudyViewColumnarServiceUtil.java +++ b/src/main/java/org/cbioportal/service/util/StudyViewColumnarServiceUtil.java @@ -5,7 +5,10 @@ import org.cbioportal.model.ClinicalDataCount; import org.cbioportal.model.ClinicalDataCountItem; import org.cbioportal.model.GenomicDataCount; +import org.cbioportal.web.parameter.ClinicalDataFilter; +import org.cbioportal.web.parameter.DataFilter; import org.cbioportal.web.parameter.DataFilterValue; +import org.cbioportal.web.parameter.GenericAssayDataFilter; import org.cbioportal.web.parameter.GenomicDataFilter; import org.cbioportal.web.parameter.StudyViewFilter; @@ -159,19 +162,34 @@ public static List normalizeDataCounts(List(normalizedDataCounts); } + public static void mergeDataFilterNumericalValues(StudyViewFilter studyViewFilter) { + if (studyViewFilter.getGenomicDataFilters() != null && !studyViewFilter.getGenomicDataFilters().isEmpty()) { + List mergedGenomicDataFilters = mergeDataFilters(studyViewFilter.getGenomicDataFilters()); + studyViewFilter.setGenomicDataFilters(mergedGenomicDataFilters); + } + + if (studyViewFilter.getClinicalDataFilters() != null && !studyViewFilter.getClinicalDataFilters().isEmpty()) { + List mergedClinicalDataFilters = mergeDataFilters(studyViewFilter.getClinicalDataFilters()); + studyViewFilter.setClinicalDataFilters(mergedClinicalDataFilters); + } + + if (studyViewFilter.getGenericAssayDataFilters() != null && !studyViewFilter.getGenericAssayDataFilters().isEmpty()) { + List mergedGenericAssayDataFilters = mergeDataFilters(studyViewFilter.getGenericAssayDataFilters()); + studyViewFilter.setGenericAssayDataFilters(mergedGenericAssayDataFilters); + } + } + /** * Merge the range of numerical bins in DataFilters to reduce the number of scans that runs on the database when filtering. */ - public static void mergeDataFilterNumericalValues(StudyViewFilter studyViewFilter) { - if (studyViewFilter.getGenomicDataFilters() == null || studyViewFilter.getGenomicDataFilters().isEmpty()) return; + private static List mergeDataFilters(List filters) { + List mergedDataFilters = new ArrayList<>(); - List mergedGenomicDataFilters = new ArrayList<>(); - - for (GenomicDataFilter genomicDataFilter : studyViewFilter.getGenomicDataFilters()) { + for (T filter : filters) { List mergedValues = new ArrayList<>(); BigDecimal mergedStart = null, mergedEnd = null; - for (DataFilterValue dataFilterValue : genomicDataFilter.getValues()) { + for (DataFilterValue dataFilterValue : filter.getValues()) { // leave non-numerical values as they are if (dataFilterValue.getValue() != null) { mergedValues.add(dataFilterValue); @@ -181,22 +199,24 @@ public static void mergeDataFilterNumericalValues(StudyViewFilter studyViewFilte BigDecimal start = dataFilterValue.getStart(); BigDecimal end = dataFilterValue.getEnd(); - if (mergedStart == null) mergedStart = start; - if (mergedEnd == null) mergedEnd = end; + if (mergedStart == null && mergedEnd == null) { + mergedStart = start; + mergedEnd = end; + } else if (mergedEnd.equals(start)) mergedEnd = end; else { mergedValues.add(new DataFilterValue(mergedStart, mergedEnd, null)); - mergedStart = null; - mergedEnd = null; + mergedStart = start; + mergedEnd = end; } } } - + mergedValues.add(new DataFilterValue(mergedStart, mergedEnd, null)); - GenomicDataFilter mergedGenomicDataFilter = new GenomicDataFilter(genomicDataFilter.getHugoGeneSymbol(), genomicDataFilter.getProfileType(), mergedValues); - mergedGenomicDataFilters.add(mergedGenomicDataFilter); + filter.setValues(mergedValues); + mergedDataFilters.add(filter); } - studyViewFilter.setGenomicDataFilters(mergedGenomicDataFilters); + return mergedDataFilters; } } \ 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 f5bd1ba283b..a7213fcd429 100644 --- a/src/test/java/org/cbioportal/service/util/StudyViewColumnarServiceUtilTest.java +++ b/src/test/java/org/cbioportal/service/util/StudyViewColumnarServiceUtilTest.java @@ -268,9 +268,10 @@ public void testNormalizeDataCounts() { .orElse(null); assertEquals(9, falseCount.getCount().intValue()); } - + + // (-2.5, -2.25], (-2.25, -2], (-2, -1.75] -> (-2.5, -1.75] @Test - public void testMergeDataFilterNumericalValues() { + public void testMergeDataFilterNumericalContinuousValues() { StudyViewFilter studyViewFilter = new StudyViewFilter(); List genomicDataFilters = new ArrayList<>(); List values = new ArrayList<>(); @@ -279,9 +280,9 @@ public void testMergeDataFilterNumericalValues() { values.add(new DataFilterValue(BigDecimal.valueOf(-2), BigDecimal.valueOf(-1.75), null)); genomicDataFilters.add(new GenomicDataFilter(null, null, values)); studyViewFilter.setGenomicDataFilters(genomicDataFilters); - + StudyViewColumnarServiceUtil.mergeDataFilterNumericalValues(studyViewFilter); - + List actualGenomicDataFilters = studyViewFilter.getGenomicDataFilters(); List actualDataFilterValues = actualGenomicDataFilters.getFirst().getValues(); BigDecimal start = actualDataFilterValues.getFirst().getStart(); @@ -289,4 +290,78 @@ public void testMergeDataFilterNumericalValues() { assertEquals(0, BigDecimal.valueOf(-2.5).compareTo(start)); assertEquals(0, BigDecimal.valueOf(-1.75).compareTo(end)); } + + // (-2.5, -2.25], (-2.25, -2], (-1.75, -1.5], (-1.5, -1.25] -> (-2.5, -2], (-1.75, -1.25] + @Test + public void testMergeDataFilterNumericalDiscontinuousValues() { + StudyViewFilter studyViewFilter = new StudyViewFilter(); + List genomicDataFilters = new ArrayList<>(); + List values = new ArrayList<>(); + values.add(new DataFilterValue(BigDecimal.valueOf(-2.5), BigDecimal.valueOf(-2.25), null)); + values.add(new DataFilterValue(BigDecimal.valueOf(-2.25), BigDecimal.valueOf(-2), null)); + values.add(new DataFilterValue(BigDecimal.valueOf(-1.75), BigDecimal.valueOf(-1.5), null)); + values.add(new DataFilterValue(BigDecimal.valueOf(-1.5), BigDecimal.valueOf(-1.25), null)); + genomicDataFilters.add(new GenomicDataFilter(null, null, values)); + studyViewFilter.setGenomicDataFilters(genomicDataFilters); + + StudyViewColumnarServiceUtil.mergeDataFilterNumericalValues(studyViewFilter); + + List actualGenomicDataFilters = studyViewFilter.getGenomicDataFilters(); + List actualDataFilterValues = actualGenomicDataFilters.getFirst().getValues(); + BigDecimal firstStart = actualDataFilterValues.getFirst().getStart(); + BigDecimal firstEnd = actualDataFilterValues.getFirst().getEnd(); + assertEquals(0, BigDecimal.valueOf(-2.5).compareTo(firstStart)); + assertEquals(0, BigDecimal.valueOf(-2).compareTo(firstEnd)); + + BigDecimal secondStart = actualDataFilterValues.get(1).getStart(); + BigDecimal secondEnd = actualDataFilterValues.get(1).getEnd(); + assertEquals(0, BigDecimal.valueOf(-1.75).compareTo(secondStart)); + assertEquals(0, BigDecimal.valueOf(-1.25).compareTo(secondEnd)); + } + + // (null, -2.25], (-2.25, -2], (-2, null] -> (null, null] + @Test + public void testMergeDataFilterNumericalInfiniteValues() { + StudyViewFilter studyViewFilter = new StudyViewFilter(); + List genomicDataFilters = new ArrayList<>(); + List values = new ArrayList<>(); + values.add(new DataFilterValue(null, BigDecimal.valueOf(-2.25), null)); + values.add(new DataFilterValue(BigDecimal.valueOf(-2.25), BigDecimal.valueOf(-2), null)); + values.add(new DataFilterValue(BigDecimal.valueOf(-2), null, null)); + genomicDataFilters.add(new GenomicDataFilter(null, null, values)); + studyViewFilter.setGenomicDataFilters(genomicDataFilters); + + StudyViewColumnarServiceUtil.mergeDataFilterNumericalValues(studyViewFilter); + + List actualGenomicDataFilters = studyViewFilter.getGenomicDataFilters(); + List actualDataFilterValues = actualGenomicDataFilters.getFirst().getValues(); + BigDecimal start = actualDataFilterValues.getFirst().getStart(); + BigDecimal end = actualDataFilterValues.getFirst().getEnd(); + assertNull(start); + assertNull(end); + } + + // (-2.5, -2.25], (-2.25, -2], "NA" -> "NA", (-2.5, -1.75] + @Test + public void testMergeDataFilterNumericalNonNumericalValues() { + StudyViewFilter studyViewFilter = new StudyViewFilter(); + List genomicDataFilters = new ArrayList<>(); + List values = new ArrayList<>(); + values.add(new DataFilterValue(BigDecimal.valueOf(-2.5), BigDecimal.valueOf(-2.25), null)); + values.add(new DataFilterValue(BigDecimal.valueOf(-2.25), BigDecimal.valueOf(-2), null)); + values.add(new DataFilterValue(null, null, "NA")); + genomicDataFilters.add(new GenomicDataFilter(null, null, values)); + studyViewFilter.setGenomicDataFilters(genomicDataFilters); + + StudyViewColumnarServiceUtil.mergeDataFilterNumericalValues(studyViewFilter); + + List actualGenomicDataFilters = studyViewFilter.getGenomicDataFilters(); + List actualDataFilterValues = actualGenomicDataFilters.getFirst().getValues(); + String value = actualDataFilterValues.getFirst().getValue(); + BigDecimal start = actualDataFilterValues.get(1).getStart(); + BigDecimal end = actualDataFilterValues.get(1).getEnd(); + assertEquals("NA", value); + assertEquals(0, BigDecimal.valueOf(-2.5).compareTo(start)); + assertEquals(0, BigDecimal.valueOf(-2).compareTo(end)); + } } \ No newline at end of file