tests/testthat/test-bacphene.R

#####Testing functions with dummy data#####

test_that("getMorphologySingle", {

  test_bacteria <- list(Reference = list(list(`@id` = 66716L, `doi/url` = "10.1099/ijsem.0.002999")),
                    Morphology = list(`cell morphology` = list(`@ref` = 66716L, `gram stain` = "negative", `cell length` = "1.7-1.9 µm",`cell width` = "0.4-0.6 µm", `cell shape` = "rod-shaped",motility = "yes", `flagellum arrangement` = "polar")),
                    General = list(`BacDive-ID` = 159837L),
                    `Name and taxonomic classification` = list(species = "Abyssibacter profundi", `type strain` = "yes"))

  result_df <- getMorphologySingle(test_bacteria)

  expect_equal(result_df, structure(list(`@ref` = 66716L, `gram stain` = "negative", `cell length` = "1.7-1.9 µm",
                                         `cell width` = "0.4-0.6 µm", `cell shape` = "rod-shaped",
                                         motility = "yes", `flagellum arrangement` = "polar", ID = 159837L,
                                         taxon = "Abyssibacter profundi", rank = "Species", type_strain = "yes",
                                         `doi/url` = "10.1099/ijsem.0.002999"), row.names = c(NA, -1L), class = c("tbl_df", "tbl", "data.frame")))

})

test_that("getOxygenSingle", {

  test_bacteria <- list(Reference = list(list(`@id` = 8685L, `doi/url` = "https://www.dsmz.de/collection/catalogue/details/culture/DSM-20278")),
                        `Physiology and metabolism` = list(
                          `oxygen tolerance` = list(`@ref` = 8685L, `oxygen tolerance` = "anaerobe")),
                        General = list(`BacDive-ID` = 12649L),
                        `Name and taxonomic classification` = list(species = "Acidipropionibacterium jensenii", `type strain` = "no"))

  result_df <- getOxygenSingle(test_bacteria)

  expect_equal(result_df, structure(list(`@ref` = 8685L, `oxygen tolerance` = "anaerobe", ID = 12649L, taxon = "Acidipropionibacterium jensenii", rank = "Species", type_strain = "no", `doi/url` = "https://www.dsmz.de/collection/catalogue/details/culture/DSM-20278"), row.names = c(NA, -1L), class = c("tbl_df", "tbl", "data.frame")))

})

test_that("getAbxSingle", {

  #entry is truncated from real data
  test_bacteria <- list(Reference = list(list(`@id` = 66598L, `doi/url` = "10.1099/ijsem.0.002846")),
                        `Physiology and metabolism` = list(`antibiotic resistance` = list(`@ref` = 66598L, ChEBI = 2637L, metabolite = "Amikacin",`is antibiotic` = "yes", `is resistant` = "yes", `resistance conc.` = "30 µg (disc)")),
                        General = list(`BacDive-ID` = 159709L),
                        `Name and taxonomic classification` = list(species = "Acidicapsa dinghuensis", `type strain` = "yes"))

  result_df <- getAbxSingle(test_bacteria)

  expect_equal(result_df, structure(list(`@ref` = 66598L, ChEBI = 2637L, metabolite = "Amikacin",`is antibiotic` = "yes", `is resistant` = "yes", `resistance conc.` = "30 µg (disc)",ID = 159709L, taxon = "Acidicapsa dinghuensis", rank = "Species",type_strain = "yes", `doi/url` = "10.1099/ijsem.0.002846"), row.names = c(NA,-1L), class = c("tbl_df", "tbl", "data.frame")))

})

test_that("getAntibiogramSingle", {

  #entry is truncated from real data
  test_bacteria <- list(Reference = list(list(`@id` = 23391L, `doi/url` = "https://www.dsmz.de/collection/catalogue/details/culture/DSM-102930")),
                        `Physiology and metabolism` = list(`antibiogram` = list(`@ref` = 23391L, medium = "Mueller-Hinton Agar", `incubation temperature` = 30L,`oxygen condition` = "aerob", `Penicillin G` = "0")),
                        General = list(`BacDive-ID` = 131595L),
                        `Name and taxonomic classification` = list(species = "Acinetobacter baumannii", `type strain` = "no"))

  result_df <- getAntibiogramSingle(test_bacteria)

  expect_equal(result_df, structure(list(`@ref` = 23391L, medium = "Mueller-Hinton Agar", `incubation temperature` = 30L, `oxygen condition` = "aerob", metabolite = "Penicillin G", diameter = "0", ID = 131595L, taxon = "Acinetobacter baumannii", rank = "Species", type_strain = "no", `doi/url` = "https://www.dsmz.de/collection/catalogue/details/culture/DSM-102930"), row.names = c(NA, -1L), class = c("tbl_df", "tbl", "data.frame")))

})

test_that("getEnzymeSingle", {

  #entry is truncated from real data
  test_bacteria <- list(Reference = list(list(`@id` = 66716L, `doi/url` = "10.1099/ijsem.0.002999")),
                        `Physiology and metabolism` = list(`enzymes` = list(`@ref` = 66716L, value = "acid phosphatase", activity = "-", ec = "3.1.3.2")),
                        General = list(`BacDive-ID` = 159837L),
                        `Name and taxonomic classification` = list(species = "Abyssibacter profundi", `type strain` = "yes"))

  result_df <- getEnzymesSingle(test_bacteria)

  expect_equal(result_df, structure(list(`@ref` = 66716L, value = "acid phosphatase", activity = "-", ec = "3.1.3.2", ID = 159837L, taxon = "Abyssibacter profundi", rank = "Species", `doi/url` = "10.1099/ijsem.0.002999"), row.names = c(NA, -1L), class = c("tbl_df", "tbl", "data.frame")))

})

