Last updated: 2022-06-14

Checks: 5 2

Knit directory: codemapper_notes/

This reproducible R Markdown analysis was created with workflowr (version 1.7.0). The Checks tab describes the reproducibility checks that were applied when the results were created. The Past versions tab lists the development history.


The R Markdown is untracked by Git. To know which version of the R Markdown file created these results, you’ll want to first commit it to the Git repo. If you’re still working on the analysis, you can ignore this warning. When you’re finished, you can run wflow_publish to commit the R Markdown file and build the HTML.

The global environment had objects present when the code in the R Markdown file was run. These objects can affect the analysis in your R Markdown file in unknown ways. For reproduciblity it’s best to always run the code in an empty environment. Use wflow_publish or wflow_build to ensure that the code is always run in an empty environment.

The following objects were defined in the global environment when these results were created:

Name Class Size
install_codemapper function 1.2 Kb

The command set.seed(20210923) was run prior to running the code in the R Markdown file. Setting a seed ensures that any results that rely on randomness, e.g. subsampling or permutations, are reproducible.

Great job! Recording the operating system, R version, and package versions is critical for reproducibility.

Nice! There were no cached chunks for this analysis, so you can be confident that you successfully produced the results during this run.

Great job! Using relative paths to the files within your workflowr project makes it easier to run your code on other machines.

Great! You are using Git for version control. Tracking code development and connecting the code version to the results is critical for reproducibility.

The results in this page were generated with repository version 195150a. See the Past versions tab to see a history of the changes made to the R Markdown and HTML files.

Note that you need to be careful to ensure that all relevant files for the analysis have been committed to Git prior to generating the results (you can use wflow_publish or wflow_git_commit). workflowr only checks the R Markdown file, but you know if there are other scripts or data files that it depends on. Below is the status of the Git repository when the results were generated:


Ignored files:
    Ignored:    .DS_Store
    Ignored:    .Renviron
    Ignored:    .Rhistory
    Ignored:    .Rproj.user/
    Ignored:    _targets/meta/process
    Ignored:    _targets/meta/progress
    Ignored:    _targets/objects/
    Ignored:    _targets/user/
    Ignored:    all_lkps_maps.db
    Ignored:    renv/library/
    Ignored:    renv/staging/
    Ignored:    tar_make.R

Untracked files:
    Untracked:  analysis/ukb_self_report_non_cancer_illness_caliber_mapping.Rmd
    Untracked:  analysis/ukb_self_report_operations_caliber_mapping.Rmd
    Untracked:  data_small/ukb_self_report_operations_to_caliber_map.csv
    Untracked:  data_small/ukb_self_report_operations_to_caliber_map_raw.csv

Unstaged changes:
    Modified:   _targets.R
    Modified:   _targets/meta/meta
    Modified:   analysis/index.Rmd
    Deleted:    analysis/ukb_self_report_caliber_mapping.Rmd

Note that any generated files, e.g. HTML, png, CSS, etc., are not included in this status report because it is ok for generated content to have uncommitted changes.


There are no past versions. Publish this analysis with wflow_publish() to start tracking its development.


library(tidyverse)
library(reactable)
library(readxl)
library(crosstalk)
library(targets)
library(codemapper)
library(flextable)

tar_load(ALL_LKPS_MAPS_DB)
con <- DBI::dbConnect(RSQLite::SQLite(), ALL_LKPS_MAPS_DB)
all_lkps_maps <- ukbwranglr::db_tables_to_list(con)

# read-opcs4 mapping tables
read_v2_opcs4 <- all_lkps_maps$read_v2_opcs4 %>% 
  collect()
read_ctv3_opcs4 <- all_lkps_maps$read_ctv3_opcs4 %>% 
  collect()

read_codes_that_map_to_opcs4 <- list(
  read2 = read_v2_opcs4,
  read3 = read_ctv3_opcs4
) %>% 
  map(~ unique(.x$read_code))

# caliber codes - filter for diseases including opcs4 codes, then filter for OPCS4/read codes only, then filter read codes for only those that can map to OPCS4
tar_load(caliber_codes)

caliber_codes_operations <- caliber_codes %>% 
  # diseases that include OPCS4 codes
  group_by(disease) %>% 
  mutate(disease_includes_opcs4 = case_when("opcs4" %in% code_type ~ TRUE,
                                    TRUE ~ FALSE)) %>% 
  ungroup() %>% 
  filter(disease_includes_opcs4) %>% 
  select(-disease_includes_opcs4) %>% 
  
  # filter for opcs4 codes, and read codes that can map to OPCS4
  filter(
    (code_type == "opcs4") |
      ((code_type == "read2") & (code %in% read_codes_that_map_to_opcs4$read2)) |
      ((code_type == "read3") & (code %in% read_codes_that_map_to_opcs4$read3))
  )

