Skip to content

Commit 28b0b19

Browse files
authored
Merge pull request #32 from annemarie-sharp/update-tests
Initial commit with updated testthat files
2 parents 028352f + a533475 commit 28b0b19

14 files changed

Lines changed: 28 additions & 136 deletions

DESCRIPTION

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,12 @@ Imports:
2929
ggplot2,
3030
kableExtra
3131
Suggests:
32-
BiocStyle,
32+
BiocStyle,
3333
knitr,
3434
rmarkdown,
3535
broom,
36-
jtools
36+
jtools,
37+
testthat (>= 3.0.0)
3738
Remotes:
3839
waldronlab/BugSigDBStats,
3940
waldronlab/bugsigdbr
@@ -43,3 +44,4 @@ VignetteBuilder: knitr
4344
RoxygenNote: 7.3.2
4445
Encoding: UTF-8
4546
URL: https://waldronlab.io/bugSigSimple
47+
Config/testthat/edition: 3

tests/testthat.R

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# This file is part of the standard setup for testthat.
2+
# It is recommended that you do not modify it.
3+
#
4+
# Where should you do additional test configuration?
5+
# Learn more about the roles of various files in:
6+
# * https://r-pkgs.org/testing-design.html#sec-tests-files-overview
7+
# * https://testthat.r-lib.org/articles/special-files.html
8+
9+
library(testthat)
10+
library(bugSigSimple)
11+
12+
test_check("bugSigSimple")

tests/testthat/test-MPA.TAX.LEVELS.R

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
#Function file: simple.R
22

