@@ -47,7 +47,68 @@ LEFT JOIN d_labitems USING (itemid)
47
47
LEFT JOIN omop_loinc USING (loinc_code)
48
48
LEFT JOIN omop_operator USING (operator_name)
49
49
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
+ )
51
112
INSERT INTO omop .measurement
52
113
SELECT
53
114
row_to_insert .measurement_id
79
140
, chartevents .mimic_id as measurement_id
80
141
, subject_id
81
142
, 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
83
145
, value as value_source_value
84
146
, substring (value,' ^(<=|>=|<|>)' ) as operator_name
85
147
, CASE WHEN trim (value) ~ ' ^(>=|<=|>|<){0,1}[+-]*([.,]{1}[0-9]+|[0-9]+[,.]{0,1}[0-9]*)$'
137
199
, d_items .mimic_id AS measurement_source_concept_id
138
200
, gcpt_lab_unit_to_concept .unit_source_value
139
201
, chartevents_lab .value_source_value
202
+ , specimen_datetime
140
203
FROM chartevents_lab
141
204
LEFT JOIN patients USING (subject_id)
142
205
LEFT JOIN admissions USING (hadm_id)
@@ -145,7 +208,68 @@ LEFT JOIN omop_loinc USING (label)
145
208
LEFT JOIN omop_operator USING (operator_name)
146
209
LEFT JOIN gcpt_lab_label_to_concept USING (label)
147
210
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
+ )
149
273
INSERT INTO omop .measurement
150
274
SELECT
151
275
row_to_insert .measurement_id
@@ -219,7 +343,7 @@ SELECT
219
343
nextval(' mimic_id_seq' ) as specimen_id -- non NULL
220
344
, patients .person_id -- non NULL
221
345
, 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
223
347
, culture .measurement_date as specimen_date -- this is not really the specimen date but better than nothing
224
348
, culture .measurement_datetime as specimen_datetime
225
349
, null ::double precision as quantity
0 commit comments