Skip to content

Commit e24ca2b

Browse files
added tests for calculations
1 parent 78deb60 commit e24ca2b

File tree

2 files changed

+48
-62
lines changed

2 files changed

+48
-62
lines changed

R/option_parsers.R

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ get_taxmap_data <- function(obj, dataset) {
8282
# Check that dataset exists
8383
error_msg <- paste0('The dataset "', dataset,
8484
'" is not in the object supplied. Datasets found include:\n ',
85-
limited_print(paste0("[", seq_along(obj$data), "] ", names(obj$data)), type = "silent"))
85+
limited_print(paste0("[", seq_along(obj$data), "] ", names(obj$data)),
86+
type = "silent"))
8687
if (is.character(dataset)) {
8788
if (! dataset %in% names(obj$data)) {
8889
stop(error_msg, call. = FALSE)
@@ -281,7 +282,8 @@ get_taxmap_other_cols <- function(obj, dataset, cols, other_cols = NULL) {
281282
in_both <- other_cols %in% cols
282283
if (sum(in_both) > 0) {
283284
warning(paste0("The following columns will be replaced in the output:\n ",
284-
limited_print(other_cols[in_both])))
285+
limited_print(other_cols[in_both], type = "silent")),
286+
call. = FALSE)
285287
}
286288
result <- other_cols[! in_both]
287289

tests/testthat/test--calculations.R

Lines changed: 44 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -35,55 +35,67 @@ test_that("Counting the number of samples with reads", {
3535

3636
test_that("Observation proportions", {
3737
# Calculate proportions for all numeric columns
38-
calc_obs_props(x, "tax_data")
38+
result <- calc_obs_props(x, "tax_data")
39+
expect_equal(colnames(x$data$tax_data)[-(1:3)], colnames(result)[-1])
40+
expect_true(all(result$`700016050` == x$data$tax_data$`700016050` / sum(x$data$tax_data$`700016050`)))
3941

4042
# Calculate proportions for a subset of columns
41-
calc_obs_props(x, "tax_data", cols = c("700035949", "700097855", "700100489"))
42-
calc_obs_props(x, "tax_data", cols = 4:6)
43-
calc_obs_props(x, "tax_data", cols = startsWith(colnames(x$data$tax_data), "70001"))
43+
col_subset <- c("700035949", "700097855", "700100489")
44+
result <- calc_obs_props(x, "tax_data", cols = col_subset)
45+
expect_equal(col_subset, colnames(result)[-1])
46+
47+
result <- calc_obs_props(x, "tax_data", cols = 4:6)
48+
expect_equal(col_subset, colnames(result)[-1])
49+
50+
result <- calc_obs_props(x, "tax_data",
51+
cols = startsWith(colnames(x$data$tax_data), "70001"))
52+
expect_equal(colnames(x$data$tax_data)[startsWith(colnames(x$data$tax_data), "70001")],
53+
colnames(result)[-1])
4454

4555
# Including all other columns in ouput
46-
calc_obs_props(x, "tax_data", other_cols = TRUE)
56+
expect_warning(result <- calc_obs_props(x, "tax_data", other_cols = TRUE))
57+
expect_true(all(c("otu_id", "lineage") %in% colnames(result)))
4758

4859
# Inlcuding specific columns in output
49-
calc_obs_props(x, "tax_data", cols = c("700035949", "700097855", "700100489"),
60+
result <- calc_obs_props(x, "tax_data", cols = col_subset,
5061
other_cols = 2:3)
62+
expect_true(all(c("otu_id", "lineage") %in% colnames(result)))
5163

5264
# Rename output columns
53-
calc_obs_props(x, "tax_data", cols = c("700035949", "700097855", "700100489"),
65+
result <- calc_obs_props(x, "tax_data", cols = col_subset,
5466
out_names = c("a", "b", "c"))
67+
expect_equal(colnames(result), c("taxon_id", "a", "b", "c"))
68+
5569
})
5670

5771

5872
test_that("Summing counts per taxon", {
5973
# Calculate the taxon abundance for each numeric column (i.e. sample)
60-
calc_taxon_abund(x, "tax_data")
74+
result <- calc_taxon_abund(x, "tax_data")
75+
expect_equal(sum(x$data$tax_data$`700035949`), result$`700035949`[1])
6176

6277
# Calculate the taxon abundance for a subset of columns
63-
calc_taxon_abund(x, "tax_data", cols = 4:5)
64-
calc_taxon_abund(x, "tax_data", cols = c("700035949", "700097855"))
65-
calc_taxon_abund(x, "tax_data", cols = startsWith(colnames(x$data$tax_data), "70001"))
66-
78+
expect_equal(calc_taxon_abund(x, "tax_data", cols = 4:5),
79+
calc_taxon_abund(x, "tax_data", cols = c("700035949", "700097855")))
80+
6781
# Calculate the taxon abundance for groups of columns (e.g. treatments)
6882
# Note that we do not need to use the "cols" option for this since all
6983
# numeric columns are samples in this dataset. If there were numeric columns
7084
# that were not samples present in hmp_samples, the "cols" would be needed.
71-
calc_taxon_abund(x, "tax_data", groups = hmp_samples$sex)
72-
calc_taxon_abund(x, "tax_data", groups = hmp_samples$body_site)
73-
74-
# The above example using the "cols" option, even though not needed in this case
75-
calc_taxon_abund(x, "tax_data", cols = hmp_samples$sample_id,
76-
groups = hmp_samples$sex)
85+
result <- calc_taxon_abund(x, "tax_data", groups = hmp_samples$sex)
86+
expect_equal(colnames(result), c("taxon_id", "female", "male"))
7787

7888
# Rename the output columns
79-
calc_taxon_abund(x, "tax_data", cols = hmp_samples$sample_id[1:10],
80-
out_names = letters[1:10])
81-
calc_taxon_abund(x, "tax_data", groups = hmp_samples$sex,
82-
out_names = c("Women", "Men"))
89+
total_counts <- sum(x$data$tax_data[, hmp_samples$sample_id])
90+
result <- calc_taxon_abund(x, "tax_data", groups = hmp_samples$sex,
91+
out_names = c("Women", "Men"))
92+
expect_equal(colnames(result), c("taxon_id", "Women", "Men"))
93+
expect_equal(total_counts, sum(result[1, c("Women", "Men")]))
8394

8495
# Geting a total for all columns
85-
calc_taxon_abund(x, "tax_data", cols = hmp_samples$sample_id,
86-
groups = rep("total", nrow(hmp_samples)))
96+
result <- calc_taxon_abund(x, "tax_data", cols = hmp_samples$sample_id,
97+
groups = rep("total", nrow(hmp_samples)))
98+
expect_equal(total_counts, result$total[1])
8799
})
88100

89101

@@ -95,52 +107,24 @@ test_that("Comparing groups of samples", {
95107
x$data$tax_table <- calc_taxon_abund(x, dataset = "otu_table", cols = hmp_samples$sample_id)
96108

97109
# Calculate difference between groups
98-
x$data$diff_table <- compare_treatments(x, dataset = "tax_table",
110+
expect_warning(x$data$diff_table <- compare_treatments(x, dataset = "tax_table",
99111
cols = hmp_samples$sample_id,
100-
groups = hmp_samples$body_site)
112+
groups = hmp_samples$body_site))
113+
expect_equal(nrow(x$data$diff_table),
114+
ncol(combn(length(unique(hmp_samples$body_site)), 2)) * nrow(x$data$tax_table))
101115

102116
})
103117

104118

105119
test_that("Rarefying observation counts", {
106120
# Rarefy all numeric columns
107-
rarefy_obs(x, "tax_data")
108-
109-
# Rarefy a subset of columns
110-
rarefy_obs(x, "tax_data", cols = c("700035949", "700097855", "700100489"))
111-
rarefy_obs(x, "tax_data", cols = 4:6)
112-
rarefy_obs(x, "tax_data", cols = startsWith(colnames(x$data$tax_data), "70001"))
113-
114-
# Including all other columns in ouput
115-
rarefy_obs(x, "tax_data", other_cols = TRUE)
116-
117-
# Inlcuding specific columns in output
118-
rarefy_obs(x, "tax_data", cols = c("700035949", "700097855", "700100489"),
119-
other_cols = 2:3)
120-
121-
# Rename output columns
122-
rarefy_obs(x, "tax_data", cols = c("700035949", "700097855", "700100489"),
123-
out_names = c("a", "b", "c"))
121+
result <- rarefy_obs(x, "tax_data")
122+
expect_equal(length(unique(colSums(result[, hmp_samples$sample_id]))), 1)
124123
})
125124

126125

127126
test_that("Converting low counts to zero", {
128127
# Calculate proportions for all numeric columns
129-
calc_obs_props(x, "tax_data")
130-
131-
# Calculate proportions for a subset of columns
132-
calc_obs_props(x, "tax_data", cols = c("700035949", "700097855", "700100489"))
133-
calc_obs_props(x, "tax_data", cols = 4:6)
134-
calc_obs_props(x, "tax_data", cols = startsWith(colnames(x$data$tax_data), "70001"))
135-
136-
# Including all other columns in ouput
137-
calc_obs_props(x, "tax_data", other_cols = TRUE)
138-
139-
# Inlcuding specific columns in output
140-
calc_obs_props(x, "tax_data", cols = c("700035949", "700097855", "700100489"),
141-
other_cols = 2:3)
142-
143-
# Rename output columns
144-
calc_obs_props(x, "tax_data", cols = c("700035949", "700097855", "700100489"),
145-
out_names = c("a", "b", "c"))
128+
result <- zero_low_counts(x, "tax_data")
129+
expect_equal(sum(result[, hmp_samples$sample_id] == 1), 0)
146130
})

0 commit comments

Comments
 (0)