#####Testing downloaded bacdive data#####

test_that("getMorphology", {

  # structure of list object gotten from bacdive
  # if bacdive changes their structure, it will break the code
  test_list <- readr::read_rds(file = "test_data.rda")

  morphology_df <- getMorphology(list_holder = test_list)

  expect_named(morphology_df, c('@ref', 'cell length', 'cell shape', 'cell width', 'doi/url', 'flagellum arrangement', 'gram stain', 'ID', 'motility', 'rank', 'taxon', 'type_strain'), ignore.order = T)

})

test_that("getOxygen", {

  test_list <- readr::read_rds(file = "test_data.rda")

  oxygen_df <- getOxygen(list_holder = test_list)

  expect_named(oxygen_df, c("@ref", "oxygen tolerance", "ID", "taxon", "rank", "doi/url", "type_strain"), ignore.order = T)

})

test_that("getPhenotypes", {

  test_list <- readr::read_rds(file = "test_data.rda")
  morphology_df <- getMorphology(list_holder = test_list)
  oxygen_df <- getOxygen(list_holder = test_list)

  phenotypes_df <- getPhenotypes(morphology_df = morphology_df, oxygen_df = oxygen_df)

  expect_named(phenotypes_df, c("taxon", "rank", "gram_stain", "aerobic_status"), ignore.order = T)

})

test_that("get multiple strains", {

  test_list <- readr::read_rds(file = "test_data.rda")
  bac_frag <- getStrainLocal(test_list, query = "Bacteroides fragilis", typestrain_only = F)

  expect_equal(bac_frag[[1]]$`Name and taxonomic classification`$species, "Bacteroides fragilis")
  expect_length(bac_frag, 2)

})

test_that("only get type strain", {

  test_list <- readr::read_rds(file = "test_data.rda")
  ent_fae <- getStrainLocal(test_list, query = "Bacteroides fragilis", typestrain_only = T)

  expect_equal(ent_fae[[1]]$`Name and taxonomic classification`$species, "Bacteroides fragilis")
  expect_equal(ent_fae[[1]]$`Name and taxonomic classification`$`type strain`, "yes")

})

test_that("getAbx", {

  test_list <- readr::read_rds(file = "test_data.rda")

  abx_df <- getAbx(test_list)

  expect_named(abx_df, c("@ref", "ChEBI", "metabolite", "is antibiotic", "is resistant",
                         "resistance conc.", "is sensitive", "sensitivity conc.", "ID",
                         "taxon", "rank", "type_strain", "doi/url", "group ID", "is intermediate",
                         "intermediate conc."), ignore.order = T)

})

test_that("getAntibiogram", {

  test_list <- readr::read_rds(file = "test_data.rda")

  antibiogram_df <- getAntibiogram(test_list)

  expect_named(antibiogram_df, c("@ref", "medium", "incubation temperature", "oxygen condition",
                         "incubation time", "metabolite", "diameter", "ID", "taxon", "rank",
                         "type_strain", "doi/url"), ignore.order = T)

})

test_that("consistent bacdive susceptibility", {
  # this tests that there aren't any conflicts
  # between the regular abx info and the antibiogram
  # note that if the data changes at bacdive
  # this might also change
  test_list <- readr::read_rds(file = "test_data.rda")

  abx_df <- getAbx(test_list)
  antibiogram_df <- getAntibiogram(test_list)

  suscept_df <-getSimplifiedAbx(data = bind_rows(abx_df, antibiogram_df), extra_info = T, most_common = F, remove_unknown = F)

  expect_equal(unique(suscept_df[suscept_df$`is sensitive` %in% "yes",]$value), c("sensitive"))
  expect_equal(unique(suscept_df[suscept_df$`is resistant` %in% "yes",]$value), c("resistant"))
  expect_equal(unique(suscept_df[suscept_df$diameter %in% "0",]$value), c("resistant"))
  expect_equal(unique(suscept_df[suscept_df$diameter %in% "n.d.",]$value), c("unknown"))
  expect_equal(sort(unique(suscept_df[is.na(suscept_df$diameter),]$value)), c("resistant", "sensitive", "unknown"))

})



test_that("getEnzymes", {

  test_list <- readr::read_rds(file = "test_data.rda")

  enzymes_df <- getEnzymes(list_holder = test_list, most_common = T, remove_unknown = T)

  expect_named(enzymes_df, c("taxon", "rank", "value", "ec", "activity"), ignore.order = T)

})

######notes for future tests#####

test_that("annotate microbiome data", {

  my_df <- data.frame(Taxa = c('E. coli', 'B. fragilis'),
                      Counts = c(100,200))

  # ... magic stuff ...

  my_vec <- c(Anaerobes = 0.5, Aerobes = 0.5)

})

test_that("get aerobic status 1", {

  my_input <- "Escherichia coli"

  my_answer <- data.frame(property = "aerobic_status", value = "facultative anaerobe", probability = 1.0)

})

test_that("get aerobic status 2", {

  my_input <- "Corynebacterium"

  my_answer <- data.frame(property = c("aerobic_status", "aerobic_status"), value = c("facultative anaerobe", "obligate aerobe"), probability = c(0.5, 0.5))

})

test_that("get aerobic status 3", {

  my_input <- "Bacteroidetes"

  my_answer <- data.frame(property = c("aerobic_status", "aerobic_status"), value = c("facultative anaerobe", "obligate anaerobe"), probability = c(0.2, 0.8))

  my_answer <- data.frame(property = c("aerobic_status", "aerobic_status", "gram_stain"), value = c("facultative anaerobe", "obligate anaerobe", "Gram negative"), probability = c(0.2, 0.8, 1.0))

})
scottdaniel/bacphene documentation built on March 25, 2023, 12:51 p.m.