Browse code

Test module for compareSamples

Jaehyun Joo authored on 02/04/2023 20:06:16
Showing 5 changed files

... ...
@@ -16,8 +16,8 @@
16 16
 ##'   label of each sample in `colData(x)`.
17 17
 ##' @param class1,class2 A string specifying the class label of samples to be
18 18
 ##'   compared. Must be one of `group` levels. No need to be specified if
19
-##'   `group` has only two levels. This function evaluates the contrast: class2
20
-##'   - class1.
19
+##'   `group` has only two levels. This function evaluates the contrast:
20
+##'   `class2 - class1`.
21 21
 ##' @param covariates A vector indicating the names of variables to be included
22 22
 ##'   in the model as covariates. The covariates must be found in `colData(x)`.
23 23
 ##' @param confint A logical specifying whether 95% confidence intervals of
... ...
@@ -40,9 +40,9 @@
40 40
 ##' @param fc A numeric value specifying a minimum fold-change to be required.
41 41
 ##'   If specified, the function output only includes metabolic features with
42 42
 ##'   absolute fold-change greater than `fc`.
43
-##' @param lfc A numeric value specifying a minimum log2-fold-change required,
44
-##'   equal to log2(fc). `fc` and `lfc` are alternative ways to specify a
45
-##'   fold-change cut-off and, if both are specified, then `fc` take precedence.
43
+##' @param lfc A numeric value specifying a minimum log-fold-change required.
44
+##'   `fc` and `lfc` are alternative ways to specify a fold-change cut-off and,
45
+##'   if both are specified, then `fc` take precedence.
46 46
 ##' @param ... Additional arguments passed to [limma::eBayes].
47 47
 ##' @return A data.frame with a row for the metabolic features and the following
48 48
 ##'   columns:
... ...
@@ -113,7 +113,6 @@ compareSamples <- function(x, i, group,
113 113
     stop(setdiff(c(class1, class2), levels(cdat[[group]])),
114 114
          " is not the levels of '", group, "'")
115 115
   }
116
-  message(paste0("Contrast: ", class2, " - ", class1))
117 116
   ## Check covariates are found in cdat
118 117
   if (!all(covariates %in% colnames(cdat))) {
119 118
     stop("`colData(x)` does not have a column(s): ",
... ...
@@ -131,6 +130,7 @@ compareSamples <- function(x, i, group,
131 130
   fit <- contrasts.fit(fit, contrasts = contrast)
132 131
   fit <- eBayes(fit, ...)
133 132
   ## Output
133
+  message(paste0("Contrast: ", class2, " - ", class1))
134 134
   topTable(fit, number = number, adjust.method = adjust.method,
135 135
            sort.by = sort.by, resort.by = resort.by, p.value = p.value,
136 136
            fc = fc, lfc = lfc, confint = confint)
... ...
@@ -33,10 +33,8 @@ label of each sample in \code{colData(x)}.}
33 33
 
34 34
 \item{class1, class2}{A string specifying the class label of samples to be
35 35
 compared. Must be one of \code{group} levels. No need to be specified if
36
-\code{group} has only two levels. This function evaluates the contrast: class2
37
-\itemize{
38
-\item class1.
39
-}}
36
+\code{group} has only two levels. This function evaluates the contrast:
37
+\code{class2 - class1}.}
40 38
 
41 39
 \item{covariates}{A vector indicating the names of variables to be included
42 40
 in the model as covariates. The covariates must be found in \code{colData(x)}.}
... ...
@@ -68,9 +66,9 @@ Only metabolic features with lower p-values are listed.}
68 66
 If specified, the function output only includes metabolic features with
69 67
 absolute fold-change greater than \code{fc}.}
70 68
 
71
-\item{lfc}{A numeric value specifying a minimum log2-fold-change required,
72
-equal to log2(fc). \code{fc} and \code{lfc} are alternative ways to specify a
73
-fold-change cut-off and, if both are specified, then \code{fc} take precedence.}
69
+\item{lfc}{A numeric value specifying a minimum log-fold-change required.
70
+\code{fc} and \code{lfc} are alternative ways to specify a fold-change cut-off and,
71
+if both are specified, then \code{fc} take precedence.}
74 72
 
75 73
 \item{...}{Additional arguments passed to \link[limma:ebayes]{limma::eBayes}.}
76 74
 }
... ...
@@ -4,6 +4,10 @@ data(faahko_se)
4 4
 faahko_sub <- faahko_se[1:50, ]
5 5
 expect_true(anyNA(assay(faahko_sub, 1)))
6 6
 
7
+## Add some covariates
8
+faahko_sub$covar1 <- rep(c("A", "A", "A", "B", "B", "B"), 2)
9
+faahko_sub$covar2 <- rep(c("C", "D"), 6)
10
+
7 11
 imputation_methods <- c("knn", "rf", "bpca", "QRILC", "MLE",
8 12
                         "MinDet", "MinProb", "min", "zero",
9 13
                         "mixed", "nbavg", "with", "none")
... ...
@@ -2,7 +2,7 @@ test_that("clusterFeatures works.", {
2 2
 
3 3
     ## Incorrect rtime varname
4 4
     expect_error(
5
-        clusterFeatures(fahhko_se, i = "knn_vsn", rtime_var = "zzz")
5
+        clusterFeatures(faahko_sub, i = "knn_vsn", rtime_var = "zzz")
6 6
     )
7 7
 
8 8
     ## Function generates "rtime_group" and "feature_group" in rowData
9 9
new file mode 100644
... ...
@@ -0,0 +1,43 @@
1
+test_that("compareSamples works.", {
2
+
3
+  ## Incorrect group varname
4
+  expect_error(
5
+    compareSamples(faahko_sub, i = "knn_vsn", group = "sample_grp")
6
+  )
7
+
8
+  ## Incorrect group level specification
9
+  expect_error(
10
+    compareSamples(faahko_sub, i = "knn_vsn", group = "sample_group",
11
+                   class1 = "WW", class2 = "KK")
12
+  )
13
+  expect_error(
14
+    compareSamples(faahko_sub, i = "knn_vsn", group = "sample_group",
15
+                   class1 = "WT", class2 = "KK")
16
+  )
17
+  expect_error(
18
+    compareSamples(faahko_sub, i = "knn_vsn", group = "sample_group",
19
+                   class1 = "WW", class2 = "KK")
20
+  )
21
+
22
+  ## Incorrect covariates varnames
23
+  expect_error(
24
+    compareSamples(faahko_sub, i = "knn_vsn", group = "sample_group",
25
+                   covariates = c("covar3", "covar4")) ## not covar3 and covar4
26
+  )
27
+  expect_error(
28
+    compareSamples(faahko_sub, i = "knn_vsn", group = "sample_group",
29
+                   covariates = c("covar1", "covar3")) ## not covar3
30
+  )
31
+
32
+  ## Confidence interval when confint is enabled
33
+  mod1 <- compareSamples(faahko_sub, i = "knn_vsn", group = "sample_group",
34
+                        confint = TRUE)
35
+  expect_s3_class(mod1, "data.frame")
36
+  expect_true(all(c("CI.L", "CI.R") %in% names(mod1)))
37
+
38
+  mod2 <- compareSamples(faahko_sub, i = "knn_vsn", group = "sample_group",
39
+                         confint = FALSE)
40
+  expect_s3_class(mod2, "data.frame")
41
+  expect_false(all(c("CI.L", "CI.R") %in% names(mod2)))
42
+
43
+})