-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcohort_extraction_for trajetories.sql
3632 lines (3631 loc) · 124 KB
/
cohort_extraction_for trajetories.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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{\rtf1\ansi\ansicpg1252\cocoartf2820
\cocoatextscaling0\cocoaplatform0{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
{\colortbl;\red255\green255\blue255;}
{\*\expandedcolortbl;;}
\paperw11900\paperh16840\margl1440\margr1440\vieww11520\viewh8400\viewkind0
\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural\partightenfactor0
\f0\fs24 \cf0 # This isn the Final script (V26) for events extraction for clustering trajetories.\
# This extraction only involved in ***guideline-based Knowledge-based Abstraction***.\
\
# April 2024 - events only using guidelines - USA / Australian / UpToDate\
\
# Included events only after first heparin dose.\
# group icd diagnosed codes and procedure codes when possible.\
# start with raw events\
# can do grouping later.\
\
\
# to do\
-- check whether all events covered from previous script\
-- check whether all diagnosis were coverd (excel sheet)\
-- check whether both icd-9 and 10 codes were defined?\
-- merge possible records\
\
--1. *** labs *** \
\
----------------------------------------------------------------------------------\
--*** 1.1 platelets-related measurements ***\
\
--*** 1.1.1 labevents (hosp) - itemid - 51265 - Platelet Count ***\
--*** 1.1.2 labevents (hosp) - itemid - 51266 - Platelet Smear ***\
--*** 1.1.3 labevents (hosp) - itemid - 51704 - Platelet_Count_chemistry ***\
--*** 1.1.4 labevents (hosp) - itemid - 52105 - Direct Antiplatelet Antibodies ***\
--*** 1.1.5 labevents (hosp) - itemid - 51264 - Platelet Clumps ***\
\
\
--*** 1.1.8 chartevents (ICU) - itemid - 220507 - Activated Clotting Time ***\
\
----------------------------------------------------------------------------------\
\
--*** 1.2 HIT may cause activation of monocytes, neutrophils, and endothelial cells ***\
\
--*** 1.2.1 labevents (hosp) - monocytes - (itemid, label) = (52074 , Absolute Monocyte Count) , (51253 , Monocyte Count) , (51254 , Monocytes) ***\
--*** 1.2.2 labevents (hosp) - monocytes - (itemid, label) = (51537 , Absolute Neutrophil) , (51697 , Neutrophils) , (52075, Absolute Neutrophil Count) , (51256, Neutrophil) ***\
--*** 1.2.3 labevents (hosp) - endothelial cells - No relevant itemid s found.\
--*** 1.2.4 chartevents (icu) - monocytes / Neutrophils / endothelial cells (No relevant itemid s found) - (itemid, label) = (229359,Absolute Count - Monos) , (225642,Differential-Monos), (229355,Absolute Neutrophil Count) ***\
\
----------------------------------------------------------------------------------\
\
--*** 1.3 HIT may cause tests - thrombin / partial thromboplastin time / prothrombin time ***\
\
--*** 1.3.1 labevents (hosp) - thrombin / partial thromboplastin time / prothrombin time - (itemid, label) which only had records in mimic = (51140 , Antithrombin) , (51297 , Thrombin) ***\
\
-- below itemid s didn't have records in mimic.chartevents, however they too were considered - (52078 , Antithrombin III) , (52948 , Thrombin) , (52187 , Thrombin Time) , (52188 , Thrombin Time Control) , (52189 , Thrombin Time Control) , (52190 , Tissue Thromboplastin Inhibitor)\
\
--*** 1.3.2 chartevents (icu) - thrombin / partial thromboplastin time / prothrombin time - (itemid, label) = (227465 , Prothrombin time) , (227469 - Thrombin) , (229374 , AT (Antithrombin funct)) , (229372 - AT ( Antithrombin funct))\
\
----------------------------------------------------------------------------------\
\
--*** 1.4 HIT antibody tests ***\
\
--*** 1.4.1 labevents (hosp) - HIT antibody tests - (itemid, label) = (52132 , HIT-Ab Numerical Result) \
--*** 1.4.2 labevents (hosp) - HIT antibody tests - (itemid, label) = (52131 , HIT-Ab Interpreted Result)\
\
----------------------------------------------------------------------------------\
\
--*** 1.5 Immunoglobulin G antibodies / IgG antibodies ***\
\
--*** 1.5.1 labevents (hosp) - (itemid, label) = (50950 , Immunoglobulin G)\
\
----------------------------------------------------------------------------------\
\
--2. *** Medication / Inputevents *** \
\
----------------------------------------------------------------------------------\
\
--*** 2.1 non-heparin anticoagulants ***\
\
--*** 2.1.1 emar (hosp) - First instance of direct oral anticoagulants - rivaroxaban. apixaban, dabigatran, edoxaban\
\
--*** 2.1.2 emar (hosp) - parenteral anticoagulants (intravenous) - argatroban, bivalirudin, danaparoid, fondaparinux, lepirudin\
\
--*** 2.1.3 inputevents (icu) - parenteral anticoagulants (intravenous) - argatroban, bivalirudin, danaparoid, fondaparinux, lepirudin\
\
----------------------------------------------------------------------------------\
\
--2.2. *** dextran 70 *** \
\
--*** 2.2.1 emar (hosp) - (mediactions) = dextran 70-hypromellose (PF) , dextran 70-hypromellose\
\
--*** 2.2.2 inputevents (icu) -(itemid,label) = (225796 , Dextran 70) , (221002 , Dextran 70 / Dextrose 5% (Gentran - Macrodex - RescueFlow)) , (221003 , Dextran 70 / Saline 0,9% (Gentran - Macrodex - RescueFlow))\
\
----------------------------------------------------------------------------------\
\
--2.3. *** Warfarin *** \
\
--*** 2.3.1 emar (hosp) - Warfarin\
--*** 2.3.2 inputevents (icu) -(itemid,label) = (225913 , Coumadin (Warfarin)\
\
----------------------------------------------------------------------------------\
\
--2.4. *** Vitamin K *** \
\
-- In the context of the statement "warfarin should be reversed with vitamin K," the term "reverse" refers to the action of counteracting or neutralizing the anticoagulant effects of warfarin.\
-- Warfarin is an anticoagulant medication that works by inhibiting the synthesis of vitamin K-dependent clotting factors in the liver, thereby preventing blood clot formation. However, in certain situations such as bleeding complications or the need for urgent surgery, it may be necessary to rapidly restore the body's ability to form blood clots.\
-- By administering vitamin K, which is essential for the synthesis of clotting factors, the anticoagulant effects of warfarin can be reversed. Vitamin K helps replenish the depleted levels of clotting factors, allowing the blood to clot effectively again.\
-- So, in this context, "reverse" means to counteract or neutralize the anticoagulant effects of warfarin by restoring the synthesis of clotting factors through the administration of vitamin K.\
\
--*** 2.4.1 emar (hosp) - (medication) = Phytonadione (Vitamin K1) (for L&D to NBN order set use only) , Phytonadione (Vitamin K1)\
\
--*** 2.4.2 inputevents (icu) -(itemid,label) = (227535 , Vitamin K (Phytonadione))\
\
----------------------------------------------------------------------------------\
\
--2.5. *** antiplatelet agents - For patients with HIT who require cardiovascular surgery - iloprost , prostacyclin analog , tirofiban , aspirin , dipyridamole *** \
\
-- antiplatelet agents - work by inhibiting platelet function and aggregation, thereby preventing the formation of blood clots. \
\
--*** 2.5.1 emar (hosp) - (medications) = Tirofiban , aspirin (multiple medications) , dipyridamole (multiple medications)\
\
--*** 2.5.2 inputevents (icu) - (itemid, label) = (225157 , Tirofiban (Aggrastat))\
\
----------------------------------------------------------------------------------\
\
--2.6. *** regional citrate *** \
\
-- why Citrate? In patients with subacute HIT A, sub- acute HIT B, or remote HIT who are receiving renal replacement therapy, are not otherwise receiving anticoagulation, and require anticoagulation to prevent thrombosis of the dialysis circuit, the ASH guideline panel suggests regional citrate rather than heparin or other non-heparin anticoagulants Citrate is not appropriate for patients with acute HIT, who require systemic rather than regional anticoagulation\
\
--*** 2.6.1 emar (hosp) - (medications) = citrate (multiple medications)\
\
--*** 2.6.2 inputevents (icu) - (itemid, label) = (228004 , Citrate (ACD-A)) , (227526 , Citrate) , (227528 , ACD-A Citrate (500ml)) , (227529 , ACD-A Citrate (1000ml)) , (225164 , Trisodium Citrate 0.4%) \
\
----------------------------------------------------------------------------------\
\
--2.7. *** antiplatelet agent - ticagrelor - making it appear that the test is negative when in fact HIT is present. One exception to the use of a functional assay is a patient receiving the anti-platelet drug ticagrelor. *** \
\
--*** 2.7.1 emar (hosp) - (medications) = ticagrelor (multiple medications)\
\
--*** 2.7.2 hosp (icd_procedures) - platelet inhibitors ( "platelet inhibitors" and "antiplatelet agents" generally refer to the same class of medications. )\
\
----------------------------------------------------------------------------------\
\
--*** 2.8 Intravenous Immunoglobulin G / IVIG ***\
\
--*** 2.8.1 emar (hosp) - (medications) = (Immune Globulin Intravenous (Human))\
\
--*** 2.8.2 inputevents (icu) - (itemid, label) = (227530 , IV Immune Globulin (IVIG))\
\
----------------------------------------------------------------------------------\
\
--*** 2.9 heparin dose\
\
--*** 2.9.1 Last heparin dose\
\
--*** 2.9.2 Last heparin dose\
\
----------------------------------------------------------------------------------\
\
--*** 2.10 protamine \
\
--*** 2.10.1 emar (hosp) - (medications) = Protamine Sulfate\
--*** 2.10.2 inputevents (icu) - Protamine Sulfate - (itemid, label) = (229068 , Protamine sulfate) \
\
----------------------------------------------------------------------------------\
\
--3. *** Procedures *** \
\
----------------------------------------------------------------------------------\
\
--*** 3.1.platelet tranfusion\
\
--*** 3.1.1 .platelet tranfusion - mimic hosp - has few icd_codes\
-- no records returned for our Hep cohort\
\
--*** 3.1.2 .platelet tranfusion - inputevents (icu) - (itemid, label) = (225170 , Platelets) , (226369 , OR Platelet Intake) , (227071 , PACU Platelet Intake)\
\
----------------------------------------------------------------------------------\
\
--*** 3.2.Amputation\
\
--*** 3.2.1 mimic hosp - has few icd_codes\
-- no records returned - there were few records for the hep cohort where they had amputations, but the procedure_date was before first_hep_admin.\
\
--*** 3.2.2 mimic icu procedureevents - not relevant itemid s.\
\
----------------------------------------------------------------------------------\
\
--*** 3.3.ultrasonography\
\
--*** 3.3.1 hosp (icd_procedures) - ultrasonography of heart / veins / arteries (for icd codes -> https://www.icd10data.com/)\
\
--*** 3.3.2 ultrasound - icu.procedureevents \
\
----------------------------------------------------------------------------------\
\
--*** 3.4.Plasma exchange / Plasma transfusion\
\
--*** 3.4.1 hosp (icd_procedures) - Transfusion of plasma into veins / arteries / circulatory system \
\
--*** 3.4.2 hosp (icd_procedures) - Pheresis of Plasma / Plasma exchange (for icd codes -> https://www.icd10data.com/)\
\
--*** 3.4.3 inputevents (icu) - Pheresis of Plasma - (itemid , label) = (220970,Fresh Frozen Plasma) , (227532,Plasma Pheresis)\
\
--*** 3.4.4 procedureevents (icu) - Pheresis of Plasma - (itemid , label) = (227551 , Plasma Pheresis.) \
\
----------------------------------------------------------------------------------\
\
--*** 3.5.CT scan / Angiography\
\
--*** 3.5.1 hosp (icd_procedures) - CT scan - several icd codes used.\
-- no icd codes found for pulmonary angiography.\
\
--*** 3.5.2 procedureevents (icu) -CT scan - (itemid , label) = (221214 , CT scan) , (229582 , Portable CT scan)\
\
--*** 3.5.3 procedureevents (icu) -Angiography - (itemid , label) = (225427 , Angiography) \
\
----------------------------------------------------------------------------------\
\
--*** 3.6.X-ray / MRI\
\
--*** 3.6.1 hosp (icd_procedures) - X-ray - several icd codes used.\
\
--*** 3.6.2 hosp (icd_procedures) - MRI - several icd codes used.\
\
--*** 3.6.3 procedureevents (icu) -x-ray - (itemid , label) = (221216 , X-ray) , (225457,Abdominal X-Ray) , (225459,Chest X-Ray) , (229581,Portable Chest X-Ray)\
\
--*** 3.6.4 procedureevents (icu) -MRI - (itemid , label) = (223253 , Magnetic Resonance Imaging\
\
----------------------------------------------------------------------------------\
\
--*** 3.7 Skin biopsy\
\
--*** 3.7.1 hosp (icd_procedures) - X-ray - (icd_code, icd_version, long_title) = (8611, 9 , Closed biopsy of skin and subcutaneous tissue)\
-- couldn't find relevant ICD-10 codes.\
-- couldn't find relevant ITEMID s in icd.ditems.\
\
----------------------------------------------------------------------------------\
\
--*** 3.8 'Thrombo' related procedures\
\
--*** 3.8.1 hosp (icd_procedures) - (icd_code, icd_version, long_title) = (8611, 9 , Closed biopsy of skin and subcutaneous tissue)\
\
--*** 3.8.2 hosp (icd_procedures) - Infuse_Prothrombin_complex - (icd_code, icd_version, long_title) = (30280B1, 10 ,Transfusion of Nonautologous 4-Factor Prothrombin Complex Concentrate into Vein, Open Approach) , (30283B1, 10 ,Transfusion of Nonautologous 4-Factor Prothrombin Complex Concentrate into Vein, Percutaneous Approach) , (0096, 9 ,Infusion of 4-Factor Prothrombin Complex Concentrate)\
\
--*** 3.8.3 hosp (icd_procedures) - Infusion thrombolytic agent - (icd_code, icd_version, long_title) = (3604, 9 ,Intracoronary artery thrombolytic infusion) , (9910, 9 ,Injection or infusion of thrombolytic agent)\
\
--*** 3.8.4 no relevant procedures were found in 'icu.procedureevents'.\
\
----------------------------------------------------------------------------------\
\
--4. *** Diagnoses (ICD diagnose codes) *** \
\
--*** 4.1 mimic_hosp.d_icd_diagnoses - skin necrosis ***\
--*** 4.2 mimic_hosp.d_icd_diagnoses - limb_gangrene ***\
--*** 4.3 mimic_hosp.d_icd_diagnoses - bleeding ***\
--*** 4.4 mimic_hosp.d_icd_diagnoses - anaphylaxis ***\
--*** 4.5 mimic_hosp.d_icd_diagnoses - myocardial infarction ***\
--*** 4.6 mimic_hosp.d_icd_diagnoses - kideny infarction ***\
--*** 4.7 mimic_hosp.d_icd_diagnoses - spleen infarction ***\
--*** 4.8 mimic_hosp.d_icd_diagnoses - thrombotic events - Arterial embolism and thrombosis ***\
--*** 4.9 mimic_hosp.d_icd_diagnoses - thrombotic events - Other venous embolism and thrombosis ***\
--*** 4.10 mimic_hosp.d_icd_diagnoses - Pulmonary embolism ***\
\
----------------------------------------------------------------------------------\
\
--5. *** --5. *** Dermagraphics\
\
-- 5.1 Admission *** \
-- 5.1 Discharge *** \
-- 5.1 Died *** \
\
----------------------------------------------------------------------------------\
\
with all_union as \
(\
--1. *** labs *** \
\
--*** 1.1 platelets-related measurements ***\
\
--*** 1.1.1 labevents (hosp) - itemid - 51265 - Platelet Count ***\
\
select * from \
(\
select cohort.hadm_id, \
'Platelet_count_hosp' as event, \
\
case\
when (safe_cast(value as NUMERIC) < ref_range_lower) \
then 'low'\
\
when (safe_cast(value as NUMERIC) > ref_range_upper) \
then 'high'\
\
when (safe_cast(value as NUMERIC) >= ref_range_lower and safe_cast(value as NUMERIC) <= ref_range_upper) \
then 'normal'\
\
--when (value is null and flag is not null) \
--then flag\
\
when (ref_range_lower is null or ref_range_upper is null) \
then 'no_ref_range'\
\
else\
'na'\
\
end as value,\
\
value as details,\
\
charttime as event_time from `physionet-data-313305.HIT_trajectories.Hep_cohort_V7` cohort # 13415 hadm_ids\
\
inner join `physionet-data.mimic_hosp.labevents` labs\
on cohort.subject_id = labs.subject_id\
\
where itemid = 51265\
and value is not null -- there were few records where 'values' is null, but flag was 'abnormal'. We ignore them too.\
and charttime between DATETIME(admittime) and DATETIME(dischtime)\
and charttime > DATETIME(hep_start) \
)\
\
\
union all\
\
--*** 1.1.2 labevents (hosp) - itemid - 51266 - Platelet Smear ***\
\
select * from \
(\
select cohort.hadm_id, \
'Platelet_Smear_hosp' as event,\
\
case\
\
when (lower(comments) like 'high%' or lower(comments) like 'very high%')\
then 'high'\
\
when (lower(comments) like 'low%' or lower(comments) like 'very low%'or lower(comments) like 'rare%' or comments = 'P0.') \
then 'low'\
\
when (lower(comments) like 'normal%' or comments = 'P6.')\
then 'normal'\
\
when (lower(comments) like 'unable to estimate due to platelet clumps%' or lower(comments) like 'unable to report due to plt clumps.%')\
then 'cannot_estimate_due_to_platelet_clumps'\
\
when (comments = '___' and flag is not null)\
then flag\
\
else -- 2 possibilities. either : (comments = '___' and flag is null) or (comments is null)\
'na'\
\
end as value,\
\
comments as details,\
\
charttime as event_time from `physionet-data-313305.HIT_trajectories.Hep_cohort_V7` cohort # 13415 hadm_ids\
\
inner join `physionet-data.mimic_hosp.labevents` labs\
on cohort.subject_id = labs.subject_id\
\
where itemid = 51266\
and charttime between DATETIME(admittime) and DATETIME(dischtime)\
and charttime > DATETIME(hep_start) \
)\
\
union all\
\
--*** 1.1.3 labevents (hosp) - itemid - 51704 - Platelet_Count_chemistry ***\
\
select * from \
(\
select cohort.hadm_id, \
'Platelet_Count_hosp_chemistry' as event, \
value as value, -- either 'abnormal' or null\
comments as details,\
charttime as event_time from `physionet-data-313305.HIT_trajectories.Hep_cohort_V7` cohort # 13415 hadm_ids\
\
inner join `physionet-data.mimic_hosp.labevents` labs\
on cohort.subject_id = labs.subject_id\
\
where itemid = 51704\
and charttime between DATETIME(admittime) and DATETIME(dischtime)\
and charttime > DATETIME(hep_start) \
)\
\
union all\
\
--*** 1.1.4 labevents (hosp) - itemid - 52105 - Direct Antiplatelet Antibodies ***\
\
select * from \
(\
select cohort.hadm_id, \
'Direct_Antiplatelet_Antibodies' as event, \
value as value, -- either 'abnormal' or null\
comments as details,\
charttime as event_time from `physionet-data-313305.HIT_trajectories.Hep_cohort_V7` cohort # 13415 hadm_ids\
\
inner join `physionet-data.mimic_hosp.labevents` labs\
on cohort.subject_id = labs.subject_id\
\
where itemid = 52105\
and charttime between DATETIME(admittime) and DATETIME(dischtime)\
and charttime > DATETIME(hep_start) \
)\
\
union all\
\
--*** 1.1.5 labevents (hosp) - itemid - 51264 - Platelet Clumps ***\
\
select * from \
(\
select cohort.hadm_id, \
'Platelet_Clumps' as event, \
value as value, -- either 'abnormal' or null\
comments as details,\
charttime as event_time from `physionet-data-313305.HIT_trajectories.Hep_cohort_V7` cohort # 13415 hadm_ids\
\
inner join `physionet-data.mimic_hosp.labevents` labs\
on cohort.subject_id = labs.subject_id\
\
where itemid = 51264\
and charttime between DATETIME(admittime) and DATETIME(dischtime)\
and charttime > DATETIME(hep_start) \
)\
\
/*\
union all\
\
--*** 1.1.8 chartevents (ICU) - itemid - 220507 - Activated Clotting Time *** - Removed this, as the value varies depens on anticoagulation theraphy (https://emedicine.medscape.com/article/2084818-overview?form=fpf)\
\
select * from \
(\
select cohort.hadm_id, \
'Activated_Clotting_Time' as event, \
\
'' as value, -- because contacatanating this with 'event' is meaningless\
value as details, -- numerical_value\
\
--value as value, -- numerical_value\
--cast(warning as string) as details,\
\
charttime as event_time from `physionet-data-313305.HIT_trajectories.Hep_cohort_V7` cohort # 13415 hadm_ids\
\
inner join `physionet-data.mimic_icu.chartevents` icuevents\
on cohort.subject_id = icuevents.subject_id\
\
where itemid = 220507\
and charttime between DATETIME(admittime) and DATETIME(dischtime)\
and charttime > DATETIME(hep_start) \
)\
*/\
\
union all\
--*** 1.2 HIT may cause activation of monocytes, neutrophils, and endothelial cells ***\
\
--*** 1.2.1 labevents (hosp) - monocytes - (itemid, label) = (52074 , Absolute Monocyte Count) , (51253 , Monocyte Count) , (51254 , Monocytes) ***\
\
select * from \
(\
select cohort.hadm_id, \
'Monocytes_hosp' as event, \
\
case\
\
when (safe_cast(value as NUMERIC) < ref_range_lower) -- when flag is null\
then 'low'\
\
when (safe_cast(value as NUMERIC) > ref_range_upper) \
then 'high'\
\
when (safe_cast(value as NUMERIC) >= ref_range_lower and safe_cast(value as NUMERIC) <= ref_range_upper) \
then 'normal'\
\
--when (value is null and flag is not null) \
--then flag\
\
when (ref_range_lower is null or ref_range_upper is null) \
then 'no_ref_range'\
\
else\
'na'\
\
end as value,\
\
value as details,\
\
charttime as event_time from `physionet-data-313305.HIT_trajectories.Hep_cohort_V7` cohort # 13415 hadm_ids\
\
inner join `physionet-data.mimic_hosp.labevents` labs\
on cohort.subject_id = labs.subject_id\
\
where itemid in (52074) --52074, 51253, 51254 - only one measurement (more practical) was considered, otherwise multiple rows will be generated for same 'event_time'.\
and value is not null -- there were few records where 'values' is null, but flag was 'abnormal'. We ignore them too.\
and charttime between DATETIME(admittime) and DATETIME(dischtime)\
and charttime > DATETIME(hep_start) \
)\
\
union all\
\
--*** 1.2.2 labevents (hosp) - Neutrophils - (itemid, label) = (51537 , Absolute Neutrophil) , (51697 , Neutrophils) , (52075, Absolute Neutrophil Count) , (51256, Neutrophil) ***\
\
select * from \
(\
select cohort.hadm_id, \
'Neutrophil_hosp' as event, \
\
case\
\
when (safe_cast(value as NUMERIC) < ref_range_lower) -- when flag is null\
then 'low'\
\
when (safe_cast(value as NUMERIC) > ref_range_upper) \
then 'high'\
\
when (safe_cast(value as NUMERIC) >= ref_range_lower and safe_cast(value as NUMERIC) <= ref_range_upper) \
then 'normal'\
\
--when (value is null and flag is not null) \
--then flag\
\
when (ref_range_lower is null or ref_range_upper is null) \
then 'no_ref_range'\
\
else\
'na'\
\
end as value ,\
\
value as details,\
\
charttime as event_time from `physionet-data-313305.HIT_trajectories.Hep_cohort_V7` cohort # 13415 hadm_ids\
\
inner join `physionet-data.mimic_hosp.labevents` labs\
on cohort.subject_id = labs.subject_id\
\
where itemid in (52075) -- 51537, 51697, 52075, 51256\
and value is not null -- there were few records where 'values' is null, but flag was 'abnormal'. We ignore them too.\
and charttime between DATETIME(admittime) and DATETIME(dischtime)\
and charttime > DATETIME(hep_start) \
)\
\
union all\
\
--*** 1.2.3 labevents (hosp) - endothelial cells - No relevant itemid s found.\
\
--*** 1.2.4 chartevents (icu) - monocytes (229359,Absolute Count - Monos) , (225642,Differential-Monos) ***\
\
--*** 1.2.5 chartevents (icu) - Neutrophils (229355,Absolute Neutrophil Count) ***\
\
--*** 1.2.6 chartevents (icu) - endothelial cells (No relevant itemid s found) \
\
\
--*** 1.3 HIT may cause tests - thrombin / partial thromboplastin time / prothrombin time ***\
\
--*** 1.3.1 labevents (hosp) - thrombin / partial thromboplastin time / prothrombin time - (itemid, label) which only had records in mimic = (51140 , Antithrombin) , (51297 , Thrombin) ***\
\
-- below itemid s didn't have records in mimic.chartevents, however they too were considered - (52078 , Antithrombin III) , (52948 , Thrombin) , (52187 , Thrombin Time) , (52188 , Thrombin Time Control) , (52189 , Thrombin Time Control) , (52190 , Tissue Thromboplastin Inhibitor)\
\
-- why 'Antithrombin' ? Antithrombin is a naturally occurring protein in the blood that acts as an anticoagulant. Its primary function is to inhibit the activity of several clotting factors, including thrombin and factor Xa. By inhibiting these factors, antithrombin helps regulate and prevent excessive blood clot formation (thrombosis). Testing antithrombin levels can provide insight into the body's ability to regulate clotting and assess the risk of thrombotic events (excessive clot formation) or bleeding disorders.\
\
-- why 'Thrombin' ? Thrombin is a key enzyme in the blood coagulation process. It is responsible for converting soluble fibrinogen into insoluble fibrin, which forms the structural framework of a blood clot. Thrombin also activates other clotting factors, amplifying the clotting process. Testing thrombin levels or activity may be done in certain clinical settings to assess coagulation status or diagnose clotting disorders.\
\
select * from \
(\
select cohort.hadm_id, \
concat(label,'_hosp') as event, \
\
case\
\
when (safe_cast(value as NUMERIC) < ref_range_lower) -- when flag is null\
then 'low'\
\
when (safe_cast(value as NUMERIC) > ref_range_upper) \
then 'high'\
\
when (safe_cast(value as NUMERIC) >= ref_range_lower and safe_cast(value as NUMERIC) <= ref_range_upper) \
then 'normal'\
\
--when (value is null and flag is not null) \
--then flag\
\
when (ref_range_lower is null or ref_range_upper is null) \
then 'no_ref_range'\
\
else\
'na'\
\
end as value,\
\
value as details,\
\
charttime as event_time from `physionet-data-313305.HIT_trajectories.Hep_cohort_V7` cohort # 13415 hadm_ids\
\
inner join `physionet-data.mimic_hosp.labevents` labs\
on cohort.subject_id = labs.subject_id\
\
left join `physionet-data.mimic_hosp.d_labitems` ditems\
on labs.itemid = ditems.itemid\
\
where labs.itemid in (51140 , 52078 , 51297 , 52948 , 52187 , 52188 , 52189 , 52190, 51274)\
and value is not null -- there were few records where 'values' is null, but flag was 'abnormal'. We ignore them too.\
and charttime between DATETIME(admittime) and DATETIME(dischtime)\
and charttime > DATETIME(hep_start) \
)\
\
union all\
\
--*** 1.3.2 chartevents (icu) - thrombin / partial thromboplastin time / prothrombin time - (itemid, label) = (227465 , Prothrombin time) , (227469 - Thrombin) , (229374 , AT (Antithrombin funct)) , (229372 - AT ( Antithrombin funct))\
\
\
--*** 1.4 HIT antibody tests ***\
\
-- merge 4.1 and 4.2 later - -- if test is positive - can consider numerical value only. \
-- but if test is negative, should check both numerical and interpret values, coz some nehative patients have num values, while some may only have interpret value\
\
--*** 1.4.1 labevents (hosp) - HIT antibody tests - (itemid, label) = (52132 , HIT-Ab Numerical Result) \
select distinct * from\
(\
select * from \
(\
select cohort.hadm_id, \
'HIT_Ab_test' as event, \
\
case\
\
when (safe_cast(value as NUMERIC) >= ref_range_lower and safe_cast(value as NUMERIC) <= ref_range_upper) \
then 'negative_HIT-Ab_test'\
\
when (safe_cast(value as NUMERIC) > ref_range_upper) \
then 'positive_HIT-Ab_test'\
\
--when (value is null and flag is not null) \
--then flag\
\
else\
'na'\
\
end as value,\
\
'' as details,\
\
charttime as event_time from `physionet-data-313305.HIT_trajectories.Hep_cohort_V7` cohort # 13415 hadm_ids\
\
inner join `physionet-data.mimic_hosp.labevents` labs\
on cohort.subject_id = labs.subject_id\
\
where itemid in (52132)\
and value is not null -- there were few records where 'values' is null, but flag was 'abnormal'. We ignore them too.\
and charttime between DATETIME(admittime) and DATETIME(dischtime)\
and charttime > DATETIME(hep_start) \
)\
\
union all\
\
--*** 1.4.2 labevents (hosp) - HIT antibody tests - (itemid, label) = (52131 , HIT-Ab Interpreted Result)\
\
select * from \
(\
select cohort.hadm_id, \
'HIT_Ab_test' as event, \
\
case\
\
when (lower(value) like '%neg%') \
then 'negative_HIT-Ab_test'\
\
when (lower(value) like '%pos%') \
then 'positive_HIT-Ab_test'\
\
--when (value is null and flag is not null) \
--then flag\
\
else\
'na'\
\
end as value,\
\
'' as details,\
\
charttime as event_time from `physionet-data-313305.HIT_trajectories.Hep_cohort_V7` cohort # 13415 hadm_ids\
\
inner join `physionet-data.mimic_hosp.labevents` labs\
on cohort.subject_id = labs.subject_id\
\
where itemid in (52131)\
and value is not null -- there were few records where 'values' is null, but flag was 'abnormal'. We ignore them too.\
and charttime between DATETIME(admittime) and DATETIME(dischtime)\
and charttime > DATETIME(hep_start) \
)\
)\
\
union all\
\
--*** 1.5 Immunoglobulin G antibodies / IgG antibodies ***\
\
--*** 1.5.1 labevents (hosp) - (itemid, label) = (50950 , Immunoglobulin G)\
\
select * from \
(\
select cohort.hadm_id, \
'ImmunoglobulinG_antibodies' as event, \
\
case\
\
when (safe_cast(value as NUMERIC) < ref_range_lower) \
then 'low'\
\
when (safe_cast(value as NUMERIC) > ref_range_upper) \
then 'high'\
\
when (safe_cast(value as NUMERIC) >= ref_range_lower and safe_cast(value as NUMERIC) <= ref_range_upper) \
then 'normal'\
\
when (ref_range_lower is null or ref_range_upper is null) \
then 'no_ref_range'\
\
else\
'na'\
\
end as value,\
\
value as details,\
\
charttime as event_time from `physionet-data-313305.HIT_trajectories.Hep_cohort_V7` cohort # 13415 hadm_ids\
\
inner join `physionet-data.mimic_hosp.labevents` labs\
on cohort.subject_id = labs.subject_id\
\
where itemid in (50950)\
and value is not null -- there were few records where 'values' is null, but flag was 'abnormal'. We ignore them too.\
and charttime between DATETIME(admittime) and DATETIME(dischtime)\
and charttime > DATETIME(hep_start) \
)\
\
union all\
\
--*** 1.6 labevents - mimic_hosp - 51196 # d-dimer ***--\
-- https://ashpublications.org/blood/article/142/Supplement%201/5522/505702/Use-of-Fibrinogen-and-D-Dimer-to-Predict-Platelet\
\
select * from \
(\
select cohort.hadm_id, \
'd_dimer_hosp' as event, \
\
case\
\
when (safe_cast(value as NUMERIC) < ref_range_lower) \
then 'low'\
\
when (safe_cast(value as NUMERIC) > ref_range_upper) \
then 'high'\
\
when (safe_cast(value as NUMERIC) >= ref_range_lower and safe_cast(value as NUMERIC) <= ref_range_upper) \
then 'normal'\
\
when (ref_range_lower is null or ref_range_upper is null) \
then 'no_ref_range'\
\
else\
'na'\
\
end as value,\
\
value as details,\
\
charttime as event_time from `physionet-data-313305.HIT_trajectories.Hep_cohort_V7` cohort # 13415 hadm_ids\
\
inner join `physionet-data.mimic_hosp.labevents` labs\
on cohort.subject_id = labs.subject_id\
\
where itemid in (51196)\
and value is not null -- there were few records where 'values' is null, but flag was 'abnormal'. We ignore them too.\
and charttime between DATETIME(admittime) and DATETIME(dischtime)\
and charttime > DATETIME(hep_start) \
)\
\
union all\
\
--*** 1.7 labevents - mimic_hosp - 51214 # Fibrinogen ***--\
-- https://ashpublications.org/blood/article/142/Supplement%201/5522/505702/Use-of-Fibrinogen-and-D-Dimer-to-Predict-Platelet\
\
select * from \
(\
select cohort.hadm_id, \
'fibrinogen_hosp' as event, \
\
case\
\
when ((safe_cast(value as NUMERIC) < ref_range_lower) or (value = '<35')) \
then 'low'\
\
when (safe_cast(value as NUMERIC) > ref_range_upper) \
then 'high'\
\
when (safe_cast(value as NUMERIC) >= ref_range_lower and safe_cast(value as NUMERIC) <= ref_range_upper) \
then 'normal'\
\
when (ref_range_lower is null or ref_range_upper is null) \
then 'no_ref_range'\
\
else\
'na'\
\
end as value,\
\
value as details,\
\
charttime as event_time from `physionet-data-313305.HIT_trajectories.Hep_cohort_V7` cohort # 13415 hadm_ids\
\
inner join `physionet-data.mimic_hosp.labevents` labs\
on cohort.subject_id = labs.subject_id\
\
where itemid in (51214)\
and value is not null -- there were few records where 'values' is null, but flag was 'abnormal'. We ignore them too.\
and charttime between DATETIME(admittime) and DATETIME(dischtime)\
and charttime > DATETIME(hep_start) \
)\
\
#---------------------------------------------------------------\
\
--2. *** Medication / Inputevents *** \
\
--*** 2.1 non-heparin anticoagulants ***\
\
union all\
\
select * from\
(\
\
with merge_icu_and_hosp as \
\
(\
SELECT \
icu_and_hosp.* , \
row_number() over(partition by hadm_id order by event_time) as rn # last hep_admin appraes first \
from\
( \
\
--*** 2.1.1 emar (hosp) - First instance of direct oral anticoagulants - rivaroxaban. apixaban, dabigatran, edoxaban AND --*** 2.1.2 emar (hosp) - parenteral anticoagulants (intravenous) - argatroban, bivalirudin, danaparoid, fondaparinux, lepirudin\
\
select * from \
( \
with doac as\
(\
select cohort.hadm_id,\
emar.medication, \
emar.charttime,\
event_txt,\
\
row_number() over(\
partition by cohort.hadm_id\
order by emar.charttime) as rn # earliest event appraes first \
\
from `physionet-data-313305.HIT_trajectories.Hep_cohort_V7` cohort # 13415 hadm_ids \
inner join `physionet-data.mimic_hosp.emar`emar \
on cohort.subject_id = emar.subject_id\
\
where \
\
(\
REGEXP_CONTAINS(LOWER(emar.medication), '(rivaroxaban|dabigatran|apixaban|edoxaban)') -- direct oral anticoagulants\
\
or\
\
REGEXP_CONTAINS(LOWER(emar.medication), '(argatroban|bivalirudin|danaparoid|fondaparinux|lepirudin)') -- parenteral anticoagulants (intravenous)\
\
)\
\
and\
\
(\
trim(event_txt) not in ('Hold Dose', 'Not Confirmed', 'Removed', 'Not Started', 'Not Applied', 'Delayed', 'Not Assessed','Documented in O.R. Holding', 'Flushed in Other Location', 'Delayed Flushed', 'Delayed Not Confirmed', 'Flushed','Not Given', 'Delayed Not Started', 'Stopped - Unscheduled', 'Stopped in Other Location','Stopped - Unscheduled in Other Location', 'Stopped As Directed', 'Delayed Stopped', 'Stopped', 'Delayed Stopped As Directed', 'Infusion Reconciliation', 'Infusion Reconciliation Not Done')\
\
or event_txt is null\
)\
\
\
--and emar.event_txt IN ("Administered" , "Confirmed", "Started", "Restarted", "Administered in Other Location", " in other Location")\
\
and charttime between DATETIME(admittime) and DATETIME(dischtime)\
and charttime > DATETIME(hep_start) \
)\
\
\
select hadm_id, \
'first_non_heparin_anticoagulant_dose_emar' as event,\
medication as value,\
\
event_txt as details,\
\
charttime as event_time\
\
from doac\
where rn = 1 \
\
)\
\
--*** 2.1.2 emar (hosp) - parenteral anticoagulants (intravenous) - argatroban, bivalirudin, danaparoid, fondaparinux, lepirudin\
\
union all\
\
--*** 2.1.3 inputevents (icu) - parenteral anticoagulants (intravenous) - argatroban, bivalirudin, danaparoid, fondaparinux, lepirudin\
\
select * from \
( \
with parental_anticoagulant_icu as\
(\
select cohort.hadm_id,\
starttime,\
ditems.label, \
inputevents.originalamount,\
statusdescription,\
\
row_number() over(\
partition by cohort.hadm_id\
order by inputevents.starttime) as rn # earliest event appraes first \
\
from `physionet-data-313305.HIT_trajectories.Hep_cohort_V7` cohort # 13415 hadm_ids \
inner join `physionet-data.mimic_icu.inputevents` inputevents\
on cohort.hadm_id = inputevents.hadm_id -- we can directly join by hadm_id, as there is no 'null' hadm_id records in this 'inputevents' table.\
\
left join `physionet-data.mimic_icu.d_items` ditems\
on inputevents.itemid = ditems.itemid\
\
where \
REGEXP_CONTAINS(LOWER(ditems.label), '(argatroban|bivalirudin|danaparoid|fondaparinux|lepirudin)')\
\
--and emar.event_txt IN ("Administered" , "Confirmed", "Started", "Restarted", "Administered in Other Location", " in other Location")\
\
and inputevents.starttime > DATETIME(hep_start) \
)\
\
select hadm_id, \
'first_parenteral_anticoagulant_dose_inputevents' as event,\
label as value,\
\
statusdescription as details, -- had 4 distinct statusdescription s. - FinishedRunning , Paused, ChangeDose/Rate , Stopped\
--cast(originalamount as string) as details,\
\
starttime as event_time\
\
from parental_anticoagulant_icu\
where rn = 1 \
\
)\
\
) icu_and_hosp\
\
)\
\
select hadm_id, \
'first_non_heparin_anticoagulant_dose' as event,\
value,\
\
details,\
\
event_time\
\
from merge_icu_and_hosp\
where rn = 1 \
\
)\
\
\
union all\
\
--2.2. *** dextran 70 *** \
\
--*** 2.2.1 emar (hosp) - (mediactions) = dextran 70-hypromellose (PF) , dextran 70-hypromellose\
\
select * from \
( \
with dextran_70_emar as\
(\
select cohort.hadm_id,\
emar.medication, \
emar.charttime,\
event_txt,\
\
row_number() over(\
partition by cohort.hadm_id\
order by emar.charttime) as rn # earliest event appraes first \
\
from `physionet-data-313305.HIT_trajectories.Hep_cohort_V7` cohort # 13415 hadm_ids \
inner join `physionet-data.mimic_hosp.emar`emar \
on cohort.subject_id = emar.subject_id\
\
where \
REGEXP_CONTAINS(LOWER(emar.medication), '(dextran 70)')\
\
and\
\
(\
trim(event_txt) not in ('Hold Dose', 'Not Confirmed', 'Removed', 'Not Started', 'Not Applied', 'Delayed', 'Not Assessed','Documented in O.R. Holding', 'Flushed in Other Location', 'Delayed Flushed', 'Delayed Not Confirmed', 'Flushed','Not Given', 'Delayed Not Started', 'Stopped - Unscheduled', 'Stopped in Other Location','Stopped - Unscheduled in Other Location', 'Stopped As Directed', 'Delayed Stopped', 'Stopped', 'Delayed Stopped As Directed', 'Infusion Reconciliation', 'Infusion Reconciliation Not Done')\
\
or event_txt is null\
)\
\
\
\
and charttime between DATETIME(admittime) and DATETIME(dischtime)\