This repository has been archived by the owner on May 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path00_basic_tables_update.R
839 lines (626 loc) · 19 KB
/
00_basic_tables_update.R
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
#--------------------------------------------------------------------------
#
# Program: 01_basic_tables.R
# Author: Patrick Rockenschaub
# Date: 20/07/2018
#
# Purpose: Create database objects of all the basic tables in the database
# to access, manipulate and join them from within R
#
# NOTE: Updated for the complete data extract
#
#--------------------------------------------------------------------------
# Helper tables -----------------------------------------------------------
in_date <- function(sql_tbl, start = "", end = ""){
# Limit a lazy sql table created with one of the below functions to a
# given time period. Start and/or end dates can be used. The complete
# table can be requested using collect().
#
# Args:
# sql_tbl - lazy table to limit
# start - follow up start date (use "" to ignore)
# end - follow up end date (use "" to ignore)
#
# Result:
# All table records in defined range as a lazy tbl
if("eventdate" %in% op_vars(sql_tbl)){
# table is a CPRD table
sql_tbl %>%
filter(
start == "" | eventdate >= start,
end == "" | eventdate <= end
)
} else if ("admidate" %in% op_vars(sql_tbl)){
# table is a HES table
sql_tbl %>%
filter(
start == "" | discharged >= start ,
end == "" | admidate <= end
)
} else if ("arrivaldate" %in% op_vars(sql_tbl)){
# table is a HES A&E table
sql_tbl %>%
filter(
start == "" | arrivaldate >= start,
end == "" | arrivaldate <= end
)
} else if (all(c("start_date", "end_date") %in% op_vars(sql_tbl))){
# table is a patient table
sql_tbl %>%
mutate(
# Patient can only enter the cohort once they have been registered
# for a year
enter_date =
dbplyr::sql(max_db(start, "DATEADD(year, 1, start_date)")),
leave_date =
dbplyr::sql(min_db(end , "end_date"))
) %>%
filter(enter_date <= leave_date)
} else {
stop(bl("Table must contain an eventdate/start_date/end_date",
"field (CPRD) or admidate/discharged/arrivaldate fields",
"(HES)"))
}
}
first_record <- function(sql_tbl, by = NULL){
# Obtain the first record of each patient in a database table, as defined
# by the eventdate
#
# Args:
# sql_tbl - lazy table from which to determine the first record
#
# Result:
# All first records as a lazy table
sql_tbl %>%
group_by_(.dots = c("patid", by)) %>%
filter(eventdate == min(eventdate, na.rm = TRUE)) %>%
distinct()
}
last_record <- function(sql_tbl, by = NULL){
# Obtain the last record of each patient in a database table, as defined
# by the eventdate
#
# Args:
# sql_tbl - lazy table from which to determine the first record
# by - optional further grouping variables, default none
#
# Result:
# All last records as a lazy table
sql_tbl %>%
group_by_(.dots = c("patid", by)) %>%
filter(eventdate == max(eventdate, na.rm = TRUE)) %>%
distinct()
}
tbl_exists <- function(name){
# Determine whether a table exists in the database. Also handles
# temporary tables prefixed with ##
#
# Args:
# name - Name of the table (including ## for temp tables)
#
# Result:
# TRUE if the table exists in the database, FALSE if not
if(length(name) > 1){
stop("Only supply one table name at a time.")
}
if(str_detect(name, "^##")){
# Temporary tables can't be detected by db_has_table()
error <- try(tbl(get_db_conn(), name), silent = TRUE)
return(class(error)[1] != "try-error")
}
db_has_table(get_db_conn(), name)
}
remove_temporary <- function(names){
# Remove tables if the table currently exists in the database
#
# Args:
# names - a character vector of table names starting with "##"
#
# Result:
# NULL
if(!all(str_detect(names, "^##"))){
stop("Only provide names suffixed with ## (i.e. no permanent tables")
}
exist <- map_lgl(names, tbl_exists)
walk(names[exist], dbRemoveTable, conn = get_db_conn())
}
# Lookup tables -----------------------------------------------------------
medical <- function(){
# Select the complete Read code dictionary in the database. The complete
# table can be requested using collect().
#
# Args:
#
# Result:
# The complete Read code dictionary as a lazy tbl
check_connection()
tbl(get_db_conn(), "lu_medical")
}
product <- function(){
# Select a copy of the product lookup table in the database. The complete
# table can be requested using collect().
#
# Args:
#
# Result:
# Product information as a lazy tbl
check_connection()
tbl(get_db_conn(), "lu_product")
}
product_name <- function(){
# Select only the columns prodcode and productname from the product
# lookup table in the database. The complete table can be requested using
# collect().
#
# Args:
#
# Result:
# Limited product information as a lazy tbl
product() %>%
dplyr::select(prodcode, productname)
}
product_bnf <- function(){
# Select only the columns prodcode and bnfcode from the product
# lookup table in the database. The complete table can be requested using
# collect().
#
# Args:
#
# Result:
# Limited product information as a lazy tbl
product() %>%
dplyr::select(prodcode, bnfcode)
}
antibiotics <- function(){
# Select the lookup table for antibiotic definitions and classifications.
# the complete table can be requested using collect().
#
# Args:
#
# Result:
# Antibiotic classifications as a lazy tbl
check_connection()
tbl(get_db_conn(), "codes_abx")
}
# Raw tables --------------------------------------------------------------
practice_db <- function(){
# Select the additional practice information in the database. The
# complete table can be requested using collect().
#
# Args:
#
# Result:
# The basic practice information in the database
check_connection()
all_patients_db() %>%
rename(uts = prac_uts, lcd = prac_lcd, region = prac_region) %>%
group_by(pracid, uts, lcd, region) %>%
summarise(linked = max(hes15, na.rm = TRUE)) %>% # has linkage if one is linked
ungroup()
}
consult_db <- function(){
# Select all consultation information in the database. The
# complete table can be requested using collect().
#
# Args:
#
# Result:
# The raw consultation information in the database (BIG!)
check_connection()
tbl(get_db_conn(), "consultation")
}
clinical_db <- function(){
# Select all clinical information in the database. The
# complete table can be requested using collect().
#
# Args:
#
# Result:
# The raw clinical information in the database (BIG!)
check_connection()
tbl(get_db_conn(), "clinical")
}
test_db <- function(){
# Select all diagnostic test information in the database.
# The complete table can be requested using collect().
#
# Args:
#
# Result:
# The raw test information in the database (BIG!)
check_connection()
tbl(get_db_conn(), "test")
}
immun_db <- function(){
# Select all immunisation information in the database.
# The complete table can be requested using collect().
#
# Args:
#
# Result:
# The raw immunisation information in the database (BIG!)
check_connection()
tbl(get_db_conn(), "immunisation")
}
referral_db <- function(){
# Select all referral information in the database.
# The complete table can be requested using collect().
#
# Args:
#
# Result:
# The raw immunisation information in the database (BIG!)
check_connection()
tbl(get_db_conn(), "referral")
}
therapy_db <- function(){
# Select all therapy information in the database. The
# complete table can be requested using collect().
#
# Args:
#
# Result:
# The raw therapy information in the database (BIG!)
check_connection()
tbl(get_db_conn(), "therapy")
}
# Code lists --------------------------------------------------------------
codes_abx_db <- function(){
# Get all types of antibiotics in the database.
#
# Args:
#
#
# Result:
# A lazy table with all antibiotic definitions in the database
check_connection()
tbl(get_db_conn(), "codes_abx")
}
codes_qof_db <- function(){
# Get all QOF Read codes in the database.
#
# Args:
#
#
# Result:
# A lazy table with all QOF Read codes in the database
check_connection()
tbl(get_db_conn(), "codes_qof")
}
codes_obese_db <- function(){
# Get all obesity related Read codes in the database.
#
# Args:
#
#
# Result:
# A lazy table with all obesity codes in the database
check_connection()
tbl(get_db_conn(), "codes_obese")
}
codes_smoke_db <- function(){
# Get all smoking related Read codes in the database.
#
# Args:
#
#
# Result:
# A lazy table with all smoke codes in the database
check_connection()
tbl(get_db_conn(), "codes_smoke")
}
# Views to speed up computation -------------------------------------------
qof_db <- function(){
# Select the view in the database that contains all the QOF codes in the
# clinical table and the test table
#
# Args:
#
#
# Result:
# The entire view in the database.
check_connection()
tbl(get_db_conn(), "vw_qof")
}
infect_db <- function(){
# Select the view in the database that contains all the infection codes in
# the clinical table and the test table
#
# Args:
#
#
# Result:
# The entire view in the database.
check_connection()
tbl(get_db_conn(), "vw_infect")
}
indication_db <- function(){
# Select the view in the database that contains all the infection codes in
# the clinical table and the test table
#
# Args:
#
#
# Result:
# The entire view in the database.
check_connection()
dplyr::union(
tbl(get_db_conn(), "vw_same_day_or_cons_clinical"),
tbl(get_db_conn(), "vw_same_day_or_cons_test")
)
}
abx_db <- function(){
# Select the view in the database that contains all the antibiotic codes
# in the therapy table
#
# Args:
#
#
# Result:
# The entire view in the database.
check_connection()
tbl(get_db_conn(), "vw_abx")
}
obese_db <- function(){
# Select the helper table in the database that contains all obesity
# records from the clinical table
#
# Args:
#
#
# Result:
# The entire table in the database.
check_connection()
tbl(get_db_conn(), "vw_clinical_obese_codes") %>%
select(-num_rows)
}
bmi_db <- function(){
# Select the views in the table that contain height and weight
# and combine them. Needs to be post-processed in R, as height and
# weight must be carried forward if they weren't measured on the
# same day.
#
# Args:
#
#
# Result:
# The combined table in the database.
check_connection()
weight <-
tbl(get_db_conn(), "vw_clinical_weight") %>%
select(patid:staffid, weight = data1, bmi = data3)
height <-
tbl(get_db_conn(), "vw_clinical_height") %>%
select(patid, eventdate, height = data1)
full_join(weight, height, by = c("patid", "eventdate"))
}
smoke_db <- function(){
# Select the helper table in the database that contains all smoke status
# records from the clinical table
#
# Args:
#
#
# Result:
# The entire table in the database.
check_connection()
tbl(get_db_conn(), "vw_clinical_smoke")
}
# Cohorts -----------------------------------------------------------------
all_patients_db <- function(){
# Select all patients in the database. This includes non-HES linked
# patients, who were not part of the original study population. Use for
# sensitivity analysis only. The complete table can be requested using
# collect().
#
# Args:
#
# Result:
# The basic cohort definition in the database
check_connection()
tbl(get_db_conn(), "cohort") %>%
mutate(
birth_date = dplyr::sql("DATEFROMPARTS(YEAR(dob), 7, 1)"),
female = gender - 1
) %>%
rename(start_date = data_start, end_date = data_end)
}
cohort_full_db <- function(){
# Select the main study population in the database. The complete table
# can be requested using collect().
#
# Args:
#
# Result:
# The basic cohort definition in the database
all_patients_db() %>%
filter(hes15 == 1, death15 == 1, lsoa15 == 1) %>%
left_join(death_db(), by = c("patid", "pracid")) %>%
select(-gen_death_id, -dod_partial) %>%
mutate (# Make sure to set end date to death date confirmed by ONS
end_date = dplyr::sql("IIF(dod IS NOT NULL and dod < end_date, dod, end_date)")
) %>%
rename(death_date = dod)
}
study_population_db <- function(link = TRUE){
# Select the main study population in the database (either HES or all).
# The complete table can be requested using collect().
#
# NOTE: ONS death date is used to ascertain death date in linked cohort.
#
# Args:
# link - if true, only linked patients are selected
#
# Result:
# The basic cohort definition in the database
cols <- c("patid", "pracid", "female", "birth_date",
"start_date", "end_date")
if(link){
cols <- c(cols, "death_date")
tab <- cohort_full_db()
} else {
cols <- c(cols, "hes15", "death15", "lsoa15")
tab <- all_patients_db()
}
tab %>% dplyr::select_(.dots = cols)
}
# Antibiotics -------------------------------------------------------------
filter_bnf <- function(sql_tbl, bnf, include = TRUE){
# Helper function to filter the BNF chapters in the database.
#
# Args:
# sql_tbl - a table object still hold in the database
# bnf - a single character string containing the bnf chapter
# include - a logical value indicating whether the chapter should
# be kept or removed
#
# Returns:
# a tbl_sql object with the filter applied
if(!inherits(sql_tbl, "tbl_sql")){
stop("Parameter sql_tbl must be of class tbl_sql.")
}
pos <- (0:9) * 9 + 1
if(include){
filter(sql_tbl, CHARINDEX(bnf, bnfcode) %in% pos)
} else {
filter(sql_tbl, !(CHARINDEX(bnf, bnfcode) %in% pos))
}
}
abx_bnf_db <- function(include, exclude = NULL){
# Select all antibiotic prescriptions in the database that match one
# inclusion chapter and potentially multiple exclusion chapters.
#
# Args:
# include - a BNF chapter string to be kept
# exclude - one or more subchapters to exclude. Leave as NULL if
# no chapter should be excluded
#
# Returns:
# a tbl_sql object with the filter applied
res <-
abx_db() %>%
inner_join(product_bnf(), by = "prodcode") %>%
filter_bnf(include)
for(code in exclude){
res <- filter_bnf(res, code, include = FALSE)
}
res
}
# Diagnoses -----------------------------------------------------------
records_db <- function(sql_tbl, code_dt, id = "medcode"){
# Select records in the database based on some form of codelist. The
# complete table can be requested using collect().
#
# Args:
# sql_tbl - a object of tbl_sql in which to search for the codes
# code_dt - a tbl_sql or data.table with a relevant id column
#
# Result:
# All records as a lazy query
check_connection()
if("tbl_sql" %in% class(code_dt)){
# If the codes are already in the database
codes <- code_dt
} else if ("data.frame" %in% class(code_dt)){
# If the codes are in a data.frame
tmp <- digest::sha1(code_dt)
# If the table has not been computed previously, compute now
if(!tbl_exists(paste0("##", tmp))){
codes <- copy_to(get_db_conn(), code_dt, tmp)
} else {
codes <- tbl(get_db_conn(), paste0("##", tmp))
}
} else {
stop("Only tbl_sql or data.frame objects allowed for code_dt")
}
semi_join(sql_tbl, codes, by = id)
}
# Hospital ----------------------------------------------------------------
# NOTE: this section is still using old tables in the database
admimeth <- function(){
# Table that contains the mapping to admission methods lookup table
#
# Args:
#
# Result:
#
check_connection()
tbl(get_db_conn(), "admimeth")
}
admissions_db <- function(){
# Select all hospital admission information from HES.
#
# Args:
#
# Result:
# All hospital admission information as a lazy tbl
check_connection()
tbl(get_db_conn(), "inpat_admission") %>%
inner_join(admimeth(), by = "admimeth") %>%
dplyr::select(-admimeth, -admidesc)
}
emergency_db <- function(){
# Select all hospital admissions from HES that are marked as emergency
# admissions.
#
# Args:
#
# Result:
# All emergency hospital admission information as a lazy tbl
admissions_db() %>%
filter(admitype == "Emergency")
}
elective_db <- function(){
# Select all hospital admissions from HES that are marked as elective
# admissions.
#
# Args:
#
# Result:
# All elective hospital admission information as a lazy tbl
admissions_db() %>%
filter(admitype == "Elective")
}
hosp_diag_db <- function(){
# Select all hospital diagnoses (only infections) from HES.
#
# Args:
#
# Result:
# All hospital infection diagnosis information
check_connection()
tbl(get_db_conn(), "inpat_diagnosis")
}
ae_db <- function(){
# Select all A&E attendances from HES.
#
# Args:
#
# Result:
# All hospital attendances as a lazy tbl
check_connection()
tbl(get_db_conn(), "ae_attendance") %>%
select(-aekey, -ethnic)
}
# Others ------------------------------------------------------------------
imd_db <- function(){
# Select all records for the Index of Multiple Deprivation in the
# database. The complete table can be requested using collect().
#
# Args:
#
# Result:
# All IMD records as a lazy tbl
tbl(get_db_conn(), "imd")
}
death_db <- function(){
# Select all death records in the database. The complete table can be
# requested using collect().
#
# Args:
#
# Result:
# All IMD records as a lazy tbl
check_connection()
tbl(get_db_conn(), "death")
}