Commit 44ad20f3 authored by Sebastian Mate's avatar Sebastian Mate
Browse files

Implemented MedicationStatement compatibility with GECCO data from odm2fhir

Generation of system identifier abbreviations now handled in function get_system_abbrv()
parent 14c461d9
......@@ -9,9 +9,9 @@
-- 2021-01-22: - Add additional test data (in the comment block at the end of the file) so that i2b2 returns a patient count
-- 2021-01-25: - Initial basic support for FHIR bundles
-- - Improved readout of Patient resource
-- 2021-01-26: - Recognize SNOMED-CT entries
-- - Update all coding systems
-- 2021-01-26: - Use now() when no date is specified (Observation, Procedure)
-- - Implemented MedicationStatement compatibility with GECCO data from odm2fhir
-- - Generation of system identifier abbreviations now handled in function get_system_abbrv()
-- ============= Create Basic Tables =============
......@@ -78,10 +78,29 @@ drop sequence if exists instance_num_seq;
create sequence instance_num_seq;
select setval('instance_num_seq', (select max(instance_num) from i2b2miracum.observation_fact), true);
-- Create the "get_system_abbrv" function that abbreviates long system identifiers:
create or replace function i2b2miracum.get_system_abbrv(systemlink text) returns text language plpgsql as
$$
begin
case when systemlink = 'http://fhir.de/CodeSystem/dimdi/icd-10-gm' then return 'ICD10';
when systemlink = 'http://fhir.de/CodeSystem/dimdi/ops' then return 'OPS';
when systemlink = 'http://fhir.de/CodeSystem/dimdi/atc' then return 'ATC';
when systemlink = 'http://snomed.info/sct' then return 'SNOMED-CT';
when systemlink = 'http://loinc.org' then return 'LOINC';
when systemlink = 'urn:oid:2.16.840.1.113883.6.18.2.6' then return 'PRODUCT-TYPE';
when systemlink = 'https://www.netzwerk-universitaetsmedizin.de/fhir/CodeSystem/ecrf-parameter-codes' then return 'ECRF';
when systemlink = 'http://fdasis.nlm.nih.gov' then return 'FDASIS';
when systemlink = 'http://dicom.nema.org/resources/ontology/DCM' then return 'DCM';
else
perform i2b2miracum.fhir_elt_log('ERROR: Coding system unknown: ' || systemlink);
return 'UNKNOWN';
end case;
end;
$$;
-- ============= i2b2 FHIR Trigger =============
-- Create the "fhir_inserted" function that is called each time a new FHIR resource is inserted:
create or replace function i2b2miracum.fhir_inserted() returns trigger language plpgsql as
$$
declare
......@@ -104,7 +123,7 @@ begin
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: ' || sqlerrm);
perform i2b2miracum.fhir_elt_log('ERROR: Exception cought: ' || sqlerrm);
insert into i2b2miracum.fhir_failed(ts, message, fhir) values (now(), sqlerrm, new.fhir);
return null;
end;
......@@ -298,13 +317,7 @@ begin
insert into i2b2miracum.observation_fact (encounter_num, patient_num, concept_cd, provider_id, start_date, modifier_cd, instance_num, valtype_cd, tval_char, nval_num, valueflag_cd, quantity_num, units_cd, end_date, location_cd, observation_blob, confidence_num, update_date, download_date, import_date, sourcesystem_cd, upload_id, text_search_index)
select i2b2_encounter_identifier, -- encounter_num
i2b2_patient_identifier, -- patient_num
case when fhir_data.system = 'http://fhir.de/CodeSystem/dimdi/icd-10-gm' then 'ICD10'
when fhir_data.system = 'http://fhir.de/CodeSystem/dimdi/ops' then 'OPS'
when fhir_data.system = 'http://fhir.de/CodeSystem/dimdi/atc' then 'ATC'
when fhir_data.system = 'http://snomed.info/sct' then 'SNOMED-CT'
when fhir_data.system = 'http://loinc.org' then 'LOINC'
else 'UNKNOWN'
end || ':' || fhir_data.code, -- concept_cd
i2b2miracum.get_system_abbrv(fhir_data.system) || ':' || fhir_data.code, -- concept_cd
'FHIR', -- provider_id
case when fhir_data.onsetDateTime is not null then fhir_data.onsetDateTime
else now()
......@@ -347,13 +360,7 @@ begin
insert into i2b2miracum.observation_fact (encounter_num, patient_num, concept_cd, provider_id, start_date, modifier_cd, instance_num, valtype_cd, tval_char, nval_num, valueflag_cd, quantity_num, units_cd, end_date, location_cd, observation_blob, confidence_num, update_date, download_date, import_date, sourcesystem_cd, upload_id, text_search_index)
select i2b2_encounter_identifier, -- encounter_num
i2b2_patient_identifier, -- patient_num
case when fhir_data.system = 'http://fhir.de/CodeSystem/dimdi/icd-10-gm' then 'ICD10'
when fhir_data.system = 'http://fhir.de/CodeSystem/dimdi/ops' then 'OPS'
when fhir_data.system = 'http://fhir.de/CodeSystem/dimdi/atc' then 'ATC'
when fhir_data.system = 'http://snomed.info/sct' then 'SNOMED-CT'
when fhir_data.system = 'http://loinc.org' then 'LOINC'
else 'UNKNOWN'
end || ':' || fhir_data.code, -- concept_cd
i2b2miracum.get_system_abbrv(fhir_data.system) || ':' || fhir_data.code, -- concept_cd
'FHIR', -- provider_id
case when fhir_data.effectiveDateTime is not null then fhir_data.effectiveDateTime
else now() end, -- start_date
......@@ -398,13 +405,7 @@ begin
insert into i2b2miracum.observation_fact (encounter_num, patient_num, concept_cd, provider_id, start_date, modifier_cd, instance_num, valtype_cd, tval_char, nval_num, valueflag_cd, quantity_num, units_cd, end_date, location_cd, observation_blob, confidence_num, update_date, download_date, import_date, sourcesystem_cd, upload_id, text_search_index)
select i2b2_encounter_identifier, -- encounter_num
i2b2_patient_identifier, -- patient_num
case when fhir_data.system = 'http://fhir.de/CodeSystem/dimdi/icd-10-gm' then 'ICD10'
when fhir_data.system = 'http://fhir.de/CodeSystem/dimdi/ops' then 'OPS'
when fhir_data.system = 'http://fhir.de/CodeSystem/dimdi/atc' then 'ATC'
when fhir_data.system = 'http://snomed.info/sct' then 'SNOMED-CT'
when fhir_data.system = 'http://loinc.org' then 'LOINC'
else 'UNKNOWN'
end || ':' || fhir_data.code, -- concept_cd
i2b2miracum.get_system_abbrv(fhir_data.system) || ':' || fhir_data.code, -- concept_cd
'FHIR', -- provider_id
case when fhir_data.performedDateTime is not null then fhir_data.performedDateTime
else now() end, -- start_date
......@@ -459,13 +460,7 @@ begin
insert into i2b2miracum.observation_fact (
select oft.encounter_num,
oft.patient_num,
case when fm.system = 'http://fhir.de/CodeSystem/dimdi/icd-10-gm' then 'ICD10'
when fm.system = 'http://fhir.de/CodeSystem/dimdi/ops' then 'OPS'
when fm.system = 'http://fhir.de/CodeSystem/dimdi/atc' then 'ATC'
when fm.system = 'http://snomed.info/sct' then 'SNOMED-CT'
when fm.system = 'http://loinc.org' then 'LOINC'
else 'UNKNOWN'
end || ':' || fhir_data.code, -- concept_cd
i2b2miracum.get_system_abbrv(fhir_data.system) || ':' || fhir_data.code, -- concept_cd
oft.provider_id,
oft.start_date,
oft.modifier_cd,
......@@ -506,12 +501,16 @@ begin
insert into i2b2miracum.observation_fact (encounter_num, patient_num, concept_cd, provider_id, start_date, modifier_cd, instance_num, valtype_cd, tval_char, nval_num, valueflag_cd, quantity_num, units_cd, end_date, location_cd, observation_blob, confidence_num, update_date, download_date, import_date, sourcesystem_cd, upload_id, text_search_index, fhir_reference)
select i2b2_encounter_identifier, -- encounter_num
i2b2_patient_identifier, -- patient_num
case when fhir_data.medication_code is null then 'Medication' else (
case when fhir_data.medication_system = 'http://fhir.de/CodeSystem/dimdi/atc' then 'ATC'
else 'UNKNOWN'
end || ':' || fhir_data.medication_code) end , -- concept_cd
case when fhir_data.code is not null then
i2b2miracum.get_system_abbrv(fhir_data.system) || ':' || fhir_data.code
when fhir_data.medication_code is not null then
i2b2miracum.get_system_abbrv(fhir_data.medication_system) || ':' || fhir_data.medication_code
else
'Medication'
end,
'FHIR', -- provider_id
fhir_data.effectiveDateTime, -- start_date
case when fhir_data.effectiveDateTime is not null then fhir_data.effectiveDateTime
else now() end, -- start_date
'@', -- modifier_cd
nextval('instance_num_seq'), -- instance_num
'@', -- valtype_cd
......@@ -520,7 +519,8 @@ begin
null, -- valueflag_cd
null, -- quantity_num
null, -- units_cd
fhir_data.effectiveDateTime, -- end_date
case when fhir_data.effectiveDateTime is not null then fhir_data.effectiveDateTime
else now() end, -- end_date
'', -- location_cd
FHIR_id, -- observation_blob
0, -- confidence_num
......@@ -534,8 +534,10 @@ begin
from (select (new_as_table.fhir->>'effectiveDateTime')::timestamp as effectiveDateTime,
fhir_medication.code as medication_code,
fhir_medication.system as medication_system,
new_as_table.fhir->'medicationReference'->>'reference' as medication_reference
from (select *) new_as_table
new_as_table.fhir->'medicationReference'->>'reference' as medication_reference, -- from i2b2 table "fhir_medication"
jsonb_array_elements(new_as_table.fhir->'medicationCodeableConcept'->'coding')->>'code' as code, -- from GECCO resource
jsonb_array_elements(new_as_table.fhir->'medicationCodeableConcept'->'coding')->>'system' as system -- from GECCO resource
from (select fhir) new_as_table
left join i2b2miracum.fhir_medication
on new_as_table.fhir->'medicationReference'->>'reference' = 'Medication/' || fhir_medication.id
) fhir_data;
......
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