# self-reported UKB non-cancer illnesses
coding5 <- tar_read(ukb_codings) %>% 
  filter(Coding == "5")

Overview

Aim: to assign UKB self-reported operation codes to CALIBER conditions.

Approach:

  • Filter CALIBER codelists for diseases including OPCS4 codes, then filter for OPCS4 and Read codes only
  • Filter Read codes for only those in the read2/3-to-OPCS4 mapping tables
  • Manually review UKB self-reported operation codes and assign to CALIBER diseases (also manually review Read codes)

UKB self-reported operations lookup table

coding5 %>% 
  select(-Coding,
         data_coding_5 = Value,
         ukb_meaning = Meaning) %>% 
  reactable(filterable = TRUE,
            searchable = TRUE,
            paginationType = "jump",
            showPageSizeOptions = TRUE,
            resizable = TRUE,
            pageSizeOptions = c(10, 25, 50, 100, 200, 300, 350))

CALIBER diseases that include OPCS4 codes

caliber_codes_operations %>% 
  distinct(disease) %>% 
  reactable(filterable = TRUE,
            paginationType = "jump",
            showPageSizeOptions = TRUE,
            resizable = TRUE,
            pageSizeOptions = c(10, 30))

Manual mapping of UKB self-reported operations codes to CALIBER diseases

Write csv file for manual mapping

# write to csv
ukb_self_report_operations_to_caliber_map_unselected <-
  caliber_codes_operations %>%
  distinct(disease) %>%
  mutate(
    description = "",
    category = "UKB self-reported",
    code_type = "data_coding_5",
    code = "",
    author = "caliber"
  )

ukb_self_report_operations_to_caliber_map_unselected %>% 
  write_csv(here::here(file.path("data_small",
                                 "ukb_self_report_operations_to_caliber_map_raw.csv")))

# NOTE: when manually reviewing, save the csv as a separate file without the '_raw' suffix
# test that `ukb_self_report_non_cancer_to_caliber_map.csv` contains the same mappings as `ukb_self_report_non_cancer_to_caliber_map_raw.csv` (apart from the manuall mapping column) - RAISE ERROR AND HALT KNITTING IF NOT
ukb_self_report_operations_to_caliber_map <- read_csv(here::here(file.path("data_small",
                                                                           "ukb_self_report_operations_to_caliber_map.csv")),
                                                      col_types = readr::cols(.default = "c"))

assertthat::are_equal(
  x = ukb_self_report_operations_to_caliber_map %>% 
    distinct(disease) %>% 
    arrange(),
  y = ukb_self_report_operations_to_caliber_map_unselected %>% 
    distinct(disease) %>% 
    arrange(),
  msg = "Manually selected mapping csv does not match 'ukb_self_report_operations_to_caliber_map_raw.csv'. These should contain the same values under column 'disease'. Has the raw version been updated?"
)

# general validation checks
ukb_self_report_operations_to_caliber_map %>% 
  filter(selected == "Y") %>% 
  select(-selected) %>% 
  ukbwranglr::validate_clinical_codes()

Review examples of questionable mappings

CALIBER disease Potential issues
End stage renal disease Includes OPCS4 code for ‘Pre-transplantation of kidney work-up - recipient’ (‘M172’).
Glaucoma Includes procedures (such as laser treatments and operations on the iris) that are not covered by the UKB self-reported code ‘glaucoma surgery/trabeculectomy’.
Peripheral arterial disease Includes procedures (such as endarterectomy, embolectomy, reconstruction) that are not covered by the UKB self-reported codes ‘fem-pop bypass/leg artery bypass’ and ‘leg artery angioplasty +/- stent’.
list(caliber_codes_operations,
     ukb_self_report_operations_to_caliber_map) %>%
  bind_rows() %>%
  filter(code_type %in% c("opcs4",
                          "data_coding_5")) %>%
  filter(disease %in% c(
    "End stage renal disease",
    "Glaucoma",
    "Peripheral arterial disease"
  )) %>%
  select(all_of(names(ukbwranglr::example_clinical_codes()))) %>% 
  reactable(
    filterable = TRUE,
    paginationType = "jump",
    showPageSizeOptions = TRUE,
    resizable = TRUE,
    pageSizeOptions = c(10, 30)
  )

