diff --git a/DESCRIPTION b/DESCRIPTION index b60fb54c..fb4059e5 100755 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: AMR -Version: 0.2.0.9014 -Date: 2018-07-19 +Version: 0.2.0.9015 +Date: 2018-07-23 Title: Antimicrobial Resistance Analysis Authors@R: c( person( diff --git a/R/g.test.R b/R/g.test.R index 962f383b..bd696846 100644 --- a/R/g.test.R +++ b/R/g.test.R @@ -254,3 +254,4 @@ ratio <- function(x, ratio) { } base::sum(x, na.rm = TRUE) * (ratio / base::sum(ratio, na.rm = TRUE)) } + diff --git a/R/key_antibiotics.R b/R/key_antibiotics.R index 9a8e937b..3b391fb1 100644 --- a/R/key_antibiotics.R +++ b/R/key_antibiotics.R @@ -25,7 +25,8 @@ #' @param universal_1,universal_2,universal_3,universal_4,universal_5,universal_6 column names of \strong{broad-spectrum} antibiotics, case-insensitive #' @param GramPos_1,GramPos_2,GramPos_3,GramPos_4,GramPos_5,GramPos_6 column names of antibiotics for \strong{Gram positives}, case-insensitive #' @param GramNeg_1,GramNeg_2,GramNeg_3,GramNeg_4,GramNeg_5,GramNeg_6 column names of antibiotics for \strong{Gram negatives}, case-insensitive -#' @details The function \code{key_antibiotics} returns a character vector with antibiotic results. +#' @param warnings give warning about missing antibiotic columns, they will anyway be ignored +#' @details The function \code{key_antibiotics} returns a character vector with 12 antibiotic results for every isolate. These isolates can then be compared using \code{key_antibiotics_equal}, to check if two isolates have generally the same antibiogram. Missing and invalid values are replaced with a dot (\code{"."}). The \code{\link{first_isolate}} function only uses this function on the same microbial species from the same patient. Using this, an MRSA will be included after a susceptible \emph{S. aureus} (MSSA) found within the same episode (see \code{episode} parameter of \code{\link{first_isolate}}). Without key antibiotic comparison it wouldn't. #' #' At default, the antibiotics that are used for \strong{Gram positive bacteria} are (colum names): \cr #' \code{"amox"}, \code{"amcl"}, \code{"cfur"}, \code{"pita"}, \code{"cipr"}, \code{"trsu"} (until here is universal), \code{"vanc"}, \code{"teic"}, \code{"tetr"}, \code{"eryt"}, \code{"oxac"}, \code{"rifa"}. @@ -34,7 +35,7 @@ #' \code{"amox"}, \code{"amcl"}, \code{"cfur"}, \code{"pita"}, \code{"cipr"}, \code{"trsu"} (until here is universal), \code{"gent"}, \code{"tobr"}, \code{"coli"}, \code{"cfot"}, \code{"cfta"}, \code{"mero"}. #' #' -#' The function \code{key_antibiotics_equal} checks the characters returned by \code{key_antibiotics} for equality, and returns a logical value. +#' The function \code{key_antibiotics_equal} checks the characters returned by \code{key_antibiotics} for equality, and returns a logical vector. #' @inheritSection first_isolate Key antibiotics #' @rdname key_antibiotics #' @export @@ -54,6 +55,16 @@ #' first_isolate(tbl, #' col_keyantibiotics = 'keyab') #' } +#' +#' # output of the `key_antibiotics` function could be like this: +#' strainA <- "SSSRR.S.R..S" +#' strainB <- "SSSIRSSSRSSS" +#' +#' key_antibiotics_equal(strainA, strainB) +#' # TRUE, because I is ignored (as are missing values) +#' +#' key_antibiotics_equal(strainA, strainB, ignore_I = FALSE) +#' # FALSE, because I is not ignored and so the 4th value differs key_antibiotics <- function(tbl, col_bactid = "bactid", universal_1 = "amox", @@ -74,7 +85,7 @@ key_antibiotics <- function(tbl, GramNeg_4 = "cfot", GramNeg_5 = "cfta", GramNeg_6 = "mero", - info = TRUE) { + warnings = TRUE) { if (!col_bactid %in% colnames(tbl)) { stop('Column ', col_bactid, ' not found.', call. = FALSE) @@ -84,7 +95,7 @@ key_antibiotics <- function(tbl, col.list <- c(universal_1, universal_2, universal_3, universal_4, universal_5, universal_6, GramPos_1, GramPos_2, GramPos_3, GramPos_4, GramPos_5, GramPos_6, GramNeg_1, GramNeg_2, GramNeg_3, GramNeg_4, GramNeg_5, GramNeg_6) - col.list <- check_available_columns(tbl = tbl, col.list = col.list, info = info) + col.list <- check_available_columns(tbl = tbl, col.list = col.list, info = warnings) universal_1 <- col.list[universal_1] universal_2 <- col.list[universal_2] universal_3 <- col.list[universal_3] @@ -173,10 +184,17 @@ key_antibiotics_equal <- function(x, x <- gsub('I', '.', x, ignore.case = TRUE) y <- gsub('I', '.', y, ignore.case = TRUE) } + for (i in 1:length(x)) { + x_split <- strsplit(x[i], "")[[1]] + y_split <- strsplit(y[i], "")[[1]] + y_split[x_split == "."] <- "." + x_split[y_split == "."] <- "." + x_checkfor <- paste(x_split, collapse = "") + y_checkfor <- paste(y_split, collapse = "") result[i] <- nchar(x[i]) == nchar(y[i]) & - (x[i] %like% paste0("^", y[i], "$") | - y[i] %like% paste0("^", x[i], "$")) + (x_checkfor %like% y_checkfor | + y_checkfor %like% x_checkfor) } return(result) diff --git a/man/key_antibiotics.Rd b/man/key_antibiotics.Rd index 9288cb4f..2e782e09 100755 --- a/man/key_antibiotics.Rd +++ b/man/key_antibiotics.Rd @@ -41,7 +41,7 @@ key_antibiotics_equal(x, y, type = c("keyantibiotics", "points"), These function can be used to determine first isolates (see \code{\link{first_isolate}}). Using key antibiotics to determine first isolates is more reliable than without key antibiotics. These selected isolates will then be called first \emph{weighted} isolates. } \details{ -The function \code{key_antibiotics} returns a character vector with antibiotic results. +The function \code{key_antibiotics} returns a character vector with 12 antibiotic results for every isolate. These isolates can then be compared using \code{key_antibiotics_equal}, to check if two isolates have generally the same antibiogram. Missing and invalid values are replaced with a dot (\code{"."}). The \code{\link{first_isolate}} function only uses this function on the same microbial species from the same patient. Using this, an MRSA will be included after a susceptible \emph{S. aureus} (MSSA) found within the same episode (see \code{episode} parameter of \code{\link{first_isolate}}). Without key antibiotic comparison it wouldn't. At default, the antibiotics that are used for \strong{Gram positive bacteria} are (colum names): \cr \code{"amox"}, \code{"amcl"}, \code{"cfur"}, \code{"pita"}, \code{"cipr"}, \code{"trsu"} (until here is universal), \code{"vanc"}, \code{"teic"}, \code{"tetr"}, \code{"eryt"}, \code{"oxac"}, \code{"rifa"}. @@ -50,7 +50,7 @@ The function \code{key_antibiotics} returns a character vector with antibiotic r \code{"amox"}, \code{"amcl"}, \code{"cfur"}, \code{"pita"}, \code{"cipr"}, \code{"trsu"} (until here is universal), \code{"gent"}, \code{"tobr"}, \code{"coli"}, \code{"cfot"}, \code{"cfta"}, \code{"mero"}. - The function \code{key_antibiotics_equal} checks the characters returned by \code{key_antibiotics} for equality, and returns a logical value. + The function \code{key_antibiotics_equal} checks the characters returned by \code{key_antibiotics} for equality, and returns a logical vector. } \section{Key antibiotics}{ @@ -77,6 +77,16 @@ tbl$first_isolate_weighed <- first_isolate(tbl, col_keyantibiotics = 'keyab') } + +# output of the `key_antibiotics` function could be like this: +strainA <- "SSSRR.S.R..S" +strainB <- "SSSIRSSSRSSS" + +key_antibiotics_equal(strainA, strainB) +# TRUE, because I is ignored (as are missing values) + +key_antibiotics_equal(strainA, strainB, ignore_I = FALSE) +# FALSE, because I is not ignored and so the 4th value differs } \seealso{ \code{\link{first_isolate}} diff --git a/tests/testthat/test-first_isolates.R b/tests/testthat/test-first_isolates.R index 54561bad..6b08494f 100755 --- a/tests/testthat/test-first_isolates.R +++ b/tests/testthat/test-first_isolates.R @@ -1,13 +1,5 @@ context("first_isolates.R") -test_that("keyantibiotics work", { - expect_equal(length(key_antibiotics(septic_patients, info = FALSE)), nrow(septic_patients)) - expect_true(key_antibiotics_equal("SSS", "SSS")) - expect_false(key_antibiotics_equal("SSS", "SRS")) - expect_true(key_antibiotics_equal("SSS", "SIS", ignore_I = TRUE)) - expect_false(key_antibiotics_equal("SSS", "SIS", ignore_I = FALSE)) -}) - test_that("first isolates work", { # septic_patients contains 1959 out of 2000 first isolates expect_equal( diff --git a/tests/testthat/test-key_antibiotics.R b/tests/testthat/test-key_antibiotics.R new file mode 100644 index 00000000..f4439c03 --- /dev/null +++ b/tests/testthat/test-key_antibiotics.R @@ -0,0 +1,11 @@ +context("key_antibiotics.R") + +test_that("keyantibiotics work", { + expect_equal(length(key_antibiotics(septic_patients, warnings = FALSE)), nrow(septic_patients)) + expect_true(key_antibiotics_equal("SSS", "SSS")) + expect_false(key_antibiotics_equal("SSS", "SRS")) + expect_true(key_antibiotics_equal("SSS", "SIS", ignore_I = TRUE)) + expect_false(key_antibiotics_equal("SSS", "SIS", ignore_I = FALSE)) + expect_true(key_antibiotics_equal(".SS", "SI.", ignore_I = TRUE)) + expect_false(key_antibiotics_equal(".SS", "SI.", ignore_I = FALSE)) +})