AMR/R/misc.R

202 lines
8.0 KiB
R
Raw Normal View History

2018-02-21 11:52:31 +01:00
# ==================================================================== #
# TITLE #
# Antimicrobial Resistance (AMR) Analysis #
# #
2019-01-02 23:24:07 +01:00
# SOURCE #
# https://gitlab.com/msberends/AMR #
2018-02-21 11:52:31 +01:00
# #
# LICENCE #
# (c) 2018-2020 Berends MS, Luz CF et al. #
2018-02-21 11:52:31 +01:00
# #
2019-01-02 23:24:07 +01:00
# This R package is free software; you can freely use and distribute #
# it for both personal and commercial purposes under the terms of the #
# GNU General Public License version 2.0 (GNU GPL-2), as published by #
# the Free Software Foundation. #
# #
# We created this package for both routine data analysis and academic #
# research and it was publicly released in the hope that it will be #
# useful, but it comes WITHOUT ANY WARRANTY OR LIABILITY. #
2019-04-05 18:47:39 +02:00
# Visit our website for more info: https://msberends.gitlab.io/AMR. #
2018-02-21 11:52:31 +01:00
# ==================================================================== #
# No export, no Rd
addin_insert_in <- function() {
rstudioapi::insertText(" %in% ")
}
# No export, no Rd
addin_insert_like <- function() {
rstudioapi::insertText(" %like% ")
}
2020-02-14 19:54:13 +01:00
check_dataset_integrity <- function() {
tryCatch({
check_microorganisms <- all(c("mo", "fullname", "kingdom", "phylum",
"class", "order", "family", "genus",
"species", "subspecies", "rank",
"col_id", "species_id", "source",
"ref", "prevalence", "snomed") %in% colnames(microorganisms),
na.rm = TRUE) & NROW(microorganisms) == NROW(microorganismsDT)
check_antibiotics <- all(c("ab", "atc", "cid", "name", "group",
"atc_group1", "atc_group2", "abbreviations",
"synonyms", "oral_ddd", "oral_units",
"iv_ddd", "iv_units", "loinc") %in% colnames(antibiotics),
na.rm = TRUE)
}, error = function(e)
stop('Please use the command \'library("AMR")\' before using this function, to load the needed reference data.', call. = FALSE)
)
if (!check_microorganisms | !check_antibiotics) {
2020-02-14 19:54:13 +01:00
stop("Data set `microorganisms` or data set `antibiotics` is overwritten by your global environment and prevents the AMR package from working correctly. Please rename your object before using this function.", call. = FALSE)
2019-11-23 12:39:57 +01:00
}
2020-02-14 19:54:13 +01:00
invisible(TRUE)
2019-11-23 12:39:57 +01:00
}
2019-03-15 13:57:25 +01:00
#' @importFrom crayon blue bold red
2019-01-15 12:45:24 +01:00
#' @importFrom dplyr %>% pull
2019-05-23 16:58:59 +02:00
search_type_in_df <- function(x, type) {
2019-01-15 12:45:24 +01:00
# try to find columns based on type
found <- NULL
2019-05-23 16:58:59 +02:00
colnames(x) <- trimws(colnames(x))
2019-11-04 12:08:08 +01:00
2019-01-15 12:45:24 +01:00
# -- mo
if (type == "mo") {
2019-05-23 16:58:59 +02:00
if ("mo" %in% lapply(x, class)) {
found <- colnames(x)[lapply(x, class) == "mo"][1]
2019-11-04 12:08:08 +01:00
} else if ("mo" %in% colnames(x) &
suppressWarnings(
all(x$mo %in% c(NA,
microorganisms$mo,
microorganisms.translation$mo_old)))) {
found <- "mo"
} else if (any(colnames(x) %like% "^(mo|microorganism|organism|bacteria|bacterie)s?$")) {
found <- colnames(x)[colnames(x) %like% "^(mo|microorganism|organism|bacteria|bacterie)s?$"][1]
} else if (any(colnames(x) %like% "^(microorganism|organism|bacteria|bacterie)")) {
found <- colnames(x)[colnames(x) %like% "^(microorganism|organism|bacteria|bacterie)"][1]
2019-05-23 16:58:59 +02:00
} else if (any(colnames(x) %like% "species")) {
found <- colnames(x)[colnames(x) %like% "species"][1]
2019-01-15 12:45:24 +01:00
}
2019-11-04 12:08:08 +01:00
2019-01-15 12:45:24 +01:00
}
# -- key antibiotics
if (type == "keyantibiotics") {
2019-05-23 16:58:59 +02:00
if (any(colnames(x) %like% "^key.*(ab|antibiotics)")) {
found <- colnames(x)[colnames(x) %like% "^key.*(ab|antibiotics)"][1]
2019-01-15 12:45:24 +01:00
}
}
# -- date
if (type == "date") {
2019-05-23 16:58:59 +02:00
if (any(colnames(x) %like% "^(specimen date|specimen_date|spec_date)")) {
2019-03-15 13:57:25 +01:00
# WHONET support
2019-05-23 16:58:59 +02:00
found <- colnames(x)[colnames(x) %like% "^(specimen date|specimen_date|spec_date)"][1]
if (!any(class(x %>% pull(found)) %in% c("Date", "POSIXct"))) {
2019-03-15 13:57:25 +01:00
stop(red(paste0("ERROR: Found column `", bold(found), "` to be used as input for `col_", type,
"`, but this column contains no valid dates. Transform its values to valid dates first.")),
call. = FALSE)
}
} else {
2019-10-11 17:21:02 +02:00
for (i in seq_len(ncol(x))) {
2019-05-23 16:58:59 +02:00
if (any(class(x %>% pull(i)) %in% c("Date", "POSIXct"))) {
found <- colnames(x)[i]
2019-03-15 13:57:25 +01:00
break
}
2019-01-15 12:45:24 +01:00
}
}
}
# -- patient id
if (type == "patient_id") {
2019-05-23 16:58:59 +02:00
if (any(colnames(x) %like% "^(identification |patient|patid)")) {
found <- colnames(x)[colnames(x) %like% "^(identification |patient|patid)"][1]
2019-01-29 00:06:50 +01:00
}
}
# -- specimen
if (type == "specimen") {
2019-05-23 16:58:59 +02:00
if (any(colnames(x) %like% "(specimen type|spec_type)")) {
found <- colnames(x)[colnames(x) %like% "(specimen type|spec_type)"][1]
} else if (any(colnames(x) %like% "^(specimen)")) {
found <- colnames(x)[colnames(x) %like% "^(specimen)"][1]
2019-01-15 12:45:24 +01:00
}
}
2020-02-20 13:19:23 +01:00
# -- UTI (urinary tract infection)
if (type == "uti") {
if (any(colnames(x) == "uti")) {
found <- colnames(x)[colnames(x) == "uti"][1]
} else if (any(colnames(x) %like% "(urine|urinary)")) {
found <- colnames(x)[colnames(x) %like% "(urine|urinary)"][1]
}
if (!is.null(found)) {
# this column should contain logicals
2020-02-20 13:19:23 +01:00
if (!is.logical(x[, found, drop = TRUE])) {
message(red(paste0("NOTE: Column `", bold(found), "` found as input for `col_", type,
"`, but this column does not contain 'logical' values (TRUE/FALSE) and was ignored.")))
found <- NULL
}
}
}
2019-01-15 12:45:24 +01:00
if (!is.null(found)) {
msg <- paste0("NOTE: Using column `", bold(found), "` as input for `col_", type, "`.")
2019-01-29 00:06:50 +01:00
if (type %in% c("keyantibiotics", "specimen")) {
2019-05-17 20:22:04 +02:00
msg <- paste(msg, "Use", bold(paste0("col_", type), "= FALSE"), "to prevent this.")
2019-01-15 12:45:24 +01:00
}
message(blue(msg))
}
found
}
2019-03-26 14:24:03 +01:00
stopifnot_installed_package <- function(package) {
2019-06-01 20:40:49 +02:00
# no "utils::installed.packages()" since it requires non-staged install since R 3.6.0
# https://developer.r-project.org/Blog/public/2019/02/14/staged-install/index.html
2019-11-30 12:01:50 +01:00
tryCatch(get(".packageName", envir = asNamespace(package)),
error = function(e) stop("package '", package, "' required but not installed",
' - try to install it with: install.packages("', package, '")',
call. = FALSE))
2019-06-01 20:40:49 +02:00
return(invisible())
2019-03-26 14:24:03 +01:00
}
2019-05-10 16:44:59 +02:00
2019-05-20 19:12:41 +02:00
"%or%" <- function(x, y) {
if (is.null(x) | is.null(y)) {
if (is.null(x)) {
return(y)
} else {
return(x)
}
}
ifelse(!is.na(x),
x,
ifelse(!is.na(y), y, NA))
2019-05-20 19:12:41 +02:00
}
class_integrity_check <- function(value, type, check_vector) {
if (!all(value[!is.na(value)] %in% check_vector)) {
warning(paste0("invalid ", type, ", NA generated"), call. = FALSE)
value[!value %in% check_vector] <- NA
}
value
}
2020-01-27 19:14:23 +01:00
# transforms data set to data.frame with only ASCII values, to comply with CRAN policies
dataset_UTF8_to_ASCII <- function(df) {
trans <- function(vect) {
iconv(vect, from = "UTF-8", to = "ASCII//TRANSLIT")
}
df <- as.data.frame(df, stringsAsFactors = FALSE)
for (i in seq_len(NCOL(df))) {
col <- df[, i]
if (is.list(col)) {
col <- lapply(col, function(j) trans(j))
df[, i] <- list(col)
} else {
if (is.factor(col)) {
levels(col) <- trans(levels(col))
} else if (is.character(col)) {
col <- trans(col)
} else {
col
}
df[, i] <- col
}
}
df
}