Skip to content

Commit a09c473

Browse files
committed
Add specimen for all lab kind of data
This generates a fictive specimen for every laboratory data. This allows to save the chartevent storetime/charttime, that define both result/specimen prelevement datetimes This is related to #23
1 parent d405439 commit a09c473

File tree

2 files changed

+131
-52
lines changed

2 files changed

+131
-52
lines changed

Diff for: etl/StandardizedClinicalDataTables/MEASUREMENT/etl.sql

+128-4
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,68 @@ LEFT JOIN d_labitems USING (itemid)
4747
LEFT JOIN omop_loinc USING (loinc_code)
4848
LEFT JOIN omop_operator USING (operator_name)
4949
LEFT JOIN gcpt_lab_label_to_concept USING (measurement_source_value)
50-
LEFT JOIN gcpt_lab_unit_to_concept USING (unit_source_value))
50+
LEFT JOIN gcpt_lab_unit_to_concept USING (unit_source_value)
51+
),
52+
"specimen_lab" AS ( --generated specimen: each lab measurement is associated with a fictive specimen
53+
SELECT
54+
nextval('mimic_id_seq') as specimen_id -- non NULL
55+
, person_id -- non NULL
56+
, 0::integer as specimen_concept_id -- non NULL
57+
, 581378 as specimen_type_concept_id -- non NULL
58+
, measurement_date as specimen_date
59+
, measurement_datetime as specimen_datetime
60+
, null::double precision as quantity
61+
, null::integer unit_concept_id
62+
, null::integer anatomic_site_concept_id
63+
, null::integer disease_status_concept_id
64+
, null::integer specimen_source_id
65+
, null::text specimen_source_value
66+
, null::text unit_source_value
67+
, null::text anatomic_site_source_value
68+
, null::text disease_status_source_value
69+
, row_to_insert.measurement_id -- usefull for fact_relationship
70+
FROM row_to_insert
71+
),
72+
"insert_specimen_lab" AS (
73+
INSERT INTO omop.specimen
74+
SELECT
75+
specimen_id -- non NULL
76+
, person_id -- non NULL
77+
, specimen_concept_id -- non NULL
78+
, specimen_type_concept_id -- non NULL
79+
, specimen_date
80+
, specimen_datetime
81+
, quantity
82+
, unit_concept_id
83+
, anatomic_site_concept_id
84+
, disease_status_concept_id
85+
, specimen_source_id
86+
, specimen_source_value
87+
, unit_source_value
88+
, anatomic_site_source_value
89+
, disease_status_source_value
90+
FROM specimen_lab
91+
RETURNING *
92+
),
93+
"insert_fact_relationship_specimen_measurement" AS (
94+
INSERT INTO omop.fact_relationship
95+
(SELECT
96+
36 AS domain_concept_id_1 -- Specimen
97+
, specimen_id as fact_id_1
98+
, 21 AS domain_concept_id_2 -- Measurement
99+
, measurement_id as fact_id_2
100+
, 44818854 as relationship_concept_id -- Specimen of (SNOMED)
101+
FROM specimen_lab
102+
UNION ALL
103+
SELECT
104+
21 AS domain_concept_id_1 -- Measurement
105+
, measurement_id as fact_id_1
106+
, 36 AS domain_concept_id_2 -- Specimen
107+
, specimen_id as fact_id_2
108+
, 44818756 as relationship_concept_id -- Has specimen (SNOMED)
109+
FROM specimen_lab
110+
)
111+
)
51112
INSERT INTO omop.measurement
52113
SELECT
53114
row_to_insert.measurement_id
@@ -79,7 +140,8 @@ WITH
79140
, chartevents.mimic_id as measurement_id
80141
, subject_id
81142
, hadm_id
82-
, charttime as measurement_datetime
143+
, storetime as measurement_datetime --according to Alistair, storetime is the result time
144+
, charttime as specimen_datetime -- according to Alistair, charttime is the specimen time
83145
, value as value_source_value
84146
, substring(value,'^(<=|>=|<|>)') as operator_name
85147
, CASE WHEN trim(value) ~ '^(>=|<=|>|<){0,1}[+-]*([.,]{1}[0-9]+|[0-9]+[,.]{0,1}[0-9]*)$'
@@ -137,6 +199,7 @@ WITH
137199
, d_items.mimic_id AS measurement_source_concept_id
138200
, gcpt_lab_unit_to_concept.unit_source_value
139201
, chartevents_lab.value_source_value
202+
, specimen_datetime
140203
FROM chartevents_lab
141204
LEFT JOIN patients USING (subject_id)
142205
LEFT JOIN admissions USING (hadm_id)
@@ -145,7 +208,68 @@ LEFT JOIN omop_loinc USING (label)
145208
LEFT JOIN omop_operator USING (operator_name)
146209
LEFT JOIN gcpt_lab_label_to_concept USING (label)
147210
LEFT JOIN gcpt_labs_from_chartevents_to_concept USING (category, label)
148-
LEFT JOIN gcpt_lab_unit_to_concept USING (unit_source_value))
211+
LEFT JOIN gcpt_lab_unit_to_concept USING (unit_source_value)
212+
),
213+
"specimen_lab" AS ( --generated specimen: each lab measurement is associated with a fictive specimen
214+
SELECT
215+
nextval('mimic_id_seq') as specimen_id -- non NULL
216+
, person_id -- non NULL
217+
, 0::integer as specimen_concept_id -- non NULL
218+
, 581378 as specimen_type_concept_id -- non NULL
219+
, specimen_datetime::date as specimen_date
220+
, specimen_datetime as specimen_datetime
221+
, null::double precision as quantity
222+
, null::integer unit_concept_id
223+
, null::integer anatomic_site_concept_id
224+
, null::integer disease_status_concept_id
225+
, null::integer specimen_source_id
226+
, null::text specimen_source_value
227+
, null::text unit_source_value
228+
, null::text anatomic_site_source_value
229+
, null::text disease_status_source_value
230+
, row_to_insert.measurement_id -- usefull for fact_relationship
231+
FROM row_to_insert
232+
),
233+
"insert_specimen_lab" AS (
234+
INSERT INTO omop.specimen
235+
SELECT
236+
specimen_id -- non NULL
237+
, person_id -- non NULL
238+
, specimen_concept_id -- non NULL
239+
, specimen_type_concept_id -- non NULL
240+
, specimen_date
241+
, specimen_datetime
242+
, quantity
243+
, unit_concept_id
244+
, anatomic_site_concept_id
245+
, disease_status_concept_id
246+
, specimen_source_id
247+
, specimen_source_value
248+
, unit_source_value
249+
, anatomic_site_source_value
250+
, disease_status_source_value
251+
FROM specimen_lab
252+
RETURNING *
253+
),
254+
"insert_fact_relationship_specimen_measurement" AS (
255+
INSERT INTO omop.fact_relationship
256+
(SELECT
257+
36 AS domain_concept_id_1 -- Specimen
258+
, specimen_id as fact_id_1
259+
, 21 AS domain_concept_id_2 -- Measurement
260+
, measurement_id as fact_id_2
261+
, 44818854 as relationship_concept_id -- Specimen of (SNOMED)
262+
FROM specimen_lab
263+
UNION ALL
264+
SELECT
265+
21 AS domain_concept_id_1 -- Measurement
266+
, measurement_id as fact_id_1
267+
, 36 AS domain_concept_id_2 -- Specimen
268+
, specimen_id as fact_id_2
269+
, 44818756 as relationship_concept_id -- Has specimen (SNOMED)
270+
FROM specimen_lab
271+
)
272+
)
149273
INSERT INTO omop.measurement
150274
SELECT
151275
row_to_insert.measurement_id
@@ -219,7 +343,7 @@ SELECT
219343
nextval('mimic_id_seq') as specimen_id -- non NULL
220344
, patients.person_id -- non NULL
221345
, 0::integer as specimen_concept_id -- non NULL
222-
, 0::integer as specimen_type_concept_id -- non NULL
346+
, 581378 as specimen_type_concept_id -- non NULL
223347
, culture.measurement_date as specimen_date -- this is not really the specimen date but better than nothing
224348
, culture.measurement_datetime as specimen_datetime
225349
, null::double precision as quantity

