Commit 5e2ad6e1 authored by Sebastian Mate's avatar Sebastian Mate
Browse files

Medication und MedicationStatement vollständig implementiert

parent c46b3879
......@@ -38,6 +38,15 @@ truncate table i2b2miracum.encounter_mapping;
truncate table i2b2miracum.observation_fact;
truncate table i2b2miracum.fhir_elt_log;
-- Modify OBSERVATION_FACT:
do $$
begin
alter table i2b2miracum.observation_fact add fhir_reference varchar(100) null;
exception
when duplicate_column then raise notice 'column already exists';
end;
$$;
-- Create and Initialize PATIENT_NUM up-counter:
drop sequence patient_num_seq;
create sequence patient_num_seq;
......@@ -67,12 +76,13 @@ declare FHIR_encounter_identifier text;
declare i2b2_patient_identifier int; declare i2b2_patient_found int;
declare i2b2_encounter_identifier int; declare i2b2_encounter_found int;
begin
if FHIR_resourcetype != 'Patient' and
if FHIR_resourcetype != 'Patient' and
FHIR_resourcetype != 'Encounter' and
FHIR_resourcetype != 'Condition' and
FHIR_resourcetype != 'Observation' and
FHIR_resourcetype != 'Procedure' and
FHIR_resourcetype != 'Medication' and
FHIR_resourcetype != 'MedicationStatement' 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;
......@@ -97,8 +107,12 @@ begin
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;
if FHIR_resourcetype = 'MedicationStatement' then
FHIR_encounter_identifier := new.fhir->'context'->'identifier'->>'value';
end if;
--
--if FHIR_patient_identifier is null then FHIR_patient_identifier := 'UNKNOWN'; end if;
--if FHIR_encounter_identifier is null then FHIR_encounter_identifier := 'UNKNOWN'; end if;
perform i2b2miracum.fhir_elt_log('FHIR_patient_identifier = ' || FHIR_patient_identifier);
perform i2b2miracum.fhir_elt_log('FHIR_encounter_identifier = ' || FHIR_encounter_identifier);
--
......@@ -346,7 +360,7 @@ begin
-- 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:
-- Store Medication information in separate table FHIR_MEDICATION:
perform i2b2miracum.fhir_elt_log('Inserting into FHIR_MEDICATION.');
insert into i2b2miracum.fhir_medication (id, code, system, display)
select FHIR_id,
......@@ -358,9 +372,56 @@ begin
jsonb_array_elements(new.fhir->'code'->'coding')->>'system' as system,
jsonb_array_elements(new.fhir->'code'->'coding')->>'display' as display
) fhir_data;
-- Update old MedicationStatement data in OBSERVATION_FACT
perform i2b2miracum.fhir_elt_log('Updating OBSERVATION_FACT.');
delete from i2b2miracum.observation_fact where fhir_reference = 'Medication/' || FHIR_id;
--
-- TODO: Update data in observation_fact
end if;
--
-- *** Handle FHIR MEDICATIONSTATEMENT resource ***
--
if FHIR_resourcetype = 'MedicationStatement' then
-- Delete previous data:
perform i2b2miracum.fhir_elt_log('Deleting previous entries in OBSERVATION_FACT.');
delete from i2b2miracum.observation_fact where observation_blob = FHIR_id;
-- Insert new data:
perform i2b2miracum.fhir_elt_log('Inserting into OBSERVATION_FACT.');
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
'FHIR', -- provider_id
fhir_data.effectiveDateTime, -- start_date
'@', -- modifier_cd
nextval('instance_num_seq'), -- instance_num
'@', -- valtype_cd
null, -- tval_char
null, -- nval_num
null, -- valueflag_cd
null, -- quantity_num
null, -- units_cd
fhir_data.effectiveDateTime, -- end_date
'', -- location_cd
FHIR_id, -- observation_blob
0, -- confidence_num
now(), -- update_date
now(), -- download_date
now(), -- import_date
FHIR_sourcesystem, -- sourcesystem_cd
1, -- upload_id
nextval('instance_num_seq'), -- text_search_index
medication_reference -- fhir_reference
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.*) new_as_table
left join i2b2miracum.fhir_medication
on new_as_table.fhir->'medicationReference'->>'reference' = 'Medication/' || fhir_medication.id
) fhir_data;
end if;
--
return null; -- Return nothing. This prevents that the FHIR data is actually written into the "FHIR" table.
......@@ -399,4 +460,10 @@ INSERT INTO i2b2miracum.fhir (fhir) VALUES ('{ "resourceType": "Procedure", "id"
-- Test mit fehlenden Daten:
INSERT INTO i2b2miracum.fhir (fhir) VALUES ('{ "resourceType": "Observation", "id": "Observation1", "meta": { "source": "Labor1.json", "profile": [ "https://www.medizininformatik-initiative.de/fhir/core/modul-labor/StructureDefinition/ObservationLab" ] }, "identifier": [ { "type": { "coding": [ { "system": "http://terminology.hl7.org/CodeSystem/v2-0203", "code": "OBI" } ] }, "system": "https://fhir.diz.uk-erlangen.de/NamingSystem/swisslab/labObservationId", "value": "123123123123-123123123123", "assigner": { "identifier": { "system": "https://www.medizininformatik-initiative.de/fhir/core/NamingSystem/org-identifier", "value": "UKER" } } } ], "status": "final", "category": [ { "coding": [ { "system": "http://loinc.org", "code": "26436-6" }, { "system": "http://terminology.hl7.org/CodeSystem/observation-category", "code": "laboratory" } ] } ], "code": { "coding": [ { "system": "http://loinc.org", "code": "41653-7" } ] }, "subject": { "identifier": { "type": { "coding": [ { "system": "http://terminology.hl7.org/CodeSystem/v2-0203", "code": "MR" } ] }, "system": "https://fhir.diz.uk-erlangen.de/NamingSystem/patientId" } }, "encounter": { "reference": "Encounter/Encounter1", "identifier": { "type": { "coding": [ { "system": "http://terminology.hl7.org/CodeSystem/v2-0203", "code": "VN" } ] }, "system": "https://fhir.diz.uk-erlangen.de/NamingSystem/encounterId", "value": "Encounter1" } }, "effectiveDateTime": "2014-12-22T16:23:00+00:00", "issued": "2014-12-22T16:23:00+00:00", "valueQuantity": { "value": 134, "unit": "mg/dL", "system": "http://unitsofmeasure.org", "code": "mg/dL" } }');
*/
\ No newline at end of file
*/
INSERT INTO i2b2miracum.fhir (fhir) VALUES ('{"id": "MedicationStatement1", "meta": {"profile": ["https://www.medizininformatik-initiative.de/fhir/core/StructureDefinition/MedicationStatement"], "security": [{"code": "PSEUDED", "system": "http://terminology.hl7.org/CodeSystem/v3-ObservationValue", "display": "part of the resource is pseudonymized"}]}, "dosage": [{"text": "Cisplatin (50 mg/m²", "doseAndRate": [{"doseQuantity": {"unit": "mg/m²", "value": 50.0}}]}], "status": "unknown", "context": {"type": "Encounter", "reference": "Encounter/XXXXXXXXXXXXX", "identifier": {"type": {"text": "Visit number", "coding": [{"code": "VN", "system": "http://terminology.hl7.org/CodeSystem/v2-0203"}]}, "value": "Encounter4", "system": "https://fhir.diz.uk-erlangen.de/NamingSystem/encounterId"}}, "subject": {"type": "Patient", "reference": "Patient/XXXXXXXXXXXXXXX", "identifier": {"type": {"text": "Medical record number", "coding": [{"code": "MR", "system": "http://terminology.hl7.org/CodeSystem/v2-0203"}]}, "value": "Patient4", "system": "https://fhir.diz.uk-erlangen.de/NamingSystem/patientId"}}, "identifier": [{"value": "Cisplatin_50.0mg/m²_208004", "system": "https://averbis.com/de.averbis.types.health.Medication"}], "dateAsserted": "2008-07-29T17:44:00+00:00", "resourceType": "MedicationStatement", "effectiveDateTime": "2008-07-29T17:44:00+00:00", "medicationReference": {"type": "Medication", "reference": "Medication/Medication1", "identifier": {"value": "Cisplatin_50.0mg/m²", "system": "https://averbis.com/de.averbis.types.health.Medication"}}}');
INSERT INTO i2b2miracum.fhir (fhir) VALUES ('{"id": "Medication1", "code": {"coding": [{"code": "N06AX16", "system": "http://fhir.de/CodeSystem/dimdi/atc", "display": "Venlafaxin"}]}, "meta": {"profile": ["https://www.medizininformatik-initiative.de/fhir/core/StructureDefinition/Medication"]}, "identifier": [{"value": "Venlafaxin_25.0mg", "system": "https://averbis.com/de.averbis.types.health.Medication"}], "ingredient": [{"itemCodeableConcept": {"coding": [{"system": "http://fhir.de/CodeSystem/dimdi/atc", "display": "Venlafaxin"}]}}], "resourceType": "Medication"}');
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