Files
ISSSTELEON/Transformation/models/cama/silver/cama_estratificadores.sql
T
rockdrigo 86b8da08bf CaMa: tumour stratifiers derived in dbt, and the BI-RADS of invasive cancers broken down by them
How: `cama_estratificadores` turns the timeline's lines read from reports into timeline facts: the clinical stage's
group, early or advanced stage, the surrogate molecular subtype from one pathology report, and T by the lesion's
size, by the rules in `vars.cama.estratos`. `cama_cancer_invasor` has one row per biopsy report with an invasive
cancer (`vars.cama.grados_invasor`) and the BI-RADS of the mammogram that led to it. CAMA_BIRADS_CANCER_INVASOR
counts those patients by BI-RADS, stratified by age group, stage, early or advanced, subtype and size. Bumps WebApi
to the stratum read by the event cut-off.

Implications: the analysis' stratifiers exist as data and reach FHIR as stratifiers. Their rules (stage groups,
Ki-67 and progesterone thresholds, T bounds) are provisional placeholders in dbt's vars. BRCA has no source yet.
2026-11-13 15:19:19 -06:00

83 lines
3.7 KiB
SQL

{# Each patient's stratifiers for the analysis, as timeline facts (`pac_id`, `tipo`, `fecha`, `conocida_en`): what a
measure's `linea_de_tiempo_*` strata ask about. Derived from cama_linea_de_tiempo's lines read from reports, by the
rules in `vars.cama.estratos`: the clinical stage's group and early or advanced stage (from a note), the surrogate
molecular subtype (from one pathology report's receptors, HER2 and Ki-67 together) and the lesion's T (from a
mammography report). A fact is dated and known as the lines it comes from. A view, like the timeline. #}
{{ config(materialized = 'view') }}
{%- set e = var('cama')['estratos'] -%}
with linea as (
select pac_id, fecha, conocida_en, tipo, valor_codigo, valor_numerico, fuente_tabla, fuente_id
from {{ ref('cama_linea_de_tiempo') }}
),
estadio as (
select
pac_id, fecha, conocida_en,
case valor_codigo
{%- for codigo, grupo in e['estadios'].items() %}
when '{{ codigo }}' then 'estadio_{{ grupo }}'
{%- endfor %}
end as estadio,
case valor_codigo
{%- for codigo, grupo in e['estadios'].items() %}
when '{{ codigo }}' then '{{ 'etapa_temprana' if grupo in e['etapa_temprana'] else 'etapa_avanzada' }}'
{%- endfor %}
end as etapa
from linea
where tipo = 'estadio_clinico'
),
reporte as (
select
pac_id, fecha, fuente_tabla, fuente_id,
max(conocida_en) as conocida_en,
max(case when tipo = 'receptor_estrogeno' then valor_codigo end) as re,
max(case when tipo = 'receptor_progesterona' then valor_codigo end) as rp,
max(case when tipo = 'receptor_progesterona_pct' then valor_numerico end) as rp_pct,
max(case when tipo = 'her2' then valor_codigo end) as her2,
max(case when tipo = 'ki67_pct' then valor_numerico end) as ki67
from linea
where tipo in ('receptor_estrogeno', 'receptor_progesterona', 'receptor_progesterona_pct', 'her2', 'ki67_pct')
group by pac_id, fecha, fuente_tabla, fuente_id
),
subtipo as (
select
pac_id, fecha, conocida_en,
case
when her2 in {{ sql_list(e['her2_positivo']) }} then 'subtipo_her2'
when her2 not in {{ sql_list(e['her2_negativo']) }} or her2 is null then null
when re = 'NEGATIVO' and rp = 'NEGATIVO' then 'subtipo_triple_negativo'
when re = 'POSITIVO' or rp = 'POSITIVO' then
case
when ki67 is null then null
when ki67 < {{ e['ki67_corte'] }} and rp = 'POSITIVO'
and (rp_pct is null or rp_pct >= {{ e['rp_corte'] }}) then 'subtipo_luminal_a'
else 'subtipo_luminal_b'
end
end as subtipo
from reporte
),
tamano as (
select
pac_id, fecha, conocida_en,
case
{%- for t in e['tamano_t'] if t['hasta_mm'] is not none %}
when valor_numerico <= {{ t['hasta_mm'] }} then 'tamano_{{ t['t'] }}'
{%- endfor %}
else 'tamano_{{ (e['tamano_t'] | last)['t'] }}'
end as t
from linea
where tipo = 'tamano_lesion_mm' and valor_numerico is not null
)
select pac_id, estadio as tipo, fecha, conocida_en from estadio where estadio is not null
union all
select pac_id, etapa as tipo, fecha, conocida_en from estadio where etapa is not null
union all
select pac_id, subtipo as tipo, fecha, conocida_en from subtipo where subtipo is not null
union all
select pac_id, t as tipo, fecha, conocida_en from tamano