Files
AMR/data-raw/microorganisms_files/build_mo_code_registry.R
T
dr. M.S. (Matthijs) BerendsandClaude Opus 5.5 d683b9e7c6 (v3.0.1.9096) MO code registry: codes never change meaning, are never guessed, and are validated
- data-raw/microorganisms_files/: registry of all MO codes of every release since v2.0.0
  (78,992 codes, built from the git tags), approved renames, and README with the rules;
  decision by Matthijs S. Berends: MO codes from before v2.0.0 are not supported
- as.mo(): codes of earlier releases are translated deterministically via the internal
  MO_RETIRED_CODES (generated from the registry); unknown codes are no longer guessed
  (previously F_TRCHP, Trichophyton in v3.0.1, became Talaromyces chlamydosporus)
- synonym_mo_to_accepted_mo(): a missing target of one source no longer overwrites a valid
  target of another source (e.g. Chaetomium abuense), and no 'renamed to itself'
- taxonomy build: codes are reused and reserved from the registry instead of the development
  data (fixes 40 codes reused in the 2026 development data), also for phylum to family;
  synonym cycles are broken; dangling 'renamed to' identifiers are removed; the build stops
  on any broken integrity rule before saving
- run_microorganisms_build.R: runs the build in chunks at its checkpoints in one R process,
  with progress logging and resuming from any checkpoint; prompt updated accordingly
- tests: shared integrity rules (helper-microorganisms.R) used by build and testthat, registry
  tests over all codes, sentinel organisms, regression tests; current defects of the data on
  main are listed in microorganisms_known_defects.csv until the next rebuild

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-03 11:45:25 +02:00

114 lines
5.3 KiB
R

# ==================================================================== #
# TITLE: #
# AMR: An R Package for Working with Antimicrobial Resistance Data #
# #
# SOURCE CODE: #
# https://github.com/msberends/AMR #
# #
# PLEASE CITE THIS SOFTWARE AS: #
# Berends MS, Luz CF, Friedrich AW, et al. (2022). #
# AMR: An R Package for Working with Antimicrobial Resistance Data. #
# Journal of Statistical Software, 104(3), 1-31. #
# https://doi.org/10.18637/jss.v104.i03 #
# #
# Developed at the University of Groningen and the University Medical #
# Center Groningen in the Netherlands, in collaboration with many #
# colleagues from around the world, see our website. #
# #
# This R package is free software; you can freely use and distribute #
# it for both personal and commercial purposes under the terms of the #
# GNU General Public License version 2.0 (GNU GPL-2), as published by #
# the Free Software Foundation. #
# We created this package for both routine data analysis and academic #
# research and it was publicly released in the hope that it will be #
# useful, but it comes WITHOUT ANY WARRANTY OR LIABILITY. #
# #
# Visit our website for the full manual and a complete tutorial about #
# how to conduct AMR data analysis: https://amr-for-r.org #
# ==================================================================== #
# This script (re)creates data-raw/microorganisms_files/mo_code_registry.csv: the registry of all MO codes
# that were ever part of a released version of the AMR package, since v2.0.0. See the README.md in this
# folder for the rules.
#
# Run it from the root of the repository, after every new release (once its git tag exists):
# source("data-raw/microorganisms_files/build_mo_code_registry.R")
# It always rebuilds the whole registry from the git tags, so it is reproducible and can be run at any time.
# Development versions (the main branch) are never part of the registry.
library(dplyr)
devtools::load_all(".", quiet = TRUE) # for %like_case%
registry_file <- "data-raw/microorganisms_files/mo_code_registry.csv"
# Decision by Matthijs S. Berends, 3 October 2026: MO codes from releases before v2.0.0 (12 March 2023)
# are not supported.
first_supported_release <- "2.0.0"
# all release tags (vX.Y.Z, so no development versions) since the first supported release
tags <- system2("git", "tag", stdout = TRUE)
tags <- tags[tags %like_case% "^v[0-9]+[.][0-9]+[.][0-9]+$"]
tags <- tags[numeric_version(sub("^v", "", tags)) >= numeric_version(first_supported_release)]
tags <- tags[order(numeric_version(sub("^v", "", tags)))]
message("Releases: ", toString(tags))
read_release <- function(tag) {
tmp <- tempfile(fileext = ".rda")
on.exit(unlink(tmp))
status <- system2("git", c("show", paste0(tag, ":data/microorganisms.rda")), stdout = tmp)
if (!identical(status, 0L)) {
stop("Could not read data/microorganisms.rda of ", tag, call. = FALSE)
}
env <- new.env()
load(tmp, envir = env)
mo <- as.data.frame(env$microorganisms, stringsAsFactors = FALSE)
# until v3.0.1, `kingdom` contained what is now called `domain`
if (!"domain" %in% colnames(mo)) {
mo$domain <- mo$kingdom
mo$domain[mo$domain == "(unknown kingdom)"] <- "(unknown domain)"
}
data.frame(
release = tag,
mo = as.character(mo$mo),
fullname = mo$fullname,
rank = mo$rank,
domain = mo$domain,
genus = mo$genus,
species = mo$species,
subspecies = mo$subspecies,
stringsAsFactors = FALSE
)
}
all_releases <- bind_rows(lapply(tags, read_release)) %>%
mutate(release_order = match(release, tags))
# one row per MO code; the name, rank and taxonomy are taken from the most recent release that contained the code
registry <- all_releases %>%
group_by(mo) %>%
arrange(desc(release_order), .by_group = TRUE) %>%
summarise(
# other names the code had in older releases (e.g. a corrected subspecies name), for transparency
# (must come first, since `fullname` is overwritten below)
previous_names = paste(setdiff(unique(fullname), first(fullname)), collapse = "; "),
fullname = first(fullname),
rank = first(rank),
domain = first(domain),
genus = first(genus),
species = first(species),
subspecies = first(subspecies),
first_release = tags[min(release_order)],
last_release = tags[max(release_order)],
.groups = "drop"
) %>%
relocate(previous_names, .after = everything()) %>%
arrange(mo)
message(
nrow(registry), " MO codes, of which ", sum(registry$previous_names != ""),
" had another name in an older release (check these when the registry changes)"
)
write.csv(registry, registry_file, row.names = FALSE, na = "")
message("Saved to ", registry_file)