3-
4-
# Unit test: MPA.TAX.LEVELS should return the correct taxonomic level abbreviation
5-
6-
library(testthat)
7-
83
test_that("MPA.TAX.LEVELS contains correct taxonomic level abbreviations", {
94
expected <- c("k", "p", "c", "o", "f", "g", "s", "t")
105
names(expected) <- c("kingdom", "phylum", "class", "order",

tests/testthat/test-countBug.R

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
#Function file: montecarlo.R
22

3-
4-
library(testthat)
5-
63
test_that(".countBug returns max frequency of most common taxon", {
74
relevant.sigs <- list(
85
sig1 = c("Bacteroides", "Escherichia"),
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#Function file: describe_curation.R
2+
3+
bsdb <- bugsigdbr::importBugSigDB()
4+
test_that("createStudyTable creates a table of all studies currently in data.frame", {
5+
expect_s3_class(createStudyTable(bsdb[1:10,]), "data.frame")
6+
})
Lines changed: 4 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,6 @@
1-
# ===== Load dependencies =====
2-
library(dplyr)
3-
library(tidyr)
4-
library(stringr)
5-
library(testthat)
1+
#Function file: describe_curation.R
62

7-
# ===== Mock getSignatures function =====
8-
getSignatures <- function(dat, tax.id.type = "metaphlan") {
9-
strsplit(dat$`MetaPhlAn taxon names`, "\\|")
10-
}
11-
12-
# ===== Helper: countTaxon =====
13-
.countTaxon <- function(dat, x, direction = c("both", "increased", "decreased")) {
14-
direction <- match.arg(direction)
15-
if (direction != "both") {
16-
dat <- dplyr::filter(dat, `Abundance in Group 1` == direction)
17-
}
18-
allnames <- getSignatures(dat)
19-
sum(vapply(allnames, function(sig) x %in% sig, integer(1)))
20-
}
21-
22-
# ===== Helper: Binomial Test Summary =====
23-
.createBinomTestSummary <- function(x, n, p = 0.5, wordy = TRUE) {
24-
bt <- binom.test(x, n, p)
25-
if (wordy) {
26-
paste0("p-value=", signif(bt$p.value, 2),
27-
" (increased freq=", x, ", total freq=", n, ")")
28-
} else {
29-
signif(bt$p.value, 2)
30-
}
31-
}
32-
33-
# ===== Mock getMostFrequentTaxa function =====
34-
getMostFrequentTaxa <- function(dat, sig.type = "both", n = 10) {
35-
tab <- table(dat$`MetaPhlAn taxon names`)
36-
df <- as.data.frame(head(sort(tab, decreasing = TRUE), n))
37-
names(df) <- c("Var1", "Freq")
38-
df
39-
}
40-
41-
# ===== Main Function: createTaxonTable =====
42-
createTaxonTable <- function(dat, n = 10) {
43-
dmap <- c("kingdom", "phylum", "class", "order", "family", "genus", "species")
44-
names(dmap) <- substring(dmap, 1, 1)
45-
46-
output <- data.frame(getMostFrequentTaxa(dat, sig.type = "both", n = n),
47-
stringsAsFactors = FALSE) %>%
48-
mutate(metaphlan_name = Var1) %>%
49-
separate(col = Var1, sep = "\\|", into = dmap, fill = "right") %>%
50-
mutate(across(kingdom:species, ~ str_replace(., ".__", ""))) %>%
51-
rename(n_signatures = Freq)
52-
53-
output <- output %>%
54-
mutate(n_signatures = sapply(metaphlan_name, function(x) {
55-
sum(grepl(x, dat$`MetaPhlAn taxon names`, fixed = TRUE))
56-
})) %>%
57-
mutate(total_signatures = sapply(metaphlan_name, function(x)
58-
.countTaxon(dat, x, "both"))) %>%
59-
mutate(increased_signatures = sapply(metaphlan_name, function(x)
60-
.countTaxon(dat, x, "increased"))) %>%
61-
mutate(decreased_signatures = sapply(metaphlan_name, function(x)
62-
.countTaxon(dat, x, "decreased"))) %>%
63-
mutate(Taxon = gsub(".+\\|", "", metaphlan_name))
64-
65-
output %>%
66-
separate(Taxon, into = c("Taxonomic Level", "Taxon Name"), sep = "__") %>%
67-
mutate(`Taxonomic Level` = unname(dmap[`Taxonomic Level`])) %>%
68-
rowwise() %>%
69-
mutate(`Binomial Test pval` = .createBinomTestSummary(increased_signatures, total_signatures, wordy = FALSE)) %>%
70-
ungroup() %>%
71-
relocate(`Taxon Name`, `Taxonomic Level`, total_signatures,
72-
increased_signatures, decreased_signatures, `Binomial Test pval`)
73-
}
74-
75-
# ======= TEST =========
76-
77-
test_that("createTaxonTable returns correct structure", {
78-
mock_data <- data.frame(
79-
`MetaPhlAn taxon names` = c(
80-
"k__Bacteria|p__Firmicutes|g__Lactobacillus",
81-
"k__Bacteria|p__Firmicutes|g__Lactobacillus",
82-
"k__Bacteria|p__Bacteroidetes|g__Bacteroides"
83-
),
84-
`Abundance in Group 1` = c("increased", "decreased", "increased"),
85-
stringsAsFactors = FALSE
86-
)
87-
88-
result <- createTaxonTable(mock_data, n = 2)
89-
90-
expect_s3_class(result, "data.frame")
91-
expect_true(all(c("Taxon Name", "Taxonomic Level", "total_signatures",
92-
"increased_signatures", "decreased_signatures", "Binomial Test pval") %in% colnames(result)))
93-
expect_gt(nrow(result), 0)
3+
bsdb <- bugsigdbr::importBugSigDB()
4+
test_that("createTaxonTable creates a table of most frequent taxa in a data.frame", {
5+
expect_s3_class(createTaxonTable(bsdb[1:10,]), "data.frame")
946
})

tests/testthat/test-frequencySigs.R

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
# Mock .getTip function since it's not in global environment
55
.getTip <- function(x) x
66

7-
8-
library(testthat)
9-
107
test_that("frequencySigs identifies the most frequently occuring taxa", {
118
test_sigs <- list(
129
"UP_sig1" = c("Bacteroides", "Escherichia"),

tests/testthat/test-getCriticalN.R

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
#Function file: montecarlo.R
22

3-
4-
library(testthat)
5-
63
test_that("getCriticalN returns 95th percentile threshold", {
74
relevant.sigs <- list(
85
sig1 = c("Bacteroides", "Escherichia"),

tests/testthat/test-getMostFrequentTaxa.R

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
#Function file: simple.R
22

3-
4-
library(testthat)
5-
library(dplyr)
6-
73
test_that("getMostFrequentTaxa returns the most frequently occurring taxa in a table of signatures", {
84
# Create test data
95
test_data <- data.frame(

tests/testthat/test-getTip.R

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
#Function file: simple.R
22

3-
4-
# Unit test: .getTip should return the last part of a taxonomic string
5-
6-
library(testthat)
7-
83
# Firstly, test when there are multiple levels
94
test_that(".getTip returns the last part of the string", {
105
s <- "k__Bacteria|p__Firmicutes|g__Lactobacillus"

0 commit comments

Comments
 (0)