Skip to content

Commit

Permalink
Append and created a testthat unit test of the fix (#34)
Browse files Browse the repository at this point in the history
* append and created a testthat unit test of the fix

* Append and created a testthat unit test of the fix

* made all the changes requested in the previous pull request

* fixed the documentation and deleted the r.data and r.history files

* updated the documentation and deleted rdata

* fixed the comments on documentation

* Remove documentation of assay as it is inherited by @inheritParam

* Fix unit tests that only occur on en_US.UTF-8

---------

Co-authored-by: Levi Waldron <[email protected]>
  • Loading branch information
2 people authored and LiNk-NY committed Mar 6, 2024
1 parent 778f46a commit 283b3ae
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 24 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
lefser.Rproj
docs
vignettes/lefser_cache/
.Rdata
.Rhistory
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@ URL: https://github.com/waldronlab/lefser
VignetteBuilder: knitr
biocViews: Software, Sequencing, DifferentialExpression, Microbiome,
StatisticalMethod, Classification
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
Roxygen: list(markdown = TRUE)
2 changes: 1 addition & 1 deletion R/lefser.R
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ lefser <-
} else {
relab_data <- assay(relab, i = assay)
}
if (checkAbundances && !identical(length(unique(colSums(relab_data))), 1L))
if (checkAbundances && !identical(all.equal(colSums(relab_data), rep(1e6, ncol(relab_data)), check.attributes = FALSE), TRUE))
warning(
"Convert counts to relative abundances with 'relativeAb()'"
)
Expand Down
27 changes: 17 additions & 10 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,23 @@
#' assay(se)
#' assay(relativeAb(se))
#'
#' @description
#' This function calculates the relative abundance of each feature in the SummarizedExperiment
#' object containing count data, expressed as counts per million (CPM)
#'
#' @returns returns a new SummarizedExperiment object with counts per million
#' calculated and added as a new assay named rel_abs.
#'
#' @export
relativeAb <- function(se, assay = 1L) {
assay_data <- assay(se, i = assay)
csums <- colSums(assay_data)
div <- matrix(rep(csums, each = nrow(assay_data)), ncol = ncol(assay_data))
res <- assay_data / div
assaylist <- assays(se)
newalist <- append(
assaylist, values = S4Vectors::SimpleList(rel_abs = res), after = 0L
)
assays(se) <- newalist
se
assay_data <- assay(se, i = assay)
csums <- colSums(assay_data)
div <- matrix(rep(csums, each = nrow(assay_data)), ncol = ncol(assay_data))
res <- assay_data / div * 1e6
assaylist <- assays(se)
newalist <- append(
assaylist, values = S4Vectors::SimpleList(rel_abs = res), after = 0L
)
assays(se) <- newalist
se
}
7 changes: 7 additions & 0 deletions man/relativeAb.Rd

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

15 changes: 15 additions & 0 deletions tests/testthat.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
library(testthat)
library(lefser)



test_check("lefser")

data(zeller14)

zeller14sub <- zeller14[, zeller14$study_condition != "adenoma"]

zeller14ra <- lefser::relativeAb(zeller14sub)


expect_warning(lefser(zeller14sub, groupCol = "study_condition"), regexp = "^Convert counts.+")
expect_equal(assayNames(zeller14ra), c("rel_abs", "exprs"))
expect_equal(colSums(assay(zeller14ra, "rel_abs")), rep(1e6, ncol(zeller14ra)), check.attributes = FALSE)
set.seed(1)
expect_no_warning(x <- lefser(zeller14ra, groupCol = "study_condition"))
28 changes: 16 additions & 12 deletions tests/testthat/test-lefser.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ test_that("lefser and lefserPlot work", {
tol <- 0.2
zellersub <- zeller14[1:150, zeller14$study_condition != "adenoma"]
## subsetting a DataFrame with NULL
expect_error(lefser(zellersub, groupCol = NULL, blockCol = NULL))
expect_warning(expect_error(lefser(zellersub, groupCol = NULL, blockCol = NULL)))
zellersub <- relativeAb(zellersub)
results <- withr::with_seed(1,
lefser(zellersub, groupCol = "study_condition", blockCol = NULL)
)
Expand All @@ -21,8 +22,8 @@ test_that("lefser and lefserPlot work", {
checkEnding(results, nrow(results), "Names", "o__Bacteroidales")
)
# TODO: compare results between LEfSe and lefser
expect_equal(results[1, "scores"], -6.431365, tolerance = tol)
expect_equal(results[nrow(results), "scores"], 6.402655, tolerance = tol)
expect_equal(results[1, "scores"], -3.688676, tolerance = tol)
expect_equal(results[nrow(results), "scores"], 3.815295, tolerance = tol)
results2 <- withr::with_seed(1,
lefser(
zellersub, groupCol = "study_condition", blockCol = "age_category"
Expand All @@ -31,26 +32,29 @@ test_that("lefser and lefserPlot work", {
expect_equal(colnames(results2), c("Names", "scores"))
expect_true(checkEnding(results2, 1, "Names", "g__Ruminococcus"))
expect_true(checkEnding(
results2, nrow(results2), "Names", "f__Clostridiales_noname"
results2, nrow(results2), "Names", "o__Bacteroidales"
))
expect_equal(results2[1, "scores"], -5.792313, tolerance = tol)
expect_equal(results2[nrow(results2), "scores"], 5.20564, tolerance = tol)
expect_equal(results2[1, "scores"], -3.58, tolerance = tol)
expect_equal(results2[nrow(results2), "scores"], 3.83, tolerance = tol)

expect_equal(nrow(results2), 6)
expect_equal(nrow(results2), 14)
expect_true(all(
mapply(endsWith, results2$Names, c(
mapply(endsWith, results2$Names[1:7], c(
"g__Ruminococcus",
"o__Lactobacillales",
"c__Bacilli",
"f__Streptococcaceae",
"s__Ruminococcus_sp_5_1_39BFAA",
"s__Eubacterium_hallii",
"f__Clostridiales_noname"
))
"c__Deltaproteobacteria"
))
))

expect_equal(results2$scores,
c(-6.1384025872238, -5.90688120617861, -5.89755502841847,
-5.57735644006024, -5.42866570267924, 4.64159299932332),
c(-3.58274695616907, -3.32856514474989, -3.31328636163603, -3.28470366735756,
-2.99971715869742, -2.82238742034892, 2.36885229314682, 2.36885229314682,
2.36885229314682, 2.53194431507908, 2.53194431507908, 3.83089005744698,
3.83093344936543, 3.83093344936543),
tolerance = tol)
plt <- lefserPlot(results2)
expect_s3_class(plt, "ggplot")
Expand Down

0 comments on commit 283b3ae

Please sign in to comment.