1
0
mirror of https://github.com/msberends/AMR.git synced 2024-12-27 22:46:11 +01:00
AMR/R/mo_history.R

135 lines
5.2 KiB
R
Raw Normal View History

2019-03-15 13:57:25 +01:00
# ==================================================================== #
# TITLE #
# Antimicrobial Resistance (AMR) Analysis #
# #
# SOURCE #
# https://gitlab.com/msberends/AMR #
# #
# LICENCE #
# (c) 2019 Berends MS (m.s.berends@umcg.nl), Luz CF (c.f.luz@umcg.nl) #
# #
# 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. #
# #
# This R package was created for academic research and 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. #
2019-03-15 13:57:25 +01:00
# ==================================================================== #
2019-03-28 21:33:28 +01:00
# print successful as.mo coercions to AMR environment
2019-03-18 14:29:41 +01:00
#' @importFrom dplyr %>% distinct filter
2019-03-26 14:24:03 +01:00
set_mo_history <- function(x, mo, uncertainty_level, force = FALSE) {
2019-04-06 09:38:23 +02:00
# disable function
return(base::invisible())
2019-03-15 17:36:42 +01:00
if (base::interactive() | force == TRUE) {
2019-03-26 14:24:03 +01:00
mo_hist <- read_mo_history(uncertainty_level = uncertainty_level, force = force)
2019-03-18 14:29:41 +01:00
df <- data.frame(x, mo, stringsAsFactors = FALSE) %>%
distinct(x, .keep_all = TRUE) %>%
filter(!is.na(x) & !is.na(mo))
if (nrow(df) == 0) {
return(base::invisible())
}
x <- toupper(df$x)
2019-03-15 17:36:42 +01:00
mo <- df$mo
for (i in 1:length(x)) {
2019-03-18 14:29:41 +01:00
# save package version too, as both the as.mo() algorithm and the reference data set may change
2019-03-26 14:24:03 +01:00
if (NROW(mo_hist[base::which(mo_hist$x == x[i] &
mo_hist$uncertainty_level >= uncertainty_level &
2019-03-28 21:33:28 +01:00
mo_hist$package_v == utils::packageVersion("AMR")),]) == 0) {
2019-03-28 22:30:19 +01:00
tryCatch(
assign(x = "mo_history",
value = rbind(mo_hist,
data.frame(
x = x[i],
mo = mo[i],
uncertainty_level = uncertainty_level,
package_v = base::as.character(utils::packageVersion("AMR")),
stringsAsFactors = FALSE)),
envir = asNamespace("AMR")),
error = function(e) invisible())
2019-03-15 17:36:42 +01:00
}
2019-03-15 13:57:25 +01:00
}
}
return(base::invisible())
}
2019-03-26 14:24:03 +01:00
get_mo_history <- function(x, uncertainty_level, force = FALSE) {
2019-04-06 09:38:23 +02:00
# disable function
return(NA)
2019-03-28 21:33:28 +01:00
history <- read_mo_history(uncertainty_level = uncertainty_level, force = force)
if (base::is.null(history)) {
2019-03-15 13:57:25 +01:00
NA
} else {
2019-03-18 14:29:41 +01:00
data.frame(x = toupper(x), stringsAsFactors = FALSE) %>%
2019-03-28 21:33:28 +01:00
left_join(history, by = "x") %>%
2019-03-15 13:57:25 +01:00
pull(mo)
}
}
2019-03-15 17:36:42 +01:00
#' @importFrom dplyr %>% filter distinct
2019-03-26 14:24:03 +01:00
read_mo_history <- function(uncertainty_level = 2, force = FALSE, unfiltered = FALSE) {
2019-04-06 09:38:23 +02:00
# disable function
return(NULL)
2019-03-28 21:33:28 +01:00
if ((!base::interactive() & force == FALSE)) {
2019-03-15 13:57:25 +01:00
return(NULL)
}
2019-03-26 14:24:03 +01:00
uncertainty_level_param <- uncertainty_level
2019-03-28 21:33:28 +01:00
history <- tryCatch(get("mo_history", envir = asNamespace("AMR")),
error = function(e) NULL)
if (is.null(history)) {
return(NULL)
}
2019-03-15 13:57:25 +01:00
# Below: filter on current package version.
2019-03-15 17:36:42 +01:00
# Even current fullnames may be replaced by new taxonomic names, so new versions of
2019-03-15 13:57:25 +01:00
# the Catalogue of Life must not lead to data corruption.
2019-03-26 14:24:03 +01:00
if (unfiltered == FALSE) {
2019-03-28 21:33:28 +01:00
history <- history %>%
filter(package_v == as.character(utils::packageVersion("AMR")),
2019-03-26 14:24:03 +01:00
# only take unknowns if uncertainty_level_param is higher
((mo == "UNKNOWN" & uncertainty_level_param == uncertainty_level) |
(mo != "UNKNOWN" & uncertainty_level_param >= uncertainty_level))) %>%
arrange(desc(uncertainty_level)) %>%
distinct(x, mo, .keep_all = TRUE)
}
2019-03-28 21:33:28 +01:00
if (nrow(history) == 0) {
2019-03-26 14:24:03 +01:00
NULL
} else {
2019-03-28 21:33:28 +01:00
history
2019-03-26 14:24:03 +01:00
}
2019-03-15 13:57:25 +01:00
}
#' @rdname as.mo
2019-03-26 14:24:03 +01:00
#' @importFrom crayon red
#' @importFrom utils menu
2019-03-15 13:57:25 +01:00
#' @export
2019-03-26 14:24:03 +01:00
clean_mo_history <- function(...) {
2019-03-28 21:33:28 +01:00
if (!is.null(read_mo_history())) {
2019-03-26 14:24:03 +01:00
if (interactive() & !isTRUE(list(...)$force)) {
q <- menu(title = paste("This will remove all",
format(nrow(read_mo_history(999, unfiltered = TRUE)), big.mark = ","),
2019-03-28 21:33:28 +01:00
"microbial IDs determined previously in this session. Are you sure?"),
2019-03-26 14:24:03 +01:00
choices = c("Yes", "No"),
graphics = FALSE)
if (q != 1) {
return(invisible())
}
}
2019-03-28 22:30:19 +01:00
tryCatch(
assign(x = "mo_history",
value = NULL,
envir = asNamespace("AMR")),
error = function(e) invisible())
2019-03-28 21:33:28 +01:00
cat(red("History removed."))
2019-03-15 13:57:25 +01:00
}
}