Skip to content

Commit

Permalink
test: fix usage of reneeDS class
Browse files Browse the repository at this point in the history
  • Loading branch information
kelly-sovacool committed Jul 31, 2024
1 parent db29180 commit 1789b4f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 21 deletions.
25 changes: 15 additions & 10 deletions R/0_renee-class.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,18 @@ reneeDataSet <- S7::new_class("renee",
#'
#' @examples
#' create_reneeDataSet_from_files(
#' system.file("extdata", "sample_metadata.tsv", package = "reneeTools"),
#' system.file("extdata", "RSEM.genes.expected_count.all_samples.txt", package = "reneeTools")
#' sample_meta_filepath = system.file("extdata", "sample_metadata.tsv", package = "reneeTools"),
#' gene_counts_filepath = system.file("extdata", "RSEM.genes.expected_count.all_samples.txt", package = "reneeTools")
#' )
create_reneeDataSet_from_files <- function(sample_meta_filepath, gene_counts_filepath,
count_type = "raw") {
count_dat <- readr::read_tsv(gene_counts_filepath)
sample_meta_dat <- readr::read_tsv(sample_meta_filepath)
return(create_reneeDataSet_from_dataframes(sample_meta_dat, list(count_type = count_dat)))
return(create_reneeDataSet_from_dataframes(
sample_meta_dat = sample_meta_dat,
count_dat = count_dat,
count_type = "raw"
))
}

#' Construct a reneeDataSet object from data frames
Expand All @@ -67,15 +71,16 @@ create_reneeDataSet_from_files <- function(sample_meta_filepath, gene_counts_fil
#' create_reneeDataSet_from_dataframes(sample_meta, gene_counts)
create_reneeDataSet_from_dataframes <- function(sample_meta_dat,
count_dat,
sample_id_colname = sample_id,
sample_id_colname = "sample_id",
count_type = "raw") {
gene_id <- GeneName <- NULL

# sample_meta_dat <- sample_meta_dat %>% meta_tbl_to_dat(sample_id_colname = {{ sample_id_colname }})


gene_columns <- c("gene_id", "GeneName", "Gene")
# sample IDs must be in the same order
if (!all(colnames(count_dat %>% dplyr::select(-c(gene_id, GeneName, Gene))) == (sample_meta_dat %>% dplyr::pull({{ sample_id_colname }})))) {
gene_sample_colnames <- count_dat %>%
dplyr::select(-tidyselect::any_of(gene_columns)) %>%
colnames()
meta_sample_colnames <- sample_meta_dat %>%
dplyr::pull(!!rlang::sym(sample_id_colname))
if (!all(gene_sample_colnames == meta_sample_colnames)) {
stop("Not all columns in the count data equal the rows in the sample metadata. Sample IDs must be in the same order.")
}
counts <- list()
Expand Down
19 changes: 8 additions & 11 deletions tests/testthat/test-0_renee-class.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
test_that("reneeDataSet from files works", {
rds <- create_reneeDataSet_from_files(
system.file("extdata", "RSEM.genes.expected_count.all_samples.txt", package = "reneeTools"),
system.file("extdata", "sample_metadata.tsv", package = "reneeTools")
system.file("extdata", "sample_metadata.tsv", package = "reneeTools"),
system.file("extdata", "RSEM.genes.expected_count.all_samples.txt", package = "reneeTools")
)
expect_equal(
rds@counts %>% head(),
rds@counts$raw %>% head(),
structure(
list(
gene_id = c(
Expand All @@ -28,13 +28,10 @@ test_that("reneeDataSet from files works", {
class = c("tbl_df", "tbl", "data.frame")
)
)
expect_equal(
rds@sample_meta,
structure(list(condition = c(
"knockout", "knockout", "wildtype",
"wildtype"
)), row.names = c("KO_S3", "KO_S4", "WT_S1", "WT_S2"), class = "data.frame")
)
expect_equal(rds@sample_meta, tibble::tibble(
sample_id = c("KO_S3", "KO_S4", "WT_S1", "WT_S2"),
condition = c("knockout", "knockout", "wildtype", "wildtype")
))
})

test_that("reneeDataSet from data frames detect problems", {
Expand All @@ -46,7 +43,7 @@ test_that("reneeDataSet from data frames detect problems", {
)
)
expect_error(
create_reneeDataSet_from_dataframes(gene_counts[, 1:4], sample_meta),
create_reneeDataSet_from_dataframes(sample_meta, gene_counts[, 1:4]),
"Not all columns"
)
})

0 comments on commit 1789b4f

Please sign in to comment.