Skip to content

Commit

Permalink
make sure study ids exist before using them in filter SQL
Browse files Browse the repository at this point in the history
  • Loading branch information
onursumer committed Dec 11, 2024
1 parent 1107607 commit af84d12
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,20 @@
JOIN genetic_profile gp ON sample_profile.genetic_profile_id = gp.genetic_profile_id
JOIN cancer_study cs ON gp.cancer_study_id = cs.cancer_study_id
JOIN sample_derived on sample_profile.sample_id = sample_derived.internal_id
<where>
sample_derived.cancer_study_identifier IN
<foreach item="studyId" collection="studyViewFilterHelper.studyViewFilter.studyIds" open="(" separator="," close=")">
#{studyId}
</foreach>
AND
<foreach item="studyId" collection="studyViewFilterHelper.studyViewFilter.studyIds" open="(" close=")" separator="OR">
<foreach item="genomicProfileId" collection="ANDGroup" open="(" separator=" OR " close=")">
gp.stable_id=concat(#{studyId}, '_', #{genomicProfileId})
<if test="studyViewFilterHelper.studyViewFilter.studyIds != null and !studyViewFilterHelper.studyViewFilter.studyIds.isEmpty()">
<where>
sample_derived.cancer_study_identifier IN
<foreach item="studyId" collection="studyViewFilterHelper.studyViewFilter.studyIds" open="(" separator="," close=")">
#{studyId}
</foreach>
</foreach>
</where>
AND
<foreach item="studyId" collection="studyViewFilterHelper.studyViewFilter.studyIds" open="(" close=")" separator="OR">
<foreach item="genomicProfileId" collection="ANDGroup" open="(" separator=" OR " close=")">
gp.stable_id=concat(#{studyId}, '_', #{genomicProfileId})
</foreach>
</foreach>
</where>
</if>
</foreach>
)

Expand Down Expand Up @@ -430,10 +432,12 @@
SELECT sample_unique_id, patient_unique_id, attribute_value
FROM clinical_data_derived
WHERE attribute_name = #{clinicalDataFilter.attributeId} AND type='${type}'
AND cancer_study_identifier IN
<foreach item="studyId" collection="studyViewFilterHelper.studyViewFilter.studyIds" open="(" separator="," close=")">
#{studyId}
</foreach>
<if test="studyViewFilterHelper.studyViewFilter.studyIds != null and !studyViewFilterHelper.studyViewFilter.studyIds.isEmpty()">
AND cancer_study_identifier IN
<foreach item="studyId" collection="studyViewFilterHelper.studyViewFilter.studyIds" open="(" separator="," close=")">
#{studyId}
</foreach>
</if>
</sql>

<sql id="categoricalClinicalDataCountFilter">
Expand Down Expand Up @@ -511,10 +515,12 @@
FROM genetic_alteration_derived
WHERE profile_type = #{genomicDataFilter.profileType}
AND hugo_gene_symbol = #{genomicDataFilter.hugoGeneSymbol}
<if test="studyViewFilterHelper.studyViewFilter.studyIds != null and !studyViewFilterHelper.studyViewFilter.studyIds.isEmpty()">
AND cancer_study_identifier IN
<foreach item="studyId" collection="studyViewFilterHelper.studyViewFilter.studyIds" open="(" separator="," close=")">
#{studyId}
</foreach>
</if>
</sql>

<sql id="applyGenericAssayDataFilter">
Expand Down Expand Up @@ -641,30 +647,40 @@
SELECT sample_unique_id
FROM sample_derived
WHERE cancer_study_identifier IN
<if test="studyViewFilterHelper.studyViewFilter.studyIds != null and !studyViewFilterHelper.studyViewFilter.studyIds.isEmpty()">
<foreach item="studyId" collection="studyViewFilterHelper.studyViewFilter.studyIds" open="(" separator="," close=")">
#{studyId}
</foreach>
</if>
),
profiled_samples AS (
SELECT DISTINCT sgp.sample_unique_id
FROM sample_to_gene_panel_derived sgp
JOIN gene_panel_to_gene_derived gpg ON sgp.gene_panel_id = gpg.gene_panel_id
WHERE cancer_study_identifier IN
WHERE
<if test="studyViewFilterHelper.studyViewFilter.studyIds != null and !studyViewFilterHelper.studyViewFilter.studyIds.isEmpty()">
cancer_study_identifier IN
<foreach item="studyId" collection="studyViewFilterHelper.studyViewFilter.studyIds" open="(" separator="," close=")">
#{studyId}
</foreach>
AND gpg.gene = #{mutationDataFilter.hugoGeneSymbol}
AND sgp.alteration_type = 'MUTATION_EXTENDED'
AND
</if>
gpg.gene = #{mutationDataFilter.hugoGeneSymbol}
AND sgp.alteration_type = 'MUTATION_EXTENDED'
),
mutated_samples AS (
SELECT DISTINCT sample_unique_id
FROM genomic_event_derived
WHERE cancer_study_identifier IN
WHERE
<if test="studyViewFilterHelper.studyViewFilter.studyIds != null and !studyViewFilterHelper.studyViewFilter.studyIds.isEmpty()">
cancer_study_identifier IN
<foreach item="studyId" collection="studyViewFilterHelper.studyViewFilter.studyIds" open="(" separator="," close=")">
#{studyId}
</foreach>
AND hugo_gene_symbol = #{mutationDataFilter.hugoGeneSymbol}
AND variant_type = 'mutation'
AND
</if>
hugo_gene_symbol = #{mutationDataFilter.hugoGeneSymbol}
AND variant_type = 'mutation'
)
SELECT DISTINCT sample_unique_id
FROM
Expand Down Expand Up @@ -706,20 +722,26 @@
FROM genetic_alteration_derived
WHERE profile_type = #{genomicDataFilter.profileType}
AND hugo_gene_symbol = #{genomicDataFilter.hugoGeneSymbol}
<if test="studyViewFilterHelper.studyViewFilter.studyIds != null and !studyViewFilterHelper.studyViewFilter.studyIds.isEmpty()">
AND cancer_study_identifier IN
<foreach item="studyId" collection="studyViewFilterHelper.studyViewFilter.studyIds" open="(" separator="," close=")">
#{studyId}
</foreach>
</if>
)
SELECT DISTINCT sd.sample_unique_id
<!-- join with sample table to get all 'NA' samples -->
FROM sample_derived sd
LEFT JOIN cna_query ON sd.sample_unique_id = cna_query.sampleUniqueId
WHERE cancer_study_identifier IN
WHERE
<if test="studyViewFilterHelper.studyViewFilter.studyIds != null and !studyViewFilterHelper.studyViewFilter.studyIds.isEmpty()">
cancer_study_identifier IN
<foreach item="studyId" collection="studyViewFilterHelper.studyViewFilter.studyIds" open="(" separator="," close=")">
#{studyId}
</foreach>
<foreach item="dataFilterValue" collection="genomicDataFilter.values" open="AND (" separator=" OR " close=")">
AND
</if>
<foreach item="dataFilterValue" collection="genomicDataFilter.values" open="(" separator=" OR " close=")">
<choose>
<!-- NA value samples -->
<when test="dataFilterValue.value == 'NA'">alteration_value IS null</when>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,11 +346,14 @@
(
SELECT count() > 0
FROM genetic_profile
WHERE patient_level = 1
AND stable_id IN
<foreach item="studyId" collection="studyViewFilterHelper.studyViewFilter.studyIds" open="(" separator="," close=")">
concat(#{studyId}, '_', #{profileType})
</foreach>
WHERE
patient_level = 1
<if test="studyViewFilterHelper.studyViewFilter.studyIds != null and !studyViewFilterHelper.studyViewFilter.studyIds.isEmpty()">
AND stable_id IN
<foreach item="studyId" collection="studyViewFilterHelper.studyViewFilter.studyIds" open="(" separator="," close=")">
concat(#{studyId}, '_', #{profileType})
</foreach>
</if>
),
(SELECT * FROM (<include refid="getTotalPatientCount"/>)),
(SELECT * FROM (<include refid="getTotalSampleCount"/>))
Expand Down

0 comments on commit af84d12

Please sign in to comment.