Skip to content

Commit

Permalink
Feature/mutated genes AlterationFilter and Fix unit test (#10803)
Browse files Browse the repository at this point in the history
* Create new wide table sql file and rename package

* Remove genomic_event view

* Add AlterationFilter to mutated_genes endpoint

* Add AlterationFilter to mutated-genes endpoint

* Fix unit test

* Fix sonar issues

* Add test for mutation types and status

* remove unused imports
  • Loading branch information
haynescd authored and haynescd committed Nov 24, 2024
1 parent 12b7b07 commit 83f2743
Show file tree
Hide file tree
Showing 17 changed files with 761 additions and 424 deletions.
18 changes: 18 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -444,9 +444,27 @@
<includes>
<include>cgds.sql</include>
<include>migration.sql</include>
<include>clickhouse/clickhouse.sql</include>
<include>clickhouse/clickhouse_views.sql</include>
<include>clickhouse/clickhouse_migration.sql</include>
</includes>
</resource>
</resources>
<testResources>
<testResource>
<directory>src/main/resources/db-scripts</directory>
<filtering>false</filtering>
<includes>
<include>clickhouse/clickhouse.sql</include>
<include>clickhouse/views.sql</include>
<include>clickhouse/clickhouse_migration.sql</include>
</includes>
</testResource>
<testResource>
<directory>src/test/resources</directory>
<filtering>false</filtering>
</testResource>
</testResources>

<plugins>
<plugin>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package org.cbioportal.persistence.helper;

import org.cbioportal.model.AlterationFilter;
import org.cbioportal.model.CNA;
import org.cbioportal.model.MutationEventType;
import org.cbioportal.model.util.Select;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;

import java.util.Objects;

public final class AlterationFilterHelper {

public static AlterationFilterHelper build(@Nullable AlterationFilter alterationFilter) {
if (Objects.isNull(alterationFilter)) {
alterationFilter = new AlterationFilter();
}
return new AlterationFilterHelper(alterationFilter);
}

private final AlterationFilter alterationFilter;
private final Select<String> mappedMutationTypes;

private AlterationFilterHelper(@NonNull AlterationFilter alterationFilter){
this.alterationFilter = alterationFilter;
this.mappedMutationTypes = buildMutationTypeList();
}

private Select<String> buildMutationTypeList() {
if (alterationFilter.getMutationTypeSelect().hasNone()) {
return Select.none();
}
if (alterationFilter.getMutationTypeSelect().hasAll()) {
return Select.all();
}
Select<String> typeSelects = alterationFilter.getMutationTypeSelect().map(MutationEventType::getMutationType);
typeSelects.inverse(alterationFilter.getMutationTypeSelect().inverse());

return typeSelects;
}

public Select<String> getMutationTypeList() {
return mappedMutationTypes;
}

public Select<Short> getCnaTypeList() {
if (alterationFilter.getCNAEventTypeSelect().hasNone()) {
return Select.none();
}
if (alterationFilter.getCNAEventTypeSelect().hasAll()) {
return Select.all();
}
return alterationFilter.getCNAEventTypeSelect().map(CNA::getCode);
}

public boolean hasDriver() {
return alterationFilter.getIncludeDriver();
}

public boolean hasVUSDriver() {
return alterationFilter.getIncludeVUS();
}

public boolean hasUnknownOncogenicity() {
return alterationFilter.getIncludeUnknownOncogenicity();
}

public boolean hasGermline() {
return alterationFilter.getIncludeGermline();
}

public boolean hasSomatic() {
return alterationFilter.getIncludeSomatic();
}

public boolean hasUnknownMutationStatus() {
return alterationFilter.getIncludeUnknownStatus();
}

public Select<String> getSelectedTiers() {
return alterationFilter.getSelectedTiers();
}

public boolean hasUnknownTier() {
return alterationFilter.getIncludeUnknownTier();
}

public boolean isAllDriverAnnotationSelected() {
return alterationFilter.getIncludeDriver() && alterationFilter.getIncludeVUS() && alterationFilter.getIncludeUnknownOncogenicity();
}

public boolean isNoDriverAnnotationSelected() {
return !alterationFilter.getIncludeDriver() && !alterationFilter.getIncludeVUS() && !alterationFilter.getIncludeUnknownOncogenicity();
}

public boolean isSomeDriverAnnotationsSelected() {
return !isAllDriverAnnotationSelected() && !isNoDriverAnnotationSelected();
}

public boolean isAllMutationStatusSelected() {
return alterationFilter.getIncludeGermline()
&& alterationFilter.getIncludeSomatic()
&& alterationFilter.getIncludeUnknownStatus();
}

public boolean isNoMutationStatusSelected() {
return !alterationFilter.getIncludeGermline()
&& !alterationFilter.getIncludeSomatic()
&& !alterationFilter.getIncludeUnknownStatus();
}

public boolean isSomeMutationStatusSelected() {
return !isAllMutationStatusSelected() && !isNoMutationStatusSelected();
}

public boolean isAllTierOptionsSelected() {
return !Objects.isNull(alterationFilter.getSelectedTiers())
&& alterationFilter.getSelectedTiers().hasAll()
&& alterationFilter.getIncludeUnknownTier();
}

public boolean isNoTierOptionsSelected() {
return (Objects.isNull(alterationFilter.getSelectedTiers()) || alterationFilter.getSelectedTiers().hasNone())
&& !alterationFilter.getIncludeUnknownTier();
}

public boolean isSomeTierOptionsSelected() {
return !isAllTierOptionsSelected() && !isNoTierOptionsSelected();
}

public boolean shouldApply() {
return isSomeDriverAnnotationsSelected()
|| isSomeMutationStatusSelected()
|| isSomeTierOptionsSelected()
|| mappedMutationTypes.hasNone()
|| (!mappedMutationTypes.hasNone() && !mappedMutationTypes.hasAll());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.cbioportal.model.ClinicalData;
import org.cbioportal.model.ClinicalDataCount;
import org.cbioportal.model.Sample;
import org.cbioportal.persistence.helper.AlterationFilterHelper;
import org.cbioportal.web.parameter.CategorizedClinicalDataCountFilter;
import org.cbioportal.web.parameter.StudyViewFilter;

Expand All @@ -12,7 +13,8 @@
public interface StudyViewMapper {
List<Sample> getFilteredSamples(StudyViewFilter studyViewFilter, CategorizedClinicalDataCountFilter categorizedClinicalDataCountFilter, boolean applyPatientIdFilters);

List<AlterationCountByGene> getMutatedGenes(StudyViewFilter studyViewFilter, CategorizedClinicalDataCountFilter categorizedClinicalDataCountFilter, boolean applyPatientIdFilters);
List<AlterationCountByGene> getMutatedGenes(StudyViewFilter studyViewFilter, CategorizedClinicalDataCountFilter categorizedClinicalDataCountFilter,
boolean applyPatientIdFilters, AlterationFilterHelper alterationFilterHelper);

List<ClinicalDataCount> getPatientClinicalDataCounts(StudyViewFilter studyViewFilter, CategorizedClinicalDataCountFilter categorizedClinicalDataCountFilter,
boolean applyPatientIdFilters, List<String> attributeIds, List<String> filteredAttributeValues);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.cbioportal.persistence.StudyViewRepository;
import org.cbioportal.persistence.enums.ClinicalAttributeDataSource;
import org.cbioportal.persistence.enums.ClinicalAttributeDataType;
import org.cbioportal.persistence.helper.AlterationFilterHelper;
import org.cbioportal.web.parameter.CategorizedClinicalDataCountFilter;
import org.cbioportal.web.parameter.StudyViewFilter;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -32,7 +33,9 @@ public List<Sample> getFilteredSamples(StudyViewFilter studyViewFilter, Categori

@Override
public List<AlterationCountByGene> getMutatedGenes(StudyViewFilter studyViewFilter, CategorizedClinicalDataCountFilter categorizedClinicalDataCountFilter) {
return mapper.getMutatedGenes(studyViewFilter, categorizedClinicalDataCountFilter, shouldApplyPatientIdFilters(categorizedClinicalDataCountFilter));
return mapper.getMutatedGenes(studyViewFilter, categorizedClinicalDataCountFilter,
shouldApplyPatientIdFilters(categorizedClinicalDataCountFilter),
AlterationFilterHelper.build(studyViewFilter.getAlterationFilter()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,8 @@ public ResponseEntity<List<AlterationCountByGene>> fetchMutatedGenes(
@RequestAttribute(required = false, value = "interceptedStudyViewFilter") StudyViewFilter interceptedStudyViewFilter
) throws StudyNotFoundException {
AlterationFilter annotationFilters = interceptedStudyViewFilter.getAlterationFilter();
List<Sample> samples = studyViewColumnarService.getFilteredSamples(interceptedStudyViewFilter);
List<String> studyIds = new ArrayList<>();
List<String> sampleIds = new ArrayList<>();
for(Sample sample : samples) {
studyIds.add(sample.getCancerStudyIdentifier());
sampleIds.add(sample.getStableId());
}
return new ResponseEntity<>(
studyViewService.getMutationAlterationCountByGenes(studyIds, sampleIds, annotationFilters),
studyViewColumnarService.getMutatedGenes(interceptedStudyViewFilter),
HttpStatus.OK
);
}
Expand Down
29 changes: 29 additions & 0 deletions src/main/resources/db-scripts/clickhouse/clickhouse.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
DROP TABLE IF EXISTS genomic_event_mutation;
DROP TABLE IF EXISTS genomic_event;

CREATE TABLE IF NOT EXISTS genomic_event
(
sample_unique_id String,
variant String,
variant_type String,
hugo_gene_symbol String,
gene_panel_stable_id String,
cancer_study_identifier String,
genetic_profile_stable_id String
) ENGINE = MergeTree
ORDER BY ( variant_type, sample_unique_id, hugo_gene_symbol);

CREATE TABLE IF NOT EXISTS genomic_event_mutation
(
sample_unique_id String,
variant String,
hugo_gene_symbol String,
gene_panel_stable_id String,
cancer_study_identifier String,
genetic_profile_stable_id String,
mutation_type String,
mutation_status String,
driver_filter String,
driver_tiers_filter String
) ENGINE = MergeTree
ORDER BY ( hugo_gene_symbol, genetic_profile_stable_id);
91 changes: 91 additions & 0 deletions src/main/resources/db-scripts/clickhouse/clickhouse_migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@

-- Genomic Event Mutation Data
Insert into genomic_event_mutation
SELECT concat(cs.cancer_study_identifier, '_', sample.stable_id) as sample_unique_id,
me.protein_change as variant,
gene.hugo_gene_symbol as hugo_gene_symbol,
gp.stable_id as gene_panel_stable_id,
cs.cancer_study_identifier as cancer_study_identifier,
g.stable_id as genetic_profile_stable_id,
me.mutation_type as mutation_type,
mutation.mutation_status as mutation_status,
'NA' as driver_filter,
'NA' as drivet_tiers_filter
FROM mutation
INNER JOIN mutation_event as me on mutation.mutation_event_id = me.mutation_event_id
INNER JOIN sample_profile sp
on mutation.sample_id = sp.sample_id and mutation.genetic_profile_id = sp.genetic_profile_id
LEFT JOIN gene_panel gp on sp.panel_id = gp.internal_id
LEFT JOIN genetic_profile g on sp.genetic_profile_id = g.genetic_profile_id
INNER JOIN cancer_study cs on g.cancer_study_id = cs.cancer_study_id
INNER JOIN sample on mutation.sample_id = sample.internal_id
LEFT JOIN gene on mutation.entrez_gene_id = gene.entrez_gene_id;

-- Genomic Event Data
Insert into genomic_event
SELECT concat(cs.cancer_study_identifier, '_', sample.stable_id) as sample_unique_id,
me.protein_change as variant,
'mutation' as variant_type,
gene.hugo_gene_symbol as hugo_gene_symbol,
gp.stable_id as gene_panel_stable_id,
cs.cancer_study_identifier as cancer_study_identifier,
g.stable_id as genetic_profile_stable_id
FROM mutation
INNER JOIN mutation_event as me on mutation.mutation_event_id = me.mutation_event_id
INNER JOIN sample_profile sp
on mutation.sample_id = sp.sample_id and mutation.genetic_profile_id = sp.genetic_profile_id
LEFT JOIN gene_panel gp on sp.panel_id = gp.internal_id
LEFT JOIN genetic_profile g on sp.genetic_profile_id = g.genetic_profile_id
INNER JOIN cancer_study cs on g.cancer_study_id = cs.cancer_study_id
INNER JOIN sample on mutation.sample_id = sample.internal_id
LEFT JOIN gene on mutation.entrez_gene_id = gene.entrez_gene_id
UNION ALL
SELECT concat(cs.cancer_study_identifier, '_', sample.stable_id) as sample_unique_id,
toString(ce.alteration) as variant,
'cna' as variant_type,
gene.hugo_gene_symbol as hugo_gene_symbol,
gp.stable_id as gene_panel_stable_id,
cs.cancer_study_identifier as cancer_study_identifier,
g.stable_id as genetic_profile_stable_id
FROM cna_event ce
INNER JOIN sample_cna_event sce ON ce.cna_event_id = sce.cna_event_id
INNER JOIN sample_profile sp ON sce.sample_id = sp.sample_id AND sce.genetic_profile_id = sp.genetic_profile_id
INNER JOIN gene_panel gp ON sp.panel_id = gp.internal_id
INNER JOIN genetic_profile g ON sp.genetic_profile_id = g.genetic_profile_id
INNER JOIN cancer_study cs ON g.cancer_study_id = cs.cancer_study_id
INNER JOIN sample ON sce.sample_id = sample.internal_id
INNER JOIN gene ON ce.entrez_gene_id = gene.entrez_gene_id
UNION ALL
SELECT
concat(cs.cancer_study_identifier, '_', s.stable_id) as sample_unique_id,
event_info as variant,
'structural_variant' as variant_type,
gene2.hugo_gene_symbol as hugo_gene_symbol,
gene_panel.stable_id as gene_panel_stable_id,
cs.cancer_study_identifier as cancer_study_identifier,
gp.stable_id as genetic_profile_stable_id
FROM
structural_variant sv
INNER JOIN genetic_profile gp ON sv.genetic_profile_id = gp.genetic_profile_id
INNER JOIN sample s ON sv.sample_id = s.internal_id
INNER JOIN cancer_study cs ON gp.cancer_study_id = cs.cancer_study_id
INNER JOIN gene gene2 ON sv.site2_entrez_gene_id = gene2.entrez_gene_id
INNER JOIN sample_profile on s.internal_id = sample_profile.sample_id
INNER JOIN gene_panel on sample_profile.panel_id = gene_panel.internal_id
UNION ALL
SELECT
concat(cs.cancer_study_identifier, '_', s.stable_id) as sample_unique_id,
event_info as variant,
'structural_variant' as variant_type,
gene1.hugo_gene_symbol as hugo_gene_symbol,
gene_panel.stable_id as gene_panel_stable_id,
cs.cancer_study_identifier as cancer_study_identifier,
gp.stable_id as genetic_profile_stable_id
FROM
structural_variant sv
INNER JOIN genetic_profile gp ON sv.genetic_profile_id = gp.genetic_profile_id
INNER JOIN sample s ON sv.sample_id = s.internal_id
INNER JOIN cancer_study cs ON gp.cancer_study_id = cs.cancer_study_id
INNER JOIN gene gene1 ON sv.site1_entrez_gene_id = gene1.entrez_gene_id
INNER JOIN sample_profile on s.internal_id = sample_profile.sample_id
INNER JOIN gene_panel on sample_profile.panel_id = gene_panel.internal_id;
Loading

0 comments on commit 83f2743

Please sign in to comment.