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) # Results of three values were guessed with uncertainty. Use mo_uncertainties() to review them. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Results of three values were guessed with uncertainty. Use mo_uncertainties() to review them. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Results of three values were guessed with uncertainty. Use mo_uncertainties() to review them. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Results of three values were guessed with uncertainty. Use mo_uncertainties() to review them. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Results of three values were guessed with uncertainty. Use mo_uncertainties() to review them. # Results of three values were guessed with uncertainty. Use mo_uncertainties() to review them. # Results of three values were guessed with uncertainty. Use mo_uncertainties() to review them. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Results of three values were guessed with uncertainty. Use mo_uncertainties() to review them. # Results of three values were guessed with uncertainty. Use mo_uncertainties() to review them. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Results of three values were guessed with uncertainty. Use mo_uncertainties() to review them. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Results of three values were guessed with uncertainty. Use mo_uncertainties() to review them. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Results of three values were guessed with uncertainty. Use mo_uncertainties() to review them. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Results of three values were guessed with uncertainty. Use mo_uncertainties() to review them. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Results of three values were guessed with uncertainty. Use mo_uncertainties() to review them. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Results of three values were guessed with uncertainty. Use mo_uncertainties() to review them. # Results of three values were guessed with uncertainty. Use mo_uncertainties() to review them. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Results of three values were guessed with uncertainty. Use mo_uncertainties() to review them. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Results of three values were guessed with uncertainty. Use mo_uncertainties() to review them. # Results of three values were guessed with uncertainty. Use mo_uncertainties() to review them. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Results of three values were guessed with uncertainty. Use mo_uncertainties() to review them. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. print(S.aureus, unit = "ms", signif = 2) # Unit: milliseconds # expr min lq mean median uq max # as.mo("sau") 9.9 13.0 24.0 17.0 39.0 45 # as.mo("stau") 200.0 210.0 240.0 240.0 260.0 290 # as.mo("STAU") 190.0 220.0 230.0 220.0 260.0 270 # as.mo("staaur") 9.4 13.0 26.0 15.0 44.0 47 # as.mo("STAAUR") 9.3 11.0 18.0 14.0 15.0 45 # as.mo("S. aureus") 21.0 25.0 30.0 26.0 26.0 50 # as.mo("S aureus") 25.0 47.0 48.0 51.0 56.0 64 # as.mo("Staphylococcus aureus") 1.5 1.9 2.3 2.4 2.5 3 # as.mo("Staphylococcus aureus (MRSA)") 860.0 900.0 930.0 920.0 950.0 1100 # as.mo("Sthafilokkockus aaureuz") 410.0 420.0 430.0 430.0 450.0 460 # as.mo("MRSA") 12.0 13.0 16.0 14.0 15.0 41 # as.mo("VISA") 15.0 21.0 38.0 22.0 47.0 130 # as.mo("VRSA") 18.0 20.0 25.0 22.0 22.0 47 # 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) 96.1 123 140 133 144 251 10
So getting official taxonomic names of 2,000,000 (!!) items consisting of 90 unique values only takes 0.133 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) # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. print(run_it, unit = "ms", signif = 3) # Unit: milliseconds # expr min lq mean median uq max neval # A 7.83 7.96 8.19 8.22 8.33 8.84 10 # B 18.10 19.50 27.80 20.20 20.70 65.90 10 # C 1.77 2.11 2.34 2.27 2.33 3.22 10
So going from mo_name("Staphylococcus aureus")
to "Staphylococcus aureus"
takes 0.0023 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.56 1.62 5.61 1.93 2.26 38.90 10 # B 1.50 1.72 1.88 1.90 2.01 2.34 10 # C 1.52 1.76 1.88 1.89 1.96 2.27 10 # D 1.47 1.62 1.85 1.86 1.89 2.80 10 # E 1.51 1.84 1.98 1.88 2.07 2.56 10 # F 1.44 1.50 1.68 1.57 1.89 2.19 10 # G 1.47 1.48 1.65 1.59 1.84 2.00 10 # H 1.55 1.60 1.75 1.69 1.81 2.34 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 13.84 14.04 20.10 14.54 16.47 59.20 100 # de 14.79 15.10 20.00 15.76 17.64 63.37 100 # nl 18.52 19.35 24.11 21.44 22.93 62.12 100 # es 14.72 15.02 20.10 16.06 17.90 60.60 100 # it 14.61 14.93 18.06 15.45 17.33 52.47 100 # fr 14.73 15.02 21.06 15.62 18.09 69.54 100 # pt 14.74 14.99 21.19 16.17 17.88 64.71 100
Currently supported are German, Dutch, Spanish, Italian, French and Portuguese.