Skip to content

Commit

Permalink
Merge pull request #91 from calico/test_cov_offline_work
Browse files Browse the repository at this point in the history
Improving test coverage
  • Loading branch information
shackett authored Sep 25, 2024
2 parents 3dadaf8 + 58a020f commit 791260b
Show file tree
Hide file tree
Showing 15 changed files with 659 additions and 28 deletions.
2 changes: 1 addition & 1 deletion R/data_classes.R
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ check_triple_omic <- function(triple_omic, fast_check = TRUE) {

if (nrow(samples_not_unique) != 0) {
stop(glue::glue(
"{nrow(samples_not_unique)} features were present multiple times with
"{nrow(samples_not_unique)} samples were present multiple times with
the same sample primary key"
))
}
Expand Down
38 changes: 21 additions & 17 deletions R/hclust.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
hclust_tidy_omic <- function(
tidy_omic,
feature_var,
sample_var,
value_var,
cluster_dim,
distance_measure = "dist",
hclust_method = "ward.D2") {
tidy_omic,
feature_var,
sample_var,
value_var,
cluster_dim,
distance_measure = "dist",
hclust_method = "ward.D2"
) {

check_tidy_omic(tidy_omic)

checkmate::assertChoice(feature_var, tidy_omic$design$features$variable)
Expand Down Expand Up @@ -37,7 +39,7 @@ hclust_tidy_omic <- function(
cluster_orders$columns <-
coerce_to_classes(
cluster_orders$columns,
tidy_omic$data[[tidy_omic$design$feature_pk]]
tidy_omic$data[[tidy_omic$design$sample_pk]]
)

# order rows and columns
Expand All @@ -49,7 +51,7 @@ hclust_tidy_omic <- function(
)

if (cluster_dim == "columns") {
# order by factor or alpha-numerically
# order features by factor or alpha-numerically

if (
any(class(distinct_features[[feature_var]]) %in% c("factor", "ordered"))
Expand Down Expand Up @@ -93,7 +95,7 @@ hclust_tidy_omic <- function(
)

if (cluster_dim == "rows") {
# order by factor or alpha-numerically
# order samples by factor or alpha-numerically

if (any(class(distinct_samples[[sample_var]]) %in% c("factor", "ordered"))) {
# retain previous ordering
Expand Down Expand Up @@ -208,13 +210,15 @@ hclust_tidy_omic <- function(
#' hclust_order(df, "letters", "numbers", "noise", "rows")
#' @export
hclust_order <- function(
df,
feature_pk,
sample_pk,
value_var,
cluster_dim,
distance_measure = "dist",
hclust_method = "ward.D2") {
df,
feature_pk,
sample_pk,
value_var,
cluster_dim,
distance_measure = "dist",
hclust_method = "ward.D2"
) {

checkmate::assertDataFrame(df)
checkmate::assertChoice(feature_pk, colnames(df))
checkmate::assertChoice(sample_pk, colnames(df))
Expand Down
3 changes: 3 additions & 0 deletions R/mutates.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
15 changes: 7 additions & 8 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,18 @@ format_names_for_plotting <- function(chars, width = 40, truncate_at = 80) {
}

coerce_to_classes <- function(obj, reference_obj) {
reference_obj_class <- class(reference_obj)
reference_obj_class <- class(reference_obj)[1]

if (any(reference_obj_class %in% "glue")) {
out <- glue::as_glue(obj)
} else if (any(reference_obj_class %in% c("factor", "ordered"))) {
out <-
do.call(
reference_obj_class,
list(
x = obj,
levels = levels(reference_obj)
)
out <- do.call(
reference_obj_class,
list(
x = obj,
levels = levels(reference_obj)
)
)
} else if (reference_obj_class == "character") {
out <- as.character(obj)
} else if (reference_obj_class == "numeric") {
Expand Down
89 changes: 89 additions & 0 deletions tests/testthat/_snaps/data_classes.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,79 @@
Error in `create_tidy_omic()`:
! measurement were assigned to multiple classes of variables each variable should only belong to one class

# Catch corner cases when reading wide data

Code
convert_wide_to_tidy_omic(wide_df_nonunique_feature_id, feature_pk = "name")
Condition
Warning in `convert_wide_to_tidy_omic()`:
4 rows did not contain a unique name; adding extra variables 'unique_name' & 'entry_number' to distinguish them
Warning:
`mutate_()` was deprecated in dplyr 0.7.0.
i Please use `mutate()` instead.
i See vignette('programming') for more help
Message
1 measurement variables were defined as the
left overs from the specified feature and sample varaibles:
abundance
Output
$data
# A tibble: 19,500 x 5
name entry_number unique_name sample abundance
<chr> <int> <chr> <chr> <chr>
1 YOL029C 1 YOL029C-1 BP biological process unknown
2 YOL029C 2 YOL029C-2 BP cytokinesis, completion of separation
3 YOL029C 3 YOL029C-3 BP biological process unknown
4 YOL029C 4 YOL029C-4 BP cell wall organization and biogenesis
5 YOL029C 5 YOL029C-5 BP cell wall organization and biogenesi~
6 FKH1 1 FKH1 BP pseudohyphal growth*
7 HOC1 1 HOC1 BP cell wall mannoprotein biosynthesis*
8 CSN12 1 CSN12 BP adaptation to pheromone during conju~
9 YAL046C 1 YAL046C BP biological process unknown
10 SLG1 1 SLG1 BP cell wall organization and biogenesi~
# i 19,490 more rows
$design
$design$features
# A tibble: 3 x 2
variable type
<chr> <chr>
1 unique_name feature_primary_key
2 name character
3 entry_number integer
$design$samples
# A tibble: 1 x 2
variable type
<chr> <chr>
1 sample sample_primary_key
$design$measurements
# A tibble: 3 x 2
variable type
<chr> <chr>
1 unique_name feature_primary_key
2 sample sample_primary_key
3 abundance character
$design$feature_pk
[1] "unique_name"
$design$sample_pk
[1] "sample"
attr(,"class")
[1] "tidy_omic" "tomic" "general"

# Find primary or foreign keys in tomic table

Code
get_identifying_keys(brauer_2008_triple, "foo")
Condition
Error in `get_identifying_keys()`:
! Assertion on 'table' failed: Must be element of set {'features','samples','measurements'}, but is 'foo'.

# Test that get_tomic_table() can retrieve various tables

Code
Expand All @@ -60,3 +133,19 @@
! based on the "tomic" primary keys, tomic_table doesn't appear to
be features, samples or measurements

# Catch corner cases when interconverting tomics

Code
check_tomic(mtcars)
Condition
Error in `check_tomic()`:
! Assertion on 'tomic' failed: Must inherit from class 'tomic', but has class 'data.frame'.

---

Code
tomic_to(romic::brauer_2008_tidy, "foo")
Condition
Error in `tomic_to()`:
! Assertion on 'to_class' failed: Must be element of set {'tidy_omic','triple_omic'}, but is 'foo'.

16 changes: 16 additions & 0 deletions tests/testthat/_snaps/design.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

20 changes: 20 additions & 0 deletions tests/testthat/_snaps/dim_reduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Sample mahalanobis distances are calculated

Code
pc_distances
Output
# A tibble: 36 x 4
sample nutrient DR pc_distance
<chr> <chr> <dbl> <dbl>
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

52 changes: 52 additions & 0 deletions tests/testthat/_snaps/mutates.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

2 changes: 1 addition & 1 deletion tests/testthat/helper.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ three_col_df <- tidyr:::expand_grid(
samples = 1:10
) %>%
dplyr::mutate(
measurement = 1
measurement = 1:100
)

simple_tidy <- create_tidy_omic(
Expand Down
Loading

0 comments on commit 791260b

Please sign in to comment.