diff --git a/DESCRIPTION b/DESCRIPTION index 78eac206..8097d6e5 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,5 +1,5 @@ Package: AMR -Version: 0.7.1.9076 +Version: 0.7.1.9077 Date: 2019-09-20 Title: Antimicrobial Resistance Analysis Authors@R: c( diff --git a/NEWS.md b/NEWS.md index 84a260f8..35028efd 100755 --- a/NEWS.md +++ b/NEWS.md @@ -1,4 +1,4 @@ -# AMR 0.7.1.9076 +# AMR 0.7.1.9077 Last updated: 20-Sep-2019 ### Breaking diff --git a/R/data.R b/R/data.R index 58c9d1bd..e4c4477d 100755 --- a/R/data.R +++ b/R/data.R @@ -98,12 +98,13 @@ catalogue_of_life <- list( #' #' A data set containing old (previously valid or accepted) taxonomic names according to the Catalogue of Life. This data set is used internally by \code{\link{as.mo}}. #' @inheritSection catalogue_of_life Catalogue of Life -#' @format A \code{\link{data.frame}} with 24,246 observations and 4 variables: +#' @format A \code{\link{data.frame}} with 24,246 observations and 5 variables: #' \describe{ #' \item{\code{col_id}}{Catalogue of Life ID that was originally given} #' \item{\code{col_id_new}}{New Catalogue of Life ID that responds to an entry in the \code{\link{microorganisms}} data set} #' \item{\code{fullname}}{Old full taxonomic name of the microorganism} #' \item{\code{ref}}{Author(s) and year of concerning scientific publication} +#' \item{\code{prevalence}}{Prevalence of the microorganism, see \code{?as.mo}} #' } #' @source Catalogue of Life: Annual Checklist (public online taxonomic database), \url{http://www.catalogueoflife.org} (check included annual version with \code{\link{catalogue_of_life_version}()}). #' @inheritSection AMR Read more on our website! diff --git a/R/mo.R b/R/mo.R index 4e03f138..fc2f2642 100755 --- a/R/mo.R +++ b/R/mo.R @@ -71,9 +71,10 @@ #' \itemize{ #' \item{Valid MO codes and full names: it first searches in already valid MO code and known genus/species combinations} #' \item{Human pathogenic prevalence: it first searches in more prevalent microorganisms, then less prevalent ones (see \emph{Microbial prevalence of pathogens in humans} below)} -#' \item{Taxonomic kingdom: it first searches in Bacteria/Chromista, then Fungi, then Protozoa} +#' \item{Taxonomic kingdom: it first searches in Bacteria, then Fungi, then Protozoa, then Archaea, then others} #' \item{Breakdown of input values: from here it starts to breakdown input values to find possible matches} #' } +#' #' #' A couple of effects because of these rules: #' \itemize{ @@ -258,7 +259,6 @@ as.mo <- function(x, Becker = FALSE, Lancefield = FALSE, allow_uncertain = TRUE, y <- mo_validate(x = x, property = "mo", Becker = Becker, Lancefield = Lancefield, allow_uncertain = uncertainty_level, reference_df = reference_df, - #force_mo_history = isTRUE(list(...)$force_mo_history), ...) } @@ -285,7 +285,7 @@ is.mo <- function(x) { # param force_mo_history logical - whether found result must be saved with set_mo_history (default FALSE on non-interactive sessions) # param disable_mo_history logical - whether set_mo_history and get_mo_history should be ignored # param debug logical - show different lookup texts while searching -# param uncertain_check_prevalence integer - the prevalence to check for when running for uncertain results, follows microorganisms$prevalence +# param reference_data_to_use data.frame - the data set to check for exec_as.mo <- function(x, Becker = FALSE, Lancefield = FALSE, @@ -936,6 +936,7 @@ exec_as.mo <- function(x, # NOW RUN THROUGH DIFFERENT PREVALENCE LEVELS check_per_prevalence <- function(data_to_check, + data.old_to_check, a.x_backup, b.x_trimmed, c.x_trimmed_without_group, @@ -1065,33 +1066,31 @@ exec_as.mo <- function(x, } # MISCELLANEOUS ---- - + # look for old taxonomic names ---- # wait until prevalence == 2 to run the old taxonomic results on both prevalence == 1 and prevalence == 2 - if (nrow(data_to_check) == nrow(microorganismsDT[prevalence == 2])) { - found <- microorganisms.oldDT[fullname_lower == tolower(a.x_backup) - | fullname_lower %like_case% d.x_withspaces_start_end,] - if (NROW(found) > 0) { - col_id_new <- found[1, col_id_new] - # when property is "ref" (which is the case in mo_ref, mo_authors and mo_year), return the old value, so: - # mo_ref("Chlamydia psittaci") = "Page, 1968" (with warning) - # mo_ref("Chlamydophila psittaci") = "Everett et al., 1999" - if (property == "ref") { - x[i] <- found[1, ref] - } else { - x[i] <- microorganismsDT[col_id == found[1, col_id_new], ..property][[1]] - } - options(mo_renamed_last_run = found[1, fullname]) - was_renamed(name_old = found[1, fullname], - name_new = microorganismsDT[col_id == found[1, col_id_new], fullname], - ref_old = found[1, ref], - ref_new = microorganismsDT[col_id == found[1, col_id_new], ref], - mo = microorganismsDT[col_id == found[1, col_id_new], mo]) - if (initial_search == TRUE) { - set_mo_history(a.x_backup, get_mo_code(x[i], property), 0, force = force_mo_history, disable = disable_mo_history) - } - return(x[i]) + found <- data.old_to_check[fullname_lower == tolower(a.x_backup) + | fullname_lower %like_case% d.x_withspaces_start_end,] + if (NROW(found) > 0) { + col_id_new <- found[1, col_id_new] + # when property is "ref" (which is the case in mo_ref, mo_authors and mo_year), return the old value, so: + # mo_ref("Chlamydia psittaci") = "Page, 1968" (with warning) + # mo_ref("Chlamydophila psittaci") = "Everett et al., 1999" + if (property == "ref") { + x[i] <- found[1, ref] + } else { + x[i] <- microorganismsDT[col_id == found[1, col_id_new], ..property][[1]] } + options(mo_renamed_last_run = found[1, fullname]) + was_renamed(name_old = found[1, fullname], + name_new = microorganismsDT[col_id == found[1, col_id_new], fullname], + ref_old = found[1, ref], + ref_new = microorganismsDT[col_id == found[1, col_id_new], ref], + mo = microorganismsDT[col_id == found[1, col_id_new], mo]) + if (initial_search == TRUE) { + set_mo_history(a.x_backup, get_mo_code(x[i], property), 0, force = force_mo_history, disable = disable_mo_history) + } + return(x[i]) } # check for uncertain results ---- @@ -1119,7 +1118,7 @@ exec_as.mo <- function(x, if (isTRUE(debug)) { message("Running '", d.x_withspaces_start_end, "' and '", e.x_withspaces_start_only, "'") } - found <- microorganisms.oldDT[fullname_lower %like_case% d.x_withspaces_start_end + found <- data.old_to_check[fullname_lower %like_case% d.x_withspaces_start_end | fullname_lower %like_case% e.x_withspaces_start_only] if (NROW(found) > 0 & nchar(g.x_backup_without_spp) >= 6) { if (property == "ref") { @@ -1521,6 +1520,7 @@ exec_as.mo <- function(x, # FIRST TRY VERY PREVALENT IN HUMAN INFECTIONS ---- x[i] <- check_per_prevalence(data_to_check = reference_data_to_use[prevalence == 1], + data.old_to_check = microorganisms.oldDT[prevalence == 1], a.x_backup = x_backup[i], b.x_trimmed = x_trimmed[i], c.x_trimmed_without_group = x_trimmed_without_group[i], @@ -1539,6 +1539,7 @@ exec_as.mo <- function(x, # THEN TRY PREVALENT IN HUMAN INFECTIONS ---- x[i] <- check_per_prevalence(data_to_check = reference_data_to_use[prevalence == 2], + data.old_to_check = microorganisms.oldDT[prevalence == 2], a.x_backup = x_backup[i], b.x_trimmed = x_trimmed[i], c.x_trimmed_without_group = x_trimmed_without_group[i], @@ -1557,6 +1558,7 @@ exec_as.mo <- function(x, # THEN UNPREVALENT IN HUMAN INFECTIONS ---- x[i] <- check_per_prevalence(data_to_check = reference_data_to_use[prevalence == 3], + data.old_to_check = microorganisms.oldDT[prevalence == 3], a.x_backup = x_backup[i], b.x_trimmed = x_trimmed[i], c.x_trimmed_without_group = x_trimmed_without_group[i], diff --git a/R/mo_history.R b/R/mo_history.R index 445d9fea..ce38e719 100644 --- a/R/mo_history.R +++ b/R/mo_history.R @@ -55,7 +55,7 @@ set_mo_history <- function(x, mo, uncertainty_level, force = FALSE, disable = FA # if (tryCatch(nrow(getOption("mo_remembered_results")), error = function(e) 1001) > 1000) { # return(base::invisible()) # } - if (is.null(mo_hist)) { + if (is.null(mo_hist) & interactive()) { message(blue(paste0("NOTE: results are saved to ", mo_history_file(), "."))) } tryCatch(write.csv(rbind(mo_hist, diff --git a/R/sysdata.rda b/R/sysdata.rda index 62f80d47..4f28c4f4 100644 Binary files a/R/sysdata.rda and b/R/sysdata.rda differ diff --git a/R/zzz.R b/R/zzz.R index 626ae0eb..be3be7f4 100755 --- a/R/zzz.R +++ b/R/zzz.R @@ -28,7 +28,7 @@ microorganisms.oldDT <- as.data.table(AMR::microorganisms.old) # for fullname_lower: keep only dots, letters, numbers, slashes, spaces and dashes microorganisms.oldDT$fullname_lower <- gsub("[^.a-z0-9/ \\-]+", "", tolower(microorganisms.oldDT$fullname)) - setkey(microorganisms.oldDT, col_id, fullname) + setkey(microorganisms.oldDT, prevalence, fullname) assign(x = "microorganismsDT", value = make_DT(), @@ -81,12 +81,17 @@ #' @importFrom data.table as.data.table setkey make_DT <- function() { - microorganismsDT <- as.data.table(AMR::microorganisms) + microorganismsDT <- as.data.table(AMR::microorganisms %>% + mutate(kingdom_index = case_when(kingdom == "Bacteria" ~ 1, + kingdom == "Fungi" ~ 2, + kingdom == "Protozoa" ~ 3, + kingdom == "Archaea" ~ 4, + TRUE ~ 6))) # for fullname_lower: keep only dots, letters, numbers, slashes, spaces and dashes microorganismsDT$fullname_lower <- gsub("[^.a-z0-9/ \\-]+", "", tolower(microorganismsDT$fullname)) setkey(microorganismsDT, prevalence, - kingdom, + kingdom_index, fullname) microorganismsDT } diff --git a/data-raw/microorganisms.translation.rds b/data-raw/microorganisms.translation.rds index 65377f50..c4509704 100644 Binary files a/data-raw/microorganisms.translation.rds and b/data-raw/microorganisms.translation.rds differ diff --git a/data-raw/reproduction_of_microorganisms.R b/data-raw/reproduction_of_microorganisms.R index f1864d97..36dddc00 100644 --- a/data-raw/reproduction_of_microorganisms.R +++ b/data-raw/reproduction_of_microorganisms.R @@ -636,6 +636,9 @@ MOs <- MOs %>% ungroup() %>% filter(fullname != "") +MOs.old <- MOs.old %>% + left_join(MOs %>% select(col_id, prevalence), by = c("col_id_new" = "col_id")) + # everything distinct? sum(duplicated(MOs$mo)) sum(duplicated(MOs$fullname)) diff --git a/data/microorganisms.old.rda b/data/microorganisms.old.rda index 73c3609c..4be20397 100644 Binary files a/data/microorganisms.old.rda and b/data/microorganisms.old.rda differ diff --git a/docs/LICENSE-text.html b/docs/LICENSE-text.html index 206af166..407b1ee8 100644 --- a/docs/LICENSE-text.html +++ b/docs/LICENSE-text.html @@ -78,7 +78,7 @@ AMR (for R) - 0.7.1.9076 + 0.7.1.9077 diff --git a/docs/articles/benchmarks.html b/docs/articles/benchmarks.html index 0f74654a..32ea701b 100644 --- a/docs/articles/benchmarks.html +++ b/docs/articles/benchmarks.html @@ -40,7 +40,7 @@ AMR (for R) - 0.7.1.9076 + 0.7.1.9077 @@ -199,7 +199,7 @@

Using the microbenchmark package, we can review the calculation performance of this function. Its function microbenchmark() runs different input expressions independently of each other and measures their time-to-result.

library(microbenchmark)
 library(AMR)
-

In the next test, we try to ‘coerce’ different input values for Staphylococcus aureus. The actual result is the same every time: it returns its MO code B_STPHY_AURS (B stands for Bacteria, the taxonomic kingdom).

+

In the next test, we try to ‘coerce’ different input values for Staphylococcus aureus. The actual result is the same every time: it returns its microorganism code B_STPHY_AURS (B stands for Bacteria, the taxonomic kingdom).

But the calculation time differs a lot:

S.aureus <- microbenchmark(
   as.mo("sau"), # WHONET code
@@ -219,193 +219,167 @@
   times = 10)
 print(S.aureus, unit = "ms", signif = 2)
 # Unit: milliseconds
-#                                   expr   min    lq  mean median    uq max
-#                           as.mo("sau")   8.4   8.8  23.0    8.8  30.0 100
-#                          as.mo("stau")  31.0  31.0  42.0   35.0  54.0  60
-#                          as.mo("STAU")  31.0  32.0  39.0   33.0  53.0  56
-#                        as.mo("staaur")   8.5   8.9  11.0    9.0   9.2  33
-#                        as.mo("STAAUR")   8.6   8.8   9.4    9.3   9.4  11
-#                     as.mo("S. aureus")  23.0  24.0  28.0   26.0  28.0  51
-#                      as.mo("S aureus")  23.0  24.0  29.0   25.0  25.0  51
-#         as.mo("Staphylococcus aureus")  28.0  29.0  30.0   29.0  30.0  32
-#  as.mo("Staphylococcus aureus (MRSA)") 600.0 620.0 640.0  620.0 640.0 800
-#       as.mo("Sthafilokkockus aaureuz") 320.0 340.0 370.0  350.0 400.0 450
-#                          as.mo("MRSA")   8.7   8.7  11.0    9.2   9.9  31
-#                          as.mo("VISA")  19.0  19.0  20.0   19.0  20.0  23
-#                          as.mo("VRSA")  19.0  19.0  25.0   20.0  28.0  44
-#                        as.mo(22242419)  18.0  19.0  47.0   31.0  48.0 190
-#  neval
-#     10
-#     10
-#     10
-#     10
-#     10
-#     10
-#     10
-#     10
-#     10
-#     10
-#     10
-#     10
-#     10
-#     10
-

+# expr min lq mean median uq +# as.mo("sau") 8.6 8.8 9.0 9.0 9.2 +# as.mo("stau") 31.0 31.0 32.0 32.0 33.0 +# as.mo("STAU") 31.0 32.0 39.0 33.0 38.0 +# as.mo("staaur") 8.6 9.1 17.0 9.7 31.0 +# as.mo("STAAUR") 8.7 9.0 14.0 9.3 9.6 +# as.mo("S. aureus") 23.0 23.0 45.0 24.0 25.0 +# as.mo("S aureus") 23.0 23.0 29.0 24.0 26.0 +# as.mo("Staphylococcus aureus") 28.0 28.0 32.0 29.0 30.0 +# as.mo("Staphylococcus aureus (MRSA)") 530.0 560.0 580.0 570.0 580.0 +# as.mo("Sthafilokkockus aaureuz") 270.0 300.0 310.0 310.0 320.0 +# as.mo("MRSA") 8.6 9.0 9.3 9.2 9.3 +# as.mo("VISA") 19.0 20.0 27.0 20.0 43.0 +# as.mo("VRSA") 19.0 20.0 26.0 21.0 23.0 +# as.mo(22242419) 18.0 18.0 24.0 19.0 22.0 +# max neval +# 9.5 10 +# 38.0 10 +# 61.0 10 +# 38.0 10 +# 35.0 10 +# 210.0 10 +# 56.0 10 +# 57.0 10 +# 660.0 10 +# 380.0 10 +# 10.0 10 +# 46.0 10 +# 48.0 10 +# 42.0 10 +

In the table above, all measurements are shown in milliseconds (thousands of seconds). A value of 5 milliseconds means it can determine 200 input values per second. It case of 100 milliseconds, this is only 10 input values per second. The second input is the only one that has to be looked up thoroughly. All the others are known codes (the first one is a WHONET code) or common laboratory codes, or common full organism names like the last one. Full organism names are always preferred.

-

To achieve this speed, the as.mo function also takes into account the prevalence of human pathogenic microorganisms. The downside is of course that less prevalent microorganisms will be determined less fast. See this example for the ID of Thermus islandicus (B_THERMS_ISLN), a bug probably never found before in humans:

-
T.islandicus <- microbenchmark(as.mo("theisl"),
-                               as.mo("THEISL"),
-                               as.mo("T. islandicus"),
-                               as.mo("T.  islandicus"),
-                               as.mo("Thermus islandicus"),
+

To achieve this speed, the as.mo function also takes into account the prevalence of human pathogenic microorganisms. The downside is of course that less prevalent microorganisms will be determined less fast. See this example for the ID of Methanosarcina semesiae (B_MTHNSR_SEMS), a bug probably never found before in humans:

+ -

That takes 8.2 times as much time on average. A value of 100 milliseconds means it can only determine ~10 different input values per second. We can conclude that looking up arbitrary codes of less prevalent microorganisms is the worst way to go, in terms of calculation performance. Full names (like Thermus islandicus) are almost fast - these are the most probable input from most data sets.

-

In the figure below, we compare Escherichia coli (which is very common) with Prevotella brevis (which is moderately common) and with Thermus islandicus (which is uncommon):

-
# Warning: 
-# Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it.
-
-# Warning: 
-# Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it.
-
-# Warning: 
-# Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it.
-
-# Warning: 
-# Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it.
-
-# Warning: 
-# Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it.
-
-# Warning: 
-# Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it.
-
-# Warning: 
-# Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it.
-
-# Warning: 
-# Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it.
-
-# Warning: 
-# Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it.
-
-# Warning: 
-# Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it.
-

+# expr min lq mean median uq +# as.mo("metsem") 1201.00 1327.00 1331.00 1340.00 1359.00 +# as.mo("METSEM") 1255.00 1298.00 1333.00 1340.00 1363.00 +# as.mo("M. semesiae") 1927.00 1943.00 1985.00 1995.00 2014.00 +# as.mo("M. semesiae") 1914.00 1953.00 1979.00 1977.00 1987.00 +# as.mo("Methanosarcina semesiae") 27.84 30.71 31.59 31.12 31.44 +# max neval +# 1371.00 10 +# 1398.00 10 +# 2040.00 10 +# 2058.00 10 +# 39.75 10
+

That takes 15.7 times as much time on average. A value of 100 milliseconds means it can only determine ~10 different input values per second. We can conclude that looking up arbitrary codes of less prevalent microorganisms is the worst way to go, in terms of calculation performance. Full names (like Methanosarcina semesiae) are almost fast - these are the most probable input from most data sets.

+

In the figure below, we compare Escherichia coli (which is very common) with Prevotella brevis (which is moderately common) and with Methanosarcina semesiae (which is uncommon):

+

In reality, the as.mo() functions learns from its own output to speed up determinations for next times. In above figure, this effect was disabled to show the difference with the boxplot below - when you would use as.mo() yourself:

-
# NOTE: results are saved to /Users/msberends/Library/R/3.6/library/AMR/mo_history/mo_history.csv.
-# Warning: 
-# Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it.
-

+

The highest outliers are the first times. All next determinations were done in only thousands of seconds.

Uncommon microorganisms take a lot more time than common microorganisms. To relieve this pitfall and further improve performance, two important calculations take almost no time at all: repetitive results and already precalculated results.

Repetitive results

Repetitive results are unique values that are present more than once. Unique values will only be calculated once by as.mo(). We will use mo_name() for this test - a helper function that returns the full microbial name (genus, species and possibly subspecies) which uses as.mo() internally.

-
library(dplyr)
-# take all MO codes from the example_isolates data set
-x <- example_isolates$mo %>%
-  # keep only the unique ones
-  unique() %>%
-  # pick 50 of them at random
-  sample(50) %>%
-  # paste that 10,000 times
-  rep(10000) %>%
-  # scramble it
-  sample()
-  
-# got indeed 50 times 10,000 = half a million?
-length(x)
-# [1] 500000
-
-# and how many unique values do we have?
-n_distinct(x)
-# [1] 50
-
-# now let's see:
-run_it <- microbenchmark(mo_name(x),
-                         times = 10)
-print(run_it, unit = "ms", signif = 3)
-# Unit: milliseconds
-#        expr min  lq mean median  uq max neval
-#  mo_name(x) 604 632  655    644 660 764    10
-

So transforming 500,000 values (!!) of 50 unique values only takes 0.64 seconds (644 ms). You only lose time on your unique input values.

+
library(dplyr)
+# take all MO codes from the example_isolates data set
+x <- example_isolates$mo %>%
+  # keep only the unique ones
+  unique() %>%
+  # pick 50 of them at random
+  sample(50) %>%
+  # paste that 10,000 times
+  rep(10000) %>%
+  # scramble it
+  sample()
+  
+# got indeed 50 times 10,000 = half a million?
+length(x)
+# [1] 500000
+
+# and how many unique values do we have?
+n_distinct(x)
+# [1] 50
+
+# now let's see:
+run_it <- microbenchmark(mo_name(x),
+                         times = 10)
+print(run_it, unit = "ms", signif = 3)
+# Unit: milliseconds
+#        expr min  lq mean median  uq max neval
+#  mo_name(x) 610 637  653    652 668 718    10
+

So transforming 500,000 values (!!) of 50 unique values only takes 0.65 seconds (652 ms). You only lose time on your unique input values.

Precalculated results

What about precalculated results? If the input is an already precalculated result of a helper function like mo_name(), it almost doesn’t take any time at all (see ‘C’ below):

-
run_it <- microbenchmark(A = mo_name("B_STPHY_AURS"),
-                         B = mo_name("S. aureus"),
-                         C = mo_name("Staphylococcus aureus"),
-                         times = 10)
-print(run_it, unit = "ms", signif = 3)
-# Unit: milliseconds
-#  expr    min     lq   mean median     uq    max neval
-#     A  6.190  6.270  6.560  6.290  7.160  7.280    10
-#     B 22.600 23.100 26.700 23.400 24.300 51.600    10
-#     C  0.704  0.765  0.827  0.829  0.906  0.913    10
+
run_it <- microbenchmark(A = mo_name("B_STPHY_AURS"),
+                         B = mo_name("S. aureus"),
+                         C = mo_name("Staphylococcus aureus"),
+                         times = 10)
+print(run_it, unit = "ms", signif = 3)
+# Unit: milliseconds
+#  expr    min     lq   mean median     uq   max neval
+#     A  6.280  6.560  9.940  6.720  6.860 39.30    10
+#     B 22.500 22.900 24.300 23.000 24.900 30.90    10
+#     C  0.805  0.829  0.871  0.847  0.869  1.09    10

So going from mo_name("Staphylococcus aureus") to "Staphylococcus aureus" takes 0.0008 seconds - it doesn’t even start calculating if the result would be the same as the expected resulting value. That goes for all helper functions:

-
run_it <- microbenchmark(A = mo_species("aureus"),
-                         B = mo_genus("Staphylococcus"),
-                         C = mo_name("Staphylococcus aureus"),
-                         D = mo_family("Staphylococcaceae"),
-                         E = mo_order("Bacillales"),
-                         F = mo_class("Bacilli"),
-                         G = mo_phylum("Firmicutes"),
-                         H = mo_kingdom("Bacteria"),
-                         times = 10)
-print(run_it, unit = "ms", signif = 3)
-# Unit: milliseconds
-#  expr   min    lq  mean median    uq   max neval
-#     A 0.456 0.476 0.484  0.483 0.488 0.516    10
-#     B 0.613 0.620 0.639  0.628 0.642 0.723    10
-#     C 0.675 0.700 0.763  0.796 0.807 0.816    10
-#     D 0.443 0.454 0.466  0.467 0.477 0.497    10
-#     E 0.453 0.459 0.465  0.464 0.472 0.483    10
-#     F 0.433 0.447 0.464  0.463 0.484 0.498    10
-#     G 0.453 0.460 0.469  0.464 0.478 0.502    10
-#     H 0.433 0.451 0.477  0.459 0.470 0.662    10
+
run_it <- microbenchmark(A = mo_species("aureus"),
+                         B = mo_genus("Staphylococcus"),
+                         C = mo_name("Staphylococcus aureus"),
+                         D = mo_family("Staphylococcaceae"),
+                         E = mo_order("Bacillales"),
+                         F = mo_class("Bacilli"),
+                         G = mo_phylum("Firmicutes"),
+                         H = mo_kingdom("Bacteria"),
+                         times = 10)
+print(run_it, unit = "ms", signif = 3)
+# Unit: milliseconds
+#  expr   min    lq  mean median    uq   max neval
+#     A 0.456 0.457 0.472  0.465 0.493 0.498    10
+#     B 0.629 0.640 0.713  0.668 0.752 0.956    10
+#     C 0.798 0.811 0.840  0.832 0.840 0.965    10
+#     D 0.428 0.453 0.473  0.464 0.503 0.518    10
+#     E 0.446 0.477 0.513  0.495 0.525 0.648    10
+#     F 0.466 0.473 0.496  0.484 0.521 0.545    10
+#     G 0.457 0.461 0.477  0.468 0.486 0.545    10
+#     H 0.456 0.467 0.478  0.477 0.482 0.512    10

Of course, when running mo_phylum("Firmicutes") the function has zero knowledge about the actual microorganism, namely S. aureus. But since the result would be "Firmicutes" too, there is no point in calculating the result. And because this package ‘knows’ all phyla of all known bacteria (according to the Catalogue of Life), it can just return the initial value immediately.

Results in other languages

When the system language is non-English and supported by this AMR package, some functions will have a translated result. This almost does’t take extra time:

-
mo_name("CoNS", language = "en") # or just mo_name("CoNS") on an English system
-# [1] "Coagulase-negative Staphylococcus (CoNS)"
-
-mo_name("CoNS", language = "es") # or just mo_name("CoNS") on a Spanish system
-# [1] "Staphylococcus coagulasa negativo (SCN)"
-
-mo_name("CoNS", language = "nl") # or just mo_name("CoNS") on a Dutch system
-# [1] "Coagulase-negatieve Staphylococcus (CNS)"
-
-run_it <- microbenchmark(en = mo_name("CoNS", language = "en"),
-                         de = mo_name("CoNS", language = "de"),
-                         nl = mo_name("CoNS", language = "nl"),
-                         es = mo_name("CoNS", language = "es"),
-                         it = mo_name("CoNS", language = "it"),
-                         fr = mo_name("CoNS", language = "fr"),
-                         pt = mo_name("CoNS", language = "pt"),
-                         times = 10)
-print(run_it, unit = "ms", signif = 4)
-# Unit: milliseconds
-#  expr   min    lq  mean median    uq   max neval
-#    en 18.18 18.24 18.54  18.44 18.58 19.41    10
-#    de 19.52 19.79 20.03  19.90 20.15 20.95    10
-#    nl 24.54 24.94 26.29  25.70 26.35 31.90    10
-#    es 19.52 19.69 22.29  19.86 20.27 44.03    10
-#    it 19.52 19.57 22.05  19.74 20.46 41.43    10
-#    fr 19.61 19.67 22.28  20.04 20.30 42.90    10
-#    pt 19.51 19.79 25.16  20.01 20.73 49.48    10
+
mo_name("CoNS", language = "en") # or just mo_name("CoNS") on an English system
+# [1] "Coagulase-negative Staphylococcus (CoNS)"
+
+mo_name("CoNS", language = "es") # or just mo_name("CoNS") on a Spanish system
+# [1] "Staphylococcus coagulasa negativo (SCN)"
+
+mo_name("CoNS", language = "nl") # or just mo_name("CoNS") on a Dutch system
+# [1] "Coagulase-negatieve Staphylococcus (CNS)"
+
+run_it <- microbenchmark(en = mo_name("CoNS", language = "en"),
+                         de = mo_name("CoNS", language = "de"),
+                         nl = mo_name("CoNS", language = "nl"),
+                         es = mo_name("CoNS", language = "es"),
+                         it = mo_name("CoNS", language = "it"),
+                         fr = mo_name("CoNS", language = "fr"),
+                         pt = mo_name("CoNS", language = "pt"),
+                         times = 10)
+print(run_it, unit = "ms", signif = 4)
+# Unit: milliseconds
+#  expr   min    lq  mean median    uq   max neval
+#    en 18.04 18.47 18.79  18.54 19.25 19.70    10
+#    de 19.36 19.88 22.78  20.17 20.39 47.73    10
+#    nl 24.57 25.38 28.46  25.63 26.12 54.82    10
+#    es 19.50 19.89 25.49  20.51 25.79 44.96    10
+#    it 19.52 19.82 20.44  20.11 20.80 23.09    10
+#    fr 19.50 19.79 20.42  19.86 20.53 23.35    10
+#    pt 19.25 19.55 22.50  19.59 20.04 47.50    10

Currently supported are German, Dutch, Spanish, Italian, French and Portuguese.

diff --git a/docs/articles/benchmarks_files/figure-html/unnamed-chunk-11-1.png b/docs/articles/benchmarks_files/figure-html/unnamed-chunk-11-1.png new file mode 100644 index 00000000..6d9e642b Binary files /dev/null and b/docs/articles/benchmarks_files/figure-html/unnamed-chunk-11-1.png differ diff --git a/docs/articles/benchmarks_files/figure-html/unnamed-chunk-5-1.png b/docs/articles/benchmarks_files/figure-html/unnamed-chunk-5-1.png index 817c1125..62be83e0 100644 Binary files a/docs/articles/benchmarks_files/figure-html/unnamed-chunk-5-1.png and b/docs/articles/benchmarks_files/figure-html/unnamed-chunk-5-1.png differ diff --git a/docs/articles/benchmarks_files/figure-html/unnamed-chunk-9-1.png b/docs/articles/benchmarks_files/figure-html/unnamed-chunk-9-1.png new file mode 100644 index 00000000..92b0e416 Binary files /dev/null and b/docs/articles/benchmarks_files/figure-html/unnamed-chunk-9-1.png differ diff --git a/docs/articles/index.html b/docs/articles/index.html index 1adba2ec..138cf7c6 100644 --- a/docs/articles/index.html +++ b/docs/articles/index.html @@ -78,7 +78,7 @@ AMR (for R) - 0.7.1.9076 + 0.7.1.9077 diff --git a/docs/authors.html b/docs/authors.html index 128b1c9e..e4b1ecbb 100644 --- a/docs/authors.html +++ b/docs/authors.html @@ -78,7 +78,7 @@ AMR (for R) - 0.7.1.9076 + 0.7.1.9077 diff --git a/docs/index.html b/docs/index.html index 9a01a1d4..54f22dcb 100644 --- a/docs/index.html +++ b/docs/index.html @@ -42,7 +42,7 @@ AMR (for R) - 0.7.1.9076 + 0.7.1.9077 diff --git a/docs/news/index.html b/docs/news/index.html index 78687526..8f3f8db0 100644 --- a/docs/news/index.html +++ b/docs/news/index.html @@ -78,7 +78,7 @@ AMR (for R) - 0.7.1.9076 + 0.7.1.9077 @@ -225,9 +225,9 @@ -
+

-AMR 0.7.1.9076 Unreleased +AMR 0.7.1.9077 Unreleased

Last updated: 20-Sep-2019

@@ -1267,7 +1267,7 @@ Using as.mo(..., allow_uncertain = 3)

Contents

@@ -309,7 +309,7 @@ The as.mo() function gains experience from previously determined mi This function uses intelligent rules to help getting fast and logical results. It tries to find matches in this order:

  • Valid MO codes and full names: it first searches in already valid MO code and known genus/species combinations

  • Human pathogenic prevalence: it first searches in more prevalent microorganisms, then less prevalent ones (see Microbial prevalence of pathogens in humans below)

  • -
  • Taxonomic kingdom: it first searches in Bacteria/Chromista, then Fungi, then Protozoa

  • +
  • Taxonomic kingdom: it first searches in Bacteria, then Fungi, then Protozoa, then Archaea, then others

  • Breakdown of input values: from here it starts to breakdown input values to find possible matches

A couple of effects because of these rules:

    diff --git a/docs/reference/index.html b/docs/reference/index.html index ace20330..b0403d5f 100644 --- a/docs/reference/index.html +++ b/docs/reference/index.html @@ -78,7 +78,7 @@ AMR (for R) - 0.7.1.9076 + 0.7.1.9077
diff --git a/docs/reference/microorganisms.old.html b/docs/reference/microorganisms.old.html index 77208e82..f5f1a7c1 100644 --- a/docs/reference/microorganisms.old.html +++ b/docs/reference/microorganisms.old.html @@ -80,7 +80,7 @@ AMR (for R) - 0.7.1.9076 + 0.7.1.9077
@@ -238,11 +238,12 @@

Format

-

A data.frame with 24,246 observations and 4 variables:

+

A data.frame with 24,246 observations and 5 variables:

col_id

Catalogue of Life ID that was originally given

col_id_new

New Catalogue of Life ID that responds to an entry in the microorganisms data set

fullname

Old full taxonomic name of the microorganism

ref

Author(s) and year of concerning scientific publication

+
prevalence

Prevalence of the microorganism, see ?as.mo

Source

diff --git a/inst/mo_history/mo_history.csv b/inst/mo_history/mo_history.csv index db967b2c..6d9dc494 100644 --- a/inst/mo_history/mo_history.csv +++ b/inst/mo_history/mo_history.csv @@ -1 +1,8 @@ -"","x","mo","uncertainty_level","package_v" +"x","mo","uncertainty_level","package_version" +"M. SEMESIAE","B_MTHNSR_SEMS",0,"0.7.1.9077" +"METSEM","B_MTHNSR_SEMS",0,"0.7.1.9077" +"METHANOSARCINA SEMESIAE","B_MTHNSR_SEMS",0,"0.7.1.9077" +"PREVOTELLA BREVIS","B_PRVTL_BRVS",0,"0.7.1.9077" +"P. BREVIS","B_PRVTL_BRVS",0,"0.7.1.9077" +"E. COLI","B_ESCHR_COLI",0,"0.7.1.9077" +"ESCHERICHIA COLI","B_ESCHR_COLI",0,"0.7.1.9077" diff --git a/man/as.mo.Rd b/man/as.mo.Rd index 64317d8f..ee8c2ebd 100644 --- a/man/as.mo.Rd +++ b/man/as.mo.Rd @@ -81,10 +81,11 @@ This function uses intelligent rules to help getting fast and logical results. I \itemize{ \item{Valid MO codes and full names: it first searches in already valid MO code and known genus/species combinations} \item{Human pathogenic prevalence: it first searches in more prevalent microorganisms, then less prevalent ones (see \emph{Microbial prevalence of pathogens in humans} below)} - \item{Taxonomic kingdom: it first searches in Bacteria/Chromista, then Fungi, then Protozoa} + \item{Taxonomic kingdom: it first searches in Bacteria, then Fungi, then Protozoa, then Archaea, then others} \item{Breakdown of input values: from here it starts to breakdown input values to find possible matches} } + A couple of effects because of these rules: \itemize{ \item{\code{"E. coli"} will return the ID of \emph{Escherichia coli} and not \emph{Entamoeba coli}, although the latter would alphabetically come first} diff --git a/man/microorganisms.old.Rd b/man/microorganisms.old.Rd index c15d42f6..17085c87 100644 --- a/man/microorganisms.old.Rd +++ b/man/microorganisms.old.Rd @@ -4,12 +4,13 @@ \name{microorganisms.old} \alias{microorganisms.old} \title{Data set with previously accepted taxonomic names} -\format{A \code{\link{data.frame}} with 24,246 observations and 4 variables: +\format{A \code{\link{data.frame}} with 24,246 observations and 5 variables: \describe{ \item{\code{col_id}}{Catalogue of Life ID that was originally given} \item{\code{col_id_new}}{New Catalogue of Life ID that responds to an entry in the \code{\link{microorganisms}} data set} \item{\code{fullname}}{Old full taxonomic name of the microorganism} \item{\code{ref}}{Author(s) and year of concerning scientific publication} + \item{\code{prevalence}}{Prevalence of the microorganism, see \code{?as.mo}} }} \source{ Catalogue of Life: Annual Checklist (public online taxonomic database), \url{http://www.catalogueoflife.org} (check included annual version with \code{\link{catalogue_of_life_version}()}). diff --git a/vignettes/benchmarks.Rmd b/vignettes/benchmarks.Rmd index fb3d4b1b..9cb03535 100755 --- a/vignettes/benchmarks.Rmd +++ b/vignettes/benchmarks.Rmd @@ -64,10 +64,13 @@ library(microbenchmark) library(AMR) ``` -In the next test, we try to 'coerce' different input values for *Staphylococcus aureus*. The actual result is the same every time: it returns its MO code `B_STPHY_AURS` (*B* stands for *Bacteria*, the taxonomic kingdom). +In the next test, we try to 'coerce' different input values for *Staphylococcus aureus*. The actual result is the same every time: it returns its microorganism code `B_STPHY_AURS` (*B* stands for *Bacteria*, the taxonomic kingdom). But the calculation time differs a lot: +```{r, echo = FALSE} +clear_mo_history() +``` ```{r, warning=FALSE} S.aureus <- microbenchmark( as.mo("sau"), # WHONET code @@ -93,68 +96,61 @@ ggplot.bm(S.aureus) In the table above, all measurements are shown in milliseconds (thousands of seconds). A value of 5 milliseconds means it can determine 200 input values per second. It case of 100 milliseconds, this is only 10 input values per second. The second input is the only one that has to be looked up thoroughly. All the others are known codes (the first one is a WHONET code) or common laboratory codes, or common full organism names like the last one. Full organism names are always preferred. -To achieve this speed, the `as.mo` function also takes into account the prevalence of human pathogenic microorganisms. The downside is of course that less prevalent microorganisms will be determined less fast. See this example for the ID of *Thermus islandicus* (`B_THERMS_ISLN`), a bug probably never found before in humans: - -```{r, warning=FALSE} -T.islandicus <- microbenchmark(as.mo("theisl"), - as.mo("THEISL"), - as.mo("T. islandicus"), - as.mo("T. islandicus"), - as.mo("Thermus islandicus"), - times = 10) -print(T.islandicus, unit = "ms", signif = 2) -``` - -That takes `r round(mean(T.islandicus$time, na.rm = TRUE) / mean(S.aureus$time, na.rm = TRUE), 1)` times as much time on average. A value of 100 milliseconds means it can only determine ~10 different input values per second. We can conclude that looking up arbitrary codes of less prevalent microorganisms is the worst way to go, in terms of calculation performance. Full names (like *Thermus islandicus*) are almost fast - these are the most probable input from most data sets. - -In the figure below, we compare *Escherichia coli* (which is very common) with *Prevotella brevis* (which is moderately common) and with *Thermus islandicus* (which is uncommon): +To achieve this speed, the `as.mo` function also takes into account the prevalence of human pathogenic microorganisms. The downside is of course that less prevalent microorganisms will be determined less fast. See this example for the ID of *Methanosarcina semesiae* (`B_MTHNSR_SEMS`), a bug probably never found before in humans: ```{r, echo = FALSE} -# ggplot.bm( -# microbenchmark(as.mo("Escherichia coli"), -# as.mo("E. coli"), -# times = 10), title = "Very common") -# -# ggplot.bm( -# microbenchmark(as.mo("Prevotella brevis"), -# as.mo("P. brevis"), -# times = 10), title = "Moderately common") -# -# ggplot.bm( -# microbenchmark(as.mo("Thermus islandicus"), -# as.mo("T. islandicus"), -# times = 10), title = "Uncommon") +clear_mo_history() +``` +```{r, warning=FALSE} +M.semesiae <- microbenchmark(as.mo("metsem"), + as.mo("METSEM"), + as.mo("M. semesiae"), + as.mo("M. semesiae"), + as.mo("Methanosarcina semesiae"), + times = 10) +print(M.semesiae, unit = "ms", signif = 4) +``` +That takes `r round(mean(M.semesiae$time, na.rm = TRUE) / mean(S.aureus$time, na.rm = TRUE), 1)` times as much time on average. A value of 100 milliseconds means it can only determine ~10 different input values per second. We can conclude that looking up arbitrary codes of less prevalent microorganisms is the worst way to go, in terms of calculation performance. Full names (like *Methanosarcina semesiae*) are almost fast - these are the most probable input from most data sets. + +In the figure below, we compare *Escherichia coli* (which is very common) with *Prevotella brevis* (which is moderately common) and with *Methanosarcina semesiae* (which is uncommon): + +```{r, echo = FALSE} +clear_mo_history() +``` +```{r, echo = FALSE} par(mar = c(5, 16, 4, 2)) boxplot(microbenchmark( - 'as.mo("Thermus islandicus")' = as.mo("Thermus islandicus"), + 'as.mo("Methanosarcina semesiae")' = as.mo("Methanosarcina semesiae"), 'as.mo("Prevotella brevis")' = as.mo("Prevotella brevis"), 'as.mo("Escherichia coli")' = as.mo("Escherichia coli"), - 'as.mo("T. islandicus")' = as.mo("T. islandicus"), + 'as.mo("M. semesiae")' = as.mo("M. semesiae"), 'as.mo("P. brevis")' = as.mo("P. brevis"), 'as.mo("E. coli")' = as.mo("E. coli"), times = 10), - horizontal = TRUE, las = 1, unit = "s", log = FALSE, - xlab = "", ylab = "Time in seconds", ylim = c(0, 0.5), + horizontal = TRUE, las = 1, unit = "s", log = TRUE, + xlab = "", ylab = "Time in seconds (log)", main = "Benchmarks per prevalence") ``` In reality, the `as.mo()` functions **learns from its own output to speed up determinations for next times**. In above figure, this effect was disabled to show the difference with the boxplot below - when you would use `as.mo()` yourself: ```{r, echo = FALSE} - clear_mo_history() +``` + +```{r, echo = FALSE} par(mar = c(5, 16, 4, 2)) boxplot(microbenchmark( - 'as.mo("Thermus islandicus")' = as.mo("Thermus islandicus", force_mo_history = TRUE), + 'as.mo("Methanosarcina semesiae")' = as.mo("Methanosarcina semesiae", force_mo_history = TRUE), 'as.mo("Prevotella brevis")' = as.mo("Prevotella brevis", force_mo_history = TRUE), 'as.mo("Escherichia coli")' = as.mo("Escherichia coli", force_mo_history = TRUE), - 'as.mo("T. islandicus")' = as.mo("T. islandicus", force_mo_history = TRUE), + 'as.mo("M. semesiae")' = as.mo("M. semesiae", force_mo_history = TRUE), 'as.mo("P. brevis")' = as.mo("P. brevis", force_mo_history = TRUE), 'as.mo("E. coli")' = as.mo("E. coli", force_mo_history = TRUE), times = 10), horizontal = TRUE, las = 1, unit = "s", log = FALSE, - xlab = "", ylab = "Time in seconds", ylim = c(0, 0.5), + xlab = "", ylab = "Time in seconds", main = "Benchmarks per prevalence") ``` @@ -197,6 +193,9 @@ So transforming 500,000 values (!!) of `r n_distinct(x)` unique values only take What about precalculated results? If the input is an already precalculated result of a helper function like `mo_name()`, it almost doesn't take any time at all (see 'C' below): +```{r, echo = FALSE} +clear_mo_history() +``` ```{r} run_it <- microbenchmark(A = mo_name("B_STPHY_AURS"), B = mo_name("S. aureus"), @@ -207,6 +206,9 @@ print(run_it, unit = "ms", signif = 3) So going from `mo_name("Staphylococcus aureus")` to `"Staphylococcus aureus"` takes `r format(round(run_it %>% filter(expr == "C") %>% pull(time) %>% median() / 1e9, 4), scientific = FALSE)` seconds - it doesn't even start calculating *if the result would be the same as the expected resulting value*. That goes for all helper functions: +```{r, echo = FALSE} +clear_mo_history() +``` ```{r} run_it <- microbenchmark(A = mo_species("aureus"), B = mo_genus("Staphylococcus"), @@ -226,6 +228,9 @@ Of course, when running `mo_phylum("Firmicutes")` the function has zero knowledg When the system language is non-English and supported by this `AMR` package, some functions will have a translated result. This almost does't take extra time: +```{r, echo = FALSE} +clear_mo_history() +``` ```{r} mo_name("CoNS", language = "en") # or just mo_name("CoNS") on an English system