-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsofa.sql
367 lines (355 loc) · 12.2 KB
/
sofa.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
-- This Code has been copied from @briangow (https://github.com/MIT-LCP/mimic-code/blob/main/mimic-iv/concepts/score/sofa.sql)
-- EDITED by Jan Mika Dietz (Technical University of Berlin)
-- Changes: SELECT just the columns stay_id, hr, starttime, endtime, respiration_24hours, coagulation_24hours, liver_24hours, cardiovascular_24hours, cns_24hours, renal_24hours, sofa_24hours
-- WHERE hr > 0
-- ------------------------------------------------------------------
-- Title: Sequential Organ Failure Assessment (SOFA)
-- This query extracts the sequential organ failure assessment (formally: sepsis-related organ failure assessment).
-- This score is a measure of organ failure for patients in the ICU.
-- The score is calculated for **every hour** of the patient's ICU stay.
-- However, as the calculation window is 24 hours, care should be taken when
-- using the score before the end of the first day, as the data window is limited.
-- ------------------------------------------------------------------
-- Reference for SOFA:
-- Jean-Louis Vincent, Rui Moreno, Jukka Takala, Sheila Willatts, Arnaldo De Mendonça,
-- Hajo Bruining, C. K. Reinhart, Peter M Suter, and L. G. Thijs.
-- "The SOFA (Sepsis-related Organ Failure Assessment) score to describe organ dysfunction/failure."
-- Intensive care medicine 22, no. 7 (1996): 707-710.
-- Variables used in SOFA:
-- GCS, MAP, FiO2, Ventilation status (sourced FROM `physionet-data.mimic_icu.chartevents`)
-- Creatinine, Bilirubin, FiO2, PaO2, Platelets (sourced FROM `physionet-data.mimic_icu.labevents`)
-- Dopamine, Dobutamine, Epinephrine, Norepinephrine (sourced FROM `physionet-data.mimic_icu.inputevents_mv` and INPUTEVENTS_CV)
-- Urine output (sourced from OUTPUTEVENTS)
-- generate a row for every hour the patient was in the ICU
-- here, we generate a starttime/endtime for every hour of the patient's ICU stay
-- all of our joins to data will use these times to extract data pertinent to only that hour
WITH co AS
(
select ih.stay_id, ie.hadm_id
, hr
-- start/endtime can be used to filter to values within this hour
, DATETIME_SUB(ih.endtime, INTERVAL '1' HOUR) AS starttime
, ih.endtime
from `physionet-data.mimic_derived.icustay_hourly` ih
INNER JOIN `physionet-data.mimic_icu.icustays` ie
ON ih.stay_id = ie.stay_id
)
, pafi as
(
-- join blood gas to ventilation durations to determine if patient was vent
select ie.stay_id
, bg.charttime
-- because pafi has an interaction between vent/PaO2:FiO2, we need two columns for the score
-- it can happen that the lowest unventilated PaO2/FiO2 is 68, but the lowest ventilated PaO2/FiO2 is 120
-- in this case, the SOFA score is 3, *not* 4.
, case when vd.stay_id is null then pao2fio2ratio else null end pao2fio2ratio_novent
, case when vd.stay_id is not null then pao2fio2ratio else null end pao2fio2ratio_vent
FROM `physionet-data.mimic_icu.icustays` ie
inner join `physionet-data.mimic_derived.bg` bg
on ie.subject_id = bg.subject_id
left join `physionet-data.mimic_derived.ventilation` vd
on ie.stay_id = vd.stay_id
and bg.charttime >= vd.starttime
and bg.charttime <= vd.endtime
and vd.ventilation_status = 'InvasiveVent'
WHERE specimen_pred = 'ART.'
)
, vs AS
(
select co.stay_id, co.hr
-- vitals
, min(vs.mbp) as meanbp_min
from co
left join `physionet-data.mimic_derived.vitalsign` vs
on co.stay_id = vs.stay_id
and co.starttime < vs.charttime
and co.endtime >= vs.charttime
group by co.stay_id, co.hr
)
, gcs AS
(
select co.stay_id, co.hr
-- gcs
, min(gcs.gcs) as gcs_min
from co
left join `physionet-data.mimic_derived.gcs` gcs
on co.stay_id = gcs.stay_id
and co.starttime < gcs.charttime
and co.endtime >= gcs.charttime
group by co.stay_id, co.hr
)
, bili AS
(
select co.stay_id, co.hr
, max(enz.bilirubin_total) as bilirubin_max
from co
left join `physionet-data.mimic_derived.enzyme` enz
on co.hadm_id = enz.hadm_id
and co.starttime < enz.charttime
and co.endtime >= enz.charttime
group by co.stay_id, co.hr
)
, cr AS
(
select co.stay_id, co.hr
, max(chem.creatinine) as creatinine_max
from co
left join `physionet-data.mimic_derived.chemistry` chem
on co.hadm_id = chem.hadm_id
and co.starttime < chem.charttime
and co.endtime >= chem.charttime
group by co.stay_id, co.hr
)
, plt AS
(
select co.stay_id, co.hr
, min(cbc.platelet) as platelet_min
from co
left join `physionet-data.mimic_derived.complete_blood_count` cbc
on co.hadm_id = cbc.hadm_id
and co.starttime < cbc.charttime
and co.endtime >= cbc.charttime
group by co.stay_id, co.hr
)
, pf AS
(
select co.stay_id, co.hr
, min(pafi.pao2fio2ratio_novent) AS pao2fio2ratio_novent
, min(pafi.pao2fio2ratio_vent) AS pao2fio2ratio_vent
from co
-- bring in blood gases that occurred during this hour
left join pafi
on co.stay_id = pafi.stay_id
and co.starttime < pafi.charttime
and co.endtime >= pafi.charttime
group by co.stay_id, co.hr
)
-- sum uo separately to prevent duplicating values
, uo as
(
select co.stay_id, co.hr
-- uo
, MAX(
CASE WHEN uo.uo_tm_24hr >= 22 AND uo.uo_tm_24hr <= 30
THEN uo.urineoutput_24hr / uo.uo_tm_24hr * 24
END) as uo_24hr
from co
left join `physionet-data.mimic_derived.urine_output_rate` uo
on co.stay_id = uo.stay_id
and co.starttime < uo.charttime
and co.endtime >= uo.charttime
group by co.stay_id, co.hr
)
-- collapse vasopressors into 1 row per hour
-- also ensures only 1 row per chart time
, vaso AS
(
SELECT
co.stay_id
, co.hr
, MAX(epi.vaso_rate) as rate_epinephrine
, MAX(nor.vaso_rate) as rate_norepinephrine
, MAX(dop.vaso_rate) as rate_dopamine
, MAX(dob.vaso_rate) as rate_dobutamine
FROM co
LEFT JOIN `physionet-data.mimic_derived.epinephrine` epi
on co.stay_id = epi.stay_id
and co.endtime > epi.starttime
and co.endtime <= epi.endtime
LEFT JOIN `physionet-data.mimic_derived.norepinephrine` nor
on co.stay_id = nor.stay_id
and co.endtime > nor.starttime
and co.endtime <= nor.endtime
LEFT JOIN `physionet-data.mimic_derived.dopamine` dop
on co.stay_id = dop.stay_id
and co.endtime > dop.starttime
and co.endtime <= dop.endtime
LEFT JOIN `physionet-data.mimic_derived.dobutamine` dob
on co.stay_id = dob.stay_id
and co.endtime > dob.starttime
and co.endtime <= dob.endtime
WHERE epi.stay_id IS NOT NULL
OR nor.stay_id IS NOT NULL
OR dop.stay_id IS NOT NULL
OR dob.stay_id IS NOT NULL
GROUP BY co.stay_id, co.hr
)
, scorecomp as
(
select
co.stay_id
, co.hr
, co.starttime, co.endtime
, pf.pao2fio2ratio_novent
, pf.pao2fio2ratio_vent
, vaso.rate_epinephrine
, vaso.rate_norepinephrine
, vaso.rate_dopamine
, vaso.rate_dobutamine
, vs.meanbp_min
, gcs.gcs_min
-- uo
, uo.uo_24hr
-- labs
, bili.bilirubin_max
, cr.creatinine_max
, plt.platelet_min
from co
left join vs
on co.stay_id = vs.stay_id
and co.hr = vs.hr
left join gcs
on co.stay_id = gcs.stay_id
and co.hr = gcs.hr
left join bili
on co.stay_id = bili.stay_id
and co.hr = bili.hr
left join cr
on co.stay_id = cr.stay_id
and co.hr = cr.hr
left join plt
on co.stay_id = plt.stay_id
and co.hr = plt.hr
left join pf
on co.stay_id = pf.stay_id
and co.hr = pf.hr
left join uo
on co.stay_id = uo.stay_id
and co.hr = uo.hr
left join vaso
on co.stay_id = vaso.stay_id
and co.hr = vaso.hr
)
, scorecalc as
(
-- Calculate the final score
-- note that if the underlying data is missing, the component is null
-- eventually these are treated as 0 (normal), but knowing when data is missing is useful for debugging
select scorecomp.*
-- Respiration
, case
when pao2fio2ratio_vent < 100 then 4
when pao2fio2ratio_vent < 200 then 3
when pao2fio2ratio_novent < 300 then 2
when pao2fio2ratio_vent < 300 then 2
when pao2fio2ratio_novent < 400 then 1
when pao2fio2ratio_vent < 400 then 1
when coalesce(pao2fio2ratio_vent, pao2fio2ratio_novent) is null then null
else 0
end as respiration
-- Coagulation
, case
when platelet_min < 20 then 4
when platelet_min < 50 then 3
when platelet_min < 100 then 2
when platelet_min < 150 then 1
when platelet_min is null then null
else 0
end as coagulation
-- Liver
, case
-- Bilirubin checks in mg/dL
when bilirubin_max >= 12.0 then 4
when bilirubin_max >= 6.0 then 3
when bilirubin_max >= 2.0 then 2
when bilirubin_max >= 1.2 then 1
when bilirubin_max is null then null
else 0
end as liver
-- Cardiovascular
, case
when rate_dopamine > 15 or rate_epinephrine > 0.1 or rate_norepinephrine > 0.1 then 4
when rate_dopamine > 5 or rate_epinephrine <= 0.1 or rate_norepinephrine <= 0.1 then 3
when rate_dopamine > 0 or rate_dobutamine > 0 then 2
when meanbp_min < 70 then 1
when coalesce(meanbp_min, rate_dopamine, rate_dobutamine, rate_epinephrine, rate_norepinephrine) is null then null
else 0
end as cardiovascular
-- Neurological failure (GCS)
, case
when (gcs_min >= 13 and gcs_min <= 14) then 1
when (gcs_min >= 10 and gcs_min <= 12) then 2
when (gcs_min >= 6 and gcs_min <= 9) then 3
when gcs_min < 6 then 4
when gcs_min is null then null
else 0
end as cns
-- Renal failure - high creatinine or low urine output
, case
when (creatinine_max >= 5.0) then 4
when uo_24hr < 200 then 4
when (creatinine_max >= 3.5 and creatinine_max < 5.0) then 3
when uo_24hr < 500 then 3
when (creatinine_max >= 2.0 and creatinine_max < 3.5) then 2
when (creatinine_max >= 1.2 and creatinine_max < 2.0) then 1
when coalesce (uo_24hr, creatinine_max) is null then null
else 0
end as renal
from scorecomp
)
, score_final as
(
select s.*
-- Combine all the scores to get SOFA
-- Impute 0 if the score is missing
-- the window function takes the max over the last 24 hours
, coalesce(
MAX(respiration) OVER (PARTITION BY stay_id ORDER BY HR
ROWS BETWEEN 23 PRECEDING AND 0 FOLLOWING)
,0) as respiration_24hours
, coalesce(
MAX(coagulation) OVER (PARTITION BY stay_id ORDER BY HR
ROWS BETWEEN 23 PRECEDING AND 0 FOLLOWING)
,0) as coagulation_24hours
, coalesce(
MAX(liver) OVER (PARTITION BY stay_id ORDER BY HR
ROWS BETWEEN 23 PRECEDING AND 0 FOLLOWING)
,0) as liver_24hours
, coalesce(
MAX(cardiovascular) OVER (PARTITION BY stay_id ORDER BY HR
ROWS BETWEEN 23 PRECEDING AND 0 FOLLOWING)
,0) as cardiovascular_24hours
, coalesce(
MAX(cns) OVER (PARTITION BY stay_id ORDER BY HR
ROWS BETWEEN 23 PRECEDING AND 0 FOLLOWING)
,0) as cns_24hours
, coalesce(
MAX(renal) OVER (PARTITION BY stay_id ORDER BY HR
ROWS BETWEEN 23 PRECEDING AND 0 FOLLOWING)
,0) as renal_24hours
-- sum together data for final SOFA
, coalesce(
MAX(respiration) OVER (PARTITION BY stay_id ORDER BY HR
ROWS BETWEEN 23 PRECEDING AND 0 FOLLOWING)
,0)
+ coalesce(
MAX(coagulation) OVER (PARTITION BY stay_id ORDER BY HR
ROWS BETWEEN 23 PRECEDING AND 0 FOLLOWING)
,0)
+ coalesce(
MAX(liver) OVER (PARTITION BY stay_id ORDER BY HR
ROWS BETWEEN 23 PRECEDING AND 0 FOLLOWING)
,0)
+ coalesce(
MAX(cardiovascular) OVER (PARTITION BY stay_id ORDER BY HR
ROWS BETWEEN 23 PRECEDING AND 0 FOLLOWING)
,0)
+ coalesce(
MAX(cns) OVER (PARTITION BY stay_id ORDER BY HR
ROWS BETWEEN 23 PRECEDING AND 0 FOLLOWING)
,0)
+ coalesce(
MAX(renal) OVER (PARTITION BY stay_id ORDER BY HR
ROWS BETWEEN 23 PRECEDING AND 0 FOLLOWING)
,0)
as sofa_24hours
from scorecalc s
WINDOW W as
(
PARTITION BY stay_id
ORDER BY hr
ROWS BETWEEN 23 PRECEDING AND 0 FOLLOWING
)
)
select stay_id, hr, starttime, endtime, respiration_24hours, coagulation_24hours, liver_24hours, cardiovascular_24hours, cns_24hours, renal_24hours, sofa_24hours from score_final
where hr >= 0