-
Notifications
You must be signed in to change notification settings - Fork 6
/
4CE_PhaseX2_Files_oracle.sql
4490 lines (4240 loc) · 241 KB
/
4CE_PhaseX2_Files_oracle.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
--##############################################################################
--##############################################################################
--### 4CE Phase 1.2 and 2.2
--### Date: April 21, 2021 translated May 19, 2021
--### Database: Oracle
--### Data Model: i2b2
--### Created By: Griffin Weber (weber@hms.harvard.edu)
--### Translated to Oracle: Michele Morris ( mim18@pitt.edu)
--##############################################################################
--##############################################################################
/*
INTRODUCTION:
This script contains code to generate both 4CE Phase 1.2 and Phase 2.2 files.
By default, it will only generate Phase 1.2 files, which contain obfuscated
aggregate counts and statistics. You need to change the settings in the
fource_config table so that it generates the Phase 2.2 files. Phase 2.2
files include non-obfuscated versions of all the Phase 1.1 files, as well as
files containing patient-level data.
PHASE 1.2 FILES:
This script creates the following Phase 1.2 files with obfuscated counts.
These files are uploaded to 4CE.
1) DailyCounts - Patient counts by calendar date
2) ClinicalCourse - Counts by number of days since hospital admission
3) AgeSex - Age and sex breakdowns
4) Labs - Lab values per day since admission
5) DiagProcMed - Diagnoses, procedures, and meds before and after admission
6) RaceByLocalCode - Breakdowns based on the codes used within your hospital
7) RaceBy4CECode - Breackdowns based on 4CE race categories
8) LabCodes - The laboratory code and unit mappings used (no patient data)
PHASE 2.2 FILES:
For Phase 2.2, the script creates a copy of Phase 1.2 tables 1-7 with "Local"
added to the beginning of the file name (e.g., LocalDailyCounts, LocalLabs,
etc.). These tables contain counts that are not obfuscated. They should be
stored locally and not shared with 4CE. Phase 2.2 creates 5 additional files
that contain patient-level data. These should also be stored locally and not
shared with 4CE.
1) LocalPatientSummary - One row per patient in each cohort
2) LocalPatientClinicalCourse - Daily summary of patient hospitalizations
3) LocalPatientObservations - Each diagnosis, lab test, etc. per day
4) LocalPatientRace - Each patient's race code(s)
5) LocalPatientMapping - Map from i2b2 patient_num to ID used in the files
CONFIGURATION AND MAPPINGS:
You will need to modify the configuration and mapping tables at the beginning
of this script. Read the instructions carefully. You might also have to edit
the logic used in the queries to identify admissions, ICU visits, and deaths,
which are placed right after the code mappings.
OUTPUT OPTIONS:
For each Phase, the script provides three output options. The first option,
"output to columns" returns the data as tables to your query tool (e.g., SSMS).
The data are not saved to the database. This is useful if you want to copy the
data into a program like Excel. The second option, "output to CSV", returns
the data as tables with a single column that contains a CSV-style string. You
can copy and paste this into a text file and save it with a .csv extension. It
will then be in the correct format to upload to 4CE. The third option saves
the data as tables in your database. You need to specify a prefix, like
"dbo_FourCE_" that will be added to the begining of each table name.
COHORTS:
By default, this script only selects patients who had a positive COVID test
and were admitted. It partitions these patients into cohorts based on the
quarter (2020 Q1, 2020 Q2, etc.) they were admitted. The configuration options
include_extra_cohorts_phase1 and include_extra_cohorts_phase2 will add extra
cohorts to the Phase 1.2 and/or Phase 2.2 files. These extra cohorts include
(1) patients who were admitted with a negative COVID test, (2) COVID positive
patients who were not admitted, (3) COVID negative patients who were not admitted,
and (4) patients with a U07.1 diagnoses (confirmed COVID), but no recorded
positive COVID test. In other words, they capture every patient who had a COVID.
test. These extra cohorts are also partitioned by quarter. Note that for patients
who were not admitted, their first COVID test (pr U07.1 diagnosis) date is used
as the "admission" date; and, "days_since_admission" is really days since the
COVID test. Note that including these extra cohorts greatly increases the sizes
of the patient level Phase 2.2 files and makes the script take much longer run.
(You can optionally define additional cohorts, based on custom inclusion or
exclusion criteria, matching algorithms, or date range partitions.)
SOURCE DATA UPDATED DATE:
Use the configuration setting source_data_updated_date to indicate when the
data that this script is run on was last updated. For example, if you are
running this script on May 1, 2021, but the data has not been updated since
April 15, 2021, then set the source_data_updated_date to April 15. This is
needed to determine the date beyond which data are censored. For example, if
a patient is still in the hospital on April 15, then the discharge date is
unknown. (You can optionally assign a different source_data_updated_date to
each cohort. This would be needed if, for example, the data on COVID positive
patients are updated more frequently than COVID negative patients.)
ALTERNATIVE SCHEMAS AND MULTIPLE FACT TABLES:
The code assumes your fact and dimension tables are in the DBO schema. If
you use a different schema, then do a search-and-replace to change "dbo_" to
your schema. The code also assumes you have a single fact table called
"dbo_observation_fact". If you use multiple fact tables, then search for
"observation_fact" and change it as needed.
*/
--##############################################################################
--##############################################################################
--##############################################################################
--##############################################################################
--###
--### Configuration and code mappings (modify for your institution)
--###
--##############################################################################
--##############################################################################
--##############################################################################
--##############################################################################
--##############################################################################
--### Set output csv file path
--### Global replace @exportFilePath with the path where you want your output
--### files to land
--### Example replace @exportFilePath with C:\User\My4ceDir
--##############################################################################
--------------------------------------------------------------------------------
-- General settings
--------------------------------------------------------------------------------
--drop table fource_config; -- make sure everything is clean
create table fource_config (
siteid varchar(20), -- Up to 20 letters or numbers, must start with letter, no spaces or special characters.
race_data_available int, -- 1 if your site collects race/ethnicity data; 0 if your site does not collect this.
icu_data_available int, -- 1 if you have data on whether patients were in the ICU
death_data_available int, -- 1 if you have data on whether patients have died
code_prefix_icd9cm varchar(50), -- prefix (scheme) used in front of a ICD9CM diagnosis code; set to '' if not collected or no prefix used
code_prefix_icd10cm varchar(50), -- prefix (scheme) used in front of a ICD10CM diagnosis code; set to '' if not collected or no prefix used
source_data_updated_date date, -- the date your source data were last updated (e.g., '3/25/2021'); set to NULL if data go to the day you run this script
-- Phase 1.2 (obfuscated aggregate data) options
include_extra_cohorts_phase1 int, -- 0 by default, 1 to include COVID negative, U07.1, and non-admitted cohorts to Phase 1 files
obfuscation_blur int, -- Add random number || /-blur to each count (0 = no blur)
obfuscation_small_count_mask int, -- Replace counts less than mask with -99 (0 = no small count masking)
obfuscation_small_count_delete int, -- Delete rows where all values are small counts (0 = no, 1 = yes)
obfuscation_agesex int, -- Replace combination of age-sex and total counts with -999 (0 = no, 1 = yes)
output_phase1_as_columns int, -- Return the data in tables with separate columns per field
output_phase1_as_csv int, -- Return the data in tables with a single column containing comma separated values
save_phase1_as_columns int, -- Save the data as tables with separate columns per field
save_phase1_as_prefix varchar(50), -- Table name prefix when saving the data as tables
-- Phase 2.2 (non-obfuscated aggregate and patient level data) options
include_extra_cohorts_phase2 int, -- 0 by default, 1 to include COVID negative, U07.1, and non-admitted cohorts to Phase 2 files
replace_patient_num int, -- Replace the patient_num with a unique random number
output_phase2_as_columns int, -- Return the data in tables with separate columns per field
output_phase2_as_csv int, -- Return the data in tables with a single column containing comma separated values
save_phase2_as_columns int, -- Save the data as tables with separate columns per field
save_phase2_as_prefix varchar(50), -- Table name prefix when saving the data as tables
eval_start_date date -- use this so that dates can be changed consitently throughout the script
--blackout_days_before -7 blackout_days_before 14 add these later
);
commit;
--truncate table fource_config;
insert into fource_config
select 'UPitt', -- siteid
1, -- race_data_available
1, -- icu_data_available
1, -- death_data_available
'ICD9CM:', -- code_prefix_icd9cm
'ICD10CM:', -- code_prefix_icd10cm
NULL, -- source_data_updated_date
-- Phase 1
0, -- include_extra_cohorts_phase1 (please set to 1 if allowed by your IRB and institution)
0, -- obfuscation_blur
10, -- obfuscation_small_count_mask
1, -- obfuscation_small_count_delete
0, -- obfuscation_agesex
0, -- output_phase1_as_columns
1, -- output_phase1_as_csv
0, -- save_phase1_as_columns
'dbo_FourCE_', -- save_phase1_as_prefix (don't use "4CE" since it starts with a number)
-- Phase 2
0, -- include_extra_cohorts_phase2 (please set to 1 if allowed by your IRB and institution)
1, -- replace_patient_num
0, -- output_phase2_as_columns
0, -- output_phase2_as_csv
0, -- save_phase2_as_columns
'dbo_FourCE_', -- save_phase2_as_prefix (don't use "4CE" since it starts with a number
to_date('01-JAN-2019')
from dual;
commit;
-- ! If your diagnosis codes do not start with a prefix (e.g., "ICD:"),
-- ! then you will need to customize queries the use the observation_fact table
-- ! so that only diagnoses are selected.
--------------------------------------------------------------------------------
-- Code mappings (excluding labs and meds)
-- * Don't change the "code" value.
-- * Modify the "local_code" to match your database.
-- * Repeat a code multiple times if you have more than one local code.
-- * Comment out rows that are not applicable to your database.
--------------------------------------------------------------------------------
--drop table fource_code_map;
create table fource_code_map (
code varchar(50) not null,
local_code varchar(50) not null
);
alter table fource_code_map add primary key (code, local_code);
-- Inpatient visit codes
-- * The SQL supports using either visit_dimension table or the observation_fact table.
-- * Change the code as needed. Comment out the versions that you do not use.
-- * You can replace this code with external mapping tables of location codes for example a list of hospital units
insert into fource_code_map
select '', '' from dual where 1=0
-- Inpatient visits (from the visit_dimension.inout_cd field)
union all select 'inpatient_inout_cd', 'I' from dual
union all select 'inpatient_inout_cd', 'IN' from dual
-- Inpatient visits (from the visit_dimension.location_cd field)
-- copy the line below for each code or location that represents an inpatient
union all select 'inpatient_location_cd', 'Inpatient' from dual
-- ICU visits (from the observation_fact.concept_cd field)
-- copy the line below for each code or location that represents an inpatient
union all select 'inpatient_concept_cd', 'UMLS:C1547137' from dual;-- from ACT ontology
commit;
-- ICU visit codes (optional)
-- * The SQL supports using either visit_dimension table or the observation_fact table.
-- * Change the code as needed. Comment out the versions that you do not use.
--truncate table fource_code_map;
insert into fource_code_map
select '', '' from dual where 1=0
-- ICU visits (from the visit_dimension.inout_cd field)
union all select 'icu_inout_cd', 'ICU' from dual
-- ICU visits (from the visit_dimension.location_cd field)
union all select 'icu_location_cd', 'ICU' from dual
-- ICU visits (from the observation_fact.concept_cd field)
union all select 'icu_concept_cd', 'UMLS:C1547136' from dual-- from ACT ontology
union all select 'icu_concept_cd', 'CPT4:99291' from dual-- from ACT ontology
union all select 'icu_concept_cd', 'CPT4:99292' from dual-- from ACT ontology
-- ICU visits (from the observation_fact.location_cd field)
union all select 'icu_fact_location_cd', 'ICU' from dual
-- ICU location_cd in observation_fact selected from external icu map
union all select 'icu_fact_location_cd', icu_unit_code from external_icu_map;
--select * from fource_code_map;
commit;
-- If you use location_cd to map ICU locations you can create a list here or load from an external mapping table
drop table fource_icu_location;
create table fource_icu_location as select cast(department_id as varchar2(50)) location_cd from icus;
--create table fource_icu_location as
--select cast('icu1' as varchar2(50)) as location_cd from dual
--union
--select cast('icu1' as varchar2(50)) as location_cd from dual;
commit;
-- Sex codes
insert into fource_code_map
select '', '' from dual where 1=0
-- Sex (from the patient_dimension.sex_cd field)
union all select 'sex_patient:male', 'M' from dual
union all select 'sex_patient:male', 'Male' from dual
union all select 'sex_patient:female', 'F' from dual
union all select 'sex_patient:female', 'Female' from dual
-- Sex (from the observation_fact.concept_cd field)
union all select 'sex_fact:male', 'DEM|SEX:M' from dual
union all select 'sex_fact:male', 'DEM|SEX:Male' from dual
union all select 'sex_fact:female', 'DEM|SEX:F' from dual
union all select 'sex_fact:female', 'DEM|SEX:Female' from dual;
-- Race codes (use the code set for your country, comment out other countries)
insert into fource_code_map
select '', '' from dual where 1=0
-------------------------------------------------------------------
-- Race: United States
-------------------------------------------------------------------
-- Race (from the patient _dimension.race_cd field)
union all select 'race_patient:american_indian', 'NA'
from dual union all select 'race_patient:asian', 'A'
from dual union all select 'race_patient:asian', 'AS'
from dual union all select 'race_patient:black', 'B'
from dual union all select 'race_patient:hawaiian_pacific_islander', 'H'
from dual union all select 'race_patient:hawaiian_pacific_islander', 'P'
from dual union all select 'race_patient:white', 'W'
from dual union all select 'race_patient:hispanic_latino', 'HL'
from dual union all select 'race_patient:other', 'O' -- include multiple if no additional information is known
from dual union all select 'race_patient:no_information', 'NI' -- unknown, not available, missing, refused to answer, not recorded, etc.
-- Race (from the observation_fact.concept_cd field)
from dual union all select 'race_fact:american_indian', 'DEM|race:NA'
from dual union all select 'race_fact:asian', 'DEM|race:A'
from dual union all select 'race_fact:asian', 'DEM|race:AS'
from dual union all select 'race_fact:black', 'DEM|race:B'
from dual union all select 'race_fact:hawaiian_pacific_islander', 'DEM|race:H'
from dual union all select 'race_fact:hawaiian_pacific_islander', 'DEM|race:P'
from dual union all select 'race_fact:white', 'DEM|race:W'
from dual union all select 'race_fact:hispanic_latino', 'DEM|HISP:Y'
from dual union all select 'race_fact:hispanic_latino', 'DEM|HISPANIC:Y'
from dual union all select 'race_fact:other', 'DEM|race:O' -- include multiple if no additional information is known
from dual union all select 'race_fact:no_information', 'DEM|race:NI' from dual; -- unknown, not available, missing, refused to answer, not recorded, etc.
-------------------------------------------------------------------
-- Race: United Kingdom (Ethnicity)
-------------------------------------------------------------------
-- Ethnicity (from the patient_dimension.race_cd field)
-- from dual union all select 'race_patient:uk_asian', 'Asian' -- Asian or Asian British (Indian, Pakistani, Bangladeshi, Chinese, other Asian background)
-- from dual union all select 'race_patient:uk_black', 'Black' -- Black, African, Carribean, or Black British (African/ Caribbean/ any other Black, African or Caribbean background)
-- from dual union all select 'race_patient:uk_white', 'White' -- White (English/ Welsh/ Scottish/Northern Irish/ British, Irish, Gypsy or Irish Traveller, other White background)
-- from dual union all select 'race_patient:uk_multiple', 'Multiple' -- Mixed or Multiple ethnic groups (White and Black Caribbean, White and Black African, White and Asian, Any other Mixed or Multiple ethnic background)
-- from dual union all select 'race_patient:uk_other', 'Other' -- Other ethnic group (Arab, other ethnic group)
-- from dual union all select 'race_patient:uk_no_information', 'NI' -- unknown, not available, missing, refused to answer, not recorded, etc.
-------------------------------------------------------------------
-- Race: Singapore
-------------------------------------------------------------------
-- Race (from the patient_dimension.race_cd field)
-- from dual union all select 'race_patient:singapore_chinese', 'Chinese'
-- from dual union all select 'race_patient:singapore_malay', 'Malay'
-- from dual union all select 'race_patient:singapore_indian', 'Indian'
-- from dual union all select 'race_patient:singapore_other', 'Other'
-- from dual union all select 'race_patient:singapore_no_information', 'NI' -- unknown, not available, missing, refused to answer, not recorded, etc.
-------------------------------------------------------------------
-- Race: Brazil
-------------------------------------------------------------------
-- Race (from the patient_dimension.race_cd field)
-- from dual union all select 'race_patient:brazil_branco', 'Branco'
-- from dual union all select 'race_patient:brazil_pardo', 'Pardo'
-- from dual union all select 'race_patient:brazil_preto', 'Preto'
-- from dual union all select 'race_patient:brazil_indigena', 'Indigena'
-- from dual union all select 'race_patient:brazil_amarelo', 'Amarelo'
-- from dual union all select 'race_patient:brazil_no_information', 'NI' -- unknown, not available, missing, refused to answer, not recorded, etc.
-- Codes that indicate a COVID-19 nucleic acid test result (use option #1 and/or option #2)
-- COVID-19 Test Option #1: individual concept_cd values
insert into fource_code_map
select 'covidpos', 'LAB|LOINC:COVID19POS' from dual
union all
select 'covidneg', 'LAB|LOINC:COVID19NEG' from dual;
-- COVID-19 Test Option #2: an ontology path (e.g., COVID ACT "Any Positive Test" path)
insert into fource_code_map
select distinct 'covidpos', concept_cd
from @crcSchema.concept_dimension c
where concept_path like '\ACT\UMLS_C0031437\SNOMED_3947185011\UMLS_C0022885\UMLS_C1335447\%'
and concept_cd is not null
and not exists (select * from fource_code_map m where m.code='covidpos' and m.local_code=c.concept_cd)
union all
select distinct 'covidneg', concept_cd
from @crcSchema.concept_dimension c
where concept_path like '\ACT\UMLS_C0031437\SNOMED_3947185011\UMLS_C0022885\UMLS_C1334932\%'
and concept_cd is not null
and not exists (select * from fource_code_map m where m.code='covidneg' and m.local_code=c.concept_cd);
-- Other codes that indicate confirmed COVID-19 (e.g., ICD-10 code U07.1, but not U07.2 or U07.3)
insert into fource_code_map
select 'covidU071', code_prefix_icd10cm || 'U07.1'
from fource_config
union all
select 'covidU071', code_prefix_icd10cm || 'U071' --place holder
from fource_config;
commit;
--------------------------------------------------------------------------------
-- Lab mappings
-- * Do not change the fource_* columns.
-- * Modify the local_* columns to match how your lab data are represented.
-- * Add another row for a lab if you use multiple codes (e.g., see PaCO2).
-- * Delete a row if you don't have that lab.
-- * Change the scale_factor if you use different units.
-- * The lab value will be multiplied by the scale_factor
-- * to convert from your units to the 4CE units.
-- * Add another row if the same code can have multiple units (e.g., see PaO2).
-- * Set local_lab_units='DEFAULT' to match labs with '' or NULL units
-- * (e.g., see PaO2). Only use this if you are sure what the units are.
-- * Add what you think the true units are to the end of the local_lab_name.
--------------------------------------------------------------------------------
--DROP TABLE fource_lab_map;
create table fource_lab_map (
fource_loinc varchar(20) not null,
fource_lab_units varchar(20) not null,
fource_lab_name varchar(100) not null,
scale_factor float not null,
local_lab_code varchar(50) not null,
local_lab_units varchar(20) not null,
local_lab_name varchar(500) not null
);
alter table fource_lab_map add primary key (fource_loinc, local_lab_code, local_lab_units);
insert into fource_lab_map
select fource_loinc, fource_lab_units, fource_lab_name,
scale_factor,
'LOINC:' || local_lab_code, -- Change "LOINC:" to your local LOINC code prefix (scheme)
local_lab_units, local_lab_name
from (
select null fource_loinc, null fource_lab_units, null fource_lab_name,
null scale_factor, null local_lab_code, null local_lab_units, null local_lab_name from dual
where 1=0
union select '1742-6', 'U/L', 'alanine aminotransferase (ALT)', 1, '1742-6', 'U/L', 'YourLocalLabName'
from dual union select '1751-7', 'g/dL', 'albumin', 1, '1751-7', 'g/dL', 'YourLocalLabName'
from dual union select '1920-8', 'U/L', 'aspartate aminotransferase (AST)', 1, '1920-8', 'U/L', 'YourLocalLabName'
from dual union select '1975-2', 'mg/dL', 'total bilirubin', 1, '1975-2', 'mg/dL', 'YourLocalLabName'
from dual union select '1988-5', 'mg/L', 'C-reactive protein (CRP) (Normal Sensitivity)', 1, '1988-5', 'mg/L', 'YourLocalLabName'
from dual union select '2019-8', 'mmHg', 'PaCO2', 1, '2019-8', 'mmHg', 'YourLocalLabName'
from dual union select '2160-0', 'mg/dL', 'creatinine', 1, '2160-0', 'mg/dL', 'YourLocalLabName'
from dual union select '2276-4', 'ng/mL', 'Ferritin', 1, '2276-4', 'ng/mL', 'YourLocalLabName'
from dual union select '2532-0', 'U/L', 'lactate dehydrogenase (LDH)', 1, '2532-0', 'U/L', 'YourLocalLabName'
from dual union select '2703-7', 'mmHg', 'PaO2', 1, '2703-7', 'mmHg', 'YourLocalLabName'
from dual union select '3255-7', 'mg/dL', 'Fibrinogen', 1, '3255-7', 'mg/dL', 'YourLocalLabName'
from dual union select '33959-8', 'ng/mL', 'procalcitonin', 1, '33959-8', 'ng/mL', 'YourLocalLabName'
from dual union select '48065-7', 'ng/mL{FEU}', 'D-dimer (FEU)', 1, '48065-7', 'ng/mL{FEU}', 'YourLocalLabName'
from dual union select '48066-5', 'ng/mL{DDU}', 'D-dimer (DDU)', 1, '48066-5', 'ng/mL{DDU}', 'YourLocalLabName'
from dual union select '49563-0', 'ng/mL', 'cardiac troponin (High Sensitivity)', 1, '49563-0', 'ng/mL', 'YourLocalLabName'
from dual union select '6598-7', 'ug/L', 'cardiac troponin (Normal Sensitivity)', 1, '6598-7', 'ug/L', 'YourLocalLabName'
from dual union select '5902-2', 's', 'prothrombin time (PT)', 1, '5902-2', 's', 'YourLocalLabName'
from dual union select '6690-2', '10*3/uL', 'white blood cell count (Leukocytes)', 1, '6690-2', '10*3/uL', 'YourLocalLabName'
from dual union select '731-0', '10*3/uL', 'lymphocyte count', 1, '731-0', '10*3/uL', 'YourLocalLabName'
from dual union select '751-8', '10*3/uL', 'neutrophil count', 1, '751-8', '10*3/uL', 'YourLocalLabName'
from dual union select '777-3', '10*3/uL', 'platelet count', 1, '777-3', '10*3/uL', 'YourLocalLabName'
from dual union select '34714-6', 'DEFAULT', 'INR', 1, '34714-6', 'DEFAULT', 'YourLocalLabName' from dual
--Example of listing an additional code for the same lab
--from dual union select '2019-8', 'mmHg', 'PaCO2', 1, 'LAB:PaCO2', 'mmHg', 'Carbon dioxide partial pressure in arterial blood'
--Examples of listing different units for the same lab
--from dual union select '2703-7', 'mmHg', 'PaO2', 10, '2703-7', 'cmHg', 'PaO2'
--from dual union select '2703-7', 'mmHg', 'PaO2', 25.4, '2703-7', 'inHg', 'PaO2'
--This will use the given scale factor (in this case 1) for any lab with NULL or empty string units
--from dual union select '2703-7', 'mmHg', 'PaO2', 1, '2703-7', 'DEFAULT', 'PaO2 [mmHg]'
) t;
commit;
-- Use the concept_dimension table to get an expanded list of local lab codes (optional).
-- This will find paths corresponding to concepts already in the fource_lab_map table,
-- and then find all the concepts corresponding to child paths. Make sure you update the
-- scale_factor, local_lab_units, and local_lab_name as needed.
-- WARNING: This query might take several minutes to run.
-- ****THIS IS UNTESTED*****
/*
insert into fource_lab_map
select distinct l.fource_loinc, l.fource_lab_units, l.fource_lab_name, l.scale_factor, d.concept_cd, l.local_lab_units, l.local_lab_name
from fource_lab_map l
inner join @crcSchema.concept_dimension c
on l.local_lab_code = c.concept_cd
inner join @crcSchema.concept_dimension d
on d.concept_path like c.concept_path || '%'
where not exists (
select *
from fource_lab_map t
where t.fource_loinc = l.fource_loinc and t.local_lab_code = d.concept_cd
)
*/
-- Use the concept_dimension table to get the local names for labs (optional).
/*
update l
set l.local_lab_name = c.name_char
from fource_lab_map l
inner join @crcSchema.concept_dimension c
on l.local_lab_code = c.concept_cd
*/
--------------------------------------------------------------------------------
-- Lab mappings report (for debugging lab mappings)
--------------------------------------------------------------------------------
-- Get a list of all the codes and units in the data for 4CE labs since 1/1/2019
create table fource_lab_units_facts (
fact_code varchar(50) not null,
fact_units varchar(50),
num_facts int,
mean_value numeric(18,5),
stdev_value numeric(18,5)
);
--188s
create index fource_lap_map_ndx on fource_lab_map(local_lab_code);
insert into fource_lab_units_facts
select * from (
with labs_in_period as (
select concept_cd, units_cd, nval_num
from @crcSchema.observation_fact f
join fource_lab_map m on m.local_lab_code = f.concept_cd
where trunc(start_date) >= (select trunc(start_date) from fource_config where rownum = 1)
)
select concept_cd, units_cd, count(*) num_facts, avg(nval_num) avg_val, stddev(nval_num) stdev_val
from labs_in_period
group by concept_cd, units_cd);
commit;
--select * from fource_lab_units_facts;
/*
insert into fource_lab_units_facts
select concept_cd, units_cd, count(*), avg(nval_num), stddev(nval_num)
from @crcSchema.observation_fact f
join fource_lab_map m on m.local_lab_code = f.concept_cd
where trunc(start_date) >= (select trunc(start_date) from fource_config where rownum = 1)
group by concept_cd, units_cd;
*/
-- Create a table that stores a report about local lab units
--drop table fource_lab_map_report;
create table fource_lab_map_report (
fource_loinc varchar(20) not null,
fource_lab_units varchar(20),
fource_lab_name varchar(100),
scale_factor float,
local_lab_code varchar(50) not null,
local_lab_units varchar(20) not null,
local_lab_name varchar(500),
num_facts int,
mean_value numeric(18,5),
stdev_value numeric(18,5),
notes varchar(1000)
)
;
alter table fource_lab_map_report add primary key (fource_loinc, local_lab_code, local_lab_units);
-- Compare the fource_lab_map table to the codes and units in the data
insert into fource_lab_map_report
select
nvl(m.fource_loinc,a.fource_loinc) fource_loinc,
nvl(m.fource_lab_units,a.fource_lab_units) fource_lab_units,
nvl(m.fource_lab_name,a.fource_lab_name) fource_lab_name,
nvl(m.scale_factor,0) scale_factor,
nvl(m.local_lab_code,f.fact_code) local_lab_code,
coalesce(m.local_lab_units,f.fact_units,'((null))') local_lab_units,
nvl(m.local_lab_name,'((missing))') local_lab_name,
nvl(f.num_facts,0) num_facts,
nvl(f.mean_value,-999) mean_value,
nvl(f.stdev_value,-999) stdev_value,
(case when scale_factor is not null and num_facts is not null then 'GOOD: Code and units found in the data'
when m.fource_loinc is not null and c.fact_code is null then 'WARNING: This code from the lab mappings table could not be found in the data -- double check if you use another loinc or local code'
when scale_factor is not null then 'WARNING: These local_lab_units in the lab mappings table could not be found in the data '
else 'WARNING: These local_lab_units exist in the data but are missing from the lab mappings table -- map to the 4CE units using scale factor'
end) notes
from fource_lab_map m
full outer join fource_lab_units_facts f
on f.fact_code=m.local_lab_code and nvl(nullif(f.fact_units,''),'DEFAULT')=m.local_lab_units
left outer join (
select distinct fource_loinc, fource_lab_units, fource_lab_name, local_lab_code
from fource_lab_map
) a on a.local_lab_code=f.fact_code
left outer join (
select distinct fact_code from fource_lab_units_facts
) c on m.local_lab_code=c.fact_code;
commit;
--select * from fource_lab_map_report;
-- View the results, including counts, to help you check your mappings (optional)
/*
select * from fource_lab_map_report order by fource_loinc, num_facts desc
*/
--------------------------------------------------------------------------------
-- Medication mappings
-- * Do not change the med_class or add additional medications.
-- * The ATC and RxNorm codes represent the same list of medications.
-- * Use ATC and/or RxNorm, depending on what your institution uses.
--------------------------------------------------------------------------------
--drop table fource_med_map;
create table fource_med_map (
med_class varchar(50) not null,
code_type varchar(10) not null,
local_med_code varchar(50) not null
)
;
alter table fource_med_map add primary key (med_class, code_type, local_med_code);
-- ATC codes (optional)
insert into fource_med_map
select m, 'ATC' t, 'ATC:' || c -- Change "ATC:" to your local ATC code prefix (scheme)
from (
-- Don't add or remove drugs
select 'ACEI' m, c from (select 'C09AA01' c from dual union select 'C09AA02' from dual union select 'C09AA03' from dual
union select 'C09AA04' from dual union select 'C09AA05' from dual union select 'C09AA06' from dual union select 'C09AA07' from dual
union select 'C09AA08' from dual union select 'C09AA09' from dual union select 'C09AA10' from dual union select 'C09AA11' from dual
union select 'C09AA13' from dual union select 'C09AA15' from dual union select 'C09AA16' from dual) t
union
select 'ARB', c from (select 'C09CA01' c from dual union select 'C09CA02' from dual union select 'C09CA03' from dual
union select 'C09CA04' from dual union select 'C09CA06' from dual union select 'C09CA07' from dual
union select 'C09CA08' from dual) t
union
select 'COAGA', c from (select 'B01AC04' c from dual union select 'B01AC05' from dual union select 'B01AC07' from dual
union select 'B01AC10' from dual union select 'B01AC13' from dual union select 'B01AC16' from dual
union select 'B01AC17' from dual union select 'B01AC22' from dual union select 'B01AC24' from dual
union select 'B01AC25' from dual union select 'B01AC26' from dual) t
union
select 'COAGB', c from (select 'B01AA01' c from dual union select 'B01AA03' from dual
union select 'B01AA04' from dual union select 'B01AA07' from dual
union select 'B01AA11' from dual union select 'B01AB01' from dual
union select 'B01AB04' from dual union select 'B01AB05' from dual
union select 'B01AB06' from dual union select 'B01AB07' from dual
union select 'B01AB08' from dual union select 'B01AB10' from dual
union select 'B01AB12' from dual union select 'B01AE01' from dual
union select 'B01AE02' from dual union select 'B01AE03' from dual
union select 'B01AE06' from dual union select 'B01AE07' from dual
union select 'B01AF01' from dual union select 'B01AF02' from dual
union select 'B01AF03' from dual union select 'B01AF04' from dual
union select 'B01AX05' from dual union select 'B01AX07' from dual) t
union
select 'COVIDVIRAL', c from (select 'J05AE10' c from dual union select 'J05AP01' from dual union select 'J05AR10' from dual) t
union
select 'DIURETIC', c from (select 'C03CA01' c from dual union select 'C03CA02' from dual
union select 'C03CA03' from dual union select 'C03CA04' from dual
union select 'C03CB01' from dual union select 'C03CB02' from dual union select 'C03CC01' from dual) t
union
select 'HCQ', c from (select 'P01BA01' c from dual union select 'P01BA02' from dual) t
union
select 'ILI', c from (select 'L04AC03' c from dual union select 'L04AC07' from dual
union select 'L04AC11' from dual union select 'L04AC14' from dual) t
union
select 'INTERFERON', c from (select 'L03AB08' c from dual union select 'L03AB11' from dual) t
union
select 'SIANES', c from (select 'M03AC03' c from dual union select 'M03AC09' from dual
union select 'M03AC11' from dual union select 'N01AX03' from dual
union select 'N01AX10' from dual union select 'N05CD08' from dual union select 'N05CM18' from dual) t
union
select 'SICARDIAC', c from (select 'B01AC09' c from dual union select 'C01CA03' from dual
union select 'C01CA04' from dual union select 'C01CA06' from dual union select 'C01CA07' from dual
union select 'C01CA24' from dual union select 'C01CE02' from dual union select 'C01CX09' from dual
union select 'H01BA01' from dual union select 'R07AX01' from dual) t
) t;
commit;
-- RxNorm codes (optional)
insert into fource_med_map
select m, 'RxNorm' t, 'RXNORM:' || c -- Change "RxNorm:" to your local RxNorm code prefix (scheme)
from (
-- Don't add or remove drugs
select 'ACEI' m, c from
(select '36908' c from dual
union select '39990' from dual
union select '104375' from dual
union select '104376' from dual
union select '104377' from dual
union select '104378' from dual
union select '104383' from dual
union select '104384' from dual
union select '104385' from dual
union select '1299896' from dual
union select '1299897' from dual
union select '1299963' from dual
union select '1299965' from dual
union select '1435623' from dual
union select '1435624' from dual
union select '1435630' from dual
union select '1806883' from dual
union select '1806884' from dual
union select '1806890' from dual
union select '18867' from dual
union select '197884' from dual
union select '198187' from dual
union select '198188' from dual
union select '198189' from dual
union select '199351' from dual
union select '199352' from dual
union select '199353' from dual
union select '199622' from dual
union select '199707' from dual
union select '199708' from dual
union select '199709' from dual
union select '1998' from dual
union select '199816' from dual
union select '199817' from dual
union select '199931' from dual
union select '199937' from dual
union select '205326' from dual
union select '205707' from dual
union select '205778' from dual
union select '205779' from dual
union select '205780' from dual
union select '205781' from dual
union select '206277' from dual
union select '206313' from dual
union select '206764' from dual
union select '206765' from dual
union select '206766' from dual
union select '206771' from dual
union select '207780' from dual
union select '207792' from dual
union select '207800' from dual
union select '207820' from dual
union select '207891' from dual
union select '207892' from dual
union select '207893' from dual
union select '207895' from dual
union select '210671' from dual
union select '210672' from dual
union select '210673' from dual
union select '21102' from dual
union select '211535' from dual
union select '213482' from dual
union select '247516' from dual
union select '251856' from dual
union select '251857' from dual
union select '260333' from dual
union select '261257' from dual
union select '261258' from dual
union select '261962' from dual
union select '262076' from dual
union select '29046' from dual
union select '30131' from dual
union select '308607' from dual
union select '308609' from dual
union select '308612' from dual
union select '308613' from dual
union select '308962' from dual
union select '308963' from dual
union select '308964' from dual
union select '310063' from dual
union select '310065' from dual
union select '310066' from dual
union select '310067' from dual
union select '310422' from dual
union select '311353' from dual
union select '311354' from dual
union select '311734' from dual
union select '311735' from dual
union select '312311' from dual
union select '312312' from dual
union select '312313' from dual
union select '312748' from dual
union select '312749' from dual
union select '312750' from dual
union select '313982' from dual
union select '313987' from dual
union select '314076' from dual
union select '314077' from dual
union select '314203' from dual
union select '317173' from dual
union select '346568' from dual
union select '347739' from dual
union select '347972' from dual
union select '348000' from dual
union select '35208' from dual
union select '35296' from dual
union select '371001' from dual
union select '371254' from dual
union select '371506' from dual
union select '372007' from dual
union select '372274' from dual
union select '372614' from dual
union select '372945' from dual
union select '373293' from dual
union select '373731' from dual
union select '373748' from dual
union select '373749' from dual
union select '374176' from dual
union select '374177' from dual
union select '374938' from dual
union select '378288' from dual
union select '3827' from dual
union select '38454' from dual
union select '389182' from dual
union select '389183' from dual
union select '389184' from dual
union select '393442' from dual
union select '401965' from dual
union select '401968' from dual
union select '411434' from dual
union select '50166' from dual
union select '542702' from dual
union select '542704' from dual
union select '54552' from dual
union select '60245' from dual
union select '629055' from dual
union select '656757' from dual
union select '807349' from dual
union select '845488' from dual
union select '845489' from dual
union select '854925' from dual
union select '854927' from dual
union select '854984' from dual
union select '854986' from dual
union select '854988' from dual
union select '854990' from dual
union select '857169' from dual
union select '857171' from dual
union select '857183' from dual
union select '857187' from dual
union select '857189' from dual
union select '858804' from dual
union select '858806' from dual
union select '858810' from dual
union select '858812' from dual
union select '858813' from dual
union select '858815' from dual
union select '858817' from dual
union select '858819' from dual
union select '858821' from dual
union select '898687' from dual
union select '898689' from dual
union select '898690' from dual
union select '898692' from dual
union select '898719' from dual
union select '898721' from dual
union select '898723' from dual
union select '898725' from dual) t
union select 'ARB' m, c from
(select '118463' c from dual
union select '108725' from dual
union select '153077' from dual
union select '153665' from dual
union select '153666' from dual
union select '153667' from dual
union select '153821' from dual
union select '153822' from dual
union select '153823' from dual
union select '153824' from dual
union select '1996253' from dual
union select '1996254' from dual
union select '199850' from dual
union select '199919' from dual
union select '200094' from dual
union select '200095' from dual
union select '200096' from dual
union select '205279' from dual
union select '205304' from dual
union select '205305' from dual
union select '2057151' from dual
union select '2057152' from dual
union select '2057158' from dual
union select '206256' from dual
union select '213431' from dual
union select '213432' from dual
union select '214354' from dual
union select '261209' from dual
union select '261301' from dual
union select '282755' from dual
union select '284531' from dual
union select '310139' from dual
union select '310140' from dual
union select '311379' from dual
union select '311380' from dual
union select '314073' from dual
union select '349199' from dual
union select '349200' from dual
union select '349201' from dual
union select '349483' from dual
union select '351761' from dual
union select '351762' from dual
union select '352001' from dual
union select '352274' from dual
union select '370704' from dual
union select '371247' from dual
union select '372651' from dual
union select '374024' from dual
union select '374279' from dual
union select '374612' from dual
union select '378276' from dual
union select '389185' from dual
union select '484824' from dual
union select '484828' from dual
union select '484855' from dual
union select '52175' from dual
union select '577776' from dual
union select '577785' from dual
union select '577787' from dual
union select '598024' from dual
union select '615856' from dual
union select '639536' from dual
union select '639537' from dual
union select '639539' from dual
union select '639543' from dual
union select '69749' from dual
union select '73494' from dual
union select '83515' from dual
union select '83818' from dual
union select '979480' from dual
union select '979482' from dual
union select '979485' from dual
union select '979487' from dual
union select '979492' from dual
union select '979494' from dual) t
union select 'COAGA' m, c from
(select '27518' c from dual
union select '10594' from dual
union select '108911' from dual
union select '1116632' from dual
union select '1116634' from dual
union select '1116635' from dual
union select '1116639' from dual
union select '1537034' from dual
union select '1537038' from dual
union select '1537039' from dual
union select '1537045' from dual
union select '1656052' from dual
union select '1656055' from dual
union select '1656056' from dual
union select '1656061' from dual
union select '1656683' from dual
union select '1666332' from dual
union select '1666334' from dual
union select '1736469' from dual
union select '1736470' from dual
union select '1736472' from dual
union select '1736477' from dual
union select '1736478' from dual
union select '1737465' from dual
union select '1737466' from dual
union select '1737468' from dual
union select '1737471' from dual
union select '1737472' from dual
union select '1812189' from dual
union select '1813035' from dual
union select '1813037' from dual
union select '197622' from dual
union select '199314' from dual
union select '200348' from dual
union select '200349' from dual
union select '205253' from dual
union select '206714' from dual
union select '207569' from dual
union select '208316' from dual
union select '208558' from dual
union select '213169' from dual
union select '213299' from dual
union select '241162' from dual
union select '261096' from dual
union select '261097' from dual
union select '309362' from dual
union select '309952' from dual
union select '309953' from dual
union select '309955' from dual
union select '313406' from dual
union select '32968' from dual
union select '333833' from dual
union select '3521' from dual
union select '371917' from dual
union select '374131' from dual
union select '374583' from dual
union select '375035' from dual
union select '392451' from dual
union select '393522' from dual
union select '613391' from dual
union select '73137' from dual
union select '749196' from dual
union select '749198' from dual
union select '75635' from dual
union select '83929' from dual
union select '855811' from dual
union select '855812' from dual
union select '855816' from dual
union select '855818' from dual
union select '855820' from dual) t
union select 'COAGB' m, c from
(select '2110605' c from dual
union select '237057' from dual
union select '69528' from dual
union select '8150' from dual
union select '163426' from dual
union select '1037042' from dual
union select '1037044' from dual
union select '1037045' from dual
union select '1037049' from dual
union select '1037179' from dual
union select '1037181' from dual
union select '1110708' from dual
union select '1114195' from dual
union select '1114197' from dual
union select '1114198' from dual
union select '1114202' from dual
union select '11289' from dual
union select '114934' from dual
union select '1232082' from dual
union select '1232084' from dual
union select '1232086' from dual
union select '1232088' from dual
union select '1241815' from dual
union select '1241823' from dual
union select '1245458' from dual
union select '1245688' from dual
union select '1313142' from dual
union select '1359733' from dual
union select '1359900' from dual
union select '1359967' from dual
union select '1360012' from dual
union select '1360432' from dual
union select '1361029' from dual
union select '1361038' from dual
union select '1361048' from dual
union select '1361226' from dual
union select '1361568' from dual
union select '1361574' from dual
union select '1361577' from dual
union select '1361607' from dual
union select '1361613' from dual
union select '1361615' from dual
union select '1361853' from dual
union select '1362024' from dual
union select '1362026' from dual
union select '1362027' from dual
union select '1362029' from dual
union select '1362030' from dual
union select '1362048' from dual
union select '1362052' from dual
union select '1362054' from dual
union select '1362055' from dual
union select '1362057' from dual
union select '1362059' from dual
union select '1362060' from dual
union select '1362061' from dual
union select '1362062' from dual
union select '1362063' from dual
union select '1362065' from dual
union select '1362067' from dual