Skip to content

Commit 28b4e24

Browse files
committed
started re-organising & extend tests and error-handling
1 parent 2e8206f commit 28b4e24

31 files changed

Lines changed: 401 additions & 163 deletions

NEWS.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
# OmicFlow 1.6.0.9001
1+
# OmicFlow 1.6.0.9003
22

33
## Changed
44
- Re-factoring `roxygen2` code & documentation style [IN PROGRESS].
55
- TODO: Add `tryCatch` where neccessary
66
- Replaced `jsonlite` by `yyjsonr` functionality, much faster and cleaner code.
77
- Replaced `tests/testthat/input/metagenomics/biom_with_taxonomy_json.biom` with one from BIOM v2 example.
8+
- Re-combined `foldchange` into `omics` and option to use different fold-change computations via argument `method`.
89

910
## Fixed
1011
- `omics$feature_merge` is optional in `omics$foldchange`

R/metagenomics-class.R

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,20 @@ metagenomics <- R6::R6Class(
7373
)
7474
) {
7575

76+
# tries to load data via super class
7677
super$initialize(
7778
countData = countData,
7879
featureData = featureData,
7980
metaData = metaData
8081
)
8182

83+
# Maybe `biomData` is supplied.. collect potential errors
84+
messages <- c()
85+
8286
if (!is.null(biomData)) {
8387

84-
if (tools::file_ext(biomData) == "biom") {
85-
88+
if (file.exists(biomData)) {
89+
8690
#---------------------#
8791
### biomData HDF5 ###
8892
#---------------------#
@@ -149,7 +153,7 @@ metagenomics <- R6::R6Class(
149153
x = private$.featureData,
150154
neworder = c(private$.feature_id, base::setdiff(colnames(private$.featureData), private$.feature_id))
151155
)
152-
}
156+
} else cli::cli_abort("{.field biomData} doesn't exist, please provide an existing {.val filepath}")
153157
}
154158

155159
#-------------------#
@@ -183,6 +187,10 @@ metagenomics <- R6::R6Class(
183187
### CLEANUP ###
184188
#-------------------#
185189

190+
# check if `countData` is not empty
191+
if (is.null(private$.countData))
192+
cli::cli_abort("{.field countData} cannot be empty.. did you forgot to specify a {.val countData} or {.val biomData} ?")
193+
186194
cli::cli_alert_info("Final steps .. cleaning & creating back-up")
187195

188196
# Removing prefix of taxonomic features

R/omics-class.R

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,15 @@ omics <- R6::R6Class(
135135
#-------------------#
136136
if (!is.null(metaData)) {
137137
duplicated_sample_ids <- FALSE
138+
139+
## check if message is returned
138140
private$.metaData <- private$check_table(metaData)
141+
if (!data.table::is.data.table(private$.metaData)) {
142+
cli::cli_abort(c(
143+
"Error in {.field metaData}:",
144+
"x" = cli::format_inline("{private$.metaData}")
145+
))
146+
}
139147
self$validate()
140148

141149
if (private$.valid_schema) {
@@ -179,8 +187,15 @@ omics <- R6::R6Class(
179187
#-------------------#
180188
if (!is.null(featureData)) {
181189
duplicated_feature_ids <- FALSE
182-
private$.featureData <- private$check_table(featureData)
183190

191+
private$.featureData <- private$check_table(featureData)
192+
if (!data.table::is.data.table(private$.featureData)) {
193+
cli::cli_abort(c(
194+
"Error in {.field featureData}:",
195+
"x" = cli::format_inline("{private$.featureData}")
196+
))
197+
}
198+
184199
if (column_exists(private$.feature_id, private$.featureData)) {
185200
duplicated_feature_idx <- base::duplicated(private$.featureData, by = private$.feature_id)
186201
duplicated_feature_ids <- any(duplicated_feature_idx)
@@ -209,6 +224,12 @@ omics <- R6::R6Class(
209224
#-------------------#
210225
if (!is.null(countData)) {
211226
private$.countData <- private$check_matrix(countData)
227+
if (!inherits(private$.countData, "sparseMatrix")) {
228+
cli::cli_abort(c(
229+
"Error in {.field countData}:",
230+
"x" = cli::format_inline("{private$.countData}")
231+
))
232+
}
212233
cli::cli_alert_success("{.field countData} is loaded.")
213234

214235
if (is.null(private$.featureData)) {
@@ -1693,6 +1714,12 @@ omics <- R6::R6Class(
16931714
# Load custom distance matrix if supplied
16941715
if (!is.null(distmat)) {
16951716
distmat <- private$check_matrix(filepath = distmat)
1717+
if (!inherits(distmat, "sparseMatrix")) {
1718+
cli::cli_abort(c(
1719+
"Error in {.field countData}:",
1720+
"x" = cli::format_inline("{private$.countData}")
1721+
))
1722+
}
16961723
distmat <- distmat[private$.metaData[[private$.sample_id]], private$.metaData[[private$.sample_id]]]
16971724
}
16981725

@@ -1984,6 +2011,12 @@ omics <- R6::R6Class(
19842011
# Keep only common samples based on metaData
19852012
if (!is.null(private$.countData)) {
19862013
private$.countData <- private$check_matrix(private$.countData)
2014+
if (!inherits(private$.countData, "sparseMatrix")) {
2015+
cli::cli_abort(c(
2016+
"Error in {.field countData}:",
2017+
"x" = cli::format_inline("{private$.countData}")
2018+
))
2019+
}
19872020
common_samples <- base::intersect(private$.metaData[[ private$.sample_id ]], colnames(private$.countData))
19882021

19892022
if (length(common_samples) == 0)
@@ -1999,6 +2032,13 @@ omics <- R6::R6Class(
19992032
cli::cli_abort("{private$.feature_id} doesn't exist in {.field featureData}.")
20002033

20012034
private$.featureData <- private$check_table(private$.featureData)
2035+
if (!data.table::is.data.table(private$.featureData)) {
2036+
cli::cli_abort(c(
2037+
"Error in {.field featureData}:",
2038+
"x" = cli::format_inline("{private$.featureData}")
2039+
))
2040+
}
2041+
20022042
colnames(private$.featureData) <- gsub("\\s+", "_", colnames(private$.featureData))
20032043

20042044
# Keep only common tips based on treeData
@@ -2057,13 +2097,13 @@ omics <- R6::R6Class(
20572097
if (is.character(data) && length(data) == 1 && file.exists(data))
20582098
return(data.table::fread(data, header = TRUE))
20592099

2060-
if (inherits(data, "data.table"))
2100+
if (inherits(data, "data.table") && !all(dim(data) == 0))
20612101
return(data)
20622102

2063-
if (is.data.frame(data))
2103+
if (is.data.frame(data) && !all(dim(data) == 0))
20642104
return(data.table::as.data.table(data))
20652105

2066-
cli::cli_abort("Input must be an existing {.val filepath}, {.cls data.frame} or {.cls data.table}.")
2106+
return("Input must be an existing {.val filepath}, non-empty {.cls data.frame} or {.cls data.table}.")
20672107
},
20682108

20692109
# Checks & loads input matrix/filepath
@@ -2094,13 +2134,13 @@ omics <- R6::R6Class(
20942134
return(methods::as(mat, "CsparseMatrix"))
20952135
}
20962136

2097-
if (inherits(data, "sparseMatrix"))
2137+
if (inherits(data, "sparseMatrix") && !all(dim(data) == 0))
20982138
return(data)
20992139

2100-
if (is.matrix(data) || inherits(data, "denseMatrix"))
2140+
if ((is.matrix(data) || inherits(data, "denseMatrix")) && !all(dim(data) == 0))
21012141
return(methods::as(data, "CsparseMatrix"))
2102-
2103-
cli::cli_abort("Input must be an existing {.val filepath}, {.cls matrix} or {.cls Matrix}.")
2142+
2143+
return("Input must be an existing {.val filepath}, non-empty {.cls matrix} or {.cls Matrix}.")
21042144
}
21052145
)
21062146
)

R/utils.R

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#'
55
#' @param x A \link[base]{matrix}, \link[Matrix]{sparseMatrix} or \link[Matrix]{Matrix}.
66
#' @return A \link[data.table]{data.table} class.
7-
#' @export
7+
#' @noRd
88
matrix_to_dtable <- function(x) {
99
if (inherits(x, "denseMatrix") || inherits(x, "matrix") || inherits(x, "sparseMatrix")) {
1010
return(data.table::data.table(as.matrix(x)))
@@ -17,7 +17,7 @@ matrix_to_dtable <- function(x) {
1717
#' @param column A character of length 1.
1818
#' @param table A \link[data.table]{data.table} or \link[base]{data.frame}.
1919
#' @return A boolean value.
20-
#' @export
20+
#' @noRd
2121
column_exists <- function(column, table) {
2222

2323
## Error handling
@@ -54,6 +54,9 @@ is.wholenumber <- function(x, tol = .Machine$double.eps^0.5) {
5454
}
5555
}
5656

57+
#' Helper function in `omics$autoFlow` to combine conditions
58+
#'
59+
#' @noRd
5760
combine_conditions <- function(condition1, condition2) {
5861
if (!is.null(condition1) && !is.null(condition2)) {
5962
if (!inherits(condition1, "data.frame") && !inherits(condition1, "data.table"))

tests/testthat.R

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,19 @@
99
library("testthat")
1010
library("OmicFlow")
1111
set.seed(100)
12-
test_check("OmicFlow")
12+
13+
## Load example data
14+
metadata_file <- system.file("extdata", "metadata.tsv", package = "OmicFlow")
15+
counts_sparse_file <- system.file("extdata", "counts.tsv", package = "OmicFlow")
16+
counts_sparse_with_rownames_file <- system.file("extdata", "counts_with_rownames.tsv", package = "OmicFlow")
17+
counts_dense_file <- system.file("extdata", "counts_dense.tsv", package = "OmicFlow")
18+
features_file <- system.file("extdata", "features.tsv", package = "OmicFlow")
19+
20+
## Start tests per section
21+
test_check("OmicFlow", filter = "omics")
22+
test_check("OmicFlow", filter = "metagenomics")
23+
test_check("OmicFlow", filter = "proteomics")
24+
test_check("OmicFlow", filter = "metrics")
25+
test_check("OmicFlow", filter = "statistics")
26+
test_check("OmicFlow", filter = "visualisation")
27+
test_check("OmicFlow", filter = "util")

tests/testthat/test-write_biom.R renamed to tests/testthat/test-metagenomics-write_biom.R

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1-
test_that("Writes biom in expected format", {
1+
taxa <- metagenomics$new(
2+
biomData = "input/metagenomics/biom_with_taxonomy_hdf5.biom",
3+
metaData = "input/metagenomics/metadata.tsv",
4+
treeData = "input/metagenomics/rooted_tree.newick"
5+
)
6+
7+
test_that("`write_biom()` -- Argument check", {
8+
9+
})
10+
11+
test_that("`write_biom()` -- Behavior check", {
212
output_file <- paste0(tempdir(),"/test.biom")
313

4-
taxa <- metagenomics$new(
5-
biomData = "input/metagenomics/biom_with_taxonomy_hdf5.biom",
6-
metaData = "input/metagenomics/metadata.tsv",
7-
treeData = "input/metagenomics/rooted_tree.newick"
8-
)
914
taxa$write_biom(filename = output_file)
1015

1116
expect_true(file.exists(output_file))

tests/testthat/test-metagenomics.R

Lines changed: 88 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,93 @@
1-
test_that("Testing Metagenomics reading and writing of BIOM files", {
2-
# Loading hdf5 format
3-
taxa_hdf5 <- metagenomics$new(
4-
biomData = "input/metagenomics/biom_with_taxonomy_hdf5.biom",
5-
metaData = "input/metagenomics/metadata.tsv",
6-
treeData = "input/metagenomics/rooted_tree.newick"
7-
)
8-
9-
taxa_ref <- metagenomics$new(
10-
countData = taxa_hdf5$countData,
11-
metaData = taxa_hdf5$metaData,
12-
treeData = taxa_hdf5$treeData,
13-
featureData = taxa_hdf5$featureData
1+
## Load example data
2+
biom_hdf5 <- "input/metagenomics/biom_with_taxonomy_hdf5.biom"
3+
biom_json <- "input/metagenomics/biom_with_taxonomy_json.biom"
4+
tree <- "input/metagenomics/rooted_tree.newick"
5+
6+
test_that("`metagenomics` -- Argument checks", {
7+
8+
## Ensuring `metaData` is supplied
9+
expect_snapshot(metagenomics$new(), error = TRUE)
10+
expect_snapshot(metagenomics$new(biomData = "nonexisting.biom"), error = TRUE)
11+
expect_snapshot(metagenomics$new(featureData = features_file), error = TRUE)
12+
expect_snapshot(metagenomics$new(countData = counts_sparse_file), error = TRUE)
13+
expect_snapshot(metagenomics$new(metaData = data.frame()), error = TRUE)
14+
expect_snapshot(metagenomics$new(metaData = data.table::data.table()), error = TRUE)
15+
16+
## Checking errors
17+
expect_snapshot(metagenomics$new(metaData = metadata_file, featureData = data.frame()), error = TRUE)
18+
expect_snapshot(metagenomics$new(metaData = metadata_file, featureData = data.table::data.table()), error = TRUE)
19+
expect_snapshot(metagenomics$new(metaData = metadata_file, biomData = "nonexisting.biom"), error = TRUE)
20+
expect_snapshot(metagenomics$new(metaData = metadata_file, biomData = metadata_file), error = TRUE)
21+
22+
expect_snapshot(metagenomics$new(metaData = metadata_file, countData = data.frame()), error = TRUE)
23+
expect_snapshot(metagenomics$new(metaData = metadata_file, countData = data.table::data.table()), error = TRUE)
24+
expect_snapshot(metagenomics$new(metaData = metadata_file, countData = matrix(0)), error = TRUE)
25+
26+
expect_snapshot(metagenomics$new(metaData = metadata_file, treeData = data.frame()), error = TRUE)
27+
expect_snapshot(metagenomics$new(metaData = metadata_file, treeData = ape::rtree(50)), error = TRUE)
28+
expect_snapshot(metagenomics$new(metaData = metadata_file, biomData = biom_hdf5, treeData = ape::rtree(50)), error = TRUE)
29+
})
30+
31+
test_that("`metagenomics` -- Behavioral checks", {
32+
# Loading biom hdf5
33+
expect_snapshot(
34+
test <- metagenomics$new(
35+
metaData = metadata_file,
36+
biomData = biom_hdf5
37+
)
1438
)
15-
16-
expect_snapshot(taxa_hdf5)
17-
expect_snapshot(taxa_ref)
18-
19-
# Adding treeData after init
20-
taxa_ref <- metagenomics$new(
21-
countData = taxa_hdf5$countData,
22-
metaData = taxa_hdf5$metaData,
23-
featureData = taxa_hdf5$featureData,
39+
expect_equal(all(rownames(test$countData) == test$featureData$FEATURE_ID), TRUE)
40+
expect_equal(all(colnames(test$countData) == test$metaData$SAMPLE_ID), TRUE)
41+
expect_equal(inherits(test$countData, "sparseMatrix"), TRUE)
42+
expect_equal(class(test$metaData)[1], "data.table")
43+
expect_equal(class(test$featureData)[1], "data.table")
44+
45+
# Loading biom json
46+
expect_snapshot(
47+
test <- metagenomics$new(
48+
biomData = biom_json,
49+
metaData = data.table::data.table(SAMPLE_ID = c(
50+
"Sample1", "Sample2", "Sample3",
51+
"Sample4", "Sample5", "Sample6"))
52+
)
2453
)
54+
expect_equal(all(rownames(test$countData) == test$featureData$FEATURE_ID), TRUE)
55+
expect_equal(all(colnames(test$countData) == test$metaData$SAMPLE_ID), TRUE)
56+
expect_equal(inherits(test$countData, "sparseMatrix"), TRUE)
57+
expect_equal(class(test$metaData)[1], "data.table")
58+
expect_equal(class(test$featureData)[1], "data.table")
2559

26-
expect_error(taxa_ref$treeData <- taxa_hdf5$countData)
27-
expect_no_error(taxa_ref$treeData <- taxa_hdf5$treeData)
60+
# Loading biom hdf5 with tree
61+
expect_snapshot(
62+
test <- metagenomics$new(
63+
metaData = metadata_file,
64+
biomData = biom_hdf5,
65+
treeData = tree
66+
)
67+
)
68+
expect_equal(all(rownames(test$countData) == test$featureData$FEATURE_ID), TRUE)
69+
expect_equal(all(rownames(test$countData) == test$treeData$tip.label), TRUE)
70+
expect_equal(all(test$treeData$tip.label == test$featureData$FEATURE_ID), TRUE)
71+
expect_equal(all(colnames(test$countData) == test$metaData$SAMPLE_ID), TRUE)
72+
expect_equal(inherits(test$countData, "sparseMatrix"), TRUE)
73+
expect_equal(class(test$metaData)[1], "data.table")
74+
expect_equal(class(test$featureData)[1], "data.table")
2875

29-
# Loading JSON format
30-
taxa_json <- metagenomics$new(
31-
biomData = "input/metagenomics/biom_with_taxonomy_json.biom",
32-
metaData = data.table::data.table(SAMPLE_ID = c(
33-
"Sample1", "Sample2", "Sample3",
34-
"Sample4", "Sample5", "Sample6"))
76+
# Checking loading metagenomics from pre-loaded test
77+
expect_snapshot(
78+
taxa_ref <- metagenomics$new(
79+
countData = test$countData,
80+
metaData = test$metaData,
81+
treeData = test$treeData,
82+
featureData = test$featureData
83+
)
3584
)
36-
expect_snapshot(taxa_json)
37-
})
85+
expect_equal(all(rownames(test$countData) == rownames(taxa_ref$countData)), TRUE)
86+
expect_equal(all(colnames(test$countData) == colnames(taxa_ref$countData)), TRUE)
87+
expect_equal(all(test$featureData$FEATURE_ID == taxa_ref$featureData$FEATURE_ID), TRUE)
88+
expect_equal(all(test$treeData$tip.label == taxa_ref$treeData$tip.label), TRUE)
89+
expect_equal(all(test$metaData$SAMPLE_ID == taxa_ref$metaData$SAMPLE_ID), TRUE)
90+
expect_equal(inherits(test$countData, "sparseMatrix"), inherits(taxa_ref$countData, "sparseMatrix"))
91+
expect_equal(class(test$metaData)[1], class(taxa_ref$metaData)[1])
92+
expect_equal(class(test$featureData)[1], class(taxa_ref$featureData)[1])
93+
})

0 commit comments

Comments
 (0)