One of the most important features of this package is the complete microbial taxonomic database, supplied by the Catalogue of Life. We created a function as.mo()
that transforms any user input value to a valid microbial ID by using intelligent rules combined with the taxonomic tree of Catalogue of Life.
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.
microbenchmark <- microbenchmark::microbenchmark
library(AMR)
library(dplyr)
In the next test, we try to ‘coerce’ different input values into the microbial code of Staphylococcus aureus. Coercion is a computational process of forcing output based on an input. For microorganism names, coercing user input to taxonomically valid microorganism names is crucial to ensure correct interpretation and to enable grouping based on taxonomic properties.
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
as.mo("stau"),
as.mo("STAU"),
as.mo("staaur"),
as.mo("STAAUR"),
as.mo("S. aureus"),
as.mo("S aureus"),
as.mo("Staphylococcus aureus"), # official taxonomic name
as.mo("Staphylococcus aureus (MRSA)"), # additional text
as.mo("Sthafilokkockus aaureuz"), # incorrect spelling
as.mo("MRSA"), # Methicillin Resistant S. aureus
as.mo("VISA"), # Vancomycin Intermediate S. aureus
as.mo("VRSA"), # Vancomycin Resistant S. aureus
times = 10)
print(S.aureus, unit = "ms", signif = 2)
# Unit: milliseconds
# expr min lq mean median uq max
# as.mo("sau") 13.0 14.0 25.0 15.0 44.0 65
# as.mo("stau") 120.0 130.0 140.0 140.0 160.0 160
# as.mo("STAU") 120.0 130.0 150.0 160.0 160.0 180
# as.mo("staaur") 13.0 13.0 14.0 14.0 14.0 15
# as.mo("STAAUR") 13.0 14.0 17.0 15.0 15.0 43
# as.mo("S. aureus") 30.0 32.0 45.0 34.0 62.0 68
# as.mo("S aureus") 31.0 34.0 42.0 35.0 61.0 63
# as.mo("Staphylococcus aureus") 2.5 2.6 2.8 2.8 2.9 3
# as.mo("Staphylococcus aureus (MRSA)") 1200.0 1200.0 1200.0 1200.0 1200.0 1200
# as.mo("Sthafilokkockus aaureuz") 550.0 560.0 570.0 560.0 570.0 610
# as.mo("MRSA") 13.0 13.0 17.0 15.0 15.0 42
# as.mo("VISA") 21.0 22.0 26.0 23.0 23.0 52
# as.mo("VRSA") 21.0 23.0 35.0 24.0 52.0 54
# neval
# 10
# 10
# 10
# 10
# 10
# 10
# 10
# 10
# 10
# 10
# 10
# 10
# 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. It is clear that accepted taxonomic names are extremely fast, but some variations can take up to 500-1000 times as much time.
To improve performance, two important calculations take almost no time at all: repetitive results and already precalculated 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.
# take all MO codes from the example_isolates data set
x <- example_isolates$mo %>%
# and copy them a thousand times
rep(1000) %>%
# then scramble them
sample()
# as the example_isolates has 2,000 rows, we should have 2 million items
length(x)
# [1] 2000000
# and how many unique values do we have?
n_distinct(x)
# [1] 90
# 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) 141 180 218 207 245 312 10
So getting official taxonomic names of 2,000,000 (!!) items consisting of 90 unique values only takes 0.207 seconds. You only lose time on your unique input values.
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("STAAUR"),
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 8.76 8.96 9.39 9.31 9.86 10.10 10
# B 27.60 28.20 33.00 28.90 29.40 71.20 10
# C 2.28 2.32 2.47 2.45 2.48 2.85 10
So going from mo_name("Staphylococcus aureus")
to "Staphylococcus aureus"
takes 0.0025 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 1.91 1.95 2.06 1.99 2.09 2.62 10
# B 1.83 1.91 2.09 2.04 2.20 2.45 10
# C 1.79 1.90 2.03 1.99 2.22 2.30 10
# D 1.90 2.01 2.18 2.12 2.25 2.71 10
# E 1.91 2.02 2.14 2.08 2.15 2.81 10
# F 1.86 1.92 2.00 2.01 2.06 2.16 10
# G 1.81 1.96 2.09 2.08 2.22 2.41 10
# H 1.90 1.93 2.05 2.00 2.22 2.29 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"
anyway, 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.
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 = 100)
print(run_it, unit = "ms", signif = 4)
# Unit: milliseconds
# expr min lq mean median uq max neval
# en 17.45 18.01 19.69 18.53 19.14 55.30 100
# de 20.58 21.54 26.69 22.08 23.96 67.16 100
# nl 33.79 34.67 39.13 35.39 36.72 74.60 100
# es 20.71 21.42 24.36 21.88 22.65 58.57 100
# it 20.65 21.18 26.50 21.53 22.68 61.96 100
# fr 20.68 21.27 25.05 21.64 22.37 58.82 100
# pt 20.69 21.44 24.36 21.94 22.99 59.66 100
Currently supported are German, Dutch, Spanish, Italian, French and Portuguese.