Skip to content

Commit

Permalink
Adjacent merging version
Browse files Browse the repository at this point in the history
  • Loading branch information
fuzhaoyuan authored and alisman committed Dec 6, 2024
1 parent 8bf6311 commit 5892eaf
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ public List<GenomicDataCountItem> getMutationTypeCountsByGeneSpecific(StudyViewF


private StudyViewFilterContext createContext(StudyViewFilter studyViewFilter) {
studyViewFilter.mergeDataFilterNumericalValues();
StudyViewColumnarServiceUtil.mergeDataFilterNumericalValues(studyViewFilter);
List<CustomSampleIdentifier> customSampleIdentifiers = customDataFilterUtil.extractCustomDataSamples(studyViewFilter);
return new StudyViewFilterContext(studyViewFilter, customSampleIdentifiers);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@
import org.cbioportal.model.ClinicalDataCount;
import org.cbioportal.model.ClinicalDataCountItem;
import org.cbioportal.model.GenomicDataCount;
import org.cbioportal.web.parameter.DataFilterValue;
import org.cbioportal.web.parameter.GenomicDataFilter;
import org.cbioportal.web.parameter.StudyViewFilter;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class StudyViewColumnarServiceUtil {
public final class StudyViewColumnarServiceUtil {

private StudyViewColumnarServiceUtil() {}

Expand Down Expand Up @@ -155,5 +159,44 @@ public static List<ClinicalDataCount> normalizeDataCounts(List<ClinicalDataCount
return new ArrayList<>(normalizedDataCounts);
}


/**
* 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;

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

for (GenomicDataFilter genomicDataFilter : studyViewFilter.getGenomicDataFilters()) {
List<DataFilterValue> mergedValues = new ArrayList<>();

BigDecimal mergedStart = null, mergedEnd = null;
for (DataFilterValue dataFilterValue : genomicDataFilter.getValues()) {
// leave non-numerical values as they are
if (dataFilterValue.getValue() != null) {
mergedValues.add(dataFilterValue);
}
// merge adjacent numerical bins
else {
BigDecimal start = dataFilterValue.getStart();
BigDecimal end = dataFilterValue.getEnd();

if (mergedStart == null) mergedStart = start;
if (mergedEnd == null) mergedEnd = end;
else if (mergedEnd.equals(start)) mergedEnd = end;
else {
mergedValues.add(new DataFilterValue(mergedStart, mergedEnd, null));
mergedStart = null;
mergedEnd = null;
}
}
}

mergedValues.add(new DataFilterValue(mergedStart, mergedEnd, null));
GenomicDataFilter mergedGenomicDataFilter = new GenomicDataFilter(genomicDataFilter.getHugoGeneSymbol(), genomicDataFilter.getProfileType(), mergedValues);
mergedGenomicDataFilters.add(mergedGenomicDataFilter);
}

studyViewFilter.setGenomicDataFilters(mergedGenomicDataFilters);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.cbioportal.web.parameter;

import java.io.Serializable;
import java.util.List;

public class GenomicDataFilter extends DataFilter implements Serializable {
private String hugoGeneSymbol;
Expand All @@ -13,6 +14,12 @@ public GenomicDataFilter(String hugoGeneSymbol, String profileType) {
this.profileType = profileType;
}

public GenomicDataFilter(String hugoGeneSymbol, String profileType, List<DataFilterValue> values) {
this.hugoGeneSymbol = hugoGeneSymbol;
this.profileType = profileType;
this.setValues(values);
}

public String getHugoGeneSymbol() {
return hugoGeneSymbol;
}
Expand Down
45 changes: 0 additions & 45 deletions src/main/java/org/cbioportal/web/parameter/StudyViewFilter.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
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;

Expand Down Expand Up @@ -43,7 +41,6 @@ public class StudyViewFilter implements Serializable {
private AlterationFilter alterationFilter;
private List<DataFilter> clinicalEventFilters;
private List<MutationDataFilter> mutationDataFilters;
private static boolean areBinsMerged = false;

@AssertTrue
private boolean isEitherSampleIdentifiersOrStudyIdsPresent() {
Expand Down Expand Up @@ -233,46 +230,4 @@ public void setClinicalEventFilters(List<DataFilter> clinicalEventFilters) {
public void setMutationDataFilters(List<MutationDataFilter> 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<GenomicDataFilter> mergedGenomicDataFilters = new ArrayList<>();

for (GenomicDataFilter genomicDataFilter : this.genomicDataFilters) {
GenomicDataFilter mergedGenomicDataFilter = new GenomicDataFilter(genomicDataFilter.getHugoGeneSymbol(), genomicDataFilter.getProfileType());
List<DataFilterValue> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@
import org.cbioportal.model.ClinicalDataCount;
import org.cbioportal.model.ClinicalDataCountItem;
import org.cbioportal.model.GenomicDataCount;
import org.cbioportal.web.parameter.DataFilterValue;
import org.cbioportal.web.parameter.GenomicDataFilter;
import org.cbioportal.web.parameter.StudyViewFilter;
import org.junit.Assert;
import org.junit.Test;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -264,5 +269,24 @@ public void testNormalizeDataCounts() {
assertEquals(9, falseCount.getCount().intValue());
}


@Test
public void testMergeDataFilterNumericalValues() {
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(-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));
}
}

0 comments on commit 5892eaf

Please sign in to comment.