Commit c46b3879 authored by Sebastian Mate's avatar Sebastian Mate
Browse files

Medication implementiert

parent 63cbe8b8
......@@ -16,6 +16,9 @@ create index idx_fhir_resourcetype on i2b2miracum.fhir using btree ((fhir->>'res
drop table if exists i2b2miracum.fhir_failed;
create table i2b2miracum.fhir_failed (ts timestamptz null, message text null, fhir jsonb null);
drop table if exists i2b2miracum.fhir_medication;
create table i2b2miracum.fhir_medication (id text, code text, system text, display text);
-- Create the "fhir_elt_log" table, which log messages:
drop table if exists i2b2miracum.fhir_elt_log;
create table i2b2miracum.fhir_elt_log (ts timestamptz null, message text null);
......@@ -69,139 +72,144 @@ begin
FHIR_resourcetype != 'Condition' and
FHIR_resourcetype != 'Observation' and
FHIR_resourcetype != 'Procedure' and
FHIR_resourcetype != 'Medication' and
FHIR_resourcetype != 'ServiceRequest' and -- Ignored, not useful for i2b2.
FHIR_resourcetype != 'DiagnosticReport' -- Ignored, not useful for i2b2.
then raise exception 'Resource type not supported.'; end if;
--
perform i2b2miracum.fhir_elt_log('--- Processing new ' || FHIR_resourcetype || ' resource:');
--
-- Extract FHIR_patient_identifier and FHIR_encounter_identifier from FHIR resource:
if FHIR_resourcetype = 'Patient' then
FHIR_patient_identifier := jsonb_array_elements(new.fhir->'identifier')->>'value';
if FHIR_patient_identifier is null then raise exception 'FHIR_patient_identifier is empty!'; end if;
else
FHIR_patient_identifier := new.fhir->'subject'->'identifier'->>'value';
end if;
--
if FHIR_resourcetype = 'Encounter' then
FHIR_encounter_identifier := jsonb_array_elements(new.fhir->'identifier')->>'value';
if FHIR_encounter_identifier is null then raise exception 'FHIR_encounter_identifier is empty!'; end if;
else
FHIR_encounter_identifier := new.fhir->'encounter'->'identifier'->>'value';
end if;
--
--if FHIR_patient_identifier is null then FHIR_patient_identifier := 'NONE'; end if;
--if FHIR_encounter_identifier is null then FHIR_encounter_identifier := 'NONE'; end if;
perform i2b2miracum.fhir_elt_log('FHIR_patient_identifier = ' || FHIR_patient_identifier);
perform i2b2miracum.fhir_elt_log('FHIR_encounter_identifier = ' || FHIR_encounter_identifier);
--
-- *** Handle i2b2 PATIENT data / references ***
--
-- Check if the patient is already known in i2b2:
select patient_num into i2b2_patient_found from i2b2miracum.patient_mapping where patient_ide = FHIR_patient_identifier;
--
if i2b2_patient_found is not null then
perform i2b2miracum.fhir_elt_log('Patient is aldready known in i2b2.');
i2b2_patient_identifier := i2b2_patient_found;
if FHIR_resourcetype != 'Medication' then -- Skip all resources that do not contribute patient data.
--
-- Extract FHIR_patient_identifier and FHIR_encounter_identifier from FHIR resource:
if FHIR_resourcetype = 'Patient' then
perform i2b2miracum.fhir_elt_log('Deleting previous entries in PATIENT_MAPPING and PATIENT_DIMENSION.');
delete from i2b2miracum.patient_dimension where patient_num in (select patient_num from i2b2miracum.patient_mapping where patient_ide = FHIR_patient_identifier);
delete from i2b2miracum.patient_mapping where patient_ide = FHIR_patient_identifier;
FHIR_patient_identifier := jsonb_array_elements(new.fhir->'identifier')->>'value';
if FHIR_patient_identifier is null then raise exception 'FHIR_patient_identifier is empty!'; end if;
else
FHIR_patient_identifier := new.fhir->'subject'->'identifier'->>'value';
end if;
else
i2b2_patient_identifier := nextval('patient_num_seq');
perform i2b2miracum.fhir_elt_log('Got new i2b2_patient_identifier = ' || i2b2_patient_identifier);
end if;
--
if i2b2_patient_found is null or FHIR_resourcetype = 'Patient' then
perform i2b2miracum.fhir_elt_log('Inserting into PATIENT_MAPPING.');
-- Populate PATIENT_MAPPING:
insert into i2b2miracum.patient_mapping (patient_ide,patient_ide_source,patient_num,patient_ide_status,project_id,upload_date,update_date,
download_date,import_date,sourcesystem_cd,upload_id)
values (FHIR_patient_identifier, -- patient_ide
'FHIR', -- patient_ide_source
i2b2_patient_identifier, -- patient_num
'A', -- patient_ide_status
'Miracum', -- project_id
now(), -- upload_date
now(), -- update_date
now(), -- download_date
now(), -- import_date
FHIR_sourcesystem, -- sourcesystem_cd
1 -- upload_id
);
--
if FHIR_resourcetype = 'Patient' then
perform i2b2miracum.fhir_elt_log('Inserting into PATIENT_DIMENSION.');
-- Populate PATIENT_DIMENSION:
insert into i2b2miracum.patient_dimension (patient_num,vital_status_cd,birth_date,death_date,sex_cd,age_in_years_num,language_cd,race_cd,
marital_status_cd,religion_cd,zip_cd,statecityzip_path,income_cd,patient_blob,update_date,
download_date,import_date,sourcesystem_cd,upload_id)
values (i2b2_patient_identifier, -- patient_num
case when (new.fhir->>'deceasedDateTime')::timestamp is not null then 'Y'
else 'N' end, -- vital_status_cd
(new.fhir->>'birthDate')::timestamp, -- birth_date
(new.fhir->>'deceasedDateTime')::timestamp, -- death_date
case when new.fhir->>'gender' = 'male' then 'm'
when new.fhir->>'gender' = 'female' then 'f'
when new.fhir->>'gender' = 'other' then 'd'
when new.fhir->>'gender' = 'unknown' then 'x'
else null end, -- sex_cd
case when (new.fhir->>'deceasedDateTime')::timestamp is null then extract(year from age(now(), (new.fhir->>'birthDate')::timestamp))
else extract(year from age((new.fhir->>'deceasedDateTime')::timestamp, (new.fhir->>'birthDate')::timestamp)) end, -- age_in_years_num
null, -- language_cd
null, -- race_cd
null, -- marital_status_cd
null, -- religion_cd
jsonb_array_elements(new.fhir->'address')->>'postalCode', -- zip_cd
null, -- statecityzip_path
null, -- income_cd
null, -- patient_blob
now(), -- update_date
now(), -- download_date
now(), -- import_date
FHIR_sourcesystem, -- sourcesystem_cd
1 -- upload_id
);
if FHIR_resourcetype = 'Encounter' then
FHIR_encounter_identifier := jsonb_array_elements(new.fhir->'identifier')->>'value';
if FHIR_encounter_identifier is null then raise exception 'FHIR_encounter_identifier is empty!'; end if;
else
FHIR_encounter_identifier := new.fhir->'encounter'->'identifier'->>'value';
end if;
end if;
--
-- *** Handle i2b2 ENCOUNTER data / references ***
--
if FHIR_resourcetype != 'Patient' then
-- Check if the encounter is already known in i2b2:
select encounter_num into i2b2_encounter_found from i2b2miracum.encounter_mapping where encounter_ide = FHIR_encounter_identifier;
--
if i2b2_encounter_found is not null then
perform i2b2miracum.fhir_elt_log('Encounter is aldready known in i2b2.');
i2b2_encounter_identifier := i2b2_encounter_found;
if FHIR_resourcetype = 'Encounter' then
perform i2b2miracum.fhir_elt_log('Deleting previous entry in ENCOUNTER_MAPPING.');
delete from i2b2miracum.encounter_mapping where encounter_ide = FHIR_encounter_identifier;
--if FHIR_patient_identifier is null then FHIR_patient_identifier := 'NONE'; end if;
--if FHIR_encounter_identifier is null then FHIR_encounter_identifier := 'NONE'; end if;
perform i2b2miracum.fhir_elt_log('FHIR_patient_identifier = ' || FHIR_patient_identifier);
perform i2b2miracum.fhir_elt_log('FHIR_encounter_identifier = ' || FHIR_encounter_identifier);
--
-- *** Handle i2b2 PATIENT data / references ***
--
-- Check if the patient is already known in i2b2:
select patient_num into i2b2_patient_found from i2b2miracum.patient_mapping where patient_ide = FHIR_patient_identifier;
--
if i2b2_patient_found is not null then
perform i2b2miracum.fhir_elt_log('Patient is aldready known in i2b2.');
i2b2_patient_identifier := i2b2_patient_found;
if FHIR_resourcetype = 'Patient' then
perform i2b2miracum.fhir_elt_log('Deleting previous entries in PATIENT_MAPPING and PATIENT_DIMENSION.');
delete from i2b2miracum.patient_dimension where patient_num in (select patient_num from i2b2miracum.patient_mapping where patient_ide = FHIR_patient_identifier);
delete from i2b2miracum.patient_mapping where patient_ide = FHIR_patient_identifier;
end if;
else
i2b2_encounter_identifier := nextval('encounter_num_seq');
perform i2b2miracum.fhir_elt_log('Got new i2b2_encounter_identifier = ' || i2b2_encounter_identifier);
i2b2_patient_identifier := nextval('patient_num_seq');
perform i2b2miracum.fhir_elt_log('Got new i2b2_patient_identifier = ' || i2b2_patient_identifier);
end if;
--
if i2b2_encounter_found is null or FHIR_resourcetype = 'Encounter' then
perform i2b2miracum.fhir_elt_log('Inserting into ENCOUNTER_MAPPING.');
-- Populate ENCOUNTER_MAPPING:
insert into i2b2miracum.encounter_mapping (encounter_ide, encounter_ide_source, project_id, encounter_num, patient_ide, patient_ide_source, encounter_ide_status, upload_date, update_date, download_date, import_date, sourcesystem_cd, upload_id)
values (FHIR_encounter_identifier, -- encounter_ide
'FHIR', -- encounter_ide_source
'Miracum', -- project_id
i2b2_encounter_identifier, -- encounter_num
FHIR_patient_identifier, -- patient_ide
if i2b2_patient_found is null or FHIR_resourcetype = 'Patient' then
perform i2b2miracum.fhir_elt_log('Inserting into PATIENT_MAPPING.');
-- Populate PATIENT_MAPPING:
insert into i2b2miracum.patient_mapping (patient_ide,patient_ide_source,patient_num,patient_ide_status,project_id,upload_date,update_date,
download_date,import_date,sourcesystem_cd,upload_id)
values (FHIR_patient_identifier, -- patient_ide
'FHIR', -- patient_ide_source
'A', -- encounter_ide_status
i2b2_patient_identifier, -- patient_num
'A', -- patient_ide_status
'Miracum', -- project_id
now(), -- upload_date
now(), -- update_date
now(), -- download_date
now(), -- import_date
FHIR_sourcesystem, -- sourcesystem_cd
1 -- upload_id
);
);
--
if FHIR_resourcetype = 'Patient' then
perform i2b2miracum.fhir_elt_log('Inserting into PATIENT_DIMENSION.');
-- Populate PATIENT_DIMENSION:
insert into i2b2miracum.patient_dimension (patient_num,vital_status_cd,birth_date,death_date,sex_cd,age_in_years_num,language_cd,race_cd,
marital_status_cd,religion_cd,zip_cd,statecityzip_path,income_cd,patient_blob,update_date,
download_date,import_date,sourcesystem_cd,upload_id)
values (i2b2_patient_identifier, -- patient_num
case when (new.fhir->>'deceasedDateTime')::timestamp is not null then 'Y'
else 'N' end, -- vital_status_cd
(new.fhir->>'birthDate')::timestamp, -- birth_date
(new.fhir->>'deceasedDateTime')::timestamp, -- death_date
case when new.fhir->>'gender' = 'male' then 'm'
when new.fhir->>'gender' = 'female' then 'f'
when new.fhir->>'gender' = 'other' then 'd'
when new.fhir->>'gender' = 'unknown' then 'x'
else null end, -- sex_cd
case when (new.fhir->>'deceasedDateTime')::timestamp is null then extract(year from age(now(), (new.fhir->>'birthDate')::timestamp))
else extract(year from age((new.fhir->>'deceasedDateTime')::timestamp, (new.fhir->>'birthDate')::timestamp)) end, -- age_in_years_num
null, -- language_cd
null, -- race_cd
null, -- marital_status_cd
null, -- religion_cd
jsonb_array_elements(new.fhir->'address')->>'postalCode', -- zip_cd
null, -- statecityzip_path
null, -- income_cd
null, -- patient_blob
now(), -- update_date
now(), -- download_date
now(), -- import_date
FHIR_sourcesystem, -- sourcesystem_cd
1 -- upload_id
);
end if;
end if;
--
-- *** Handle i2b2 ENCOUNTER data / references ***
--
if FHIR_resourcetype != 'Patient' then
-- Check if the encounter is already known in i2b2:
select encounter_num into i2b2_encounter_found from i2b2miracum.encounter_mapping where encounter_ide = FHIR_encounter_identifier;
--
if i2b2_encounter_found is not null then
perform i2b2miracum.fhir_elt_log('Encounter is aldready known in i2b2.');
i2b2_encounter_identifier := i2b2_encounter_found;
if FHIR_resourcetype = 'Encounter' then
perform i2b2miracum.fhir_elt_log('Deleting previous entry in ENCOUNTER_MAPPING.');
delete from i2b2miracum.encounter_mapping where encounter_ide = FHIR_encounter_identifier;
end if;
else
i2b2_encounter_identifier := nextval('encounter_num_seq');
perform i2b2miracum.fhir_elt_log('Got new i2b2_encounter_identifier = ' || i2b2_encounter_identifier);
end if;
--
if i2b2_encounter_found is null or FHIR_resourcetype = 'Encounter' then
perform i2b2miracum.fhir_elt_log('Inserting into ENCOUNTER_MAPPING.');
-- Populate ENCOUNTER_MAPPING:
insert into i2b2miracum.encounter_mapping (encounter_ide, encounter_ide_source, project_id, encounter_num, patient_ide, patient_ide_source, encounter_ide_status, upload_date, update_date, download_date, import_date, sourcesystem_cd, upload_id)
values (FHIR_encounter_identifier, -- encounter_ide
'FHIR', -- encounter_ide_source
'Miracum', -- project_id
i2b2_encounter_identifier, -- encounter_num
FHIR_patient_identifier, -- patient_ide
'FHIR', -- patient_ide_source
'A', -- encounter_ide_status
now(), -- upload_date
now(), -- update_date
now(), -- download_date
now(), -- import_date
FHIR_sourcesystem, -- sourcesystem_cd
1 -- upload_id
);
end if;
end if;
end if;
--
......@@ -332,7 +340,30 @@ begin
) fhir_data;
end if;
--
return null;
-- *** Handle FHIR MEDICATION resource ***
--
if FHIR_resourcetype = 'Medication' then
-- Delete previous data:
perform i2b2miracum.fhir_elt_log('Deleting previous entries in FHIR_MEDICATION.');
delete from i2b2miracum.fhir_medication where id = FHIR_id;
-- Insert new data:
perform i2b2miracum.fhir_elt_log('Inserting into FHIR_MEDICATION.');
insert into i2b2miracum.fhir_medication (id, code, system, display)
select FHIR_id,
fhir_data.code,
fhir_data.system,
fhir_data.display
from (SELECT
jsonb_array_elements(new.fhir->'code'->'coding')->>'code' as code,
jsonb_array_elements(new.fhir->'code'->'coding')->>'system' as system,
jsonb_array_elements(new.fhir->'code'->'coding')->>'display' as display
) fhir_data;
--
-- TODO: Update data in observation_fact
--
end if;
--
return null; -- Return nothing. This prevents that the FHIR data is actually written into the "FHIR" table.
exception
when others then
perform i2b2miracum.fhir_elt_log('Exception cought.');
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment