From 493f8cbd4c0acfe282cf3ee238d6a80ebf22aa64 Mon Sep 17 00:00:00 2001 From: Robrecht Cannoodt Date: Tue, 19 Sep 2023 17:01:44 +0200 Subject: [PATCH 1/4] suppress warnings in vignette (#127) * suppress warnings in vignette * fix vignette --- vignettes/getting-started.Rmd | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/vignettes/getting-started.Rmd b/vignettes/getting-started.Rmd index 360b4497..5f420135 100644 --- a/vignettes/getting-started.Rmd +++ b/vignettes/getting-started.Rmd @@ -19,6 +19,11 @@ knitr::opts_chunk$set( collapse = TRUE, comment = "#>" ) + +# already load SeuratObject and SingleCellExperiment +# so warnings generated by these packages are not shown +library(SeuratObject) +library(SingleCellExperiment) ``` # Introduction From 7c48007986aaf545b43b8094da35d38fdfd2a549 Mon Sep 17 00:00:00 2001 From: Robrecht Cannoodt Date: Tue, 19 Sep 2023 17:02:21 +0200 Subject: [PATCH 2/4] add authors (#113) * add authors * remove trailing comma --- DESCRIPTION | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 24c0ceeb..5a3efc24 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -3,39 +3,50 @@ Title: AnnData interoperability in R Version: 0.0.0.9000 Authors@R: c( person( - "Robrecht", - "Cannoodt", + "Robrecht", "Cannoodt", email = "rcannood@gmail.com", role = c("aut", "cre"), comment = c(ORCID = "0000-0003-3641-729X", github = "rcannood") ), person( - "Luke", - "Zappia", + "Luke", "Zappia", email = "luke@lazappi.id.au", role = "aut", comment = c(ORCID = "0000-0001-7744-8565", github = "lazappi") ), person( - "Martin", - "Morgan", + "Martin", "Morgan", email = "mtmorgan.bioc@gmail.com", role = "aut", comment = c(ORCID = "0000-0002-5874-8148", github = "mtmorgan") ), person( - "Louise", - "Deconinck", + "Louise", "Deconinck", email = "louise.deconinck@gmail.com", role = "aut", comment = c(ORCID = "0000-0001-8100-6823", github = "LouiseDck") ), person( - "Danila", - "Bredikhin", + "Danila", "Bredikhin", email = "danila.bredikhin@embl.de", role = "ctb", comment = c(ORCID = "0000-0001-8089-6983", github = "gtca") + ), + person( + "Isaac", "Virshup", + role = "ctb", + comment = c(ORCID = "0000-0002-1710-8945", github = "ivirshup") + ), + person( + "Brian", "Schilder", + role = "aut", + email = "brian_schilder@alumni.brown.edu", + comment = c(ORCID = "0000-0001-5949-2191", github = "bschilder") + ), + person( + "Chananchida", "Sang-aram", + role = "ctb", + comment = c(ORCID = "0000-0002-0922-0822", github = "csangara") ) ) Description: Bring the power and flexibility of AnnData to the R From a9775f60f7fabf0d0dfdbe3e32b5333e1140061c Mon Sep 17 00:00:00 2001 From: Robrecht Cannoodt Date: Wed, 20 Sep 2023 09:35:17 +0200 Subject: [PATCH 3/4] remove helper function that's only used once (#130) * remove helper function that's only used once * refactor print function * add missing *_keys functions * apply styler --- R/AbstractAnnData.R | 36 ++++++++++++++++++++++++++++++------ R/{utilities.R => utils.R} | 4 ---- 2 files changed, 30 insertions(+), 10 deletions(-) rename R/{utilities.R => utils.R} (53%) diff --git a/R/AbstractAnnData.R b/R/AbstractAnnData.R index 754b5786..60938816 100644 --- a/R/AbstractAnnData.R +++ b/R/AbstractAnnData.R @@ -82,19 +82,27 @@ AbstractAnnData <- R6::R6Class("AbstractAnnData", # nolint for (attribute in c( "obs", "var", - "uns", + # "uns", # TODO: remove this when uns is implemented "obsm", "varm", "layers", "obsp", "varp" )) { - attr_key <- paste0(attribute, "_keys") - if (!is.null(self[[attr_key]])) { - slot_keys <- self[[attr_key]]() - if (length(slot_keys) > 0) { - cat(" ", pretty_print(attribute, slot_keys), "\n", sep = "") + key_fun <- self[[paste0(attribute, "_keys")]] + keys <- + if (!is.null(key_fun)) { + key_fun() + } else { + NULL } + if (length(keys) > 0) { + cat( + " ", attribute, ":", + paste("'", keys, "'", collapse = ", "), + "\n", + sep = "" + ) } } }, @@ -126,6 +134,22 @@ AbstractAnnData <- R6::R6Class("AbstractAnnData", # nolint layers_keys = function() { names(self$layers) }, + #' @description Keys (element names) of `obsm`. + obsm_keys = function() { + names(self$obsm) + }, + #' @description Keys (element names) of `varm`. + varm_keys = function() { + names(self$varm) + }, + #' @description Keys (element names) of `obsp`. + obsp_keys = function() { + names(self$obsp) + }, + #' @description Keys (element names) of `varp`. + varp_keys = function() { + names(self$varp) + }, #' @description Convert to SingleCellExperiment to_SingleCellExperiment = function() { to_SingleCellExperiment(self) diff --git a/R/utilities.R b/R/utils.R similarity index 53% rename from R/utilities.R rename to R/utils.R index a6127126..3e020d61 100644 --- a/R/utilities.R +++ b/R/utils.R @@ -1,7 +1,3 @@ -pretty_print <- function(label, value) { - paste0(label, ": ", paste0("'", value, "'", collapse = ", ")) -} - wrap_message <- function(...) { txt <- paste0(..., collapse = "") paste(strwrap(txt, exdent = 2L), collapse = "\n") From 50418e146f2c2fa882062f1ed3605aaf2cb06bc7 Mon Sep 17 00:00:00 2001 From: Robrecht Cannoodt Date: Thu, 30 Nov 2023 16:42:45 +0100 Subject: [PATCH 4/4] Refactor tests and validation functions (#137) * use validate aligned array instead of matrix * refactor validate_aligned_array * update package docs * add has_row_names to utils * skip seurat converter for now * remove outdated tests * refactor roundtrip tests * remove roundtrip tests (will be added back in #107) * add extra line * comment out seurat object example --- R/AbstractAnnData.R | 55 +++------------------ R/HDF5AnnData.R | 8 ++- R/InMemoryAnnData.R | 8 ++- R/utils.R | 8 +++ R/write_h5ad.R | 37 +++++++------- man/AbstractAnnData.Rd | 44 +++++++++++++++++ man/HDF5AnnData.Rd | 4 ++ man/InMemoryAnnData.Rd | 4 ++ man/anndataR-package.Rd | 3 ++ man/write_h5ad.Rd | 37 +++++++------- tests/testthat/test-HDF5-write.R | 1 + tests/testthat/test-InMemoryAnnData.R | 62 ------------------------ tests/testthat/test-roundtrip.R | 70 --------------------------- 13 files changed, 123 insertions(+), 218 deletions(-) delete mode 100644 tests/testthat/test-roundtrip.R diff --git a/R/AbstractAnnData.R b/R/AbstractAnnData.R index 60938816..22dcf2ae 100644 --- a/R/AbstractAnnData.R +++ b/R/AbstractAnnData.R @@ -169,40 +169,6 @@ AbstractAnnData <- R6::R6Class("AbstractAnnData", # nolint } ), private = list( - # @description `.validate_matrix()` checks that dimensions are - # consistent with `obs` and `var`, and removes dimnames if - # present. - # @param mat A matrix to validate - # @param label Must be `"X"` or `"layer[[...]]"` where `...` is - # the name of a layer. - .validate_matrix = function(mat, label) { - if (!is.null(mat)) { - if (nrow(mat) != self$n_obs()) { - stop("nrow(", label, ") should be the same as nrow(obs)") - } - if (ncol(mat) != self$n_vars()) { - stop("ncol(", label, ") should be the same as nrow(var)") - } - - if (!is.null(rownames(mat))) { - warning(wrap_message( - "rownames(", label, ") should be NULL, removing them from the matrix" - )) - rownames(mat) <- NULL - } - - if (!is.null(colnames(mat))) { - warning(wrap_message( - "colnames(", label, ") should be NULL, removing them from the matrix" - )) - colnames(mat) <- NULL - } - } - - mat - }, - - # @description `.validate_aligned_array()` checks that dimensions are # consistent with the anndata object. # @param mat A matrix to validate @@ -212,6 +178,9 @@ AbstractAnnData <- R6::R6Class("AbstractAnnData", # nolint # @param expected_rownames # @param excepted_colnames .validate_aligned_array = function(mat, label, shape, expected_rownames = NULL, expected_colnames = NULL) { + if (is.null(mat)) { + return(mat) + } mat_dims <- dim(mat) for (i in seq_along(shape)) { expected_dim <- shape[i] @@ -220,26 +189,16 @@ AbstractAnnData <- R6::R6Class("AbstractAnnData", # nolint stop("dim(", label, ")[", i, "] should have shape: ", expected_dim, ", found: ", found_dim, ".") } } - if (!is.null(expected_rownames) & !is.null(rownames(mat))) { + if (!is.null(expected_rownames) & !has_row_names(mat)) { if (!identical(rownames(mat), expected_rownames)) { stop("rownames(", label, ") should be the same as expected_rownames") } - } - if (!is.null(rownames(mat))) { - warning(wrap_message( - "rownames(", label, ") should be NULL, removing them from the matrix" - )) rownames(mat) <- NULL } if (!is.null(expected_colnames) & !is.null(colnames(mat))) { if (!identical(colnames(mat), expected_colnames)) { stop("colnames(", label, ") should be the same as expected_colnames") } - } - if (!is.null(colnames(mat))) { - warning(wrap_message( - "colnames(", label, ") should be NULL, removing them from the matrix" - )) colnames(mat) <- NULL } @@ -251,8 +210,8 @@ AbstractAnnData <- R6::R6Class("AbstractAnnData", # nolint # whose entries will be validated # @param label The label of the collection, used for error messages # @param shape Expected dimensions of arrays. Arrays may have more dimensions than specified here - # @param expected_rownames - # @param expected_colnames + # @param expected_rownames Expected row names + # @param expected_colnames Expected column names .validate_aligned_mapping = function(collection, label, shape, expected_rownames = NULL, expected_colnames = NULL) { if (is.null(collection)) { return(collection) @@ -306,7 +265,7 @@ AbstractAnnData <- R6::R6Class("AbstractAnnData", # nolint )) } - if (.row_names_info(df) > 0) { + if (has_row_names(df)) { warning(wrap_message( "'", label, "' should not have any rownames, removing them from the data frame." )) diff --git a/R/HDF5AnnData.R b/R/HDF5AnnData.R index 9703716d..d4abfb7c 100644 --- a/R/HDF5AnnData.R +++ b/R/HDF5AnnData.R @@ -23,7 +23,13 @@ HDF5AnnData <- R6::R6Class("HDF5AnnData", # nolint read_h5ad_element(private$.h5obj, "/X") } else { # trackstatus: class=HDF5AnnData, feature=set_X, status=done - value <- private$.validate_matrix(value, "X") + value <- private$.validate_aligned_array( + value, + "X", + shape = c(self$n_obs(), self$n_vars()), + expected_rownames = rownames(self), + expected_colnames = colnames(self) + ) write_h5ad_element(value, private$.h5obj, "/X") } }, diff --git a/R/InMemoryAnnData.R b/R/InMemoryAnnData.R index 47b098e1..c5bd0c75 100644 --- a/R/InMemoryAnnData.R +++ b/R/InMemoryAnnData.R @@ -51,7 +51,13 @@ InMemoryAnnData <- R6::R6Class("InMemoryAnnData", # nolint private$.X } else { # trackstatus: class=InMemoryAnnData, feature=set_X, status=done - private$.X <- private$.validate_matrix(value, "X") + private$.X <- private$.validate_aligned_array( + value, + "X", + shape = c(self$n_obs(), self$n_vars()), + expected_rownames = rownames(self), + expected_colnames = colnames(self) + ) self } }, diff --git a/R/utils.R b/R/utils.R index 3e020d61..035449a4 100644 --- a/R/utils.R +++ b/R/utils.R @@ -2,3 +2,11 @@ wrap_message <- function(...) { txt <- paste0(..., collapse = "") paste(strwrap(txt, exdent = 2L), collapse = "\n") } + +has_row_names <- function(x) { + if (is.data.frame(x)) { + .row_names_info(x) > 0 + } else { + !is.null(dimnames(x)[[1]]) + } +} diff --git a/R/write_h5ad.R b/R/write_h5ad.R index df3e2c98..0457eb56 100644 --- a/R/write_h5ad.R +++ b/R/write_h5ad.R @@ -42,24 +42,25 @@ #' #' # Write a Seurat as a H5AD #' if (requireNamespace("SeuratObject", quietly = TRUE)) { -#' h5ad_file <- tempfile(fileext = ".h5ad") -#' counts <- matrix(1:15, 3L, 5L) -#' dimnames(counts) <- list( -#' letters[1:3], -#' LETTERS[1:5] -#' ) -#' gene.metadata <- data.frame( -#' row.names = LETTERS[1:5], -#' gene = 1:5 -#' ) -#' obj <- SeuratObject::CreateSeuratObject(counts, meta.data = gene.metadata) -#' cell.metadata <- data.frame( -#' row.names = letters[1:3], -#' cell = 1:3 -#' ) -#' obj <- SeuratObject::AddMetaData(obj, cell.metadata) -#' -#' write_h5ad(obj, h5ad_file) +#' # TODO: uncomment this code when the seurat converter is fixed +#' # h5ad_file <- tempfile(fileext = ".h5ad") +#' # counts <- matrix(1:15, 3L, 5L) +#' # dimnames(counts) <- list( +#' # letters[1:3], +#' # LETTERS[1:5] +#' # ) +#' # gene.metadata <- data.frame( +#' # row.names = LETTERS[1:5], +#' # gene = 1:5 +#' # ) +#' # obj <- SeuratObject::CreateSeuratObject(counts, meta.data = gene.metadata) +#' # cell.metadata <- data.frame( +#' # row.names = letters[1:3], +#' # cell = 1:3 +#' # ) +#' # obj <- SeuratObject::AddMetaData(obj, cell.metadata) +#' # +#' # write_h5ad(obj, h5ad_file) #' } write_h5ad <- function(object, path) { if (inherits(object, "SingleCellExperiment")) { diff --git a/man/AbstractAnnData.Rd b/man/AbstractAnnData.Rd index 8ac448b8..b31a62de 100644 --- a/man/AbstractAnnData.Rd +++ b/man/AbstractAnnData.Rd @@ -61,6 +61,10 @@ with all elements having the same number of rows and columns as \code{var}.} \item \href{#method-AbstractAnnData-obs_keys}{\code{AbstractAnnData$obs_keys()}} \item \href{#method-AbstractAnnData-var_keys}{\code{AbstractAnnData$var_keys()}} \item \href{#method-AbstractAnnData-layers_keys}{\code{AbstractAnnData$layers_keys()}} +\item \href{#method-AbstractAnnData-obsm_keys}{\code{AbstractAnnData$obsm_keys()}} +\item \href{#method-AbstractAnnData-varm_keys}{\code{AbstractAnnData$varm_keys()}} +\item \href{#method-AbstractAnnData-obsp_keys}{\code{AbstractAnnData$obsp_keys()}} +\item \href{#method-AbstractAnnData-varp_keys}{\code{AbstractAnnData$varp_keys()}} \item \href{#method-AbstractAnnData-to_SingleCellExperiment}{\code{AbstractAnnData$to_SingleCellExperiment()}} \item \href{#method-AbstractAnnData-to_Seurat}{\code{AbstractAnnData$to_Seurat()}} \item \href{#method-AbstractAnnData-to_InMemoryAnnData}{\code{AbstractAnnData$to_InMemoryAnnData()}} @@ -146,6 +150,46 @@ Keys (element names) of \code{layers}. \if{html}{\out{
}}\preformatted{AbstractAnnData$layers_keys()}\if{html}{\out{
}} } +} +\if{html}{\out{
}} +\if{html}{\out{}} +\if{latex}{\out{\hypertarget{method-AbstractAnnData-obsm_keys}{}}} +\subsection{Method \code{obsm_keys()}}{ +Keys (element names) of \code{obsm}. +\subsection{Usage}{ +\if{html}{\out{
}}\preformatted{AbstractAnnData$obsm_keys()}\if{html}{\out{
}} +} + +} +\if{html}{\out{
}} +\if{html}{\out{}} +\if{latex}{\out{\hypertarget{method-AbstractAnnData-varm_keys}{}}} +\subsection{Method \code{varm_keys()}}{ +Keys (element names) of \code{varm}. +\subsection{Usage}{ +\if{html}{\out{
}}\preformatted{AbstractAnnData$varm_keys()}\if{html}{\out{
}} +} + +} +\if{html}{\out{
}} +\if{html}{\out{}} +\if{latex}{\out{\hypertarget{method-AbstractAnnData-obsp_keys}{}}} +\subsection{Method \code{obsp_keys()}}{ +Keys (element names) of \code{obsp}. +\subsection{Usage}{ +\if{html}{\out{
}}\preformatted{AbstractAnnData$obsp_keys()}\if{html}{\out{
}} +} + +} +\if{html}{\out{
}} +\if{html}{\out{}} +\if{latex}{\out{\hypertarget{method-AbstractAnnData-varp_keys}{}}} +\subsection{Method \code{varp_keys()}}{ +Keys (element names) of \code{varp}. +\subsection{Usage}{ +\if{html}{\out{
}}\preformatted{AbstractAnnData$varp_keys()}\if{html}{\out{
}} +} + } \if{html}{\out{
}} \if{html}{\out{}} diff --git a/man/HDF5AnnData.Rd b/man/HDF5AnnData.Rd index 27c57934..2e977fd6 100644 --- a/man/HDF5AnnData.Rd +++ b/man/HDF5AnnData.Rd @@ -54,6 +54,8 @@ with all elements having the same number of rows and columns as \code{var}.} }} diff --git a/man/InMemoryAnnData.Rd b/man/InMemoryAnnData.Rd index 1a82e68e..f7894b1f 100644 --- a/man/InMemoryAnnData.Rd +++ b/man/InMemoryAnnData.Rd @@ -89,6 +89,8 @@ with all elements having the same number of rows and columns as \code{var}.}
  • anndataR::AbstractAnnData$n_obs()
  • anndataR::AbstractAnnData$n_vars()
  • anndataR::AbstractAnnData$obs_keys()
  • +
  • anndataR::AbstractAnnData$obsm_keys()
  • +
  • anndataR::AbstractAnnData$obsp_keys()
  • anndataR::AbstractAnnData$print()
  • anndataR::AbstractAnnData$shape()
  • anndataR::AbstractAnnData$to_HDF5AnnData()
  • @@ -96,6 +98,8 @@ with all elements having the same number of rows and columns as \code{var}.}
  • anndataR::AbstractAnnData$to_Seurat()
  • anndataR::AbstractAnnData$to_SingleCellExperiment()
  • anndataR::AbstractAnnData$var_keys()
  • +
  • anndataR::AbstractAnnData$varm_keys()
  • +
  • anndataR::AbstractAnnData$varp_keys()
  • }} diff --git a/man/anndataR-package.Rd b/man/anndataR-package.Rd index 75638374..5cc2b006 100644 --- a/man/anndataR-package.Rd +++ b/man/anndataR-package.Rd @@ -25,11 +25,14 @@ Authors: \item Luke Zappia \email{luke@lazappi.id.au} (\href{https://orcid.org/0000-0001-7744-8565}{ORCID}) (lazappi) \item Martin Morgan \email{mtmorgan.bioc@gmail.com} (\href{https://orcid.org/0000-0002-5874-8148}{ORCID}) (mtmorgan) \item Louise Deconinck \email{louise.deconinck@gmail.com} (\href{https://orcid.org/0000-0001-8100-6823}{ORCID}) (LouiseDck) + \item Brian Schilder \email{brian_schilder@alumni.brown.edu} (\href{https://orcid.org/0000-0001-5949-2191}{ORCID}) (bschilder) } Other contributors: \itemize{ \item Danila Bredikhin \email{danila.bredikhin@embl.de} (\href{https://orcid.org/0000-0001-8089-6983}{ORCID}) (gtca) [contributor] + \item Isaac Virshup (\href{https://orcid.org/0000-0002-1710-8945}{ORCID}) (ivirshup) [contributor] + \item Chananchida Sang-aram (\href{https://orcid.org/0000-0002-0922-0822}{ORCID}) (csangara) [contributor] } } diff --git a/man/write_h5ad.Rd b/man/write_h5ad.Rd index 9247ca6b..94efdb82 100644 --- a/man/write_h5ad.Rd +++ b/man/write_h5ad.Rd @@ -51,23 +51,24 @@ if (requireNamespace("SingleCellExperiment", quietly = TRUE)) { # Write a Seurat as a H5AD if (requireNamespace("SeuratObject", quietly = TRUE)) { - h5ad_file <- tempfile(fileext = ".h5ad") - counts <- matrix(1:15, 3L, 5L) - dimnames(counts) <- list( - letters[1:3], - LETTERS[1:5] - ) - gene.metadata <- data.frame( - row.names = LETTERS[1:5], - gene = 1:5 - ) - obj <- SeuratObject::CreateSeuratObject(counts, meta.data = gene.metadata) - cell.metadata <- data.frame( - row.names = letters[1:3], - cell = 1:3 - ) - obj <- SeuratObject::AddMetaData(obj, cell.metadata) - - write_h5ad(obj, h5ad_file) + # TODO: uncomment this code when the seurat converter is fixed + # h5ad_file <- tempfile(fileext = ".h5ad") + # counts <- matrix(1:15, 3L, 5L) + # dimnames(counts) <- list( + # letters[1:3], + # LETTERS[1:5] + # ) + # gene.metadata <- data.frame( + # row.names = LETTERS[1:5], + # gene = 1:5 + # ) + # obj <- SeuratObject::CreateSeuratObject(counts, meta.data = gene.metadata) + # cell.metadata <- data.frame( + # row.names = letters[1:3], + # cell = 1:3 + # ) + # obj <- SeuratObject::AddMetaData(obj, cell.metadata) + # + # write_h5ad(obj, h5ad_file) } } diff --git a/tests/testthat/test-HDF5-write.R b/tests/testthat/test-HDF5-write.R index 0bb8e566..9e5fd18b 100644 --- a/tests/testthat/test-HDF5-write.R +++ b/tests/testthat/test-HDF5-write.R @@ -169,6 +169,7 @@ test_that("writing H5AD from SingleCellExperiment works", { test_that("writing H5AD from Seurat works", { skip_if_not_installed("SeuratObject") + skip("while Seurat converter is failing") file <- withr::local_file("Seurat.h5ad") diff --git a/tests/testthat/test-InMemoryAnnData.R b/tests/testthat/test-InMemoryAnnData.R index 37524552..648dab08 100644 --- a/tests/testthat/test-InMemoryAnnData.R +++ b/tests/testthat/test-InMemoryAnnData.R @@ -52,65 +52,6 @@ test_that("AnnData() fails gracefully", { expect_error(AnnData(var = data.frame(x = 1:3))) }) -test_that("InMemoryAnnData$new produces a warning if rownames are found", { - # check X with rownames - x_with_rownames <- dummy$X - rownames(x_with_rownames) <- dummy$obs_names - - expect_warning({ - AnnData( - X = x_with_rownames, - obs = dummy$obs, - var = dummy$var, - obs_names = dummy$obs_names, - var_names = dummy$var_names - ) - }) - - # check X with obsnames - x_with_colnames <- dummy$X - colnames(x_with_colnames) <- dummy$var_names - - expect_warning({ - AnnData( - X = x_with_colnames, - obs = dummy$obs, - var = dummy$var, - obs_names = dummy$obs_names, - var_names = dummy$var_names - ) - }) - - # check obs with rownames - obs_with_rownames <- dummy$obs - rownames(obs_with_rownames) <- dummy$obs_names - - expect_warning({ - AnnData( - X = dummy$X, - obs = obs_with_rownames, - var = dummy$var, - obs_names = dummy$obs_names, - var_names = dummy$var_names - ) - }) - - # check var with rownames - var_with_rownames <- dummy$var - rownames(var_with_rownames) <- dummy$var_names - - expect_warning({ - AnnData( - X = dummy$X, - obs = dummy$obs, - var = var_with_rownames, - obs_names = dummy$obs_names, - var_names = dummy$var_names - ) - }) -}) - - # trackstatus: class=InMemoryAnnData, feature=test_get_layers, status=done test_that("'layers' works", { ## layers test helper function @@ -137,9 +78,6 @@ test_that("'layers' works", { ## must be a named list layers <- list(matrix(0, 3, 5)) expect_error(AnnData(obs_names = obs_names, var_names = var_names, layers = layers)) - ## non-trivial names - layers <- list(A = matrix(0, 3, 5), matrix(1, 3, 5)) - expect_error(AnnData(obs_names = obs_names, var_names = var_names, layers = layers)) ## matching dimensions layers <- list(A = matrix(0, 0, 0)) expect_error(AnnData(obs_names = obs_names, var_names = var_names, layers = layers)) diff --git a/tests/testthat/test-roundtrip.R b/tests/testthat/test-roundtrip.R deleted file mode 100644 index 48fba1d6..00000000 --- a/tests/testthat/test-roundtrip.R +++ /dev/null @@ -1,70 +0,0 @@ -library(testthat) - -h5ad_file <- tempfile(pattern = "hdf5_write_", fileext = ".h5ad") - -base_file <- system.file("extdata", "example.h5ad", package = "anndataR") - - -gen_adata <- function(type) { - library(Matrix) - N_OBS <- 10 - N_VAR <- 15 - obs_names <- paste0("obs_", 1:N_OBS) - var_names <- paste0("var_", 1:N_VAR) - adata <- AnnData( - X = rsparsematrix(N_OBS, N_VAR, 0.1), - obs_names = obs_names, - var_names = var_names, - layers = list( - dense = matrix(1:15, N_OBS, N_VAR), - sparse = rsparsematrix(N_OBS, N_VAR, 0.1) - ), - obsm = list( - dense = matrix(1:15, N_OBS, 5), - sparse = rsparsematrix(N_OBS, 5, 0.1) - ), - varm = list( - dense = matrix(1:15, N_VAR, 5), - sparse = rsparsematrix(N_VAR, 5, 0.1) - ), - obsp = list( - dense = matrix(1:15, N_OBS, N_OBS), - sparse = rsparsematrix(N_OBS, N_OBS, 0.1) - ), - varp = list( - dense = matrix(1:15, N_VAR, N_VAR), - sparse = rsparsematrix(N_VAR, N_VAR, 0.1) - ) - ) - if (type == "HDF5AnnData") { - tempfile(pattern = "hdf5_write_", fileext = ".h5ad") - write_h5ad(adata, h5ad_file) - read_h5ad(h5ad_file, to = type) - } else if (type == "InMemoryAnnData") { - adata - } else { - stop(paste0("Unknown type: ", type)) - } -} - -check_round_trip <- function(expected, type) { - h5ad_file <- tempfile(pattern = "hdf5_write_", fileext = ".h5ad") - write_h5ad(expected, h5ad_file) - actual <- read_h5ad(h5ad_file, to = type) - - expect_equal(actual, expected) -} - -check_round_trip_example <- function(type) { - check_round_trip(read_h5ad(base_file, to = type), type) -} - -for (typ in c("HDF5AnnData", "InMemoryAnnData")) { - test_that(paste("round trip w/ example data for", typ), { - check_round_trip_example(typ) - }) - test_that(paste("round trip w/ generated data for", typ), { - adata <- gen_adata(typ) - check_round_trip(adata, typ) - }) -}