Diff for: etl/StandardizedClinicalDataTables/SPECIMEN/etl.sql

+3-48
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,9 @@
11
-- generated id from microbiologyevent (date is the chartdate by default)
2-
3-
specimen_id -- non NULL
4-
, person_id -- non NULL
5-
, specimen_concept_id -- non NULL
6-
, specimen_type_concept_id -- non NULL
7-
, specimen_date -- non NULL
8-
, specimen_datetime
9-
, quantity
10-
, unit_concept_id
11-
, anatomic_site_concept_id
12-
, disease_status_concept_id
13-
, specimen_source_id
14-
, specimen_source_value
15-
, unit_source_value
16-
, anatomic_site_source_value
17-
, disease_status_source_value
2+
-- CF MEASUREMENT etl
183

194
-- generated id from chartevents lab (storedtime)
20-
21-
specimen_id -- non NULL
22-
, person_id -- non NULL
23-
, specimen_concept_id -- non NULL
24-
, specimen_type_concept_id -- non NULL
25-
, specimen_date -- non NULL
26-
, specimen_datetime
27-
, quantity
28-
, unit_concept_id
29-
, anatomic_site_concept_id
30-
, disease_status_concept_id
31-
, specimen_source_id
32-
, specimen_source_value
33-
, unit_source_value
34-
, anatomic_site_source_value
35-
, disease_status_source_value
36-
5+
-- CF MEASUREMENT etl
376

387
-- generated id from labevents (date is the chartdate by default)
8+
-- CF MEASUREMENT etl
399

40-
specimen_id -- non NULL
41-
, person_id -- non NULL
42-
, specimen_concept_id -- non NULL
43-
, specimen_type_concept_id -- non NULL
44-
, specimen_date -- non NULL
45-
, specimen_datetime
46-
, quantity
47-
, unit_concept_id
48-
, anatomic_site_concept_id
49-
, disease_status_concept_id
50-
, specimen_source_id
51-
, specimen_source_value
52-
, unit_source_value
53-
, anatomic_site_source_value
54-
, disease_status_source_value

0 commit comments

Comments
 (0)