diff --git a/R/mutates.R b/R/mutates.R index 54b0195..67a6a76 100644 --- a/R/mutates.R +++ b/R/mutates.R @@ -150,6 +150,9 @@ center <- function(x) { #' mutate(new_sample_var = "foo") %>% #' select(-DR) #' new_variable_tables <- c("new_sample_var" = "samples") +#' +#' update_tidy_omic(tidy_omic, updated_tidy_data, new_variable_tables) +#' #' @export update_tidy_omic <- function( tidy_omic, diff --git a/tests/testthat/_snaps/design.md b/tests/testthat/_snaps/design.md index 67f9031..660023d 100644 --- a/tests/testthat/_snaps/design.md +++ b/tests/testthat/_snaps/design.md @@ -17,3 +17,19 @@ 9 sample sample_primary_key measurements 10 expression numeric measurements +# Catch malformed design objects + + Code + check_design(malformed_design) + Condition + Error in `check_design()`: + ! The following unexpected attributes were found in the design: foo + +--- + + Code + check_design(malformed_design) + Condition + Error in `check_design()`: + ! The following attributes were missing in the design: feature_pk + diff --git a/tests/testthat/_snaps/dim_reduction.md b/tests/testthat/_snaps/dim_reduction.md new file mode 100644 index 0000000..f906529 --- /dev/null +++ b/tests/testthat/_snaps/dim_reduction.md @@ -0,0 +1,20 @@ +# Sample mahalanobis distances are calculated + + Code + pc_distances + Output + # A tibble: 36 x 4 + sample nutrient DR pc_distance + + 1 G0.05 G 0.05 188. + 2 G0.1 G 0.1 125. + 3 G0.15 G 0.15 128. + 4 G0.2 G 0.2 125. + 5 G0.25 G 0.25 83.4 + 6 G0.3 G 0.3 101. + 7 N0.05 N 0.05 371. + 8 N0.1 N 0.1 226. + 9 N0.15 N 0.15 123. + 10 N0.2 N 0.2 100. + # i 26 more rows + diff --git a/tests/testthat/_snaps/mutates.md b/tests/testthat/_snaps/mutates.md index 833d626..9932b94 100644 --- a/tests/testthat/_snaps/mutates.md +++ b/tests/testthat/_snaps/mutates.md @@ -36,3 +36,55 @@ ! bar is not present in measurements, valid value_vars include: expression +# Factor levels can be updated using a list of factor orders + + Code + brauer_w_mystery_nutrients <- update_sample_factors(brauer_2008_tidy_w_NAs, + list(nutrient = NUTRIENT_ORDER)) + Message + ! NA was present in the sample metadata's nutrient field but did not have a corresponding factor level in the `factor_levels` list. They will be added to the end of the specified factor levels + ! The nutrient field in the sample metadata contains 2 NA values. These entries will be replaced with an "unspecified" level. + +--- + + Code + reordered_tidy <- update_sample_factors(brauer_2008_tidy, list(nutrient = CONFUSED_NUTRIENT_ORDER)) + Message + ! "G" was present in the sample metadata's nutrient field but did not have a corresponding factor level in the `factor_levels` list. They will be added to the end of the specified factor levels + ! "C" was present in `factor_levels` for nutrient but did not have a corresponding entry in the sample metadata. + +--- + + Code + update_sample_factors(brauer_2008_tidy, list(nutrient = 1:5)) + Condition + Error in `set_factor_levels()`: + ! The factor levels for nutrient were "integer". This should be a character vector. + +--- + + Code + update_sample_factors(brauer_2008_tidy, list(nutrient = c("G", "G", "N", "L"))) + Condition + Error in `set_factor_levels()`: + ! 1 factor levels was duplicated in the `factor_levels` specification for "nutrient": G + +--- + + Code + update_sample_factors(brauer_2008_tidy, list(DR = seq(0.05, 0.3, by = 0.05))) + Condition + Error in `set_factor_levels()`: + ! The factor levels for DR were "numeric". This should be a character vector. + +# Update tidy omics with new added variables + + Code + update_tidy_omic(tidy_omic, updated_tidy_data, c()) + Condition + Error in `update_tidy_omic()`: + ! updated_tidy_data contains 1 + - new fields: new_sample_var. + - Add these to "new_variable_tables" so that romic know how to + - use them. + diff --git a/tests/testthat/test-design.R b/tests/testthat/test-design.R index da50d69..c857f99 100644 --- a/tests/testthat/test-design.R +++ b/tests/testthat/test-design.R @@ -5,3 +5,31 @@ test_that("extract design as a table", { expect_invisible(check_design_in_tomic(brauer_2008_tidy)) }) +test_that("get_design_tbl() works when directly passing a design instead of a tomic", { + expect_equal( + get_design_tbl(brauer_2008_tidy), + get_design_tbl(brauer_2008_tidy$design) + ) +}) + +test_that("Catch malformed design objects", { + + malformed_design <- brauer_2008_tidy$design + malformed_design$foo <- "bar" + + expect_snapshot( + check_design(malformed_design), + error = TRUE + ) + + + malformed_design <- brauer_2008_tidy$design + malformed_design$feature_pk <- NULL + + expect_snapshot( + check_design(malformed_design), + error = TRUE + ) + +}) + diff --git a/tests/testthat/test-dim_reduction.R b/tests/testthat/test-dim_reduction.R index f77db3b..dd6a889 100644 --- a/tests/testthat/test-dim_reduction.R +++ b/tests/testthat/test-dim_reduction.R @@ -41,3 +41,10 @@ test_that("Matrices keys are reconstructed with appropriate classes", { } }) + +test_that("Sample mahalanobis distances are calculated", { + + pc_distances <- calculate_sample_mahalanobis_distances(brauer_2008_tidy) + expect_snapshot(pc_distances) + +}) diff --git a/tests/testthat/test-mutates.R b/tests/testthat/test-mutates.R index 268332f..3f444a2 100644 --- a/tests/testthat/test-mutates.R +++ b/tests/testthat/test-mutates.R @@ -75,3 +75,87 @@ test_that("Sort tomic applies a sort to features and/or samples", { expect_equal("fully sorted") }) +test_that("Factor levels can be updated using a list of factor orders", { + + NUTRIENT_ORDER <- c("G", "N", "P", "S", "L", "U") + + reordered_tidy <- update_sample_factors( + brauer_2008_tidy, + list(nutrient = NUTRIENT_ORDER) + ) + + expect_equal(levels(reordered_tidy$data$nutrient), NUTRIENT_ORDER) + + # test NA handling + brauer_samples <- get_tomic_table(brauer_2008_tidy, "samples") + brauer_samples$nutrient[1:2] <- NA + + brauer_2008_tidy_w_NAs <- update_tomic( + brauer_2008_tidy, + brauer_samples + ) + + expect_snapshot( + brauer_w_mystery_nutrients <- update_sample_factors( + brauer_2008_tidy_w_NAs, + list(nutrient = NUTRIENT_ORDER) + ) + ) + + expect_equal( + levels(brauer_w_mystery_nutrients$data$nutrient), + c(NUTRIENT_ORDER, "unspecified") + ) + + # W/ unexpected levels + + CONFUSED_NUTRIENT_ORDER <- c("C", "N", "P", "S", "L", "U") + + expect_snapshot( + reordered_tidy <- update_sample_factors( + brauer_2008_tidy, + list(nutrient = CONFUSED_NUTRIENT_ORDER) + )) + + expect_equal(levels(reordered_tidy$data$nutrient), c(CONFUSED_NUTRIENT_ORDER, "G")) + + # invalid factor specifications + + expect_snapshot( + update_sample_factors(brauer_2008_tidy, list(nutrient = 1:5)), + error = TRUE + ) + + expect_snapshot( + update_sample_factors(brauer_2008_tidy, list(nutrient = c("G", "G", "N", "L"))), + error = TRUE + ) + + expect_snapshot( + update_sample_factors(brauer_2008_tidy, list(DR = seq(0.05, 0.3, by = 0.05))), + error = TRUE + ) + +}) + +test_that("Update tidy omics with new added variables", { + + tidy_omic <- brauer_2008_tidy + updated_tidy_data <- tidy_omic$data %>% + dplyr::mutate(new_sample_var = "foo") %>% + dplyr::select(-DR) + + new_variable_tables <- c("new_sample_var" = "samples") + + tidy_w_updated_samples <- update_tidy_omic(tidy_omic, updated_tidy_data, new_variable_tables) + expect_equal( + tidy_w_updated_samples$design$samples$variable, + c('sample', "nutrient", "new_sample_var") + ) + + expect_snapshot( + update_tidy_omic(tidy_omic, updated_tidy_data, c()), + error = TRUE + ) + +})