Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: bugphyzz
Title: A harmonized data resource and software for enrichment analysis of microbial physiologies
Version: 1.7.11
Version: 1.7.12
Authors@R: c(
person("Samuel", "Gamboa", , "Samuel.Gamboa.Tuz@gmail.com", role = "aut",
comment = c(ORCID = "0000-0002-6863-7943")),
Expand Down
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# bugphyzz 1.7.12
+ `importBugphyzz()` gains `includeDroppedAsr`: with a `devel` or commit
version it adds back the imputed annotations the export removed at the
default thresholds, so `mccThreshold` and `r2Threshold` can be set lower
to evaluate a threshold against known ground truth.

# bugphyzz 1.7.11
+ The Microbe Directory now comes from bugphyzzExports `sources/`
(waldronlab/bugphyzzExports#87), imported from its published table with
Expand Down
55 changes: 54 additions & 1 deletion R/bugphyzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ utils::globalVariables(c(
#' will always return a shortened version of the source. Please use
#' vigette("sources", "bugphyz") to see the full sources.
#'
#' @param includeDroppedAsr Logical. If TRUE, add back the imputed (ASR)
#' annotations the export removed for failing the default thresholds (or for
#' frequency "rarely"/"never"), so that `mccThreshold` and `r2Threshold` can
#' be set below the export's 0.7 and 0.5. Only for `version = "devel"` or a
#' commit hash; Zenodo releases carry only the filtered data. Default FALSE.
#'
#' @return A list of tidy data frames.
#' @export
#'
Expand All @@ -72,7 +78,8 @@ utils::globalVariables(c(
#'
importBugphyzz <- function(
version = "10.5281/zenodo.12574596", forceDownload = FALSE,
mccThreshold = 0.7, r2Threshold = 0.5, excludeRarely = TRUE, v = NULL
mccThreshold = 0.7, r2Threshold = 0.5, excludeRarely = TRUE,
includeDroppedAsr = FALSE, v = NULL
) {
if (!is.null(v)) {
warning(
Expand All @@ -89,6 +96,9 @@ importBugphyzz <- function(
## output is a list of three data.frames
## one of each: binary, multistate, numeric
output <- .downloadResource(version, forceDownload)
if (includeDroppedAsr) {
output <- .addDroppedAsr(output, version, forceDownload)
}

output <- lapply(output, function(x) split(x, x$Attribute))
output <- purrr::list_flatten(output)
Expand All @@ -113,6 +123,49 @@ importBugphyzz <- function(
return(output)
}

## The export applies the default thresholds and writes the ASR rows it
## removed to bugphyzz_asr_dropped.csv.gz (devel and commit versions only).
## Add them back so the thresholds below can be set lower than the export's.
.addDroppedAsr <- function(output, version, forceDownload) {
if (stringr::str_detect(version, "^10.5281/zenodo.[0-9]+$")) {
stop(
"`includeDroppedAsr = TRUE` needs a GitHub version ('devel' or a",
" commit hash); releases on Zenodo carry only the filtered data.",
call. = FALSE
)
}
url <- paste0(
"https://github.com/waldronlab/bugphyzzExports/raw/", version,
"/bugphyzz_asr_dropped.csv.gz"
)
message("Importing dropped ASR annotations...")
rpath <- .getResource(
rname = paste0("bugphyzz_asr_dropped_", version, ".csv.gz"),
url = url, verbose = TRUE, force = forceDownload
)
dropped <- utils::read.csv(
gzfile(rpath), header = TRUE, skip = 1, colClasses = "character"
) |>
dplyr::mutate(Attribute = tolower(Attribute)) |>
dplyr::select(-dplyr::any_of("Validation"))
types <- c(binary = "binary", multistate = "multistate", numeric = "numeric")
lapply(seq_along(output), function(i) {
type <- types[[i]]
part <- if (type == "multistate") {
dplyr::filter(dropped, grepl("^multistate", Attribute_type))
} else {
dplyr::filter(dropped, Attribute_type == type)
}
## the side file is read as character; give each column the type it
## has in the main table so bind_rows() can't fail on a type clash
part <- part[intersect(names(output[[i]]), names(part))]
for (col in names(part)) {
part[[col]] <- methods::as(part[[col]], class(output[[i]][[col]])[1])
}
dplyr::bind_rows(output[[i]], part)
})
}

.checkThreshold <- function(x, name) {
if (!is.numeric(x) || length(x) != 1 || is.na(x) || x < 0 || x > 1) {
stop(
Expand Down
7 changes: 7 additions & 0 deletions man/importBugphyzz.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 52 additions & 0 deletions tests/testthat/test-importBugphyzz-filters.R
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,55 @@ test_that("shipped validation table is v1.0.5 and has one row per key", {
expect_equal(nrow(aer), 1L)
expect_false("tail" %in% all_rank$attribute[all_rank$physiology == "shape"])
})

test_that("includeDroppedAsr refuses a Zenodo version", {
expect_error(
.addDroppedAsr(list(), "10.5281/zenodo.12574596", FALSE),
"GitHub version"
)
})

test_that("includeDroppedAsr brings dropped ASR rows under a lower threshold", {
cols <- function(...) data.frame(..., stringsAsFactors = FALSE)
main <- list(
binary = cols(NCBI_ID = "562", Taxon_name = "Escherichia coli", Rank = "species",
Attribute = "motility", Attribute_value = "TRUE", Evidence = "exp",
Frequency = "always", Score = 1, Attribute_source = "BacDive",
Confidence_in_curation = "high", Attribute_type = "binary", NSTI = NA_real_),
multistate = cols(NCBI_ID = "1279", Taxon_name = "Staphylococcus", Rank = "genus",
Attribute = "aerophilicity", Attribute_value = "anaerobic", Evidence = "asr",
Frequency = "usually", Score = 0.8, Attribute_source = NA_character_,
Confidence_in_curation = NA_character_, Attribute_type = "multistate-intersection", NSTI = 0.1),
numeric = cols(NCBI_ID = "562", Taxon_name = "Escherichia coli", Rank = "species",
Attribute = "length", Attribute_value = "2", Evidence = "exp",
Frequency = "always", Score = 1, Attribute_source = "BacDive",
Confidence_in_curation = "high", Attribute_type = "numeric", nsti = NA_real_)
)
dropped <- cols(NCBI_ID = c("1279", "1279"), Taxon_name = "Staphylococcus", Rank = "genus",
Attribute = "aerophilicity", Attribute_value = c("aerobic", "aerobic"),
Evidence = "asr", Frequency = c("usually", "never"), Score = c(0.7, 0.1),
Attribute_source = NA, Confidence_in_curation = NA,
Attribute_type = "multistate-intersection", NSTI = 0.1, Validation = 0.66)
tmp <- tempfile(fileext = ".csv.gz")
con <- gzfile(tmp, "w"); cat("# header\n", file = con)
suppressWarnings(write.table(dropped, con, sep = ",", row.names = FALSE, append = TRUE, col.names = TRUE))
close(con)
val <- data.frame(rank = "all", physiology = "aerophilicity",
attribute = c("aerobic", "anaerobic"), mcc_mean = c(0.66, 0.85), r2_mean = NA_real_,
value = c(0.66, 0.85))
local_mocked_bindings(
.downloadResource = function(version, forceDownload) main,
.getResource = function(...) tmp,
.validationData = function() val
)
bp <- importBugphyzz(version = "devel", includeDroppedAsr = TRUE)
expect_equal(sort(unique(bp$aerophilicity$Attribute_value)), "anaerobic")
bp <- importBugphyzz(version = "devel", includeDroppedAsr = TRUE, mccThreshold = 0.6)
expect_equal(sum(bp$aerophilicity$Attribute_value == "aerobic"), 1L) # "never" row still excluded
expect_false("Validation" %in% names(bp$aerophilicity) && all(is.na(bp$aerophilicity$Validation)))
bp <- importBugphyzz(version = "devel", includeDroppedAsr = TRUE, mccThreshold = 0.6, excludeRarely = FALSE)
expect_equal(sum(bp$aerophilicity$Attribute_value == "aerobic"), 2L)
expect_type(bp$aerophilicity$Score, "double")
bp <- importBugphyzz(version = "devel", includeDroppedAsr = FALSE)
expect_false("aerobic" %in% bp$aerophilicity$Attribute_value)
})
Loading