Skip to content

Commit

Permalink
Cover edge cases & generic
Browse files Browse the repository at this point in the history
  • Loading branch information
fuzhaoyuan authored and alisman committed Dec 6, 2024
1 parent 5892eaf commit fbd3653
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -159,19 +162,34 @@ public static List<ClinicalDataCount> normalizeDataCounts(List<ClinicalDataCount
return new ArrayList<>(normalizedDataCounts);
}

public static void mergeDataFilterNumericalValues(StudyViewFilter studyViewFilter) {
if (studyViewFilter.getGenomicDataFilters() != null && !studyViewFilter.getGenomicDataFilters().isEmpty()) {
List<GenomicDataFilter> mergedGenomicDataFilters = mergeDataFilters(studyViewFilter.getGenomicDataFilters());
studyViewFilter.setGenomicDataFilters(mergedGenomicDataFilters);
}

if (studyViewFilter.getClinicalDataFilters() != null && !studyViewFilter.getClinicalDataFilters().isEmpty()) {
List<ClinicalDataFilter> mergedClinicalDataFilters = mergeDataFilters(studyViewFilter.getClinicalDataFilters());
studyViewFilter.setClinicalDataFilters(mergedClinicalDataFilters);
}

if (studyViewFilter.getGenericAssayDataFilters() != null && !studyViewFilter.getGenericAssayDataFilters().isEmpty()) {
List<GenericAssayDataFilter> 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 <T extends DataFilter> List<T> mergeDataFilters(List<T> filters) {
List<T> mergedDataFilters = new ArrayList<>();

List<GenomicDataFilter> mergedGenomicDataFilters = new ArrayList<>();

for (GenomicDataFilter genomicDataFilter : studyViewFilter.getGenomicDataFilters()) {
for (T filter : filters) {
List<DataFilterValue> 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);
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<GenomicDataFilter> genomicDataFilters = new ArrayList<>();
List<DataFilterValue> values = new ArrayList<>();
Expand All @@ -279,14 +280,88 @@ 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<GenomicDataFilter> actualGenomicDataFilters = studyViewFilter.getGenomicDataFilters();
List<DataFilterValue> actualDataFilterValues = actualGenomicDataFilters.getFirst().getValues();
BigDecimal start = actualDataFilterValues.getFirst().getStart();
BigDecimal end = actualDataFilterValues.getFirst().getEnd();
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<GenomicDataFilter> genomicDataFilters = new ArrayList<>();
List<DataFilterValue> 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<GenomicDataFilter> actualGenomicDataFilters = studyViewFilter.getGenomicDataFilters();
List<DataFilterValue> 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<GenomicDataFilter> genomicDataFilters = new ArrayList<>();
List<DataFilterValue> 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<GenomicDataFilter> actualGenomicDataFilters = studyViewFilter.getGenomicDataFilters();
List<DataFilterValue> 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<GenomicDataFilter> genomicDataFilters = new ArrayList<>();
List<DataFilterValue> 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<GenomicDataFilter> actualGenomicDataFilters = studyViewFilter.getGenomicDataFilters();
List<DataFilterValue> 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));
}
}

0 comments on commit fbd3653

Please sign in to comment.