One of the most important features of this package is the complete microbial taxonomic database, supplied by the Catalogue of Life (CoL) and the List of Prokaryotic names with Standing in Nomenclature (LPSN). We created a function as.mo()
that transforms any user input value to a valid microbial ID by using intelligent rules combined with the microbial taxonomy.
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.
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, its 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
times = 25)
print(S.aureus, unit = "ms", signif = 2)
# Unit: milliseconds
# expr min lq mean median uq max neval
# as.mo("sau") 12.0 12.0 17.0 14.0 15.0 56 25
# as.mo("stau") 51.0 60.0 81.0 87.0 98.0 120 25
# as.mo("STAU") 51.0 64.0 83.0 90.0 98.0 130 25
# as.mo("staaur") 9.7 12.0 15.0 15.0 15.0 46 25
# as.mo("STAAUR") 11.0 14.0 17.0 15.0 16.0 45 25
# as.mo("S. aureus") 24.0 31.0 43.0 33.0 63.0 77 25
# as.mo("S aureus") 26.0 28.0 45.0 34.0 62.0 76 25
# as.mo("Staphylococcus aureus") 3.0 3.7 5.4 3.8 4.6 37 25
# as.mo("Staphylococcus aureus (MRSA)") 240.0 250.0 270.0 260.0 270.0 310 25
# as.mo("Sthafilokkockus aaureuz") 180.0 190.0 210.0 200.0 220.0 260 25
# as.mo("MRSA") 10.0 13.0 20.0 15.0 17.0 73 25
# as.mo("VISA") 20.0 23.0 38.0 26.0 49.0 150 25
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 200 milliseconds, this is only 5 input values per second. It is clear that accepted taxonomic names are extremely fast, but some variations are up to 69 times slower to determine.
To improve performance, we implemented two important algorithms to save unnecessary calculations: repetitive results and already precalculated results.
Repetitive results are values that are present more than once in a vector. Unique values will only be calculated once by as.mo()
. So running as.mo(c("E. coli", "E. coli"))
will check the value "E. coli"
only once.
To prove this, we will use mo_name()
for testing - a helper function that returns the full microbial name (genus, species and possibly subspecies) which uses as.mo()
internally.
# start with the example_isolates data set
x <- example_isolates %>%
# take all MO codes from the 'mo' column
pull(mo) %>%
# and copy them a thousand times
rep(1000) %>%
# then scramble them
sample()
# what do these values look like? They are of class <mo>:
head(x)
# Class <mo>
# [1] B_STRPT_PNMN B_ESCHR_COLI B_STPHY_AURS B_STPHY_CONS B_ENTRC
# [6] B_STPHY_HMNS
# as the example_isolates data set 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) 205 234 315 310 393 447 10
So getting official taxonomic names of 2,000,000 (!!) items consisting of 90 unique values only takes 0.31 seconds. That is 155 nanoseconds on average. 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 such as mo_name()
, it almost doesn’t take any time at all. In other words, if you run mo_name()
on a valid taxonomic name, it will return the results immediately (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 7.89 8.95 14.80 9.21 10.50 64.60 10
# B 22.60 26.10 33.80 26.90 29.80 89.20 10
# C 1.76 2.04 2.44 2.49 2.73 3.37 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.62 1.67 1.81 1.71 1.75 2.45 10
# B 1.59 1.62 1.94 1.75 2.15 3.08 10
# C 1.61 1.64 1.74 1.70 1.73 2.09 10
# D 1.58 1.66 1.87 1.69 2.07 2.63 10
# E 1.59 1.61 1.70 1.64 1.72 2.15 10
# F 1.57 1.60 1.69 1.66 1.71 2.07 10
# G 1.56 1.60 1.75 1.67 1.73 2.21 10
# H 1.60 1.62 1.67 1.64 1.70 1.81 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 contains all phyla of all known bacteria, 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 (compare “en” from the table below with the other languages):
CoNS <- as.mo("CoNS")
CoNS
# Class <mo>
# [1] B_STPHY_CONS
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(da = mo_name(CoNS, language = "da"),
de = mo_name(CoNS, language = "de"),
en = mo_name(CoNS, language = "en"),
es = mo_name(CoNS, language = "es"),
fr = mo_name(CoNS, language = "fr"),
it = mo_name(CoNS, language = "it"),
nl = mo_name(CoNS, language = "nl"),
pt = mo_name(CoNS, language = "pt"),
ru = mo_name(CoNS, language = "ru"),
sv = mo_name(CoNS, language = "sv"),
times = 100)
print(run_it, unit = "ms", signif = 4)
# Unit: milliseconds
# expr min lq mean median uq max neval
# da 1.9160 2.012 2.308 2.213 2.445 4.040 100
# de 1.9130 2.057 3.165 2.358 2.495 44.140 100
# en 0.8684 0.910 1.076 1.004 1.137 2.173 100
# es 1.9060 2.066 3.304 2.335 2.595 60.500 100
# fr 1.7950 1.888 2.169 2.104 2.338 3.366 100
# it 1.8830 2.025 2.809 2.342 2.536 45.730 100
# nl 1.9370 2.034 3.482 2.316 2.626 60.780 100
# pt 1.8450 1.983 3.125 2.293 2.447 43.830 100
# ru 1.8260 1.932 3.454 2.018 2.413 87.210 100
# sv 1.8260 1.930 3.428 2.207 2.441 72.080 100
Currently supported languages are Danish, Dutch, English, French, German, Italian, Portuguese, Russian, Spanish and Swedish.