Skip to content

Commit

Permalink
Update Total profile Count to filter on genetic profile
Browse files Browse the repository at this point in the history
  • Loading branch information
haynescd committed Nov 15, 2024
1 parent b08c011 commit 354924c
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public interface StudyViewRepository {

List<CaseListDataCount> getCaseListDataCountsPerStudy(StudyViewFilterContext studyViewFilterContext);

Map<String, Integer> getTotalProfiledCounts(StudyViewFilterContext studyViewFilterContext, String alterationType);
Map<String, Integer> getTotalProfiledCounts(StudyViewFilterContext studyViewFilterContext, String alterationType, List<MolecularProfile> molecularProfiles);

List<ClinicalAttribute> getClinicalAttributes();

Expand Down Expand Up @@ -87,4 +87,6 @@ public interface StudyViewRepository {
List<ClinicalDataCount> getGenericAssayDataBinCounts(StudyViewFilterContext studyViewFilterContext, List<GenericAssayDataBinFilter> genericAssayDataBinFilters);

List<MolecularProfile> getGenericAssayProfiles();

List<MolecularProfile> getFilteredMolecularProfilesByAlterationType(StudyViewFilterContext studyViewFilterContext, String alterationType);
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public interface StudyViewMapper {

List<ClinicalData> getPatientClinicalDataFromStudyViewFilter(StudyViewFilterHelper studyViewFilterHelper, List<String> attributeIds);

List<AlterationCountByGene> getTotalProfiledCounts(StudyViewFilterHelper studyViewFilterHelper, String alterationType);
List<AlterationCountByGene> getTotalProfiledCounts(StudyViewFilterHelper studyViewFilterHelper, String alterationType, List<MolecularProfile> molecularProfiles);

int getFilteredSamplesCount(@Param("studyViewFilterHelper") StudyViewFilterHelper studyViewFilterHelper);

Expand Down Expand Up @@ -85,4 +85,6 @@ public interface StudyViewMapper {
List<ClinicalDataCount> getGenericAssayDataBinCounts(StudyViewFilterHelper studyViewFilterHelper, List<GenericAssayDataBinFilter> genericAssayDataBinFilters);

List<MolecularProfile> getGenericAssayProfiles();

List<MolecularProfile> getFilteredMolecularProfilesByAlterationType(StudyViewFilterHelper studyViewFilterHelper, String alterationType);
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,17 @@
@Repository
public class StudyViewMyBatisRepository implements StudyViewRepository {

private final StudyViewMapper studyViewMapper;
private Map<DataSource, List<ClinicalAttribute>> clinicalAttributesMap = new EnumMap<>(DataSource.class);
private Map<DataSource, List<MolecularProfile>> genericAssayProfilesMap = new EnumMap<>(DataSource.class);

private static final List<String> FILTERED_CLINICAL_ATTR_VALUES = Collections.emptyList();
private final StudyViewMapper mapper;

@Autowired
public StudyViewMyBatisRepository(StudyViewMapper mapper) {
this.mapper = mapper;
public StudyViewMyBatisRepository(StudyViewMapper mapper, StudyViewMapper studyViewMapper) {
this.mapper = mapper;
this.studyViewMapper = studyViewMapper;
}

@Override
Expand Down Expand Up @@ -126,6 +128,11 @@ public List<MolecularProfile> getGenericAssayProfiles() {
return mapper.getGenericAssayProfiles();
}

@Override
public List<MolecularProfile> getFilteredMolecularProfilesByAlterationType(StudyViewFilterContext studyViewFilterContext, String alterationType) {
return studyViewMapper.getFilteredMolecularProfilesByAlterationType(createStudyViewFilterHelper(studyViewFilterContext), alterationType);
}

@Override
public Map<String, ClinicalDataType> getClinicalAttributeDatatypeMap() {
if (clinicalAttributesMap.isEmpty()) {
Expand Down Expand Up @@ -166,8 +173,8 @@ public List<ClinicalData> getPatientClinicalData(StudyViewFilterContext studyVie
}

@Override
public Map<String, Integer> getTotalProfiledCounts(StudyViewFilterContext studyViewFilterContext, String alterationType) {
return mapper.getTotalProfiledCounts(createStudyViewFilterHelper(studyViewFilterContext), alterationType)
public Map<String, Integer> getTotalProfiledCounts(StudyViewFilterContext studyViewFilterContext, String alterationType, List<MolecularProfile> molecularProfiles) {
return mapper.getTotalProfiledCounts(createStudyViewFilterHelper(studyViewFilterContext), alterationType, molecularProfiles)
.stream()
.collect(Collectors.groupingBy(AlterationCountByGene::getHugoGeneSymbol,
Collectors.mapping(AlterationCountByGene::getNumberOfProfiledCases, Collectors.summingInt(Integer::intValue))));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,9 @@ public List<AlterationCountByGene> getStructuralVariantGenes(StudyViewFilterCont
private < T extends AlterationCountByGene> List<T> populateAlterationCounts(@NonNull List<T> alterationCounts,
@NonNull StudyViewFilterContext studyViewFilterContext,
@NonNull AlterationType alterationType) {
final var firstMolecularProfileForEachStudy = getFirstMolecularProfileGroupedByStudy(studyViewFilterContext, alterationType);
final int totalProfiledCount = studyViewRepository.getTotalProfiledCountsByAlterationType(studyViewFilterContext, alterationType.toString());
var profiledCountsMap = studyViewRepository.getTotalProfiledCounts(studyViewFilterContext, alterationType.toString());
var profiledCountsMap = studyViewRepository.getTotalProfiledCounts(studyViewFilterContext, alterationType.toString(), firstMolecularProfileForEachStudy);
final var matchingGenePanelIdsMap = studyViewRepository.getMatchingGenePanelIds(studyViewFilterContext, alterationType.toString());
final int sampleProfileCountWithoutGenePanelData = studyViewRepository.getSampleProfileCountWithoutPanelData(studyViewFilterContext, alterationType.toString());

Expand Down Expand Up @@ -340,6 +341,19 @@ private List<CopyNumberCountByGene> populateAlterationCountsWithCNASigQValue(Lis
return alterationCountByGenes;
}

private List<MolecularProfile> getFirstMolecularProfileGroupedByStudy(StudyViewFilterContext studyViewFilterContext, AlterationType alterationType) {
final var molecularProfiles = studyViewRepository.getFilteredMolecularProfilesByAlterationType(studyViewFilterContext, alterationType.toString());

return molecularProfiles.stream()
.collect(Collectors.toMap(
MolecularProfile::getCancerStudyIdentifier,
Function.identity(),
(existing, replacement) -> existing // Keep the first occurrence
))
.values()
.stream()
.toList();
}

/**
* Combines alteration counts by Hugo gene symbols. If multiple entries exist for the same
Expand Down
8 changes: 5 additions & 3 deletions src/main/resources/db-scripts/clickhouse/clickhouse.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,19 @@ CREATE TABLE sample_to_gene_panel_derived
sample_unique_id String,
alteration_type LowCardinality(String),
gene_panel_id LowCardinality(String),
cancer_study_identifier LowCardinality(String)
cancer_study_identifier LowCardinality(String),
genetic_profile_id LowCardinality(String)
) ENGINE = MergeTree()
ORDER BY (gene_panel_id, alteration_type, sample_unique_id);
ORDER BY (gene_panel_id, alteration_type, genetic_profile_id, sample_unique_id);

INSERT INTO sample_to_gene_panel_derived
SELECT
concat(cs.cancer_study_identifier, '_', sample.stable_id) AS sample_unique_id,
genetic_alteration_type AS alteration_type,
-- If a mutation is found in a gene that is not in a gene panel we assume Whole Exome Sequencing WES
ifnull(gene_panel.stable_id, 'WES') AS gene_panel_id,
cs.cancer_study_identifier AS cancer_study_identifier
cs.cancer_study_identifier AS cancer_study_identifier,
gp.stable_id AS genetic_profile_id
FROM sample_profile sp
INNER JOIN genetic_profile gp ON sample_profile.genetic_profile_id = gp.genetic_profile_id
LEFT JOIN gene_panel ON sp.panel_id = gene_panel.internal_id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -471,12 +471,18 @@
<select id="getTotalProfiledCounts" resultType="org.cbioportal.model.AlterationCountByGene">
SELECT
gene as hugoGeneSymbol,
COUNT(DISTINCT sample_unique_id) as numberOfProfiledCases
COUNT(*) as numberOfProfiledCases
FROM sample_to_gene_panel_derived stgp
INNER JOIN gene_panel_to_gene_derived gptg on stgp.gene_panel_id = gptg.gene_panel_id
<where>
stgp.alteration_type = #{alterationType}
AND stgp.gene_panel_id != 'WES'
<if test="molecularProfiles != null and !molecularProfiles.isEmpty()">
AND stgp.genetic_profile_id IN
<foreach item="molecularProfile" collection="molecularProfiles" open="(" separator="," close=")">
#{molecularProfile.stableId}
</foreach>
</if>
AND
<include refid="applyStudyViewFilter">
<property name="filter_type" value="'SAMPLE_ID_ONLY'"/>
Expand Down Expand Up @@ -781,6 +787,31 @@
INNER JOIN reference_genome ON cancer_study.reference_genome_id = reference_genome.reference_genome_id
WHERE genetic_profile.genetic_alteration_type = 'GENERIC_ASSAY'
</select>

<select id="getFilteredMolecularProfilesByAlterationType" resultType="org.cbioportal.model.MolecularProfile">
WITH unique_study_ids AS (
SELECT
DISTINCT cancer_study_identifier
FROM sample_derived
<where>
<include refid="applyStudyViewFilter">
<property name="filter_type" value="'SAMPLE_ID_ONLY'"/>
</include>
</where>
)
SELECT
gp.stable_id AS stableId,
gp.datatype AS datatype,
cancer_study.cancer_study_identifier AS cancerStudyIdentifier
FROM genetic_profile gp
INNER JOIN cancer_study ON gp.cancer_study_id = cancer_study.cancer_study_id
<where>
gp.genetic_alteration_type = #{alterationType}
<!-- Currently we only look at CNA dataType Discrete -->
AND gp.datatype != 'CONTINUOUS'
AND cancer_study.cancer_study_identifier IN (unique_study_ids);
</where>
</select>

<!--
Abstraction for StudyViewFilter. Currently, we have many ch tables that have both
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ public void getTotalProfiledCountsByGene() {
studyViewFilter.setStudyIds(List.of(STUDY_TCGA_PUB));

var totalProfiledCountsMap = studyViewMapper.getTotalProfiledCounts(StudyViewFilterHelper.build(studyViewFilter, null, null),
"MUTATION_EXTENDED");
"MUTATION_EXTENDED", List.of());

assertEquals(3, totalProfiledCountsMap.size());

Expand Down

0 comments on commit 354924c

Please sign in to comment.