From 2fe0db074ae01238cdfa12f6450dc6d4b1b99a70 Mon Sep 17 00:00:00 2001 From: Sean Hackett Date: Mon, 19 Feb 2024 13:10:25 -1000 Subject: [PATCH 1/2] allow for testing design lists which are not in a tomic object --- NAMESPACE | 1 + R/data_classes.R | 4 +-- R/design.R | 56 +++++++++++++++++++++++++++++------- man/check_design.Rd | 23 +++++++++++++++ man/romic-package.Rd | 9 ++++++ tests/testthat/test-design.R | 2 +- 6 files changed, 81 insertions(+), 14 deletions(-) create mode 100644 man/check_design.Rd diff --git a/NAMESPACE b/NAMESPACE index 253f09c..a3e96c3 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -5,6 +5,7 @@ export(app_flow) export(app_heatmap) export(app_pcs) export(center_tomic) +export(check_design) export(check_tomic) export(convert_wide_to_tidy_omic) export(create_tidy_omic) diff --git a/R/data_classes.R b/R/data_classes.R index 75636ea..e4a9869 100644 --- a/R/data_classes.R +++ b/R/data_classes.R @@ -166,7 +166,7 @@ check_tidy_omic <- function(tidy_omic, fast_check = TRUE) { checkmate::assertClass(tidy_omic, "tidy_omic") checkmate::assertLogical(fast_check, len = 1) # check design - check_design(tidy_omic) + check_design_in_tomic(tidy_omic) feature_pk <- tidy_omic$design$feature_pk sample_pk <- tidy_omic$design$sample_pk @@ -475,7 +475,7 @@ check_triple_omic <- function(triple_omic, fast_check = TRUE) { checkmate::assertClass(triple_omic, "triple_omic") checkmate::assertLogical(fast_check, len = 1) # check design - check_design(triple_omic) + check_design_in_tomic(triple_omic) # variables are same as design checkmate::assertNames( diff --git a/R/design.R b/R/design.R index 517a737..ef5d0d1 100644 --- a/R/design.R +++ b/R/design.R @@ -20,17 +20,51 @@ get_design_tbl <- function(tomic) { dplyr::bind_rows() } -check_design <- function(tomic) { +#' Check Design +#' +#' Check that the design list embedded in `tomic` objects is properly +#' formatted. +#' +#' @param tomic_design a list with named attributes describing feature, +#' sample, and measurement variables. +#' +#' @return 0, invisibly +#' +#' @examples +#' check_design(brauer_2008_triple$design) +#' +#' @export +check_design <- function(tomic_design) { + + checkmate::assertList(tomic_design) + + EXPECTED_ATTRIBUTES <- c("feature_pk", "features", "measurements", "sample_pk", "samples") + extra_elements <- setdiff(names(tomic_design), EXPECTED_ATTRIBUTES) + if (length(extra_elements) > 0) { + cli::cli_abort( + "The following unexpected attributes were found in the design: {.field {extra_elements}}" + ) + } + + missing_elements <- setdiff(EXPECTED_ATTRIBUTES, names(tomic_design)) + if (length(missing_elements) > 0) { + cli::cli_abort( + "The following attributes were missing in the design: {.field {missing_elements}}" + ) + } + + checkmate::assertString(tomic_design$feature_pk) + checkmate::assertDataFrame(tomic_design$features) + checkmate::assertDataFrame(tomic_design$measurements) + checkmate::assertString(tomic_design$sample_pk) + checkmate::assertDataFrame(tomic_design$samples) +} + +check_design_in_tomic <- function(tomic) { + checkmate::assertClass(tomic, "tomic") stopifnot("design" %in% names(tomic)) - stopifnot(all( - sort(names(tomic$design)) == - c("feature_pk", "features", "measurements", "sample_pk", "samples") - )) - - checkmate::assertString(tomic$design$feature_pk) - checkmate::assertDataFrame(tomic$design$features) - checkmate::assertDataFrame(tomic$design$measurements) - checkmate::assertString(tomic$design$sample_pk) - checkmate::assertDataFrame(tomic$design$samples) + check_design(tomic$design) + } + diff --git a/man/check_design.Rd b/man/check_design.Rd new file mode 100644 index 0000000..46c1b3c --- /dev/null +++ b/man/check_design.Rd @@ -0,0 +1,23 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/design.R +\name{check_design} +\alias{check_design} +\title{Check Design} +\usage{ +check_design(tomic_design) +} +\arguments{ +\item{tomic_design}{a list with named attributes describing feature, +sample, and measurement variables.} +} +\value{ +0, invisibly +} +\description{ +Check that the design list embedded in `tomic` objects is properly +formatted. +} +\examples{ +check_design(brauer_2008_triple$design) + +} diff --git a/man/romic-package.Rd b/man/romic-package.Rd index d9db4ef..c905615 100644 --- a/man/romic-package.Rd +++ b/man/romic-package.Rd @@ -13,6 +13,15 @@ manner. 'romic' takes advantage of these transformations to create interactive shiny apps for exploratory data analysis such as an interactive heatmap. +} +\seealso{ +Useful links: +\itemize{ + \item \url{https://calico.github.io/romic/} + \item \url{https://github.com/calico/romic} + \item Report bugs at \url{https://github.com/calico/romic/issues} +} + } \author{ \strong{Maintainer}: Sean Hackett \email{sean@calicolabs.com} (\href{https://orcid.org/0000-0002-9553-4341}{ORCID}) diff --git a/tests/testthat/test-design.R b/tests/testthat/test-design.R index a9483c5..da50d69 100644 --- a/tests/testthat/test-design.R +++ b/tests/testthat/test-design.R @@ -2,6 +2,6 @@ test_that("extract design as a table", { get_design_tbl(brauer_2008_tidy) %>% expect_snapshot() - expect_invisible(check_design(brauer_2008_tidy)) + expect_invisible(check_design_in_tomic(brauer_2008_tidy)) }) From 55811f407ac604c3797170792b7d678cf0b3b47d Mon Sep 17 00:00:00 2001 From: Sean Hackett Date: Mon, 19 Feb 2024 14:39:52 -1000 Subject: [PATCH 2/2] updating testthat reproduced github actions failures; fixed actions locally with new version --- tests/testthat/_snaps/data_classes.md | 25 +++++++++++++++---------- tests/testthat/_snaps/filters.md | 8 +++++--- tests/testthat/_snaps/mutates.md | 10 ++++++---- tests/testthat/test-filters.R | 18 ++++++++++-------- 4 files changed, 36 insertions(+), 25 deletions(-) diff --git a/tests/testthat/_snaps/data_classes.md b/tests/testthat/_snaps/data_classes.md index 928fa7a..8f5cefb 100644 --- a/tests/testthat/_snaps/data_classes.md +++ b/tests/testthat/_snaps/data_classes.md @@ -2,8 +2,9 @@ Code check_tidy_omic(double_data_tidy, fast_check = FALSE) - Error - 100 measurements were present multiple times with + Condition + Error in `check_tidy_omic()`: + ! 100 measurements were present multiple times with the same feature and sample primary keys For example: @@ -25,8 +26,9 @@ create_tidy_omic(degenerate_attributes %>% select(-degen_sample_var), feature_pk = "features", sample_pk = "samples", feature_var = "degen_feature_var", verbose = FALSE) - Error - "degen_feature_var" was duplicated for 10 features + Condition + Error in `check_tidy_omic()`: + ! "degen_feature_var" was duplicated for 10 features this variable should not be a feature attribute. --- @@ -35,8 +37,9 @@ create_tidy_omic(degenerate_attributes %>% select(-degen_feature_var), feature_pk = "features", sample_pk = "samples", sample_var = "degen_sample_var", verbose = FALSE) - Error - "degen_sample_var" was duplicated for 10 features + Condition + Error in `check_tidy_omic()`: + ! "degen_sample_var" was duplicated for 10 features this variable should not be a feature attribute. # Factor primary keys are preserved when converting from a tidy to a triple @@ -44,14 +47,16 @@ Code create_tidy_omic(three_col_df_fct, feature_pk = "features", sample_pk = "samples", sample_vars = "measurement", feature_vars = "measurement", verbose = FALSE) - Error - measurement were assigned to multiple classes of variables each variable should only belong to one class + Condition + Error in `create_tidy_omic()`: + ! measurement were assigned to multiple classes of variables each variable should only belong to one class # Test that get_tomic_table() can retrieve various tables Code infer_tomic_table_type(simple_tidy, samples_df %>% rename(fake_samples = samples)) - Error - based on the "tomic" primary keys, tomic_table doesn't appear to + Condition + Error in `infer_tomic_table_type()`: + ! based on the "tomic" primary keys, tomic_table doesn't appear to be features, samples or measurements diff --git a/tests/testthat/_snaps/filters.md b/tests/testthat/_snaps/filters.md index f90c9a6..ba9f2bb 100644 --- a/tests/testthat/_snaps/filters.md +++ b/tests/testthat/_snaps/filters.md @@ -11,8 +11,10 @@ --- Code - . - Error - bar is not a valid value for "filter_type", + filter_tomic(brauer_2008_triple, filter_type = "category", filter_table = "features", + filter_variable = "bar", filter_value = "biological process unknown") + Condition + Error in `filter_tomic()`: + ! bar is not a valid value for "filter_type", valid values are all variables within the "features" table: name, BP, MF, systematic_name diff --git a/tests/testthat/_snaps/mutates.md b/tests/testthat/_snaps/mutates.md index 9213b1c..833d626 100644 --- a/tests/testthat/_snaps/mutates.md +++ b/tests/testthat/_snaps/mutates.md @@ -2,8 +2,9 @@ Code center_tomic(brauer_2008_tidy, measurement_vars = "foo") - Error - foo are not valid numeric or integer measurement variables. + Condition + Error in `center_tomic()`: + ! foo are not valid numeric or integer measurement variables. Valid measurements are: expression # Sort tables and update primary keys with new sort @@ -30,7 +31,8 @@ Code . - Error - bar is not present in measurements, valid value_vars include: + Condition + Error in `sort_triple_hclust()`: + ! bar is not present in measurements, valid value_vars include: expression diff --git a/tests/testthat/test-filters.R b/tests/testthat/test-filters.R index b6a1d81..adc1332 100644 --- a/tests/testthat/test-filters.R +++ b/tests/testthat/test-filters.R @@ -42,14 +42,16 @@ test_that("Try all of the filters", { expect_equal(1) # edge cases - filter_tomic( - brauer_2008_triple, - filter_type = "category", - filter_table = "features", - filter_variable = "bar", - filter_value = "biological process unknown" - ) %>% - expect_snapshot(error = TRUE) + expect_snapshot( + filter_tomic( + brauer_2008_triple, + filter_type = "category", + filter_table = "features", + filter_variable = "bar", + filter_value = "biological process unknown" + ), + error = TRUE + ) expect_warning( filter_tomic(