From 7cf0368ba1918629f8c261c08978fa8ae178e455 Mon Sep 17 00:00:00 2001 From: "Zhaoyuan (Ryan) Fu" Date: Mon, 2 Dec 2024 10:36:50 -0500 Subject: [PATCH] Merge genomic data bins working --- .../impl/StudyViewColumnarServiceImpl.java | 1 + .../web/parameter/DataFilterValue.java | 8 ++++ .../web/parameter/StudyViewFilter.java | 45 +++++++++++++++++++ 3 files changed, 54 insertions(+) diff --git a/src/main/java/org/cbioportal/service/impl/StudyViewColumnarServiceImpl.java b/src/main/java/org/cbioportal/service/impl/StudyViewColumnarServiceImpl.java index 7063f64f560..422401e62af 100644 --- a/src/main/java/org/cbioportal/service/impl/StudyViewColumnarServiceImpl.java +++ b/src/main/java/org/cbioportal/service/impl/StudyViewColumnarServiceImpl.java @@ -285,6 +285,7 @@ public List getMutationTypeCountsByGeneSpecific(StudyViewF private StudyViewFilterContext createContext(StudyViewFilter studyViewFilter) { + studyViewFilter.mergeDataFilterNumericalValues(); List customSampleIdentifiers = customDataFilterUtil.extractCustomDataSamples(studyViewFilter); return new StudyViewFilterContext(studyViewFilter, customSampleIdentifiers); } diff --git a/src/main/java/org/cbioportal/web/parameter/DataFilterValue.java b/src/main/java/org/cbioportal/web/parameter/DataFilterValue.java index 184aee40f29..af434cfa55d 100644 --- a/src/main/java/org/cbioportal/web/parameter/DataFilterValue.java +++ b/src/main/java/org/cbioportal/web/parameter/DataFilterValue.java @@ -9,6 +9,14 @@ public class DataFilterValue implements Serializable { private BigDecimal end; private String value; + public DataFilterValue() {} + + public DataFilterValue(BigDecimal start, BigDecimal end, String value) { + this.start = start; + this.end = end; + this.value = value; + } + public BigDecimal getStart() { return start; } diff --git a/src/main/java/org/cbioportal/web/parameter/StudyViewFilter.java b/src/main/java/org/cbioportal/web/parameter/StudyViewFilter.java index 738d3632a70..95ae884bddd 100644 --- a/src/main/java/org/cbioportal/web/parameter/StudyViewFilter.java +++ b/src/main/java/org/cbioportal/web/parameter/StudyViewFilter.java @@ -1,6 +1,8 @@ package org.cbioportal.web.parameter; import java.io.Serializable; +import java.math.BigDecimal; +import java.util.ArrayList; import java.util.List; import java.util.Objects; @@ -41,6 +43,7 @@ public class StudyViewFilter implements Serializable { private AlterationFilter alterationFilter; private List clinicalEventFilters; private List mutationDataFilters; + private static boolean areBinsMerged = false; @AssertTrue private boolean isEitherSampleIdentifiersOrStudyIdsPresent() { @@ -230,4 +233,46 @@ public void setClinicalEventFilters(List clinicalEventFilters) { public void setMutationDataFilters(List mutationDataFilters) { this.mutationDataFilters = mutationDataFilters; } + + /** + * Merge the range of numerical values in DataFilters to reduce the number of scans that runs on the database. + * Variable 'areBinsMerged' is static so this method only gets run once. + */ + public void mergeDataFilterNumericalValues() { + if (areBinsMerged || this.genomicDataFilters == null || this.genomicDataFilters.isEmpty()) return; + + List mergedGenomicDataFilters = new ArrayList<>(); + + for (GenomicDataFilter genomicDataFilter : this.genomicDataFilters) { + GenomicDataFilter mergedGenomicDataFilter = new GenomicDataFilter(genomicDataFilter.getHugoGeneSymbol(), genomicDataFilter.getProfileType()); + List mergedValues = new ArrayList<>(); + + boolean hasNullStart = false, hasNullEnd = false; + BigDecimal mergedStart = null, mergedEnd = null; + for (DataFilterValue dataFilterValue : genomicDataFilter.getValues()) { + // filter non-numerical values and keep them intact + if (dataFilterValue.getValue() != null) { + mergedValues.add(dataFilterValue); + } + // record if numerical values have null start or end, otherwise record their start-end range + else { + if (dataFilterValue.getStart() == null) hasNullStart = true; + else if (mergedStart == null) mergedStart = dataFilterValue.getStart(); + else if (dataFilterValue.getStart().compareTo(mergedStart) < 0) mergedStart = dataFilterValue.getStart(); + if (dataFilterValue.getEnd() == null) hasNullEnd = true; + else if (mergedEnd == null) mergedEnd = dataFilterValue.getEnd(); + else if (dataFilterValue.getEnd().compareTo(mergedEnd) > 0) mergedEnd = dataFilterValue.getEnd(); + } + } + if (hasNullStart) mergedStart = null; + if (hasNullEnd) mergedEnd = null; + + mergedValues.add(new DataFilterValue(mergedStart, mergedEnd, null)); + mergedGenomicDataFilter.setValues(mergedValues); + mergedGenomicDataFilters.add(mergedGenomicDataFilter); + } + + this.genomicDataFilters = mergedGenomicDataFilters; + areBinsMerged = true; + } }