(v3.0.1.9121) styler

This commit is contained in:
dr. M.S. (Matthijs) Berends committed 2026-10-12 11:42:06 +02:00
1 parent c8cb36ddcf
commit 0293d296aa
14 files changed
+457 -259

No files matched your search

+1 -1
View File
@@ -1,5 +1,5 @@
Package: AMR Package: AMR
Version: 3.0.1.9120 Version: 3.0.1.9121
Date: 2026-10-12 Date: 2026-10-12
Title: Antimicrobial Resistance Data Analysis Title: Antimicrobial Resistance Data Analysis
Description: Functions to simplify and standardise antimicrobial resistance (AMR) Description: Functions to simplify and standardise antimicrobial resistance (AMR)
+1 -1
View File
@@ -1,4 +1,4 @@
# AMR 3.0.1.9120 # AMR 3.0.1.9121
Planned as v3.1.0, mid October 2026. Planned as v3.1.0, mid October 2026.
+10 -6
View File
@@ -73,7 +73,6 @@
#' # Though they are primarily intended to use for selections and filters. #' # Though they are primarily intended to use for selections and filters.
#' # Examples sections below are split into 'dplyr', 'base R', and 'data.table': #' # Examples sections below are split into 'dplyr', 'base R', and 'data.table':
#' #'
#' \donttest{
#' \dontrun{ #' \dontrun{
#' # dplyr ------------------------------------------------------------------- #' # dplyr -------------------------------------------------------------------
#' #'
@@ -94,8 +93,10 @@
#' # get AMR for all aminoglycosides e.g., per ward: #' # get AMR for all aminoglycosides e.g., per ward:
#' example_isolates %>% #' example_isolates %>%
#' group_by(ward) %>% #' group_by(ward) %>%
#' summarise(across(aminoglycosides(), #' summarise(across(
#' resistance)) #' aminoglycosides(),
#' resistance
#' ))
#' #'
#' # You can combine selectors with '&' to be more specific: #' # You can combine selectors with '&' to be more specific:
#' example_isolates %>% #' example_isolates %>%
@@ -105,8 +106,10 @@
#' example_isolates %>% #' example_isolates %>%
#' filter(mo_genus() %in% c("Escherichia", "Klebsiella")) %>% #' filter(mo_genus() %in% c("Escherichia", "Klebsiella")) %>%
#' group_by(ward) %>% #' group_by(ward) %>%
#' summarise_at(not_intrinsic_resistant(), #' summarise_at(
#' resistance) #' not_intrinsic_resistant(),
#' resistance
#' )
#' #'
#' # get susceptibility for antimicrobials whose name contains "trim": #' # get susceptibility for antimicrobials whose name contains "trim":
#' example_isolates %>% #' example_isolates %>%
@@ -159,8 +162,9 @@
#' y <- example_isolates %>% filter(carbapenems() == "R") #' y <- example_isolates %>% filter(carbapenems() == "R")
#' z <- example_isolates %>% filter(if_all(carbapenems(), ~ .x == "R")) #' z <- example_isolates %>% filter(if_all(carbapenems(), ~ .x == "R"))
#' identical(x, y) && identical(y, z) #' identical(x, y) && identical(y, z)
#'
#' } #' }
#'
#' \donttest{
#' # base R ------------------------------------------------------------------ #' # base R ------------------------------------------------------------------
#' #'
#' # select columns 'IPM' (imipenem) and 'MEM' (meropenem) #' # select columns 'IPM' (imipenem) and 'MEM' (meropenem)
+2 -1
View File
@@ -207,7 +207,8 @@
#' top_n_microorganisms(n = 10) %>% #' top_n_microorganisms(n = 10) %>%
#' group_by( #' group_by(
#' age_group = age_groups(age, c(25, 50, 75)), #' age_group = age_groups(age, c(25, 50, 75)),
#' gender) %>% #' gender
#' ) %>%
#' wisca(antimicrobials = c("TZP", "TZP+TOB", "TZP+GEN")) #' wisca(antimicrobials = c("TZP", "TZP+TOB", "TZP+GEN"))
#' } #' }
#' #'
BIN
View File
Binary file not shown.
@@ -66,13 +66,15 @@ parse_log$no_breakpoints_determined <- NULL
# output. # output.
split_rich_text <- function(fmt_list, cell_strike = rep(FALSE, length(fmt_list))) { split_rich_text <- function(fmt_list, cell_strike = rep(FALSE, length(fmt_list))) {
map2_dfr(fmt_list, cell_strike, function(fmt, cell_struck) { map2_dfr(fmt_list, cell_strike, function(fmt, cell_struck) {
if (is.null(fmt) || nrow(fmt) == 0) if (is.null(fmt) || nrow(fmt) == 0) {
return(tibble(is_rich = FALSE, base_text = NA_character_, note_super = NA_character_, full_text = NA_character_)) return(tibble(is_rich = FALSE, base_text = NA_character_, note_super = NA_character_, full_text = NA_character_))
}
# A run without its own strike setting inherits the cell's font # A run without its own strike setting inherits the cell's font
is_struck <- coalesce(fmt$strike, isTRUE(cell_struck)) is_struck <- coalesce(fmt$strike, isTRUE(cell_struck))
fmt <- fmt[!is_struck, , drop = FALSE] fmt <- fmt[!is_struck, , drop = FALSE]
if (nrow(fmt) == 0) if (nrow(fmt) == 0) {
return(tibble(is_rich = TRUE, base_text = NA_character_, note_super = NA_character_, full_text = NA_character_)) return(tibble(is_rich = TRUE, base_text = NA_character_, note_super = NA_character_, full_text = NA_character_))
}
is_super <- !is.na(fmt$vertAlign) & fmt$vertAlign == "superscript" is_super <- !is.na(fmt$vertAlign) & fmt$vertAlign == "superscript"
tibble( tibble(
is_rich = TRUE, is_rich = TRUE,
@@ -115,7 +117,8 @@ parse_cells <- function(cells, formats) {
TRUE ~ base_text TRUE ~ base_text
), ),
note_super = if_else(is.na(note_super) | note_super == "", note_super = if_else(is.na(note_super) | note_super == "",
NA_character_, note_super) NA_character_, note_super
)
) |> ) |>
filter(!is.na(base_text) | !is.na(note_super) | !is.na(full_text)) |> filter(!is.na(base_text) | !is.na(note_super) | !is.na(full_text)) |>
select(row, col, base_text, note_super, full_text) select(row, col, base_text, note_super, full_text)
@@ -229,11 +232,17 @@ split_agent_organism <- function(cells, formats, sheet_name, valid_rows) {
# as a single agent name) or the bold pattern never settles into an # as a single agent name) or the bold pattern never settles into an
# all-non-bold tail, which is the genuinely unexpected case. # all-non-bold tail, which is the genuinely unexpected case.
if (all(bold_resolved) || !any(bold_resolved)) { if (all(bold_resolved) || !any(bold_resolved)) {
out[[i]] <- tibble(row = row_i, out[[i]] <- tibble(
ab_text = paste(c(paste0(fmt$character, collapse = ""), row = row_i,
trailing_qualifier[!is.na(trailing_qualifier)]), ab_text = paste(
collapse = " "), c(
mo_override = NA_character_) paste0(fmt$character, collapse = ""),
trailing_qualifier[!is.na(trailing_qualifier)]
),
collapse = " "
),
mo_override = NA_character_
)
next next
} }
stop( stop(
@@ -281,7 +290,9 @@ split_agent_organism <- function(cells, formats, sheet_name, valid_rows) {
# 2. Notes block parser # 2. Notes block parser
# ============================================================================== # ==============================================================================
parse_notes_block <- function(txt) { parse_notes_block <- function(txt) {
if (is.na(txt) || txt == "") return(list()) if (is.na(txt) || txt == "") {
return(list())
}
txt <- gsub("\r\n", "\n", txt) txt <- gsub("\r\n", "\n", txt)
txt <- gsub("\r", "\n", txt) txt <- gsub("\r", "\n", txt)
@@ -289,13 +300,18 @@ parse_notes_block <- function(txt) {
find_keys <- function(pattern, txt) { find_keys <- function(pattern, txt) {
m <- gregexpr(pattern, txt, perl = TRUE)[[1]] m <- gregexpr(pattern, txt, perl = TRUE)[[1]]
if (m[1] == -1) if (m[1] == -1) {
return(data.frame(pos = integer(0), key = character(0), return(data.frame(
mlen = integer(0), stringsAsFactors = FALSE)) pos = integer(0), key = character(0),
mlen = integer(0), stringsAsFactors = FALSE
))
}
lens <- attr(m, "match.length") lens <- attr(m, "match.length")
keys <- gsub("[.\\s]+$", "", trimws(substring(txt, m, m + lens - 1)), perl = TRUE) keys <- gsub("[.\\s]+$", "", trimws(substring(txt, m, m + lens - 1)), perl = TRUE)
data.frame(pos = as.integer(m), key = keys, mlen = as.integer(lens), data.frame(
stringsAsFactors = FALSE) pos = as.integer(m), key = keys, mlen = as.integer(lens),
stringsAsFactors = FALSE
)
} }
# A note key only starts at the beginning of a line or directly after the # A note key only starts at the beginning of a line or directly after the
@@ -322,7 +338,9 @@ parse_notes_block <- function(txt) {
r4 <- find_keys("(?:^|(?<=\\n))[ \\t]*\\d+[ \\t]+(?=[A-Z])", txt) r4 <- find_keys("(?:^|(?<=\\n))[ \\t]*\\d+[ \\t]+(?=[A-Z])", txt)
all_keys <- rbind(r1, r2, r3, r4) all_keys <- rbind(r1, r2, r3, r4)
if (nrow(all_keys) == 0) return(list()) if (nrow(all_keys) == 0) {
return(list())
}
# At the same position, keep the longest (most specific) match, e.g. "1/A." # At the same position, keep the longest (most specific) match, e.g. "1/A."
# over "1.", and drop any match that starts within an earlier kept match # over "1.", and drop any match that starts within an earlier kept match
all_keys <- all_keys[order(all_keys$pos, -all_keys$mlen), ] all_keys <- all_keys[order(all_keys$pos, -all_keys$mlen), ]
@@ -344,12 +362,16 @@ parse_notes_block <- function(txt) {
# reference "A" individually # reference "A" individually
for (k in unique(c(key, if (grepl("/", key)) strsplit(key, "/")[[1]]))) { for (k in unique(c(key, if (grepl("/", key)) strsplit(key, "/")[[1]]))) {
if (!nzchar(body)) { if (!nzchar(body)) {
stop("parse_notes_block(): note key '", key, "' has an empty body, which means a ", stop(
"cross-reference or other text was taken for a key. Text: '", substr(txt, 1, 300), "'") "parse_notes_block(): note key '", key, "' has an empty body, which means a ",
"cross-reference or other text was taken for a key. Text: '", substr(txt, 1, 300), "'"
)
} }
if (!is.null(notes[[k]]) && !identical(notes[[k]], body)) { if (!is.null(notes[[k]]) && !identical(notes[[k]], body)) {
stop("parse_notes_block(): note key '", k, "' occurs more than once with different ", stop(
"bodies. Text: '", substr(txt, 1, 300), "'") "parse_notes_block(): note key '", k, "' occurs more than once with different ",
"bodies. Text: '", substr(txt, 1, 300), "'"
)
} }
notes[[k]] <- body notes[[k]] <- body
} }
@@ -366,13 +388,17 @@ resolve_notes <- function(notes_list, mic_super, disk_super, abbreviations = lis
# Staphylococcus) or "1" with superscript "4.5" (v16.1, H. influenzae) # Staphylococcus) or "1" with superscript "4.5" (v16.1, H. influenzae)
mic_refs <- character(0) mic_refs <- character(0)
disk_refs <- character(0) disk_refs <- character(0)
if (!is.na(mic_super) && mic_super != "") if (!is.na(mic_super) && mic_super != "") {
mic_refs <- trimws(unlist(strsplit(mic_super, "[,.[:space:]]+"))) mic_refs <- trimws(unlist(strsplit(mic_super, "[,.[:space:]]+")))
if (!is.na(disk_super) && disk_super != "") }
if (!is.na(disk_super) && disk_super != "") {
disk_refs <- trimws(unlist(strsplit(disk_super, "[,.[:space:]]+"))) disk_refs <- trimws(unlist(strsplit(disk_super, "[,.[:space:]]+")))
}
all_refs <- unique(c(mic_refs, disk_refs)) all_refs <- unique(c(mic_refs, disk_refs))
all_refs <- all_refs[nzchar(all_refs)] all_refs <- all_refs[nzchar(all_refs)]
if (length(all_refs) == 0) return(NA_character_) if (length(all_refs) == 0) {
return(NA_character_)
}
# A reference that is not a key of the table's own notes block can be an # A reference that is not a key of the table's own notes block can be an
# abbreviation from the workbook's general "Notes" sheet, e.g. superscript # abbreviation from the workbook's general "Notes" sheet, e.g. superscript
@@ -383,9 +409,11 @@ resolve_notes <- function(notes_list, mic_super, disk_super, abbreviations = lis
nl <- c(nl, abbreviations[from_abbr]) nl <- c(nl, abbreviations[from_abbr])
unresolved <- setdiff(all_refs, names(nl)) unresolved <- setdiff(all_refs, names(nl))
if (length(unresolved) > 0) { if (length(unresolved) > 0) {
stop("resolve_notes(): note reference(s) '", paste(unresolved, collapse = "', '"), stop(
"resolve_notes(): note reference(s) '", paste(unresolved, collapse = "', '"),
"' not found in the notes block (keys found: ", paste(names(notes_list), collapse = " "), "' not found in the notes block (keys found: ", paste(names(notes_list), collapse = " "),
") nor in the workbook abbreviations (", paste(names(abbreviations), collapse = " "), ")") ") nor in the workbook abbreviations (", paste(names(abbreviations), collapse = " "), ")"
)
} }
# Detect combined keys (e.g. "1/A") # Detect combined keys (e.g. "1/A")
@@ -432,7 +460,9 @@ resolve_notes <- function(notes_list, mic_super, disk_super, abbreviations = lis
} }
} }
if (length(parts_out) == 0) return(NA_character_) if (length(parts_out) == 0) {
return(NA_character_)
}
paste(parts_out, collapse = " | ") paste(parts_out, collapse = " | ")
} }
@@ -441,7 +471,9 @@ resolve_notes <- function(notes_list, mic_super, disk_super, abbreviations = lis
# ============================================================================== # ==============================================================================
get_cell <- function(df, r, c) { get_cell <- function(df, r, c) {
hit <- df[df$row == r & df$col == c, ] hit <- df[df$row == r & df$col == c, ]
if (nrow(hit) == 0) return(list(base = NA_character_, super = NA_character_)) if (nrow(hit) == 0) {
return(list(base = NA_character_, super = NA_character_))
}
base <- hit$base_text[1] base <- hit$base_text[1]
super <- hit$note_super[1] super <- hit$note_super[1]
# EUCAST occasionally omits the superscript formatting of a note reference # EUCAST occasionally omits the superscript formatting of a note reference
@@ -456,7 +488,9 @@ get_cell <- function(df, r, c) {
} }
parse_bp <- function(txt) { parse_bp <- function(txt) {
if (is.na(txt) || txt == "") return(NA_character_) if (is.na(txt) || txt == "") {
return(NA_character_)
}
t <- trimws(txt) t <- trimws(txt)
# placeholders such as "-" (susceptibility testing not recommended), "IE" # placeholders such as "-" (susceptibility testing not recommended), "IE"
# or "Note" are kept as they are: the cleanup turns them into blocking rows # or "Note" are kept as they are: the cleanup turns them into blocking rows
@@ -485,14 +519,20 @@ is_bracketed_bp <- function(txt) {
# marks screening tests in the agent name instead, e.g. "Cefoxitin # marks screening tests in the agent name instead, e.g. "Cefoxitin
# (screen only)". The definition is read per workbook by # (screen only)". The definition is read per workbook by
# parse_workbook_notes() and added to the note of every bracketed row. # parse_workbook_notes() and added to the note of every bracketed row.
if (is.na(txt) || txt == "") return(FALSE) if (is.na(txt) || txt == "") {
return(FALSE)
}
grepl("^\\(.+\\)$", trimws(txt)) grepl("^\\(.+\\)$", trimws(txt))
} }
format_disk_dose <- function(dose) { format_disk_dose <- function(dose) {
if (is.na(dose) || dose == "") return(NA_character_) if (is.na(dose) || dose == "") {
return(NA_character_)
}
d <- trimws(dose) d <- trimws(dose)
if (grepl("mcg|unit", d, ignore.case = TRUE)) return(d) if (grepl("mcg|unit", d, ignore.case = TRUE)) {
return(d)
}
d <- gsub("-", "/", d) d <- gsub("-", "/", d)
paste0(d, " mcg") paste0(d, " mcg")
} }
@@ -515,8 +555,10 @@ detect_version <- function(cells) {
mutate(text = coalesce(character, "")) mutate(text = coalesce(character, ""))
version_text <- paste(version_cells$text, collapse = " ") version_text <- paste(version_cells$text, collapse = " ")
version <- regmatches(version_text, version <- regmatches(
regexpr("v\\.?\\s*[\\d.]+", version_text, perl = TRUE)) version_text,
regexpr("v\\.?\\s*[\\d.]+", version_text, perl = TRUE)
)
version <- gsub("v\\.?\\s*", "", version) version <- gsub("v\\.?\\s*", "", version)
if (length(version) == 0) version <- NA_character_ if (length(version) == 0) version <- NA_character_
@@ -586,12 +628,16 @@ parse_sheet <- function(xlsx_path, sheet_name, abbreviations = list()) {
} }
parse_log$mic_scope_extensions <- rbind( parse_log$mic_scope_extensions <- rbind(
parse_log$mic_scope_extensions, parse_log$mic_scope_extensions,
data.frame(guideline = ver$guideline, sheet = sheet_name, data.frame(
from_text = paste(genus, m[3]), to_text = paste(genus, m[5]), statement = m[1]) guideline = ver$guideline, sheet = sheet_name,
from_text = paste(genus, m[3]), to_text = paste(genus, m[5]), statement = m[1]
)
) )
} else if (grepl("breakpoints can (also )?be applied to [A-Z]\\.", txt_squished)) { } else if (grepl("breakpoints can (also )?be applied to [A-Z]\\.", txt_squished)) {
stop("parse_sheet(): unrecognised statement about applying breakpoints to another species on sheet '", stop(
sheet_name, "': ", substr(txt_squished, 1, 300)) "parse_sheet(): unrecognised statement about applying breakpoints to another species on sheet '",
sheet_name, "': ", substr(txt_squished, 1, 300)
)
} }
} }
@@ -613,9 +659,11 @@ parse_sheet <- function(xlsx_path, sheet_name, abbreviations = list()) {
if (length(no_bp_statement) > 0) { if (length(no_bp_statement) > 0) {
parse_log$no_breakpoints_determined <- rbind( parse_log$no_breakpoints_determined <- rbind(
parse_log$no_breakpoints_determined, parse_log$no_breakpoints_determined,
data.frame(guideline = ver$guideline, sheet = sheet_name, data.frame(
guideline = ver$guideline, sheet = sheet_name,
organism = trimws(get_cell(parsed, 1, 1)$base), organism = trimws(get_cell(parsed, 1, 1)$base),
statement = gsub("\\s+", " ", trimws(no_bp_statement[1]))) statement = gsub("\\s+", " ", trimws(no_bp_statement[1]))
)
) )
message(" EUCAST has not determined breakpoints in sheet '", sheet_name, "', skipping") message(" EUCAST has not determined breakpoints in sheet '", sheet_name, "', skipping")
return(NULL) return(NULL)
@@ -686,7 +734,9 @@ parse_sheet <- function(xlsx_path, sheet_name, abbreviations = list()) {
# Multi-organism: discover organism names above each header row. # Multi-organism: discover organism names above each header row.
# Organism name is a standalone row (col A only, no data in B:H) # Organism name is a standalone row (col A only, no data in B:H)
# found between the previous table's end and this header row. # found between the previous table's end and this header row.
rows_with_b <- parsed |> filter(col == 2) |> pull(row) rows_with_b <- parsed |>
filter(col == 2) |>
pull(row)
tables <- list() tables <- list()
for (i in seq_along(header_rows)) { for (i in seq_along(header_rows)) {
@@ -696,11 +746,15 @@ parse_sheet <- function(xlsx_path, sheet_name, abbreviations = list()) {
# Search backwards from the header row for the organism name # Search backwards from the header row for the organism name
search_from <- if (i == 1) 1 else sub_header_rows[i - 1] + 1 search_from <- if (i == 1) 1 else sub_header_rows[i - 1] + 1
candidates <- parsed |> candidates <- parsed |>
filter(col == 1, row >= search_from, row < hr, filter(
!row %in% rows_with_b) |> col == 1, row >= search_from, row < hr,
!row %in% rows_with_b
) |>
# Exclude known non-organism text patterns # Exclude known non-organism text patterns
filter(!grepl("Antimicrobial|MIC determination|Disk diffusion|Breakpoints for|Expert Rules|For abbreviations|For species|Examples of|haze|Isolated|Ignore haemolysis|Numbered|Lettered|Medium:|Inoculum:|Incubation:|Reading:|Quality control:|See disk", filter(!grepl(
base_text)) "Antimicrobial|MIC determination|Disk diffusion|Breakpoints for|Expert Rules|For abbreviations|For species|Examples of|haze|Isolated|Ignore haemolysis|Numbered|Lettered|Medium:|Inoculum:|Incubation:|Reading:|Quality control:|See disk",
base_text
))
# Take the last candidate (closest to the header) # Take the last candidate (closest to the header)
if (nrow(candidates) > 0) { if (nrow(candidates) > 0) {
@@ -733,13 +787,16 @@ parse_sheet <- function(xlsx_path, sheet_name, abbreviations = list()) {
} }
# Notes cell: notes_col, in the data range (usually at first_data, merged) # Notes cell: notes_col, in the data range (usually at first_data, merged)
note_cell <- parsed |> filter(col == notes_col, row >= first_data, row <= last_data) |> note_cell <- parsed |>
filter(col == notes_col, row >= first_data, row <= last_data) |>
slice_min(row, n = 1) slice_min(row, n = 1)
note_text <- if (nrow(note_cell) > 0) note_cell$full_text[1] else NA_character_ note_text <- if (nrow(note_cell) > 0) note_cell$full_text[1] else NA_character_
notes_parsed <- parse_notes_block(note_text) notes_parsed <- parse_notes_block(note_text)
tables[[i]] <- list(organism = organism, first_data = first_data, tables[[i]] <- list(
last_data = last_data, notes = notes_parsed) organism = organism, first_data = first_data,
last_data = last_data, notes = notes_parsed
)
} }
} else { } else {
# Single-organism: organism name from row 1, col A # Single-organism: organism name from row 1, col A
@@ -775,8 +832,10 @@ parse_sheet <- function(xlsx_path, sheet_name, abbreviations = list()) {
# Notes: look in notes_col from header row through end of section # Notes: look in notes_col from header row through end of section
note_cells <- parsed |> note_cells <- parsed |>
filter(col == notes_col, row >= hr, row <= last_data, filter(
!grepl("^Notes", base_text)) col == notes_col, row >= hr, row <= last_data,
!grepl("^Notes", base_text)
)
note_text <- if (nrow(note_cells) > 0) { note_text <- if (nrow(note_cells) > 0) {
paste(note_cells$full_text, collapse = "\n") paste(note_cells$full_text, collapse = "\n")
} else { } else {
@@ -784,8 +843,10 @@ parse_sheet <- function(xlsx_path, sheet_name, abbreviations = list()) {
} }
notes_parsed <- parse_notes_block(note_text) notes_parsed <- parse_notes_block(note_text)
tables[[i]] <- list(organism = organism, first_data = first_data, tables[[i]] <- list(
last_data = last_data, notes = notes_parsed) organism = organism, first_data = first_data,
last_data = last_data, notes = notes_parsed
)
} }
} }
@@ -817,8 +878,10 @@ parse_sheet <- function(xlsx_path, sheet_name, abbreviations = list()) {
# columns; on MIC-only sheets col 5 is the notes column and must not # columns; on MIC-only sheets col 5 is the notes column and must not
# be treated as a data indicator. # be treated as a data indicator.
data_cols <- if (has_disk_columns) c(2, 3, 5, 6, 7) else c(2, 3) data_cols <- if (has_disk_columns) c(2, 3, 5, 6, 7) else c(2, 3)
has_any_data <- any(!is.na(vapply(data_cols, function(cc) get_cell(parsed, r, cc)$base, has_any_data <- any(!is.na(vapply(
character(1)))) data_cols, function(cc) get_cell(parsed, r, cc)$base,
character(1)
)))
if (is.na(agent$base)) { if (is.na(agent$base)) {
# A row with values but without an agent name would otherwise be # A row with values but without an agent name would otherwise be
# skipped silently, e.g. if a future table merged agent cells. Rows # skipped silently, e.g. if a future table merged agent cells. Rows
@@ -894,7 +957,9 @@ parse_sheet <- function(xlsx_path, sheet_name, abbreviations = list()) {
combine_super <- function(...) { combine_super <- function(...) {
parts <- c(...) parts <- c(...)
parts <- parts[!is.na(parts) & parts != ""] parts <- parts[!is.na(parts) & parts != ""]
if (length(parts) == 0) return(NA_character_) if (length(parts) == 0) {
return(NA_character_)
}
paste(parts, collapse = ",") paste(parts, collapse = ",")
} }
mic_note <- resolve_notes(nl, combine_super(mic_s$super, agent$super), NA, abbreviations = abbreviations) mic_note <- resolve_notes(nl, combine_super(mic_s$super, agent$super), NA, abbreviations = abbreviations)
@@ -952,7 +1017,9 @@ parse_sheet <- function(xlsx_path, sheet_name, abbreviations = list()) {
} }
} }
if (idx == 0) return(NULL) if (idx == 0) {
return(NULL)
}
out <- bind_rows(results[seq_len(idx)]) out <- bind_rows(results[seq_len(idx)])
# The non-species related PK-PD breakpoints (v9.0-v13.1) come with EUCAST's # The non-species related PK-PD breakpoints (v9.0-v13.1) come with EUCAST's
@@ -1011,8 +1078,11 @@ parse_topical_sheet <- function(xlsx_path, abbreviations = list()) {
ab_info[[as.character(col_idx)]] <- list( ab_info[[as.character(col_idx)]] <- list(
name = gsub("\r\n", " ", name_cell$base), name = gsub("\r\n", " ", name_cell$base),
name_super = name_cell$super, name_super = name_cell$super,
dose = if (!is.na(dose_cell$base) && !dose_cell$base %in% c("-", "ND")) dose = if (!is.na(dose_cell$base) && !dose_cell$base %in% c("-", "ND")) {
paste0(dose_cell$base, " mcg") else NA_character_ paste0(dose_cell$base, " mcg")
} else {
NA_character_
}
) )
} }
@@ -1035,12 +1105,18 @@ parse_topical_sheet <- function(xlsx_path, abbreviations = list()) {
} }
# Notes: find in col 1, rows after the last organism data row # Notes: find in col 1, rows after the last organism data row
last_data_row <- if (length(organism_pairs) > 0) last_data_row <- if (length(organism_pairs) > 0) {
max(vapply(organism_pairs, function(x) x$disk_row, numeric(1))) else 0L max(vapply(organism_pairs, function(x) x$disk_row, numeric(1)))
} else {
0L
}
notes_cells <- parsed |> notes_cells <- parsed |>
filter(col == 1, row > last_data_row, !grepl("^Notes$", base_text)) filter(col == 1, row > last_data_row, !grepl("^Notes$", base_text))
notes_text <- if (nrow(notes_cells) > 0) notes_text <- if (nrow(notes_cells) > 0) {
paste(notes_cells$full_text, collapse = "\n") else "" paste(notes_cells$full_text, collapse = "\n")
} else {
""
}
if (is.na(notes_text)) notes_text <- "" if (is.na(notes_text)) notes_text <- ""
notes_lookup <- parse_notes_block(notes_text) notes_lookup <- parse_notes_block(notes_text)
@@ -1101,7 +1177,9 @@ parse_topical_sheet <- function(xlsx_path, abbreviations = list()) {
} }
} }
if (idx == 0) return(NULL) if (idx == 0) {
return(NULL)
}
bind_rows(results[seq_len(idx)]) bind_rows(results[seq_len(idx)])
} }
@@ -1144,8 +1222,12 @@ parse_yeast_sheet <- function(xlsx_path, sheet_name, abbreviations = list()) {
# Each block starts at a cell reading "S <=" (S \u2264) and runs through the # Each block starts at a cell reading "S <=" (S \u2264) and runs through the
# next "R >" and, where present, an immediately following "ATU" column, # next "R >" and, where present, an immediately following "ATU" column,
# stopping before the next "S <=" or the end of populated columns. # stopping before the next "S <=" or the end of populated columns.
sub_cells <- parsed |> filter(row == sub_row) |> arrange(col) sub_cells <- parsed |>
s_cols <- sub_cells |> filter(grepl("^S\\b", base_text)) |> pull(col) filter(row == sub_row) |>
arrange(col)
s_cols <- sub_cells |>
filter(grepl("^S\\b", base_text)) |>
pull(col)
if (length(s_cols) == 0) { if (length(s_cols) == 0) {
message(" No species S/R columns found in sheet '", sheet_name, "', skipping") message(" No species S/R columns found in sheet '", sheet_name, "', skipping")
@@ -1251,7 +1333,9 @@ parse_yeast_sheet <- function(xlsx_path, sheet_name, abbreviations = list()) {
} }
} }
if (idx == 0) return(NULL) if (idx == 0) {
return(NULL)
}
bind_rows(results[seq_len(idx)]) bind_rows(results[seq_len(idx)])
} }
@@ -1289,8 +1373,10 @@ parse_workbook_notes <- function(xlsx_path) {
all_sheets <- xlsx_sheet_names(xlsx_path) all_sheets <- xlsx_sheet_names(xlsx_path)
notes_sheet <- all_sheets[grepl("^\\s*(\\d+\\.\\s*)?Notes\\s*$", all_sheets, ignore.case = TRUE)] notes_sheet <- all_sheets[grepl("^\\s*(\\d+\\.\\s*)?Notes\\s*$", all_sheets, ignore.case = TRUE)]
if (length(notes_sheet) != 1) { if (length(notes_sheet) != 1) {
stop("parse_workbook_notes(): expected exactly one 'Notes' sheet in '", basename(xlsx_path), stop(
"', found: ", paste(notes_sheet, collapse = ", ")) "parse_workbook_notes(): expected exactly one 'Notes' sheet in '", basename(xlsx_path),
"', found: ", paste(notes_sheet, collapse = ", ")
)
} }
txt <- parse_cells(xlsx_cells(xlsx_path, sheets = notes_sheet), xlsx_formats(xlsx_path)) |> txt <- parse_cells(xlsx_cells(xlsx_path, sheets = notes_sheet), xlsx_formats(xlsx_path)) |>
filter(!is.na(full_text)) |> filter(!is.na(full_text)) |>
@@ -1322,10 +1408,12 @@ parse_workbook_notes <- function(xlsx_path) {
# 10. Parse all data sheets # 10. Parse all data sheets
# ============================================================================== # ==============================================================================
parse_workbook <- function(xlsx_path, parse_workbook <- function(xlsx_path,
skip_sheets = c("Content", "Changes", "Notes", skip_sheets = c(
"Content", "Changes", "Notes",
"Guidance", "Dosages", "Guidance", "Dosages",
"Technical uncertainty", "Technical uncertainty",
"Non-species related breakpoints")) { "Non-species related breakpoints"
)) {
all_sheets <- xlsx_sheet_names(xlsx_path) all_sheets <- xlsx_sheet_names(xlsx_path)
# Some workbooks (e.g. the antifungal AFST tables) number-prefix their # Some workbooks (e.g. the antifungal AFST tables) number-prefix their
@@ -1337,8 +1425,10 @@ parse_workbook <- function(xlsx_path,
# (with underscores read as spaces) by whether a skip name occurs anywhere # (with underscores read as spaces) by whether a skip name occurs anywhere
# in the (trimmed) sheet name instead. # in the (trimmed) sheet name instead.
is_skipped <- vapply(gsub("_", " ", trimws(all_sheets)), function(s) { is_skipped <- vapply(gsub("_", " ", trimws(all_sheets)), function(s) {
any(vapply(skip_sheets, function(skip) grepl(tolower(skip), tolower(s), fixed = TRUE), any(vapply(
logical(1))) skip_sheets, function(skip) grepl(tolower(skip), tolower(s), fixed = TRUE),
logical(1)
))
}, logical(1)) }, logical(1))
# Hidden sheets are not part of the published tables: v14.0-v16.1 contain a # Hidden sheets are not part of the published tables: v14.0-v16.1 contain a
# hidden, outdated copy of the v14.0 PK-PD table with editorial comments # hidden, outdated copy of the v14.0 PK-PD table with editorial comments
@@ -1386,13 +1476,16 @@ parse_workbook <- function(xlsx_path,
# data (they are excluded from the clinical_breakpoints table). # data (they are excluded from the clinical_breakpoints table).
if (any(out$is_bracketed)) { if (any(out$is_bracketed)) {
if (is.na(wb_notes$bracket_definition)) { if (is.na(wb_notes$bracket_definition)) {
stop("parse_workbook(): '", basename(xlsx_path), "' has bracketed breakpoints, ", stop(
"but no definition of breakpoints in brackets was found on its Notes sheet") "parse_workbook(): '", basename(xlsx_path), "' has bracketed breakpoints, ",
"but no definition of breakpoints in brackets was found on its Notes sheet"
)
} }
bracket_note <- paste0("[( )] ", wb_notes$bracket_definition) bracket_note <- paste0("[( )] ", wb_notes$bracket_definition)
out$note[out$is_bracketed] <- ifelse(is.na(out$note[out$is_bracketed]), out$note[out$is_bracketed] <- ifelse(is.na(out$note[out$is_bracketed]),
bracket_note, bracket_note,
paste(bracket_note, out$note[out$is_bracketed], sep = " | ")) paste(bracket_note, out$note[out$is_bracketed], sep = " | ")
)
} }
out out
} }
@@ -1401,17 +1494,18 @@ parse_workbook <- function(xlsx_path,
# 11. Run # 11. Run
# ============================================================================== # ==============================================================================
breakpoint_files <- list.files(path = "data-raw", breakpoint_files <- list.files(
path = "data-raw",
pattern = "breakpoint.*table.*[.]xlsx$", pattern = "breakpoint.*table.*[.]xlsx$",
full.names = TRUE, full.names = TRUE,
recursive = FALSE, recursive = FALSE,
ignore.case = TRUE) ignore.case = TRUE
)
breakpoint_files <- breakpoint_files[breakpoint_files %unlike% "dosages"] breakpoint_files <- breakpoint_files[breakpoint_files %unlike% "dosages"]
breakpoints_eucast <- tibble() breakpoints_eucast <- tibble()
for (xlsx_path in breakpoint_files) { for (xlsx_path in breakpoint_files) {
message("Parsing ", basename(xlsx_path)) message("Parsing ", basename(xlsx_path))
result <- suppressMessages(parse_workbook(xlsx_path)) result <- suppressMessages(parse_workbook(xlsx_path))
@@ -1436,8 +1530,10 @@ for (xlsx_path in breakpoint_files) {
breakpoints_eucast_raw <- breakpoints_eucast breakpoints_eucast_raw <- breakpoints_eucast
if (!is.null(parse_log$disk_dose_inherited)) { if (!is.null(parse_log$disk_dose_inherited)) {
message("NOTE: ", nrow(parse_log$disk_dose_inherited), " disk breakpoint row(s) had no disk content and took it ", message(
"from the previous row of the same agent:") "NOTE: ", nrow(parse_log$disk_dose_inherited), " disk breakpoint row(s) had no disk content and took it ",
"from the previous row of the same agent:"
)
print(parse_log$disk_dose_inherited) print(parse_log$disk_dose_inherited)
} }
@@ -1525,7 +1621,8 @@ split_organism_from_ab <- function(ab, mo) {
# or contains known taxonomic suffixes (cocci, bacill, etc.) # or contains known taxonomic suffixes (cocci, bacill, etc.)
is_organism <- grepl( is_organism <- grepl(
"^(other\\b|except\\b|[a-z]*cocc|[a-z]*bacill|[A-Z][a-z]{2,}\\b)", "^(other\\b|except\\b|[a-z]*cocc|[a-z]*bacill|[A-Z][a-z]{2,}\\b)",
after, ignore.case = FALSE, perl = TRUE after,
ignore.case = FALSE, perl = TRUE
) )
if (!is_organism) next if (!is_organism) next
@@ -1637,8 +1734,10 @@ build_site <- function(sheet_site, route, qualifier) {
q <- tolower(gsub("\\s+", " ", trimws(qualifier))) q <- tolower(gsub("\\s+", " ", trimws(qualifier)))
unknown <- unique(qualifier[!is.na(q) & !q %in% names(eucast_site_vocabulary)]) unknown <- unique(qualifier[!is.na(q) & !q %in% names(eucast_site_vocabulary)])
if (length(unknown) > 0) { if (length(unknown) > 0) {
stop("build_site(): unknown indication qualifier(s), add to eucast_site_vocabulary: '", stop(
paste(unknown, collapse = "', '"), "'") "build_site(): unknown indication qualifier(s), add to eucast_site_vocabulary: '",
paste(unknown, collapse = "', '"), "'"
)
} }
indication <- unname(eucast_site_vocabulary[q]) indication <- unname(eucast_site_vocabulary[q])
vapply(seq_along(q), function(i) { vapply(seq_along(q), function(i) {
@@ -1808,7 +1907,9 @@ split_mo_list_one <- function(s, sheet_genus_fallback) {
pieces <- strip_mo_exceptions(pieces) pieces <- strip_mo_exceptions(pieces)
pieces <- trimws(gsub("\\s*\\(\\s*\\)\\s*$", "", pieces)) # drop now-empty "()" leftovers pieces <- trimws(gsub("\\s*\\(\\s*\\)\\s*$", "", pieces)) # drop now-empty "()" leftovers
if (length(pieces) <= 1) return(s_clean) if (length(pieces) <= 1) {
return(s_clean)
}
# Only treat as a genuine multi-taxon list if every piece looks like a # Only treat as a genuine multi-taxon list if every piece looks like a
# taxon on its own: either an abbreviated genus initial ("E.", "P." # taxon on its own: either an abbreviated genus initial ("E.", "P."
@@ -1844,7 +1945,9 @@ split_mo_list_one <- function(s, sheet_genus_fallback) {
# whole cell falls back to the original, unsplit string. # whole cell falls back to the original, unsplit string.
looks_like_taxon <- grepl("^[A-Z]\\.\\s", pieces) | grepl("^[A-Z][a-z]+(\\.|\\s|$)", pieces) | looks_like_taxon <- grepl("^[A-Z]\\.\\s", pieces) | grepl("^[A-Z][a-z]+(\\.|\\s|$)", pieces) |
grepl("^[a-z]+$", pieces) | grepl("^coagulase-negative staphylococci$", pieces, ignore.case = TRUE) grepl("^[a-z]+$", pieces) | grepl("^coagulase-negative staphylococci$", pieces, ignore.case = TRUE)
if (!all(looks_like_taxon)) return(s_clean) if (!all(looks_like_taxon)) {
return(s_clean)
}
# Reject a piece containing more than one capitalised, 3+ letter word # Reject a piece containing more than one capitalised, 3+ letter word
# (a rough genus/proper-name detector): this signals two taxa were # (a rough genus/proper-name detector): this signals two taxa were
@@ -1856,9 +1959,13 @@ split_mo_list_one <- function(s, sheet_genus_fallback) {
# as.mo("Citrobacter spp. Klebsiella spp.") resolves to Citrobacter # as.mo("Citrobacter spp. Klebsiella spp.") resolves to Citrobacter
# enshiensis) -- so this must be caught before as.mo() ever sees it, # enshiensis) -- so this must be caught before as.mo() ever sees it,
# by falling back to the unsplit original string. # by falling back to the unsplit original string.
genus_like_count <- vapply(pieces, function(p) length(gregexpr("\\b[A-Z][a-z]{2,}\\b", p, perl = TRUE)[[1]]), genus_like_count <- vapply(
integer(1)) pieces, function(p) length(gregexpr("\\b[A-Z][a-z]{2,}\\b", p, perl = TRUE)[[1]]),
if (any(genus_like_count > 1)) return(s_clean) integer(1)
)
if (any(genus_like_count > 1)) {
return(s_clean)
}
expand_abbreviated_genus(pieces, fallback_genus = sheet_genus_fallback) expand_abbreviated_genus(pieces, fallback_genus = sheet_genus_fallback)
} }
@@ -1908,10 +2015,14 @@ breakpoints_eucast <- breakpoints_eucast_raw |>
is_blocker = is_bracketed | (!is_bp_value(breakpoint_S) & !is_bp_value(breakpoint_R)), is_blocker = is_bracketed | (!is_bp_value(breakpoint_S) & !is_bp_value(breakpoint_R)),
note = case_when( note = case_when(
is_bracketed ~ blocking_note(paste0("EUCAST gives breakpoints in brackets only (S ", breakpoint_S, ", R ", breakpoint_R, ")"), note), is_bracketed ~ blocking_note(paste0("EUCAST gives breakpoints in brackets only (S ", breakpoint_S, ", R ", breakpoint_R, ")"), note),
is_blocker ~ blocking_note(paste0("EUCAST gives ", is_blocker ~ blocking_note(paste0(
coalesce(bp_placeholder_meaning[toupper(trimws(breakpoint_S))], "EUCAST gives ",
coalesce(
bp_placeholder_meaning[toupper(trimws(breakpoint_S))],
bp_placeholder_meaning[toupper(trimws(breakpoint_R))], bp_placeholder_meaning[toupper(trimws(breakpoint_R))],
"no value")), note), "no value"
)
), note),
TRUE ~ note TRUE ~ note
), ),
# placeholders become NA; a row keeps a breakpoint as long as one of S and # placeholders become NA; a row keeps a breakpoint as long as one of S and
@@ -1929,8 +2040,10 @@ breakpoints_eucast <- breakpoints_eucast_raw |>
ab_mo_fix <- split_organism_from_ab(breakpoints_eucast$ab, breakpoints_eucast$mo) ab_mo_fix <- split_organism_from_ab(breakpoints_eucast$ab, breakpoints_eucast$mo)
fixed_rows <- !is.na(ab_mo_fix$mo_from_ab) fixed_rows <- !is.na(ab_mo_fix$mo_from_ab)
if (any(fixed_rows)) { if (any(fixed_rows)) {
message("NOTE: ", sum(fixed_rows), " row(s) had an organism restriction embedded in the ab column; ", message(
"splitting into ab + mo (e.g. '", breakpoints_eucast$ab[which(fixed_rows)[1]], "').") "NOTE: ", sum(fixed_rows), " row(s) had an organism restriction embedded in the ab column; ",
"splitting into ab + mo (e.g. '", breakpoints_eucast$ab[which(fixed_rows)[1]], "')."
)
breakpoints_eucast$ab[fixed_rows] <- ab_mo_fix$ab_clean[fixed_rows] breakpoints_eucast$ab[fixed_rows] <- ab_mo_fix$ab_clean[fixed_rows]
breakpoints_eucast$mo[fixed_rows] <- ab_mo_fix$mo_from_ab[fixed_rows] breakpoints_eucast$mo[fixed_rows] <- ab_mo_fix$mo_from_ab[fixed_rows]
} }
@@ -1939,8 +2052,10 @@ if (any(fixed_rows)) {
breakpoints_eucast <- breakpoints_eucast |> breakpoints_eucast <- breakpoints_eucast |>
mutate(note = case_when( mutate(note = case_when(
trimws(gsub("\\s+", " ", mo)) != "MRSA" ~ note, trimws(gsub("\\s+", " ", mo)) != "MRSA" ~ note,
is_blocker ~ trimws(paste0(trimws(sub("^(\\[No breakpoint\\][^|]*).*$", "\\1", note)), " | [Scope] EUCAST gives this for MRSA ", is_blocker ~ trimws(paste0(
sub("^\\[No breakpoint\\][^|]*", "", note))), trimws(sub("^(\\[No breakpoint\\][^|]*).*$", "\\1", note)), " | [Scope] EUCAST gives this for MRSA ",
sub("^\\[No breakpoint\\][^|]*", "", note)
)),
is.na(note) ~ "[Scope] EUCAST gives this breakpoint for MRSA", is.na(note) ~ "[Scope] EUCAST gives this breakpoint for MRSA",
TRUE ~ paste("[Scope] EUCAST gives this breakpoint for MRSA", note, sep = " | ") TRUE ~ paste("[Scope] EUCAST gives this breakpoint for MRSA", note, sep = " | ")
)) ))
@@ -1981,7 +2096,8 @@ extract_mo_exceptions <- function(x) {
pieces <- pieces[nzchar(pieces)] pieces <- pieces[nzchar(pieces)]
if (nzchar(genus)) { if (nzchar(genus)) {
pieces <- ifelse(grepl("^[A-Z]\\.\\s*[a-z]", pieces) & substr(pieces, 1, 1) == substr(genus, 1, 1), pieces <- ifelse(grepl("^[A-Z]\\.\\s*[a-z]", pieces) & substr(pieces, 1, 1) == substr(genus, 1, 1),
sub("^[A-Z]\\.\\s*", paste0(genus, " "), pieces), pieces) sub("^[A-Z]\\.\\s*", paste0(genus, " "), pieces), pieces
)
} }
pieces pieces
})) }))
@@ -1994,9 +2110,11 @@ exceptions <- breakpoints_eucast |>
tidyr::unnest(exception) tidyr::unnest(exception)
if (nrow(exceptions) > 0) { if (nrow(exceptions) > 0) {
exception_blockers <- exceptions |> exception_blockers <- exceptions |>
mutate(note = blocking_note(paste0("EUCAST excludes ", exception, " ('", gsub("\\s+", " ", mo), "')"), NA_character_), mutate(
note = blocking_note(paste0("EUCAST excludes ", exception, " ('", gsub("\\s+", " ", mo), "')"), NA_character_),
mo = exception, site = NA_character_, is_blocker = TRUE, mo = exception, site = NA_character_, is_blocker = TRUE,
breakpoint_S = NA_character_, breakpoint_R = NA_character_) |> breakpoint_S = NA_character_, breakpoint_R = NA_character_
) |>
select(-exception) select(-exception)
breakpoints_eucast <- bind_rows(breakpoints_eucast, exception_blockers) breakpoints_eucast <- bind_rows(breakpoints_eucast, exception_blockers)
} }
@@ -2042,21 +2160,25 @@ mo_uncertainties_eucast <- AMR:::AMR_env$mo_uncertainties
# does not cover the group, as happened with "S. anginosus group". # does not cover the group, as happened with "S. anginosus group".
unmapped_groups <- breakpoints_eucast |> unmapped_groups <- breakpoints_eucast |>
distinct(mo_text, mo) |> distinct(mo_text, mo) |>
filter(grepl("group|anaerob|MRSA|MSSA|-negative|-positive|cocci\\b|bacteria\\b", mo_text, ignore.case = TRUE), filter(
grepl("group|anaerob|MRSA|MSSA|-negative|-positive|cocci\\b|bacteria\\b", mo_text, ignore.case = TRUE),
!grepl("spp\\.$", mo_text), !grepl("spp\\.$", mo_text),
# a text that is exactly the name of the taxon it resolved to, such # a text that is exactly the name of the taxon it resolved to, such
# as "Peptostreptococcus anaerobius", is not a descriptive group # as "Peptostreptococcus anaerobius", is not a descriptive group
tolower(mo_text) != tolower(mo_name(mo, keep_synonyms = TRUE)), tolower(mo_text) != tolower(mo_name(mo, keep_synonyms = TRUE)),
!mo_rank(mo, keep_synonyms = TRUE) %in% "species group", !mo_rank(mo, keep_synonyms = TRUE) %in% "species group",
!mo %in% unlist(eucast_organism_groups)) !mo %in% unlist(eucast_organism_groups)
)
if (nrow(unmapped_groups) > 0) { if (nrow(unmapped_groups) > 0) {
print(unmapped_groups |> mutate(name = mo_name(mo, keep_synonyms = TRUE))) print(unmapped_groups |> mutate(name = mo_name(mo, keep_synonyms = TRUE)))
stop(nrow(unmapped_groups), " descriptive organism group(s) resolved to a single taxon, add them to eucast_organism_groups") stop(nrow(unmapped_groups), " descriptive organism group(s) resolved to a single taxon, add them to eucast_organism_groups")
} }
if (nrow(mo_uncertainties_eucast) > 0) { if (nrow(mo_uncertainties_eucast) > 0) {
message("NOTE: ", nrow(mo_uncertainties_eucast), " organism string(s) were resolved with uncertainty ", message(
"NOTE: ", nrow(mo_uncertainties_eucast), " organism string(s) were resolved with uncertainty ",
"(matched below the default confidence, or against multiple candidates). ", "(matched below the default confidence, or against multiple candidates). ",
"Inspect `mo_uncertainties_eucast` and cross-check against the source sheet before trusting these rows.") "Inspect `mo_uncertainties_eucast` and cross-check against the source sheet before trusting these rows."
)
} }
breakpoints_eucast <- breakpoints_eucast |> breakpoints_eucast <- breakpoints_eucast |>
@@ -2100,9 +2222,11 @@ ab_previously_coerced_eucast <- AMR:::AMR_env$ab_previously_coerced |>
filter(tolower(trimws(x_bak)) %in% tolower(trimws(unique(breakpoints_eucast$ab_input)))) filter(tolower(trimws(x_bak)) %in% tolower(trimws(unique(breakpoints_eucast$ab_input))))
breakpoints_eucast <- breakpoints_eucast |> select(-ab_input) breakpoints_eucast <- breakpoints_eucast |> select(-ab_input)
if (nrow(ab_previously_coerced_eucast) > 0) { if (nrow(ab_previously_coerced_eucast) > 0) {
message("NOTE: ", nrow(ab_previously_coerced_eucast), " antibiotic string(s) were resolved via approximate/previous-coercion ", message(
"NOTE: ", nrow(ab_previously_coerced_eucast), " antibiotic string(s) were resolved via approximate/previous-coercion ",
"matching rather than an exact code or name match. ", "matching rather than an exact code or name match. ",
"Inspect `ab_previously_coerced_eucast` and cross-check against the source sheet before trusting these rows.") "Inspect `ab_previously_coerced_eucast` and cross-check against the source sheet before trusting these rows."
)
} }
# Restrictions in the notes ---- # Restrictions in the notes ----
@@ -2185,8 +2309,10 @@ taxon_contains <- function(broad, narrow) {
broad %in% AMR::microorganisms.groups$mo_group[AMR::microorganisms.groups$mo == as.character(narrow)] broad %in% AMR::microorganisms.groups$mo_group[AMR::microorganisms.groups$mo == as.character(narrow)]
} }
to_blocker <- function(df, reason) { to_blocker <- function(df, reason) {
df |> mutate(is_blocker = TRUE, breakpoint_S = NA_character_, breakpoint_R = NA_character_, df |> mutate(
site = NA_character_, note = blocking_note(reason, note)) is_blocker = TRUE, breakpoint_S = NA_character_, breakpoint_R = NA_character_,
site = NA_character_, note = blocking_note(reason, note)
)
} }
for (i in seq_len(nrow(eucast_note_rules))) { for (i in seq_len(nrow(eucast_note_rules))) {
rule <- eucast_note_rules[i, ] rule <- eucast_note_rules[i, ]
@@ -2213,7 +2339,9 @@ for (i in seq_len(nrow(eucast_note_rules))) {
breakpoints_eucast$site[governed] <- if_else(is.na(breakpoints_eucast$site[governed]), rule$target, breakpoints_eucast$site[governed] <- if_else(is.na(breakpoints_eucast$site[governed]), rule$target,
if_else(grepl(rule$target, breakpoints_eucast$site[governed], fixed = TRUE), if_else(grepl(rule$target, breakpoints_eucast$site[governed], fixed = TRUE),
breakpoints_eucast$site[governed], breakpoints_eucast$site[governed],
paste(breakpoints_eucast$site[governed], rule$target, sep = ", "))) paste(breakpoints_eucast$site[governed], rule$target, sep = ", ")
)
)
} else if (rule$action == "restrict") { } else if (rule$action == "restrict") {
rows <- which(governed & !breakpoints_eucast$is_blocker) rows <- which(governed & !breakpoints_eucast$is_blocker)
for (r in rows) { for (r in rows) {
@@ -2269,8 +2397,10 @@ derived_rows <- list()
# (only the rows of the test itself, e.g. "Gentamicin (test for high-level # (only the rows of the test itself, e.g. "Gentamicin (test for high-level
# aminoglycoside resistance)"; other aminoglycosides refer to the same note) # aminoglycoside resistance)"; other aminoglycosides refer to the same note)
hlar_rows <- breakpoints_eucast |> hlar_rows <- breakpoints_eucast |>
filter(grepl("Negative test: Isolates with (gentamicin|streptomycin) MIC \u2264", note), filter(
ab %in% c("GEN", "STR1"), site %in% "Screen") grepl("Negative test: Isolates with (gentamicin|streptomycin) MIC \u2264", note),
ab %in% c("GEN", "STR1"), site %in% "Screen"
)
for (r in seq_len(nrow(hlar_rows))) { for (r in seq_len(nrow(hlar_rows))) {
row <- hlar_rows[r, ] row <- hlar_rows[r, ]
# the note of a row can also hold the test for the other agent, so the # the note of a row can also hold the test for the other agent, so the
@@ -2285,27 +2415,35 @@ for (r in seq_len(nrow(hlar_rows))) {
value <- if (row$method == "MIC") neg[2] else neg[3] value <- if (row$method == "MIC") neg[2] else neg[3]
if (!nzchar(value)) next # e.g. no zone diameter given in this version if (!nzchar(value)) next # e.g. no zone diameter given in this version
derived_rows[[length(derived_rows) + 1]] <- row |> derived_rows[[length(derived_rows) + 1]] <- row |>
mutate(ab = as.ab(if (agent == "gentamicin") "GEH" else "STH"), site = NA_character_, is_blocker = FALSE, mutate(
ab = as.ab(if (agent == "gentamicin") "GEH" else "STH"), site = NA_character_, is_blocker = FALSE,
breakpoint_S = value, breakpoint_R = value, breakpoint_S = value, breakpoint_R = value,
note = sub("^\\[No breakpoint\\][^|]*(\\| )?", "", note)) note = sub("^\\[No breakpoint\\][^|]*(\\| )?", "", note)
)
} }
fox_mic <- breakpoints_eucast |> fox_mic <- breakpoints_eucast |>
filter(ab == "FOX", method == "MIC", filter(
grepl("S\\. aureus and S\\. lugdunensis with cefoxitin MIC values >[0-9]+ mg/L and S\\. saprophyticus with cefoxitin MIC values >[0-9]+ mg/L are methicillin resistant", note)) |> ab == "FOX", method == "MIC",
grepl("S\\. aureus and S\\. lugdunensis with cefoxitin MIC values >[0-9]+ mg/L and S\\. saprophyticus with cefoxitin MIC values >[0-9]+ mg/L are methicillin resistant", note)
) |>
distinct(guideline, .keep_all = TRUE) distinct(guideline, .keep_all = TRUE)
for (r in seq_len(nrow(fox_mic))) { for (r in seq_len(nrow(fox_mic))) {
row <- fox_mic[r, ] row <- fox_mic[r, ]
m <- regmatches(row$note, regexec("S\\. aureus and S\\. lugdunensis with cefoxitin MIC values >([0-9]+) mg/L and S\\. saprophyticus with cefoxitin MIC values >([0-9]+) mg/L", row$note))[[1]] m <- regmatches(row$note, regexec("S\\. aureus and S\\. lugdunensis with cefoxitin MIC values >([0-9]+) mg/L and S\\. saprophyticus with cefoxitin MIC values >([0-9]+) mg/L", row$note))[[1]]
for (sp in list(c("B_STPHY_AURS", m[2]), c("B_STPHY_LGDN", m[2]), c("B_STPHY_SPRP", m[3]))) { for (sp in list(c("B_STPHY_AURS", m[2]), c("B_STPHY_LGDN", m[2]), c("B_STPHY_SPRP", m[3]))) {
derived_rows[[length(derived_rows) + 1]] <- row |> derived_rows[[length(derived_rows) + 1]] <- row |>
mutate(mo = as.mo(sp[1]), site = "Screen", is_blocker = FALSE, breakpoint_S = sp[2], breakpoint_R = sp[2], mutate(
note = sub("^\\[No breakpoint\\][^|]*(\\| )?", "", note)) mo = as.mo(sp[1]), site = "Screen", is_blocker = FALSE, breakpoint_S = sp[2], breakpoint_R = sp[2],
note = sub("^\\[No breakpoint\\][^|]*(\\| )?", "", note)
)
} }
} }
if (length(derived_rows) > 0) { if (length(derived_rows) > 0) {
derived <- bind_rows(derived_rows) derived <- bind_rows(derived_rows)
message("NOTE: ", nrow(derived), " screening breakpoint row(s) taken from the notes (", message(
paste(unique(paste(derived$guideline, derived$ab, derived$method)), collapse = ", "), ")") "NOTE: ", nrow(derived), " screening breakpoint row(s) taken from the notes (",
paste(unique(paste(derived$guideline, derived$ab, derived$method)), collapse = ", "), ")"
)
breakpoints_eucast <- bind_rows(breakpoints_eucast, derived) breakpoints_eucast <- bind_rows(breakpoints_eucast, derived)
} }
cons_rule <- "If coagulase-negative staphylococci are not identified to species level, use zone diameter breakpoints S\u2265([0-9]+), R<([0-9]+) mm" cons_rule <- "If coagulase-negative staphylococci are not identified to species level, use zone diameter breakpoints S\u2265([0-9]+), R<([0-9]+) mm"
@@ -2363,8 +2501,10 @@ breakpoints_eucast <- bind_rows(breakpoints_eucast, scope_blockers)
if (!is.null(parse_log$mic_scope_extensions)) { if (!is.null(parse_log$mic_scope_extensions)) {
scope_ext <- parse_log$mic_scope_extensions |> scope_ext <- parse_log$mic_scope_extensions |>
distinct() |> distinct() |>
mutate(from_mo = as.character(as.mo(from_text, info = FALSE)), mutate(
to_mo = as.character(as.mo(to_text, info = FALSE))) from_mo = as.character(as.mo(from_text, info = FALSE)),
to_mo = as.character(as.mo(to_text, info = FALSE))
)
if (anyNA(scope_ext$from_mo) || anyNA(scope_ext$to_mo) || any(c(scope_ext$from_mo, scope_ext$to_mo) == "UNKNOWN")) { if (anyNA(scope_ext$from_mo) || anyNA(scope_ext$to_mo) || any(c(scope_ext$from_mo, scope_ext$to_mo) == "UNKNOWN")) {
print(scope_ext) print(scope_ext)
stop("species in a MIC scope statement could not be resolved, see above") stop("species in a MIC scope statement could not be resolved, see above")
@@ -2373,18 +2513,25 @@ if (!is.null(parse_log$mic_scope_extensions)) {
filter(method == "MIC") |> filter(method == "MIC") |>
mutate(mo_chr = as.character(mo)) |> mutate(mo_chr = as.character(mo)) |>
inner_join(scope_ext |> select(guideline, sheet, mo_chr = from_mo, to_mo, statement), inner_join(scope_ext |> select(guideline, sheet, mo_chr = from_mo, to_mo, statement),
by = c("guideline", "sheet", "mo_chr")) |> by = c("guideline", "sheet", "mo_chr")
mutate(mo = as.mo(to_mo), ) |>
mutate(
mo = as.mo(to_mo),
note = case_when( note = case_when(
is_blocker ~ trimws(paste0(trimws(sub("^(\\[No breakpoint\\][^|]*).*$", "\\1", note)), " | [Scope] ", statement, " ", is_blocker ~ trimws(paste0(
sub("^\\[No breakpoint\\][^|]*", "", note))), trimws(sub("^(\\[No breakpoint\\][^|]*).*$", "\\1", note)), " | [Scope] ", statement, " ",
sub("^\\[No breakpoint\\][^|]*", "", note)
)),
is.na(note) ~ paste0("[Scope] ", statement), is.na(note) ~ paste0("[Scope] ", statement),
TRUE ~ paste0("[Scope] ", statement, " | ", note) TRUE ~ paste0("[Scope] ", statement, " | ", note)
)) |> )
) |>
select(-mo_chr, -to_mo, -statement) |> select(-mo_chr, -to_mo, -statement) |>
anti_join(breakpoints_eucast |> filter(method == "MIC"), by = c("guideline", "mo", "ab", "method", "site")) anti_join(breakpoints_eucast |> filter(method == "MIC"), by = c("guideline", "mo", "ab", "method", "site"))
message("NOTE: ", nrow(scope_rows), " MIC breakpoint row(s) added following EUCAST's statement(s): ", message(
paste(unique(paste0(scope_ext$guideline, " ", scope_ext$from_text, " -> ", scope_ext$to_text)), collapse = ", ")) "NOTE: ", nrow(scope_rows), " MIC breakpoint row(s) added following EUCAST's statement(s): ",
paste(unique(paste0(scope_ext$guideline, " ", scope_ext$from_text, " -> ", scope_ext$to_text)), collapse = ", ")
)
breakpoints_eucast <- bind_rows(breakpoints_eucast, scope_rows) breakpoints_eucast <- bind_rows(breakpoints_eucast, scope_rows)
} }
@@ -2470,8 +2617,12 @@ breakpoints_eucast <- breakpoints_eucast |>
# the parsing above (e.g. a new free-text pattern), never a genuine ambiguity. # the parsing above (e.g. a new free-text pattern), never a genuine ambiguity.
# The only exception is the non-species related PK-PD table, which is coded # The only exception is the non-species related PK-PD table, which is coded
# as "UNKNOWN" on purpose (as.sir() uses it as the last resort). # as "UNKNOWN" on purpose (as.sir() uses it as the last resort).
unresolved_mo <- breakpoints_eucast |> filter(is.na(mo) | (mo == "UNKNOWN" & !grepl("^PK", sheet))) |> distinct(sheet, ref_tbl, ab, mo_text) unresolved_mo <- breakpoints_eucast |>
unresolved_ab <- breakpoints_eucast |> filter(is.na(ab)) |> distinct(sheet, ref_tbl) filter(is.na(mo) | (mo == "UNKNOWN" & !grepl("^PK", sheet))) |>
distinct(sheet, ref_tbl, ab, mo_text)
unresolved_ab <- breakpoints_eucast |>
filter(is.na(ab)) |>
distinct(sheet, ref_tbl)
if (nrow(unresolved_mo) > 0 || nrow(unresolved_ab) > 0) { if (nrow(unresolved_mo) > 0 || nrow(unresolved_ab) > 0) {
print(unresolved_mo, n = Inf) print(unresolved_mo, n = Inf)
print(unresolved_ab, n = Inf) print(unresolved_ab, n = Inf)
@@ -2551,10 +2702,14 @@ if (nrow(bad_rows) > 0) {
} }
breakpoints_eucast <- breakpoints_eucast |> breakpoints_eucast <- breakpoints_eucast |>
select(guideline, type, host, method, site, mo, rank_index, ab, ref_tbl, select(
disk_dose, breakpoint_S, breakpoint_R, uti, is_SDD, note) guideline, type, host, method, site, mo, rank_index, ab, ref_tbl,
disk_dose, breakpoint_S, breakpoint_R, uti, is_SDD, note
)
breakpoints_eucast |> count(guideline) |> print() breakpoints_eucast |>
count(guideline) |>
print()
# Which guideline years these workbooks cover, separately for bacteria and # Which guideline years these workbooks cover, separately for bacteria and
# fungi: the bacterial tables and the antifungal (AFST) tables are separate # fungi: the bacterial tables and the antifungal (AFST) tables are separate
@@ -2578,7 +2733,8 @@ merge_eucast_breakpoints <- function(clinical_breakpoints, breakpoints_eucast, e
scope_of <- function(mo, ab) { scope_of <- function(mo, ab) {
if_else(mo_kingdom(mo, keep_synonyms = TRUE) %in% "Fungi" | if_else(mo_kingdom(mo, keep_synonyms = TRUE) %in% "Fungi" |
(as.character(mo) == "UNKNOWN" & ab_group(ab) %in% "Antifungals"), (as.character(mo) == "UNKNOWN" & ab_group(ab) %in% "Antifungals"),
"Fungi", "Bacteria") "Fungi", "Bacteria"
)
} }
# the organisms in the EUCAST rows must agree with the type of workbook # the organisms in the EUCAST rows must agree with the type of workbook
# they come from # they come from
@@ -2595,9 +2751,11 @@ merge_eucast_breakpoints <- function(clinical_breakpoints, breakpoints_eucast, e
filter(type == "human") filter(type == "human")
kept <- clinical_breakpoints |> kept <- clinical_breakpoints |>
anti_join(replaced, by = names(clinical_breakpoints)) anti_join(replaced, by = names(clinical_breakpoints))
message("Replacing ", nrow(replaced), " WHONET-based human EUCAST rows (", message(
"Replacing ", nrow(replaced), " WHONET-based human EUCAST rows (",
paste(sort(unique(paste(replaced$guideline, replaced$scope))), collapse = ", "), paste(sort(unique(paste(replaced$guideline, replaced$scope))), collapse = ", "),
") with ", nrow(breakpoints_eucast), " rows from the EUCAST workbooks") ") with ", nrow(breakpoints_eucast), " rows from the EUCAST workbooks"
)
# rows without any breakpoint are only kept as the blocking rows of the # rows without any breakpoint are only kept as the blocking rows of the
# EUCAST workbooks (see "Blocking rows") # EUCAST workbooks (see "Blocking rows")
kept |> kept |>
@@ -63,8 +63,10 @@ all(int_resis3$ab %in% antimicrobials$ab)
intrinsic_resistant <- int_resis3 intrinsic_resistant <- int_resis3
intrinsic_resistant$version <- paste(EUCAST_VERSION_EXPECTED_PHENOTYPES$`1.2`$title, intrinsic_resistant$version <- paste(
EUCAST_VERSION_EXPECTED_PHENOTYPES$`1.2`$version_txt) EUCAST_VERSION_EXPECTED_PHENOTYPES$`1.2`$title,
EUCAST_VERSION_EXPECTED_PHENOTYPES$`1.2`$version_txt
)
usethis::use_data(intrinsic_resistant, internal = FALSE, overwrite = TRUE, version = 2, compress = "xz") usethis::use_data(intrinsic_resistant, internal = FALSE, overwrite = TRUE, version = 2, compress = "xz")
rm(intrinsic_resistant) rm(intrinsic_resistant)
@@ -359,7 +359,8 @@ get_lpsn_and_author <- function(rank, name, tries = 3) {
for (try in seq_len(tries)) { for (try in seq_len(tries)) {
# be polite to the LPSN server # be polite to the LPSN server
Sys.sleep(0.1) Sys.sleep(0.1)
page_txt <- tryCatch({ page_txt <- tryCatch(
{
resp <- curl::curl_fetch_memory(url) resp <- curl::curl_fetch_memory(url)
if (resp$status_code == 200) { if (resp$status_code == 200) {
read_html(rawToChar(resp$content)) read_html(rawToChar(resp$content))
@@ -368,7 +369,9 @@ get_lpsn_and_author <- function(rank, name, tries = 3) {
not_found <- resp$status_code == 404 not_found <- resp$status_code == 404
NULL NULL
} }
}, error = function(e) NULL) },
error = function(e) NULL
)
if (!is.null(page_txt) || not_found) { if (!is.null(page_txt) || not_found) {
break break
} }
@@ -1434,21 +1437,29 @@ taxonomy_gbif <- taxonomy_gbif %>%
# Step 1: parse the synonym's own name components from fullname # Step 1: parse the synonym's own name components from fullname
kingdom = if_else(is.na(kingdom) & rank == "kingdom", kingdom = if_else(is.na(kingdom) & rank == "kingdom",
sapply(name_parts, `[`, 1L), kingdom), sapply(name_parts, `[`, 1L), kingdom
),
phylum = if_else(is.na(phylum) & rank == "phylum", phylum = if_else(is.na(phylum) & rank == "phylum",
sapply(name_parts, `[`, 1L), phylum), sapply(name_parts, `[`, 1L), phylum
),
class = if_else(is.na(class) & rank == "class", class = if_else(is.na(class) & rank == "class",
sapply(name_parts, `[`, 1L), class), sapply(name_parts, `[`, 1L), class
),
order = if_else(is.na(order) & rank == "order", order = if_else(is.na(order) & rank == "order",
sapply(name_parts, `[`, 1L), order), sapply(name_parts, `[`, 1L), order
),
family = if_else(is.na(family) & rank == "family", family = if_else(is.na(family) & rank == "family",
sapply(name_parts, `[`, 1L), family), sapply(name_parts, `[`, 1L), family
),
genus = if_else(is.na(genus) & rank %in% c("genus", "species", "subspecies"), genus = if_else(is.na(genus) & rank %in% c("genus", "species", "subspecies"),
sapply(name_parts, `[`, 1L), genus), sapply(name_parts, `[`, 1L), genus
),
species = if_else(is.na(species) & rank %in% c("species", "subspecies"), species = if_else(is.na(species) & rank %in% c("species", "subspecies"),
sapply(name_parts, `[`, 2L), species), sapply(name_parts, `[`, 2L), species
),
subspecies = if_else(is.na(subspecies) & rank == "subspecies", subspecies = if_else(is.na(subspecies) & rank == "subspecies",
sapply(name_parts, `[`, 3L), subspecies), sapply(name_parts, `[`, 3L), subspecies
),
# Step 2: fill domain and kingdom from the accepted name, only for ranks ABOVE the synonym's rank # Step 2: fill domain and kingdom from the accepted name, only for ranks ABOVE the synonym's rank
domain = if_else(is.na(domain) & !is.na(acc_domain), acc_domain, domain), domain = if_else(is.na(domain) & !is.na(acc_domain), acc_domain, domain),
@@ -1932,7 +1943,8 @@ taxonomy <- taxonomy %>%
rank == "kingdom" ~ kingdom, rank == "kingdom" ~ kingdom,
rank == "domain" ~ domain, rank == "domain" ~ domain,
TRUE ~ paste(genus, species, subspecies) # already trimmed 7 lines up TRUE ~ paste(genus, species, subspecies) # already trimmed 7 lines up
))) %>% ))
) %>%
# keep the record of the most authoritative source: LPSN > MycoBank > GBIF > other # keep the record of the most authoritative source: LPSN > MycoBank > GBIF > other
mutate(sprio = source_prio(source)) %>% mutate(sprio = source_prio(source)) %>%
arrange(fullname, sprio) %>% arrange(fullname, sprio) %>%
@@ -2181,7 +2193,8 @@ rank_pairs <- tibble(
for (d in unique(taxonomy$domain[taxonomy$domain != "(unknown domain)"])) { for (d in unique(taxonomy$domain[taxonomy$domain != "(unknown domain)"])) {
src <- if (d == "Fungi") "MycoBank" else "LPSN" src <- if (d == "Fungi") "MycoBank" else "LPSN"
message("Fixing GBIF taxonomy for domain ", d, " based on ", src, "...", message("Fixing GBIF taxonomy for domain ", d, " based on ", src, "...",
appendLF = FALSE) appendLF = FALSE
)
for (i in seq_len(nrow(rank_pairs))) { for (i in seq_len(nrow(rank_pairs))) {
child_col <- rank_pairs$child[i] child_col <- rank_pairs$child[i]
@@ -2809,10 +2822,14 @@ plasmodia <- data.frame(
status = "accepted", status = "accepted",
prevalence = 1.25 prevalence = 1.25
) %>% ) %>%
mutate(rank = ifelse(species == "", "genus", mutate(
rank = ifelse(species == "", "genus",
ifelse(subspecies != "", "subspecies", ifelse(subspecies != "", "subspecies",
"species")), "species"
fullname = trimws(paste(genus, species, subspecies))) )
),
fullname = trimws(paste(genus, species, subspecies))
)
taxonomy <- taxonomy %>% taxonomy <- taxonomy %>%
filter(genus != "Plasmodium") %>% filter(genus != "Plasmodium") %>%
@@ -2898,8 +2915,10 @@ if (nrow(isospora_belli) == 1 && !"Cystoisospora belli" %in% taxonomy$fullname)
status = "synonym", status = "synonym",
source = "manually added", source = "manually added",
ref = "", ref = "",
across(any_of(c("lpsn", "lpsn_parent", "lpsn_renamed_to", "mycobank", "mycobank_parent", "mycobank_renamed_to", across(any_of(c(
"gbif", "gbif_parent")), ~NA_character_), "lpsn", "lpsn_parent", "lpsn_renamed_to", "mycobank", "mycobank_parent", "mycobank_renamed_to",
"gbif", "gbif_parent"
)), ~NA_character_),
gbif_renamed_to = isospora_belli$gbif gbif_renamed_to = isospora_belli$gbif
) )
) %>% ) %>%
@@ -3186,9 +3205,11 @@ taxonomy <- taxonomy %>%
distinct(fullname, .keep_all = TRUE) %>% distinct(fullname, .keep_all = TRUE) %>%
compute_prevalence() compute_prevalence()
message("Restored ", nrow(restored), " released taxa (", nrow(taxonomy) - n_before, " records including their parents)") message("Restored ", nrow(restored), " released taxa (", nrow(taxonomy) - n_before, " records including their parents)")
rm(restore_registry, restore_renames, renamed, missing_released, homonyms_removed, retirements, new_retirements, source_names, older_homonyms, older_homonym_retirements, rm(
restore_registry, restore_renames, renamed, missing_released, homonyms_removed, retirements, new_retirements, source_names, older_homonyms, older_homonym_retirements,
released_records, genus_domains, conflicting, conflict_retirements, kingdom_of_phylum, lineage_of_genus, restored, released_records, genus_domains, conflicting, conflict_retirements, kingdom_of_phylum, lineage_of_genus, restored,
rank_order) rank_order
)
# Link orthographic variants without a current name ---------------------------------------------- # Link orthographic variants without a current name ----------------------------------------------
@@ -3495,8 +3516,10 @@ if (nrow(new_retirements) > 0) {
na = "" na = ""
) )
} }
rm(no_current_name, lpsn_outcome, prokaryotes, to_remove, has_kept_children, outcome, removed, registry_now, rm(
retirements_now, new_retirements) no_current_name, lpsn_outcome, prokaryotes, to_remove, has_kept_children, outcome, removed, registry_now,
retirements_now, new_retirements
)
# Fix genera that are synonyms while they contain accepted species -------------------------------- # Fix genera that are synonyms while they contain accepted species --------------------------------
@@ -3617,8 +3640,10 @@ for (r in c("family", "order", "class", "phylum")) {
new_record$status <- "accepted" new_record$status <- "accepted"
new_record$source <- "manually added" new_record$source <- "manually added"
new_record$ref <- NA_character_ new_record$ref <- NA_character_
for (col in intersect(c("lpsn", "lpsn_parent", "lpsn_renamed_to", "mycobank", "mycobank_parent", for (col in intersect(c(
"mycobank_renamed_to", "gbif", "gbif_parent", "gbif_renamed_to"), colnames(new_record))) { "lpsn", "lpsn_parent", "lpsn_renamed_to", "mycobank", "mycobank_parent",
"mycobank_renamed_to", "gbif", "gbif_parent", "gbif_renamed_to"
), colnames(new_record))) {
new_record[[col]] <- NA_character_ new_record[[col]] <- NA_character_
} }
for (lf in c(r, lower)) new_record[[lf]] <- "" for (lf in c(r, lower)) new_record[[lf]] <- ""
@@ -4489,7 +4514,8 @@ bacdive_shape_other <- taxonomy %>%
morphology = case_when( morphology = case_when(
fullname %like% "coccus" ~ "cocci", fullname %like% "coccus" ~ "cocci",
fullname %in% taxonomy$fullname[taxonomy$order %in% c("Enterobacterales", "Caryophanales", "Lactobacillales")] ~ morphology, fullname %in% taxonomy$fullname[taxonomy$order %in% c("Enterobacterales", "Caryophanales", "Lactobacillales")] ~ morphology,
TRUE ~ paste("likely", morphology)) TRUE ~ paste("likely", morphology)
)
) %>% ) %>%
select(fullname, morphology) %>% select(fullname, morphology) %>%
distinct(fullname, .keep_all = TRUE) distinct(fullname, .keep_all = TRUE)
@@ -4922,20 +4948,24 @@ save_df <- function(...) {
) )
} }
microorganisms.codes <- fix_old_mos(AMR::microorganisms.codes, taxonomy, drop = TRUE) microorganisms.codes <- fix_old_mos(AMR::microorganisms.codes, taxonomy, drop = TRUE)
if (!identical(microorganisms.codes, AMR::microorganisms.codes)) save_df(microorganisms.codes); rm(microorganisms.codes) if (!identical(microorganisms.codes, AMR::microorganisms.codes)) save_df(microorganisms.codes)
rm(microorganisms.codes)
clinical_breakpoints <- fix_old_mos(AMR::clinical_breakpoints, taxonomy) clinical_breakpoints <- fix_old_mos(AMR::clinical_breakpoints, taxonomy)
if (!identical(clinical_breakpoints, AMR::clinical_breakpoints)) save_df(clinical_breakpoints); rm(clinical_breakpoints) if (!identical(clinical_breakpoints, AMR::clinical_breakpoints)) save_df(clinical_breakpoints)
rm(clinical_breakpoints)
example_isolates <- fix_old_mos(AMR::example_isolates, taxonomy) example_isolates <- fix_old_mos(AMR::example_isolates, taxonomy)
if (!identical(example_isolates, AMR::example_isolates)) save_df(example_isolates); rm(example_isolates) if (!identical(example_isolates, AMR::example_isolates)) save_df(example_isolates)
rm(example_isolates)
# (intrinsic_resistant is not updated here: it is computed from the new data set afterwards, see # (intrinsic_resistant is not updated here: it is computed from the new data set afterwards, see
# data-raw/_reproduction_scripts/reproduction_of_intrinsic_resistant.R) # data-raw/_reproduction_scripts/reproduction_of_intrinsic_resistant.R)
microorganisms.groups <- fix_old_mos(AMR::microorganisms.groups, taxonomy, col = "mo") microorganisms.groups <- fix_old_mos(AMR::microorganisms.groups, taxonomy, col = "mo")
microorganisms.groups <- fix_old_mos(microorganisms.groups, taxonomy, col = "mo_group") microorganisms.groups <- fix_old_mos(microorganisms.groups, taxonomy, col = "mo_group")
if (!identical(microorganisms.groups, AMR::microorganisms.groups)) save_df(microorganisms.groups); rm(microorganisms.groups) if (!identical(microorganisms.groups, AMR::microorganisms.groups)) save_df(microorganisms.groups)
rm(microorganisms.groups)
# *** Save to package *** ------------------------------------------------------------------------- # *** Save to package *** -------------------------------------------------------------------------
@@ -56,12 +56,12 @@ step("microorganisms.groups", "data-raw/_reproduction_scripts/reproduction_of_mi
# 2. EUCAST breakpoints, merged into clinical_breakpoints (as at the end of reproduction_of_clinical_breakpoints.R) # 2. EUCAST breakpoints, merged into clinical_breakpoints (as at the end of reproduction_of_clinical_breakpoints.R)
merge_script <- tempfile(fileext = ".R") merge_script <- tempfile(fileext = ".R")
writeLines(c( writeLines(c(
'devtools::load_all()', "devtools::load_all()",
'library(dplyr, warn.conflicts = FALSE)', "library(dplyr, warn.conflicts = FALSE)",
'source("data-raw/_reproduction_scripts/reproduction_of_clinical_breakpoints_eucast.R")', 'source("data-raw/_reproduction_scripts/reproduction_of_clinical_breakpoints_eucast.R")',
'# the rank of an organism can have changed with the new data set', "# the rank of an organism can have changed with the new data set",
'breakpoints_now <- clinical_breakpoints %>%', "breakpoints_now <- clinical_breakpoints %>%",
' mutate(rank_index = case_when(', " mutate(rank_index = case_when(",
' mo_rank(mo, keep_synonyms = TRUE) %like% "(infra|sub)" ~ 1,', ' mo_rank(mo, keep_synonyms = TRUE) %like% "(infra|sub)" ~ 1,',
' mo_rank(mo, keep_synonyms = TRUE) == "species" ~ 2,', ' mo_rank(mo, keep_synonyms = TRUE) == "species" ~ 2,',
' mo_rank(mo, keep_synonyms = TRUE) == "species group" ~ 2.5,', ' mo_rank(mo, keep_synonyms = TRUE) == "species group" ~ 2.5,',
@@ -69,9 +69,9 @@ writeLines(c(
' mo_rank(mo, keep_synonyms = TRUE) == "family" ~ 4,', ' mo_rank(mo, keep_synonyms = TRUE) == "family" ~ 4,',
' mo_rank(mo, keep_synonyms = TRUE) == "order" ~ 5,', ' mo_rank(mo, keep_synonyms = TRUE) == "order" ~ 5,',
' mo != "UNKNOWN" ~ 6,', ' mo != "UNKNOWN" ~ 6,',
' TRUE ~ 7', " TRUE ~ 7",
' ) + if_else(is.na(breakpoint_S) & is.na(breakpoint_R) & guideline %like% "EUCAST", 0.1, 0))', ' ) + if_else(is.na(breakpoint_S) & is.na(breakpoint_R) & guideline %like% "EUCAST", 0.1, 0))',
'clinical_breakpoints <- merge_eucast_breakpoints(breakpoints_now, breakpoints_eucast, eucast_coverage)', "clinical_breakpoints <- merge_eucast_breakpoints(breakpoints_now, breakpoints_eucast, eucast_coverage)",
'usethis::use_data(clinical_breakpoints, overwrite = TRUE, compress = "xz", version = 2)' 'usethis::use_data(clinical_breakpoints, overwrite = TRUE, compress = "xz", version = 2)'
), merge_script) ), merge_script)
step("clinical_breakpoints (EUCAST)", merge_script) step("clinical_breakpoints (EUCAST)", merge_script)
+40 -40
View File
@@ -26,7 +26,6 @@ wisca_params <- function(x, antibiotic_in, pathogen_in, analysis, exclude, isola
comb_ab <- str_to_title(abo_in[str_detect(abo_in, fixed("+"))]) # identify combination regimens comb_ab <- str_to_title(abo_in[str_detect(abo_in, fixed("+"))]) # identify combination regimens
if (length(comb_ab) > 0) { if (length(comb_ab) > 0) {
comb_antibiotic_x_total <- data.frame() comb_antibiotic_x_total <- data.frame()
for (jj in 1:length(comb_ab)) { # generate dataframe for combination antimicrobials with abbreviations and fullnames for (jj in 1:length(comb_ab)) { # generate dataframe for combination antimicrobials with abbreviations and fullnames
comb_ab_sep <- unlist(str_split(comb_ab[jj], fixed("+"))) comb_ab_sep <- unlist(str_split(comb_ab[jj], fixed("+")))
@@ -41,17 +40,17 @@ wisca_params <- function(x, antibiotic_in, pathogen_in, analysis, exclude, isola
comb_antibiotic_x <- pivot_wider(comb_antibiotic_x_pre, names_from = name, values_from = ab) comb_antibiotic_x <- pivot_wider(comb_antibiotic_x_pre, names_from = name, values_from = ab)
comb_antibiotic_x_set <- comb_antibiotic_x %>% comb_antibiotic_x_set <- comb_antibiotic_x %>%
mutate(namerx = str_flatten(str_c(names(comb_antibiotic_x), collapse = "+"))) mutate(namerx = str_flatten(str_c(names(comb_antibiotic_x), collapse = "+")))
new_ab <- sapply(X = as.character(1:(ncol(comb_antibiotic_x_set)-1)), FUN = function(x){str_flatten(str_c("ab",x))}) new_ab <- sapply(X = as.character(1:(ncol(comb_antibiotic_x_set) - 1)), FUN = function(x) {
str_flatten(str_c("ab", x))
})
names(comb_antibiotic_x_set)[1:(ncol(comb_antibiotic_x_set) - 1)] <- new_ab names(comb_antibiotic_x_set)[1:(ncol(comb_antibiotic_x_set) - 1)] <- new_ab
names(comb_antibiotic_x_set)[ncol(comb_antibiotic_x_set)] <- "namerx" names(comb_antibiotic_x_set)[ncol(comb_antibiotic_x_set)] <- "namerx"
comb_antibiotic_x_total <- rbind.data.frame(comb_antibiotic_x_total, comb_antibiotic_x_set, row.names = NULL) comb_antibiotic_x_total <- rbind.data.frame(comb_antibiotic_x_total, comb_antibiotic_x_set, row.names = NULL)
} }
antibiotic_x_set <- full_join(x = mono_antibiotic_x_set, y = comb_antibiotic_x_total) # generate full antibiotic dataset for analysis antibiotic_x_set <- full_join(x = mono_antibiotic_x_set, y = comb_antibiotic_x_total) # generate full antibiotic dataset for analysis
antibiotic_x_set <- select(antibiotic_x_set, starts_with("ab"), starts_with("namerx")) antibiotic_x_set <- select(antibiotic_x_set, starts_with("ab"), starts_with("namerx"))
} else { } else {
antibiotic_x_set <- mono_antibiotic_x_set antibiotic_x_set <- mono_antibiotic_x_set
} }
@@ -62,7 +61,9 @@ wisca_params <- function(x, antibiotic_in, pathogen_in, analysis, exclude, isola
# if the dataset names are in different format to built-in set # if the dataset names are in different format to built-in set
if ((length(names(x)[sapply(names(x), function(x1){any(str_detect(x1,"[A-Z]$") & !(x1 %in% antimicrobials$ab) & !(x1 %in% antimicrobials$name))})])>0)){ if ((length(names(x)[sapply(names(x), function(x1) {
any(str_detect(x1, "[A-Z]$") & !(x1 %in% antimicrobials$ab) & !(x1 %in% antimicrobials$name))
})]) > 0)) {
names(x)[which(str_detect(names(x), "[a-z]+") & !str_detect(names(x), "fullname") & !str_detect(names(x), "mo"))] <- str_to_upper(names(x)[which(str_detect(names(x), "[a-z]+") & !str_detect(names(x), "fullname") & !str_detect(names(x), "mo"))]) names(x)[which(str_detect(names(x), "[a-z]+") & !str_detect(names(x), "fullname") & !str_detect(names(x), "mo"))] <- str_to_upper(names(x)[which(str_detect(names(x), "[a-z]+") & !str_detect(names(x), "fullname") & !str_detect(names(x), "mo"))])
names(x)[which(str_detect(names(x), "^ORG") & str_detect(names(x), "NAME$"))] <- "fullname" names(x)[which(str_detect(names(x), "^ORG") & str_detect(names(x), "NAME$"))] <- "fullname"
names(x)[which(str_detect(names(x), "^ORG") & str_detect(names(x), "CODE$"))] <- "organism" names(x)[which(str_detect(names(x), "^ORG") & str_detect(names(x), "CODE$"))] <- "organism"
@@ -71,7 +72,6 @@ wisca_params <- function(x, antibiotic_in, pathogen_in, analysis, exclude, isola
names(x)[which(str_detect(names(x), "^WARD"))] <- "ward" names(x)[which(str_detect(names(x), "^WARD"))] <- "ward"
names(x)[which(str_detect(names(x), "^HOSP"))] <- "hospital" names(x)[which(str_detect(names(x), "^HOSP"))] <- "hospital"
names(x)[which(str_detect(names(x), "DATE") & (str_detect(names(x), "REGISTRATION")))] <- "date" names(x)[which(str_detect(names(x), "DATE") & (str_detect(names(x), "REGISTRATION")))] <- "date"
} }
# remove any duplicated variables # remove any duplicated variables
@@ -84,12 +84,13 @@ wisca_params <- function(x, antibiotic_in, pathogen_in, analysis, exclude, isola
x <- arrange(x, "date") # arrange in increasing date x <- arrange(x, "date") # arrange in increasing date
# if your dataset does not have an episode identifier # if your dataset does not have an episode identifier
if (length(names(x)[sapply(names(x), function(x1){any(str_detect(x1,"patient"))})]) == 0){ if (length(names(x)[sapply(names(x), function(x1) {
any(str_detect(x1, "patient"))
})]) == 0) {
x$patient <- seq(1, nrow(x), 1) x$patient <- seq(1, nrow(x), 1)
} }
# find demographic data within dataset # find demographic data within dataset
micro_df_ind <- which(sapply(x, function(y) any(str_detect(y, "^S$")) | any(str_detect(y, "^R$")) | any(str_detect(y, "^SENSITIVE$")) | any(str_detect(y, "^RESISTANT$"))) == TRUE) micro_df_ind <- which(sapply(x, function(y) any(str_detect(y, "^S$")) | any(str_detect(y, "^R$")) | any(str_detect(y, "^SENSITIVE$")) | any(str_detect(y, "^RESISTANT$"))) == TRUE)
micro_df <- names(x)[micro_df_ind] # column names for susceptibility data micro_df <- names(x)[micro_df_ind] # column names for susceptibility data
@@ -135,7 +136,9 @@ wisca_params <- function(x, antibiotic_in, pathogen_in, analysis, exclude, isola
rx_regs_comb_x <- pivot_wider(rx_regs_comb_pre, names_from = name, values_from = ab) rx_regs_comb_x <- pivot_wider(rx_regs_comb_pre, names_from = name, values_from = ab)
rx_regs_comb_x_set <- rx_regs_comb_x %>% rx_regs_comb_x_set <- rx_regs_comb_x %>%
mutate(namerx = str_flatten(str_c(names(rx_regs_comb_x), collapse = "+"))) # generate new antimicrobials table mutate(namerx = str_flatten(str_c(names(rx_regs_comb_x), collapse = "+"))) # generate new antimicrobials table
rx_regs_new_ab <- sapply(X = as.character(1:(ncol(rx_regs_comb_x_set)-1)), FUN = function(x){str_flatten(str_c("ab",x))}) rx_regs_new_ab <- sapply(X = as.character(1:(ncol(rx_regs_comb_x_set) - 1)), FUN = function(x) {
str_flatten(str_c("ab", x))
})
names(rx_regs_comb_x_set)[1:(ncol(rx_regs_comb_x_set) - 1)] <- rx_regs_new_ab names(rx_regs_comb_x_set)[1:(ncol(rx_regs_comb_x_set) - 1)] <- rx_regs_new_ab
names(rx_regs_comb_x_set)[ncol(rx_regs_comb_x_set)] <- "namerx" names(rx_regs_comb_x_set)[ncol(rx_regs_comb_x_set)] <- "namerx"
rx_regs_comb_x_total <- rbind.data.frame(rx_regs_comb_x_total, rx_regs_comb_x_set, row.names = NULL) rx_regs_comb_x_total <- rbind.data.frame(rx_regs_comb_x_total, rx_regs_comb_x_set, row.names = NULL)
@@ -148,19 +151,16 @@ wisca_params <- function(x, antibiotic_in, pathogen_in, analysis, exclude, isola
rx_regs_comb_rename_2 <- unlist(rx_regs_comb_rename[k, ]) rx_regs_comb_rename_2 <- unlist(rx_regs_comb_rename[k, ])
rx_regs_comb_rename_vec <- str_flatten(str_c(rx_regs_comb_rename_2, collapse = "+")) rx_regs_comb_rename_vec <- str_flatten(str_c(rx_regs_comb_rename_2, collapse = "+"))
names(rx_abo)[names(rx_abo) == rx_regs_comb[k]] <- rx_regs_comb_rename_vec names(rx_abo)[names(rx_abo) == rx_regs_comb[k]] <- rx_regs_comb_rename_vec
} }
}) })
names(rx_abo)[!(str_detect(names(rx_abo), fixed("+"))) & !(names(rx_abo) %in% demograph_df)] <- names(rx_abo)[!(str_detect(names(rx_abo), fixed("+"))) & !(names(rx_abo) %in% demograph_df)] <-
as.ab(names(rx_abo)[!(str_detect(names(rx_abo), fixed("+"))) & !(names(rx_abo) %in% demograph_df)]) as.ab(names(rx_abo)[!(str_detect(names(rx_abo), fixed("+"))) & !(names(rx_abo) %in% demograph_df)])
names(rx_abo)[which(duplicated(names(rx_abo)))] <- str_c(names(rx_abo)[which(duplicated(names(rx_abo)))], ".x") names(rx_abo)[which(duplicated(names(rx_abo)))] <- str_c(names(rx_abo)[which(duplicated(names(rx_abo)))], ".x")
rx_abo_upd <- left_join_microorganisms(x = rx_abo, by = "mo")[, c(names(rx_abo), "fullname")] rx_abo_upd <- left_join_microorganisms(x = rx_abo, by = "mo")[, c(names(rx_abo), "fullname")]
} else { } else {
names(rx_abo)[!(names(rx_abo) %in% demograph_df)] <- as.ab(names(rx_abo)[!(names(rx_abo) %in% demograph_df)]) names(rx_abo)[!(names(rx_abo) %in% demograph_df)] <- as.ab(names(rx_abo)[!(names(rx_abo) %in% demograph_df)])
names(rx_abo)[which(duplicated(names(rx_abo)))] <- str_c(names(rx_abo)[which(duplicated(names(rx_abo)))], ".x") names(rx_abo)[which(duplicated(names(rx_abo)))] <- str_c(names(rx_abo)[which(duplicated(names(rx_abo)))], ".x")
rx_abo_upd <- left_join_microorganisms(x = rx_abo, by = "mo")[, c(names(rx_abo), "fullname")] rx_abo_upd <- left_join_microorganisms(x = rx_abo, by = "mo")[, c(names(rx_abo), "fullname")]
} }
} else { } else {
# If fullname included in dataset: # If fullname included in dataset:
@@ -196,7 +196,9 @@ wisca_params <- function(x, antibiotic_in, pathogen_in, analysis, exclude, isola
rx_regs_comb_x <- pivot_wider(rx_regs_comb_pre, names_from = name, values_from = ab) rx_regs_comb_x <- pivot_wider(rx_regs_comb_pre, names_from = name, values_from = ab)
rx_regs_comb_x_set <- rx_regs_comb_x %>% rx_regs_comb_x_set <- rx_regs_comb_x %>%
mutate(namerx = str_flatten(str_c(names(rx_regs_comb_x), collapse = "+"))) mutate(namerx = str_flatten(str_c(names(rx_regs_comb_x), collapse = "+")))
rx_regs_new_ab <- sapply(X = as.character(1:(ncol(rx_regs_comb_x_set)-1)), FUN = function(x){str_flatten(str_c("ab",x))}) rx_regs_new_ab <- sapply(X = as.character(1:(ncol(rx_regs_comb_x_set) - 1)), FUN = function(x) {
str_flatten(str_c("ab", x))
})
names(rx_regs_comb_x_set)[1:(ncol(rx_regs_comb_x_set) - 1)] <- rx_regs_new_ab names(rx_regs_comb_x_set)[1:(ncol(rx_regs_comb_x_set) - 1)] <- rx_regs_new_ab
names(rx_regs_comb_x_set)[ncol(rx_regs_comb_x_set)] <- "namerx" names(rx_regs_comb_x_set)[ncol(rx_regs_comb_x_set)] <- "namerx"
rx_regs_comb_x_total <- rbind.data.frame(rx_regs_comb_x_total, rx_regs_comb_x_set, row.names = NULL) rx_regs_comb_x_total <- rbind.data.frame(rx_regs_comb_x_total, rx_regs_comb_x_set, row.names = NULL)
@@ -215,12 +217,10 @@ wisca_params <- function(x, antibiotic_in, pathogen_in, analysis, exclude, isola
as.ab(names(rx_abo)[!(str_detect(names(rx_abo), fixed("+"))) & !(names(rx_abo) %in% demograph_df)]) as.ab(names(rx_abo)[!(str_detect(names(rx_abo), fixed("+"))) & !(names(rx_abo) %in% demograph_df)])
names(rx_abo)[which(duplicated(names(rx_abo)))] <- str_c(names(rx_abo)[which(duplicated(names(rx_abo)))], ".x") names(rx_abo)[which(duplicated(names(rx_abo)))] <- str_c(names(rx_abo)[which(duplicated(names(rx_abo)))], ".x")
rx_abo_upd <- left_join_microorganisms(x = rx_abo, by = "fullname")[, c(names(rx_abo), "mo")] rx_abo_upd <- left_join_microorganisms(x = rx_abo, by = "fullname")[, c(names(rx_abo), "mo")]
} else { } else {
names(rx_abo)[!(names(rx_abo) %in% demograph_df)] <- as.ab(names(rx_abo)[!(names(rx_abo) %in% demograph_df)]) names(rx_abo)[!(names(rx_abo) %in% demograph_df)] <- as.ab(names(rx_abo)[!(names(rx_abo) %in% demograph_df)])
names(rx_abo)[which(duplicated(names(rx_abo)))] <- str_c(names(rx_abo)[which(duplicated(names(rx_abo)))], ".x") names(rx_abo)[which(duplicated(names(rx_abo)))] <- str_c(names(rx_abo)[which(duplicated(names(rx_abo)))], ".x")
rx_abo_upd <- left_join_microorganisms(x = rx_abo, by = "fullname")[, c(names(rx_abo), "mo")] rx_abo_upd <- left_join_microorganisms(x = rx_abo, by = "fullname")[, c(names(rx_abo), "mo")]
} }
} }
@@ -283,24 +283,31 @@ wisca_params <- function(x, antibiotic_in, pathogen_in, analysis, exclude, isola
if (isolate_first == "yes") { # if dataset has already been deduplicated for first isolates if (isolate_first == "yes") { # if dataset has already been deduplicated for first isolates
test_isolates_first <- rx_df test_isolates_first <- rx_df
} else { } else {
test_isolates_first <- rx_df[first_isolate(x = rx_df, test_isolates_first <- rx_df[first_isolate(
x = rx_df,
method = "e", method = "e",
episode_days = 14, episode_days = 14,
col_date = var_date, col_date = var_date,
col_mo = "mo") == TRUE,] #Only include isolates for microorganisms < 14 days apart col_mo = "mo"
) == TRUE, ] # Only include isolates for microorganisms < 14 days apart
} }
test_isolates_first_upd <<- test_isolates_first %>% test_isolates_first_upd <<- test_isolates_first %>%
pivot_longer(cols = names(test_isolates_first)[which(sapply(test_isolates_first, pivot_longer(
function(y) any(str_detect(y,"^S$"))|any(str_detect(y,"^R$"))|any(str_detect(y,"^SENSITIVE$"))| any(str_detect(y,"^RESISTANT$"))))], cols = names(test_isolates_first)[which(sapply(
names_to = "keyantimicrobials", values_to = "antibiogram") %>% #combine all regimens and antibiograms into single variables test_isolates_first,
function(y) any(str_detect(y, "^S$")) | any(str_detect(y, "^R$")) | any(str_detect(y, "^SENSITIVE$")) | any(str_detect(y, "^RESISTANT$"))
))],
names_to = "keyantimicrobials", values_to = "antibiogram"
) %>% # combine all regimens and antibiograms into single variables
mutate(antibiogram = ifelse(antibiogram == "I" | antibiogram == "INTERMEDIATE", susceptible_I, antibiogram)) %>% # simplify susceptibility outputs - 90-60 rule mutate(antibiogram = ifelse(antibiogram == "I" | antibiogram == "INTERMEDIATE", susceptible_I, antibiogram)) %>% # simplify susceptibility outputs - 90-60 rule
mutate(antibiogram = ifelse(antibiogram == "SENSITIVE", "S", antibiogram)) %>% mutate(antibiogram = ifelse(antibiogram == "SENSITIVE", "S", antibiogram)) %>%
mutate(antibiogram = ifelse(antibiogram == "RESISTANT", "R", antibiogram)) %>% mutate(antibiogram = ifelse(antibiogram == "RESISTANT", "R", antibiogram)) %>%
mutate(antibiogram = factor(antibiogram), mutate(
keyantimicrobials = as.factor(keyantimicrobials)) antibiogram = factor(antibiogram),
keyantimicrobials = as.factor(keyantimicrobials)
)
# recode susceptibility of regimes if not already in the dataset: if any S, then all S; if any NA and not S, then combination NA; if all R, then combination R. # recode susceptibility of regimes if not already in the dataset: if any S, then all S; if any NA and not S, then combination NA; if all R, then combination R.
@@ -336,7 +343,6 @@ wisca_params <- function(x, antibiotic_in, pathogen_in, analysis, exclude, isola
test_isolates_final <- bind_rows(test_isolates_temp4, test_isolates_temp5) test_isolates_final <- bind_rows(test_isolates_temp4, test_isolates_temp5)
test_isolates_final <- test_isolates_final %>% test_isolates_final <- test_isolates_final %>%
arrange(patient) arrange(patient)
} else { } else {
test_isolates_final <- test_isolates_first_upd test_isolates_final <- test_isolates_first_upd
} }
@@ -370,7 +376,8 @@ wisca_params <- function(x, antibiotic_in, pathogen_in, analysis, exclude, isola
mutate(n = ifelse(mo == "B_KLBSL", klebsiella_combine$n_kleb, n)) %>% mutate(n = ifelse(mo == "B_KLBSL", klebsiella_combine$n_kleb, n)) %>%
distinct(mo, .keep_all = TRUE) distinct(mo, .keep_all = TRUE)
organism_count_total_upd <- rename(organism_count_total_upd, organism_count_total_upd <- rename(organism_count_total_upd,
"n_inc" = "n") "n_inc" = "n"
)
regimen_organism <- slice_max(organism_count_total_upd, order_by = n_inc, n = pathogen_in) # choose only the top n number of occurring pathogens to consider in WISCA regimen_organism <- slice_max(organism_count_total_upd, order_by = n_inc, n = pathogen_in) # choose only the top n number of occurring pathogens to consider in WISCA
total_pathogens <- sum(regimen_organism$n_inc) # total number of occurrences of pathogens total_pathogens <- sum(regimen_organism$n_inc) # total number of occurrences of pathogens
@@ -391,14 +398,12 @@ wisca_params <- function(x, antibiotic_in, pathogen_in, analysis, exclude, isola
params_set_out <- data.frame() params_set_out <- data.frame()
if (analysis %in% names(test_isolates_final)) { if (analysis %in% names(test_isolates_final)) {
# if there is a facility/ward/category level analysis required # if there is a facility/ward/category level analysis required
for (j in 1:length(unique(test_isolates_final[[analysis]]))) { # for every facility/ward/category for (j in 1:length(unique(test_isolates_final[[analysis]]))) { # for every facility/ward/category
infection_episodes <- length(unique(test_isolates_final[test_isolates_final[[analysis]] == unique(test_isolates_final[[analysis]])[j], ]$patient)) infection_episodes <- length(unique(test_isolates_final[test_isolates_final[[analysis]] == unique(test_isolates_final[[analysis]])[j], ]$patient))
if (infection_episodes < 100) { # must have at least 100 infection episodes for WISCA if (infection_episodes < 100) { # must have at least 100 infection episodes for WISCA
warning(paste0("Number of infection episodes = ", infection_episodes, ". Minimum sample size for WISCA is 100 infection episodes. Below minimum sample size will produce inaccurate coverage estimates.")) warning(paste0("Number of infection episodes = ", infection_episodes, ". Minimum sample size for WISCA is 100 infection episodes. Below minimum sample size will produce inaccurate coverage estimates."))
} else { } else {
print(paste0("Number of infection episodes = ", infection_episodes, " > 100 minimum required. Sample size adequate for WISCA analysis.")) print(paste0("Number of infection episodes = ", infection_episodes, " > 100 minimum required. Sample size adequate for WISCA analysis."))
} }
@@ -456,8 +461,10 @@ wisca_params <- function(x, antibiotic_in, pathogen_in, analysis, exclude, isola
organism_count_df_combine_tested <- full_join(x = organism_count_df_tested, y = organism_count_df_ntested, by = c("mo", "keyantimicrobials")) # combine tested and nontested organism_count_df_combine_tested <- full_join(x = organism_count_df_tested, y = organism_count_df_ntested, by = c("mo", "keyantimicrobials")) # combine tested and nontested
organism_count_df_combine_tested <- organism_count_df_combine_tested %>% organism_count_df_combine_tested <- organism_count_df_combine_tested %>%
mutate(tested_n = ifelse(is.na(tested_n), 0 ,tested_n), mutate(
ntested_n = ifelse(is.na(ntested_n), 0, ntested_n)) tested_n = ifelse(is.na(tested_n), 0, tested_n),
ntested_n = ifelse(is.na(ntested_n), 0, ntested_n)
)
organism_count_df_regimen_tested <- left_join(x = organism_count_df, y = organism_count_df_combine_tested[, c("mo", "keyantimicrobials", "tested_n")], by = "mo") organism_count_df_regimen_tested <- left_join(x = organism_count_df, y = organism_count_df_combine_tested[, c("mo", "keyantimicrobials", "tested_n")], by = "mo")
@@ -492,13 +499,10 @@ wisca_params <- function(x, antibiotic_in, pathogen_in, analysis, exclude, isola
params_set_out$fullname <- droplevels(params_set_out$fullname) params_set_out$fullname <- droplevels(params_set_out$fullname)
params_set_out[[names(test_isolates_final)[names(test_isolates_final) == analysis]]] <- as.factor(params_set_out[[names(test_isolates_final)[names(test_isolates_final) == analysis]]]) params_set_out[[names(test_isolates_final)[names(test_isolates_final) == analysis]]] <- as.factor(params_set_out[[names(test_isolates_final)[names(test_isolates_final) == analysis]]])
} else { } else {
infection_episodes <- length(unique(test_isolates_final$patient)) infection_episodes <- length(unique(test_isolates_final$patient))
if (infection_episodes < 100) { # must have at least 100 infection episodes for WISCA if (infection_episodes < 100) { # must have at least 100 infection episodes for WISCA
warning(paste0("Number of infection episodes = ", infection_episodes, ". Minimum sample size for WISCA is 100 infection episodes. Below minimum sample size will produce inaccurate coverage estimates.")) warning(paste0("Number of infection episodes = ", infection_episodes, ". Minimum sample size for WISCA is 100 infection episodes. Below minimum sample size will produce inaccurate coverage estimates."))
} else { } else {
print(paste0("Number of infection episodes = ", infection_episodes, " > 100 minimum required. Sample size adequate for WISCA analysis.")) print(paste0("Number of infection episodes = ", infection_episodes, " > 100 minimum required. Sample size adequate for WISCA analysis."))
} }
@@ -520,7 +524,6 @@ wisca_params <- function(x, antibiotic_in, pathogen_in, analysis, exclude, isola
total_sites_pathogens <- sum(organism_count_df$n) total_sites_pathogens <- sum(organism_count_df$n)
organism_count_df <- organism_count_df %>% organism_count_df <- organism_count_df %>%
mutate(prop.n = n / total_pathogens) # pathogen incidence = number of pathogen occurring/total pathogen occurrences included in WISCA mutate(prop.n = n / total_pathogens) # pathogen incidence = number of pathogen occurring/total pathogen occurrences included in WISCA
@@ -553,8 +556,10 @@ wisca_params <- function(x, antibiotic_in, pathogen_in, analysis, exclude, isola
organism_count_df_combine_tested <- full_join(x = organism_count_df_tested, y = organism_count_df_ntested, by = c("mo", "keyantimicrobials")) # combine tested and nontested organism_count_df_combine_tested <- full_join(x = organism_count_df_tested, y = organism_count_df_ntested, by = c("mo", "keyantimicrobials")) # combine tested and nontested
organism_count_df_combine_tested <- organism_count_df_combine_tested %>% organism_count_df_combine_tested <- organism_count_df_combine_tested %>%
mutate(tested_n = ifelse(is.na(tested_n), 0 ,tested_n), mutate(
ntested_n = ifelse(is.na(ntested_n), 0, ntested_n)) tested_n = ifelse(is.na(tested_n), 0, tested_n),
ntested_n = ifelse(is.na(ntested_n), 0, ntested_n)
)
organism_count_df_regimen_tested <- left_join(x = organism_count_df, y = organism_count_df_combine_tested[, c("mo", "keyantimicrobials", "tested_n")], by = "mo") organism_count_df_regimen_tested <- left_join(x = organism_count_df, y = organism_count_df_combine_tested[, c("mo", "keyantimicrobials", "tested_n")], by = "mo")
@@ -583,12 +588,10 @@ wisca_params <- function(x, antibiotic_in, pathogen_in, analysis, exclude, isola
sensitivity$perc.S <- sensitivity$prop.S * 100 sensitivity$perc.S <- sensitivity$prop.S * 100
params_set_out <- rbind.data.frame(params_set_out, sensitivity, row.names = NULL) params_set_out <- rbind.data.frame(params_set_out, sensitivity, row.names = NULL)
} else { } else {
next next
} }
params_set_out$fullname <- droplevels(params_set_out$fullname) params_set_out$fullname <- droplevels(params_set_out$fullname)
} }
@@ -615,8 +618,5 @@ wisca_params <- function(x, antibiotic_in, pathogen_in, analysis, exclude, isola
# } # }
return(params_set_out) return(params_set_out)
} }
+25 -19
View File
@@ -1,4 +1,3 @@
# Copyright (c) [2022] [Larisse Bolton (author), Aislinn Cook (contributor)] # Copyright (c) [2022] [Larisse Bolton (author), Aislinn Cook (contributor)]
# Adapted from: Bielicki JA, Sharland M, Heath PT, et al. Evaluation of the coverage of 3 antibiotic regimens for # Adapted from: Bielicki JA, Sharland M, Heath PT, et al. Evaluation of the coverage of 3 antibiotic regimens for
# neonatal sepsis in the hospital setting across Asian countries. JAMA Netw Open. # neonatal sepsis in the hospital setting across Asian countries. JAMA Netw Open.
@@ -6,7 +5,6 @@
wisca_funct <- function(wisca_in, analysis) { wisca_funct <- function(wisca_in, analysis) {
# function used to gerenrate coverage estimate parameters from input data # function used to gerenrate coverage estimate parameters from input data
priors <- function(wisca_prior_in) { priors <- function(wisca_prior_in) {
# INCIDENCE # INCIDENCE
@@ -43,15 +41,25 @@ priors <- function(wisca_prior_in){
# function to generate simulations # function to generate simulations
coverage <- function(wisca_sim_in) { coverage <- function(wisca_sim_in) {
sim_coverage <- wisca_sim_in %>% sim_coverage <- wisca_sim_in %>%
mutate(random_incidence = runif(n = nrow(wisca_sim_in), min = 0, max = 1), mutate(
random_susceptibility = runif(n = nrow(wisca_sim_in), min = 0, max = 1)) %>% random_incidence = runif(n = nrow(wisca_sim_in), min = 0, max = 1),
mutate(simulation_bug = qgamma(random_incidence, shape = gamma_A, scale = B), random_susceptibility = runif(n = nrow(wisca_sim_in), min = 0, max = 1)
simulation_suscep = qbeta(p = random_susceptibility, ) %>%
shape1 = post_beta_1, shape2 = post_beta_2)) %>% mutate(
mutate(sim_inc_total = sum(simulation_bug), simulation_bug = qgamma(random_incidence, shape = gamma_A, scale = B),
simulated_incidence = simulation_bug/sim_inc_total) %>% simulation_suscep = qbeta(
mutate(simulated_bugcover = simulated_incidence *simulation_suscep, p = random_susceptibility,
coverage = sum(simulated_bugcover)) shape1 = post_beta_1, shape2 = post_beta_2
)
) %>%
mutate(
sim_inc_total = sum(simulation_bug),
simulated_incidence = simulation_bug / sim_inc_total
) %>%
mutate(
simulated_bugcover = simulated_incidence * simulation_suscep,
coverage = sum(simulated_bugcover)
)
sim_coverage_out <- slice_head(sim_coverage, n = 1)$coverage sim_coverage_out <- slice_head(sim_coverage, n = 1)$coverage
return(sim_coverage_out) return(sim_coverage_out)
} }
@@ -82,13 +90,12 @@ for (gg in 1:length(unique(wisca_in$keyantimicrobials))){
av_coverage, av_coverage,
ci_coverage_lower, ci_coverage_lower,
ci_coverage_upper, ci_coverage_upper,
row.names = NULL) row.names = NULL
)
names(combine_out) <- c("Regimen", str_to_sentence(analysis), "Coverage", "Lower_CI", "Upper_CI") names(combine_out) <- c("Regimen", str_to_sentence(analysis), "Coverage", "Lower_CI", "Upper_CI")
cover_level <- rbind.data.frame(cover_level, combine_out, row.names = NULL) cover_level <- rbind.data.frame(cover_level, combine_out, row.names = NULL)
} }
cover <- rbind.data.frame(cover, cover_level, row.names = NULL) cover <- rbind.data.frame(cover, cover_level, row.names = NULL)
} else { } else {
params_priors <- priors(wisca_prior_in = wisca) params_priors <- priors(wisca_prior_in = wisca)
@@ -102,21 +109,20 @@ for (gg in 1:length(unique(wisca_in$keyantimicrobials))){
av_coverage, av_coverage,
ci_coverage_lower, ci_coverage_lower,
ci_coverage_upper, ci_coverage_upper,
row.names = NULL) row.names = NULL
)
names(combine_out) <- c("Regimen", "Coverage", "Lower_CI", "Upper_CI") names(combine_out) <- c("Regimen", "Coverage", "Lower_CI", "Upper_CI")
cover <- rbind.data.frame(cover, combine_out, row.names = NULL) cover <- rbind.data.frame(cover, combine_out, row.names = NULL)
} }
} }
cover <- cover %>% cover <- cover %>%
mutate(across(.cols = Coverage:Upper_CI, ~ round(.x * 100, 1))) %>% mutate(across(.cols = Coverage:Upper_CI, ~ round(.x * 100, 1))) %>%
rowwise() %>% rowwise() %>%
mutate(Regimen_full = ifelse(str_detect(Regimen, fixed("+")), mutate(Regimen_full = ifelse(str_detect(Regimen, fixed("+")),
str_c(sapply(unlist(unique(str_split(Regimen, fixed("+")))), ab_name, USE.NAMES = FALSE), collapse = " + "), str_c(sapply(unlist(unique(str_split(Regimen, fixed("+")))), ab_name, USE.NAMES = FALSE), collapse = " + "),
ab_name(Regimen))) %>% ab_name(Regimen)
)) %>%
relocate(Regimen_full, .before = Regimen) relocate(Regimen_full, .before = Regimen)
return(cover) return(cover)
} }
+7 -15
View File
@@ -170,13 +170,9 @@ example_isolates %>%
#> ℹ Using column mo as input for `mo_fullname()` #> ℹ Using column mo as input for `mo_fullname()`
#> ℹ Using column mo as input for `mo_is_gram_negative()` #> ℹ Using column mo as input for `mo_is_gram_negative()`
#> ℹ Using column mo as input for `mo_is_intrinsic_resistant()` #> ℹ Using column mo as input for `mo_is_intrinsic_resistant()`
#> ℹ Determining intrinsic resistance based on 'EUCAST Expected #> ℹ Determining intrinsic resistance based on 'EUCAST Expected Resistant Phenotypes' v1.2 (2023). This note will be shown once per session.
#> Resistant Phenotypes' v1.2 (2023). This note will be shown #> ℹ For `aminoglycosides()` using columns GEN (gentamicin), TOB (tobramycin), AMK (amikacin), and KAN (kanamycin)
#> once per session. #> ℹ For `carbapenems()` using columns IPM (imipenem) and MEM (meropenem)
#> ℹ For `aminoglycosides()` using columns GEN (gentamicin), TOB
#> (tobramycin), AMK (amikacin), and KAN (kanamycin)
#> ℹ For `carbapenems()` using columns IPM (imipenem) and MEM
#> (meropenem)
#> # A tibble: 35 × 7 #> # A tibble: 35 × 7
#> bacteria GEN TOB AMK KAN IPM MEM #> bacteria GEN TOB AMK KAN IPM MEM
#> <chr> <sir> <sir> <sir> <sir> <sir> <sir> #> <chr> <sir> <sir> <sir> <sir> <sir> <sir>
@@ -253,8 +249,7 @@ for tracking resistance per species over time:
antibiogram(example_isolates, antibiogram(example_isolates,
mo_transform = "gramstain", mo_transform = "gramstain",
antimicrobials = c("AMC", carbapenems(), "TZP")) antimicrobials = c("AMC", carbapenems(), "TZP"))
#> ℹ For `carbapenems()` using columns IPM (imipenem) and MEM #> ℹ For `carbapenems()` using columns IPM (imipenem) and MEM (meropenem)
#> (meropenem)
``` ```
| Pathogen | Amoxicillin/clavulanic acid | Imipenem | Meropenem | Piperacillin/tazobactam | | Pathogen | Amoxicillin/clavulanic acid | Imipenem | Meropenem | Piperacillin/tazobactam |
@@ -369,16 +364,13 @@ out <- example_isolates %>%
# calculate AMR using resistance(), over all aminoglycosides and polymyxins: # calculate AMR using resistance(), over all aminoglycosides and polymyxins:
summarise(across(c(aminoglycosides(), polymyxins()), summarise(across(c(aminoglycosides(), polymyxins()),
resistance)) resistance))
#> ℹ For `aminoglycosides()` using columns GEN (gentamicin), TOB #> ℹ For `aminoglycosides()` using columns GEN (gentamicin), TOB (tobramycin), AMK (amikacin), and KAN (kanamycin)
#> (tobramycin), AMK (amikacin), and KAN (kanamycin)
#> ℹ For `polymyxins()` using column COL (colistin) #> ℹ For `polymyxins()` using column COL (colistin)
#> Warning: There was 1 warning in `summarise()`. #> Warning: There was 1 warning in `summarise()`.
#> ℹ In argument: `across(c(aminoglycosides(), polymyxins()), #> ℹ In argument: `across(c(aminoglycosides(), polymyxins()), resistance)`.
#> resistance)`.
#> ℹ In group 3: `ward = "Outpatient"`. #> ℹ In group 3: `ward = "Outpatient"`.
#> Caused by warning: #> Caused by warning:
#> ! Introducing NA: only 23 results available for KAN in group: #> ! Introducing NA: only 23 results available for KAN in group: ward = "Outpatient" (whilst `minimum = 30`).
#> ward = "Outpatient" (whilst `minimum = 30`).
out out
#> # A tibble: 3 × 6 #> # A tibble: 3 × 6
#> ward GEN TOB AMK KAN COL #> ward GEN TOB AMK KAN COL
+2 -1
View File
@@ -334,7 +334,8 @@ if (requireNamespace("dplyr")) {
top_n_microorganisms(n = 10) \%>\% top_n_microorganisms(n = 10) \%>\%
group_by( group_by(
age_group = age_groups(age, c(25, 50, 75)), age_group = age_groups(age, c(25, 50, 75)),
gender) \%>\% gender
) \%>\%
wisca(antimicrobials = c("TZP", "TZP+TOB", "TZP+GEN")) wisca(antimicrobials = c("TZP", "TZP+TOB", "TZP+GEN"))
} }
+10 -6
View File
@@ -308,7 +308,6 @@ carbapenems()
# Though they are primarily intended to use for selections and filters. # Though they are primarily intended to use for selections and filters.
# Examples sections below are split into 'dplyr', 'base R', and 'data.table': # Examples sections below are split into 'dplyr', 'base R', and 'data.table':
\donttest{
\dontrun{ \dontrun{
# dplyr ------------------------------------------------------------------- # dplyr -------------------------------------------------------------------
@@ -329,8 +328,10 @@ example_isolates \%>\% select(administrable_per_os())
# get AMR for all aminoglycosides e.g., per ward: # get AMR for all aminoglycosides e.g., per ward:
example_isolates \%>\% example_isolates \%>\%
group_by(ward) \%>\% group_by(ward) \%>\%
summarise(across(aminoglycosides(), summarise(across(
resistance)) aminoglycosides(),
resistance
))
# You can combine selectors with '&' to be more specific: # You can combine selectors with '&' to be more specific:
example_isolates \%>\% example_isolates \%>\%
@@ -340,8 +341,10 @@ example_isolates \%>\%
example_isolates \%>\% example_isolates \%>\%
filter(mo_genus() \%in\% c("Escherichia", "Klebsiella")) \%>\% filter(mo_genus() \%in\% c("Escherichia", "Klebsiella")) \%>\%
group_by(ward) \%>\% group_by(ward) \%>\%
summarise_at(not_intrinsic_resistant(), summarise_at(
resistance) not_intrinsic_resistant(),
resistance
)
# get susceptibility for antimicrobials whose name contains "trim": # get susceptibility for antimicrobials whose name contains "trim":
example_isolates \%>\% example_isolates \%>\%
@@ -394,8 +397,9 @@ x <- example_isolates[carbapenems() == "R", ]
y <- example_isolates \%>\% filter(carbapenems() == "R") y <- example_isolates \%>\% filter(carbapenems() == "R")
z <- example_isolates \%>\% filter(if_all(carbapenems(), ~ .x == "R")) z <- example_isolates \%>\% filter(if_all(carbapenems(), ~ .x == "R"))
identical(x, y) && identical(y, z) identical(x, y) && identical(y, z)
} }
\donttest{
# base R ------------------------------------------------------------------ # base R ------------------------------------------------------------------
# select columns 'IPM' (imipenem) and 'MEM' (meropenem) # select columns 'IPM' (imipenem) and 'MEM' (meropenem)