Manually annotated mapping file

ukb_self_report_operations_to_caliber_map %>% 
  mutate(selected = case_when(is.na(selected) ~ "N",
                              TRUE ~ selected)) %>% 
  reactable(filterable = TRUE,
            searchable = TRUE,
            paginationType = "jump",
            showPageSizeOptions = TRUE,
            pageSizeOptions = c(10, 25, 50))

Final operations-only CALIBER codelist

ukb_self_report_operations_to_caliber_map %>%
  filter(selected == "Y") %>%
  select(-selected) %>%
  bind_rows(caliber_codes_operations) %>% 
  arrange(disease,
          category,
          code_type) %>% 
    reactable(filterable = TRUE,
            searchable = TRUE,
            paginationType = "jump",
            showPageSizeOptions = TRUE,
            resizable = TRUE,
            pageSizeOptions = c(10, 25, 50, 100, 200, 300, 350))  

sessionInfo()
R version 4.2.0 (2022-04-22)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Big Sur/Monterey 10.16

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRlapack.dylib

locale:
[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8

attached base packages:
[1] stats     graphics  grDevices datasets  utils     methods   base     

other attached packages:
 [1] flextable_0.7.0       codemapper_0.0.0.9001 targets_0.12.0       
 [4] crosstalk_1.2.0       readxl_1.4.0          reactable_0.3.0      
 [7] forcats_0.5.1         stringr_1.4.0         dplyr_1.0.9          
[10] purrr_0.3.4           readr_2.1.2           tidyr_1.2.0          
[13] tibble_3.1.7          ggplot2_3.3.5         tidyverse_1.3.1      
[16] workflowr_1.7.0      

loaded via a namespace (and not attached):
 [1] fs_1.5.2              bit64_4.0.5           lubridate_1.8.0      
 [4] httr_1.4.2            rprojroot_2.0.3       tools_4.2.0          
 [7] backports_1.4.1       bslib_0.3.1           utf8_1.2.2           
[10] R6_2.5.1              DBI_1.1.2             colorspace_2.0-3     
[13] withr_2.5.0           tidyselect_1.1.2      processx_3.5.3       
[16] bit_4.0.4             compiler_4.2.0        git2r_0.30.1         
[19] cli_3.3.0             rvest_1.0.2           xml2_1.3.3           
[22] officer_0.4.2         sass_0.4.1            scales_1.2.0         
[25] callr_3.7.0           systemfonts_1.0.4     digest_0.6.29        
[28] rmarkdown_2.14        base64enc_0.1-3       pkgconfig_2.0.3      
[31] htmltools_0.5.2       dbplyr_2.2.0          fastmap_1.1.0        
[34] highr_0.9             htmlwidgets_1.5.4     rlang_1.0.2          
[37] RSQLite_2.2.14        rstudioapi_0.13       shiny_1.7.1          
[40] jquerylib_0.1.4       generics_0.1.2        jsonlite_1.8.0       
[43] vroom_1.5.7           zip_2.2.0             magrittr_2.0.3       
[46] Rcpp_1.0.8.3          munsell_0.5.0         fansi_1.0.3          
[49] gdtools_0.2.4         lifecycle_1.0.1       stringi_1.7.6        
[52] whisker_0.4           yaml_2.3.5            blob_1.2.3           
[55] grid_4.2.0            parallel_4.2.0        promises_1.2.0.1     
[58] crayon_1.5.1          haven_2.5.0           hms_1.1.1            
[61] knitr_1.39            ps_1.7.0              pillar_1.7.0         
[64] uuid_1.1-0            igraph_1.3.1          base64url_1.4        
[67] codetools_0.2-18      reprex_2.0.1          glue_1.6.2           
[70] evaluate_0.15         getPass_0.2-2         ukbwranglr_0.0.0.9000
[73] data.table_1.14.2     renv_0.13.2           modelr_0.1.8         
[76] vctrs_0.4.1           tzdb_0.3.0            httpuv_1.6.5         
[79] cellranger_1.1.0      gtable_0.3.0          reactR_0.4.4         
[82] assertthat_0.2.1      cachem_1.0.6          xfun_0.30            
[85] mime_0.12             xtable_1.8-4          broom_0.8.0          
[88] later_1.3.0           memoise_2.0.1         ellipsis_0.3.2       
[91] here_1.0.1