From b1b373d9c159a41dc85358c454070292d6654472 Mon Sep 17 00:00:00 2001 From: Luke Zappia Date: Thu, 17 Oct 2024 12:16:47 +0200 Subject: [PATCH] Style package --- NEWS.md | 1 + R/AnnData2SCE.R | 51 +++++++++++-------- R/SCE2AnnData.R | 33 ++++++------ R/basilisk.R | 5 +- R/read.R | 35 ++++++++----- R/reticulate.R | 8 +-- R/ui.R | 8 --- R/validation.R | 4 +- R/write.R | 19 ++++--- tests/testthat/test-read.R | 85 +++++++++++++++++++------------ tests/testthat/test-validation.R | 9 ++-- tests/testthat/test-write.R | 8 +-- tests/testthat/test-zzz-anndata.R | 35 ++++++++----- vignettes/zellkonverter.Rmd | 20 ++++---- 14 files changed, 182 insertions(+), 139 deletions(-) diff --git a/NEWS.md b/NEWS.md index a713b50..8a14e6a 100644 --- a/NEWS.md +++ b/NEWS.md @@ -5,6 +5,7 @@ * Fix correctly assign levels to factors in R reader with **anndata** v0.7 files (Fixes #122) * Add environment for **anndata** v0.10.9 * Avoid deprecation warning due to setting `dtype` when creating Python `AnnData` objects +* Standardise code styling using **{styler}** ## zellkonverter 1.15.3 (2024-10-04) diff --git a/R/AnnData2SCE.R b/R/AnnData2SCE.R index 697b570..0a765ad 100644 --- a/R/AnnData2SCE.R +++ b/R/AnnData2SCE.R @@ -30,8 +30,7 @@ #' #' If `skip_assays = TRUE`, empty sparse matrices are created for all assays, #' regardless of whether they might be convertible to an R format or not. -#' In both cases, the user is expected to fill in the assays on the R side, -#' see [`readH5AD()`] for an example. +#' In both cases, the user is expected to fill in the assays on the R side. #' #' We attempt to convert between items in the \linkS4class{SingleCellExperiment} #' [`metadata()`] slot and the `AnnData` `uns` slot. If an item cannot be @@ -98,12 +97,10 @@ NULL #' @importFrom methods selectMethod is #' @importFrom S4Vectors DataFrame make_zero_col_DFrame #' @importFrom reticulate import_builtins -AnnData2SCE <- function(adata, X_name = NULL, layers = TRUE, uns = TRUE, - var = TRUE, obs = TRUE, varm = TRUE, obsm = TRUE, - varp = TRUE, obsp = TRUE, raw = FALSE, - skip_assays = FALSE, hdf5_backed = TRUE, - verbose = NULL) { - +AnnData2SCE <- function( + adata, X_name = NULL, layers = TRUE, uns = TRUE, + var = TRUE, obs = TRUE, varm = TRUE, obsm = TRUE, varp = TRUE, obsp = TRUE, + raw = FALSE, skip_assays = FALSE, hdf5_backed = TRUE, verbose = NULL) { # In case the user accidentally passes an AnnDataR6 object if (is(adata, "AnnDataR6")) { .ui_warn(paste( @@ -254,17 +251,24 @@ AnnData2SCE <- function(adata, X_name = NULL, layers = TRUE, uns = TRUE, } reddim_list <- .convert_anndata_slot( - adata, "obsm", py_to_r(adata$obsm_keys()), "reducedDims", select = obsm + adata, "obsm", + py_to_r(adata$obsm_keys()), + "reducedDims", + select = obsm ) reddim_list <- lapply(reddim_list, as.matrix) varp_list <- .convert_anndata_slot( - adata, "varp", py_builtins$list(adata$varp$keys()), "rowPairs", + adata, "varp", + py_builtins$list(adata$varp$keys()), + "rowPairs", select = varp ) obsp_list <- .convert_anndata_slot( - adata, "obsp", py_builtins$list(adata$obsp$keys()), "colPairs", + adata, "obsp", + py_builtins$list(adata$obsp$keys()), + "colPairs", select = obsp ) @@ -312,12 +316,19 @@ AnnData2SCE <- function(adata, X_name = NULL, layers = TRUE, uns = TRUE, ) colnames(raw_x$mat) <- colnames(output) - raw_rowData <- .convert_anndata_df(py_to_r(adata$raw$var), "raw var", - "raw rowData", select = TRUE) + raw_rowData <- .convert_anndata_df( + py_to_r(adata$raw$var), + "raw var", + "raw rowData", + select = TRUE + ) raw_varm_list <- .convert_anndata_slot( - adata, "varm", py_builtins$list(adata$raw$varm$keys()), - "raw rowData$varm", select = TRUE, raw = TRUE + adata, + "varm", + py_builtins$list(adata$raw$varm$keys()), + "raw rowData$varm", + select = TRUE, raw = TRUE ) if (length(raw_varm_list) > 0) { @@ -342,7 +353,7 @@ AnnData2SCE <- function(adata, X_name = NULL, layers = TRUE, uns = TRUE, #' @importFrom Matrix t .extract_or_skip_assay <- function(skip_assays, hdf5_backed, dims, mat, - filepath, name) { + filepath, name) { skipped <- FALSE if (isTRUE(skip_assays)) { @@ -411,8 +422,7 @@ AnnData2SCE <- function(adata, X_name = NULL, layers = TRUE, uns = TRUE, } .convert_anndata_slot <- function(adata, slot_name, slot_keys, to_name, - select = TRUE, raw = FALSE) { - + select = TRUE, raw = FALSE) { verbose <- parent.frame()$verbose if (isFALSE(select)) { @@ -456,7 +466,7 @@ AnnData2SCE <- function(adata, X_name = NULL, layers = TRUE, uns = TRUE, } .convert_anndata_list <- function(adata_list, parent, - keys = names(adata_list)) { + keys = names(adata_list)) { py_builtins <- import_builtins() converted_list <- list() @@ -578,7 +588,6 @@ AnnData2SCE <- function(adata, X_name = NULL, layers = TRUE, uns = TRUE, } .convert_anndata_df <- function(adata_df, slot_name, to_name, select = TRUE) { - verbose <- parent.frame()$verbose if (isFALSE(select)) { @@ -600,7 +609,7 @@ AnnData2SCE <- function(adata, X_name = NULL, layers = TRUE, uns = TRUE, select <- setdiff(select, missing) } } else { - select <- colnames(adata_df) + select <- colnames(adata_df) } df <- adata_df[, select, drop = FALSE] diff --git a/R/SCE2AnnData.R b/R/SCE2AnnData.R index 9479b5c..0bfaa6c 100644 --- a/R/SCE2AnnData.R +++ b/R/SCE2AnnData.R @@ -16,10 +16,11 @@ #' @importFrom utils capture.output #' @importFrom S4Vectors metadata make_zero_col_DFrame #' @importFrom reticulate import r_to_py py_to_r -SCE2AnnData <- function(sce, X_name = NULL, assays = TRUE, colData = TRUE, - rowData = TRUE, varm = TRUE, reducedDims = TRUE, - metadata = TRUE, colPairs = TRUE, rowPairs = TRUE, - skip_assays = FALSE, verbose = NULL) { +SCE2AnnData <- function( + sce, X_name = NULL, assays = TRUE, colData = TRUE, + rowData = TRUE, varm = TRUE, reducedDims = TRUE, + metadata = TRUE, colPairs = TRUE, rowPairs = TRUE, + skip_assays = FALSE, verbose = NULL) { anndata <- import("anndata") # Create a list to store parts of the AnnData @@ -132,7 +133,6 @@ SCE2AnnData <- function(sce, X_name = NULL, assays = TRUE, colData = TRUE, adata_list$varm <- varm_list cli::cli_progress_done() } - } else { .ui_info("{.field rowData$varm} is empty and was skipped") } @@ -142,7 +142,8 @@ SCE2AnnData <- function(sce, X_name = NULL, assays = TRUE, colData = TRUE, } else { sce <- .store_non_atomic(sce, "rowData") adata_list$var <- .convert_sce_df(rowData(sce), "rowData", "var", - select = rowData) + select = rowData + ) } if (is.null(adata_list$var)) { @@ -168,8 +169,9 @@ SCE2AnnData <- function(sce, X_name = NULL, assays = TRUE, colData = TRUE, ) red_dims <- as.list(reducedDims(sce)) if (is.character(reducedDims)) { - reducedDims <- .check_select(reducedDims, "reducedDims", - names(red_dims)) + reducedDims <- .check_select( + reducedDims, "reducedDims", names(red_dims) + ) red_dims <- red_dims[reducedDims] } red_dims <- lapply(red_dims, .makeNumpyFriendly, transpose = FALSE) @@ -278,10 +280,9 @@ SCE2AnnData <- function(sce, X_name = NULL, assays = TRUE, colData = TRUE, } .store_non_atomic <- function(sce, slot = c("rowData", "colData")) { - slot <- match.arg(slot) - df <- switch (slot, + df <- switch(slot, rowData = rowData(sce), colData = colData(sce) ) @@ -316,15 +317,14 @@ SCE2AnnData <- function(sce, X_name = NULL, assays = TRUE, colData = TRUE, if (slot == "rowData") { rowData(sce) <- df - } else ( + } else { colData(sce) <- df - ) + } return(sce) } .check_select <- function(select, slot_name, options) { - verbose <- parent.frame()$verbose if (!all(select %in% options)) { @@ -341,7 +341,6 @@ SCE2AnnData <- function(sce, X_name = NULL, assays = TRUE, colData = TRUE, } .convert_sce_df <- function(sce_df, slot_name, to_name, select = TRUE) { - if (ncol(sce_df) == 0) { .ui_info("{.field {slot_name}} is empty and was skipped") return(NULL) @@ -352,7 +351,6 @@ SCE2AnnData <- function(sce, X_name = NULL, assays = TRUE, colData = TRUE, msg_done = "{.field {slot_name}} converted to {.field {to_name}}" ) if (is.character(select)) { - select <- .check_select(select, slot_name, colnames(sce_df)) if (length(select) == 0) { @@ -378,8 +376,7 @@ SCE2AnnData <- function(sce, X_name = NULL, assays = TRUE, colData = TRUE, } .convert_sce_pairs <- function(sce, slot_name = c("rowPairs", "colPairs"), - to_name, select) { - + to_name, select) { slot_name <- match.arg(slot_name) @@ -388,7 +385,7 @@ SCE2AnnData <- function(sce, X_name = NULL, assays = TRUE, colData = TRUE, return(NULL) } - pairs <- switch (slot_name, + pairs <- switch(slot_name, rowPairs = as.list(rowPairs(sce, asSparse = TRUE)), colPairs = as.list(colPairs(sce, asSparse = TRUE)) ) diff --git a/R/basilisk.R b/R/basilisk.R index 8c84988..d385637 100644 --- a/R/basilisk.R +++ b/R/basilisk.R @@ -75,11 +75,9 @@ NULL #' #' @export AnnDataDependencies <- function(version = .AnnDataVersions) { - version <- match.arg(version) - switch ( - version, + switch(version, "0.7.6" = c( "anndata==0.7.6", "h5py==3.2.1", @@ -160,7 +158,6 @@ AnnDataDependencies <- function(version = .AnnDataVersions) { #' @include ui.R #' @export zellkonverterAnnDataEnv <- function(version = .AnnDataVersions) { - version <- match.arg(version) basilisk::BasiliskEnvironment( diff --git a/R/read.R b/R/read.R index 09f9827..928bd38 100644 --- a/R/read.R +++ b/R/read.R @@ -57,8 +57,8 @@ #' @importFrom basilisk basiliskRun #' @importFrom methods slot readH5AD <- function(file, X_name = NULL, use_hdf5 = FALSE, - reader = c("python", "R"), version = NULL, verbose = NULL, - ...) { + reader = c("python", "R"), version = NULL, verbose = NULL, + ...) { file <- path.expand(file) reader <- match.arg(reader) @@ -77,7 +77,6 @@ readH5AD <- function(file, X_name = NULL, use_hdf5 = FALSE, verbose = verbose, ... ) - } else if (reader == "R") { sce <- .native_reader(file, backed = use_hdf5, verbose = verbose) } @@ -95,8 +94,11 @@ readH5AD <- function(file, X_name = NULL, use_hdf5 = FALSE, ) adata <- anndata$read_h5ad(file, backed = if (backed) "r" else FALSE) cli::cli_progress_done() - AnnData2SCE(adata, X_name = X_name, hdf5_backed = backed, verbose = verbose, - ...) + + AnnData2SCE( + adata, + X_name = X_name, hdf5_backed = backed, verbose = verbose, ... + ) } #' @importFrom S4Vectors I DataFrame wmsg @@ -230,7 +232,8 @@ readH5AD <- function(file, X_name = NULL, use_hdf5 = FALSE, { uns <- rhdf5::h5read(file, "uns") uns <- .convert_element( - uns, "uns", file, recursive=TRUE + uns, "uns", file, + recursive = TRUE ) metadata(sce) <- uns }, @@ -244,7 +247,7 @@ readH5AD <- function(file, X_name = NULL, use_hdf5 = FALSE, } if (("X_name" %in% names(metadata(sce))) && ("X" %in% names(contents))) { - stopifnot(names(assays(sce))[1] == "X") #should be true b/c X is read 1st + stopifnot(names(assays(sce))[1] == "X") # should be true b/c X is read 1st names(assays(sce))[1] <- metadata(sce)[["X_name"]] metadata(sce)[["X_name"]] <- NULL } @@ -263,14 +266,14 @@ readH5AD <- function(file, X_name = NULL, use_hdf5 = FALSE, x <- list() } x[[series[1]]] <- value + x } contents <- list() for (i in seq_len(nrow(manifest))) { components <- c( - strsplit(manifest[i, "group"], "/")[[1]], - manifest[i, "name"] + strsplit(manifest[i, "group"], "/")[[1]], manifest[i, "name"] ) if (components[1] == "") { components <- components[-1] @@ -292,6 +295,7 @@ readH5AD <- function(file, X_name = NULL, use_hdf5 = FALSE, } else { mat <- HDF5Array::H5SparseMatrix(file, path) } + if (!backed) { if (DelayedArray::is_sparse(mat)) { mat <- as(mat, "sparseMatrix") @@ -299,10 +303,11 @@ readH5AD <- function(file, X_name = NULL, use_hdf5 = FALSE, mat <- as.matrix(mat) } } + mat } -.convert_element <- function(obj, path, file, recursive=FALSE) { +.convert_element <- function(obj, path, file, recursive = FALSE) { element_attrs <- rhdf5::h5readAttributes(file, path) # Convert categorical element for AnnData v0.8+ @@ -314,7 +319,7 @@ readH5AD <- function(file, X_name = NULL, use_hdf5 = FALSE, ord <- as.logical(element_attrs[["ordered"]]) - obj <- factor(levels[codes], levels=levels, ordered=ord) + obj <- factor(levels[codes], levels = levels, ordered = ord) return(obj) } @@ -331,7 +336,8 @@ readH5AD <- function(file, X_name = NULL, use_hdf5 = FALSE, obj[[k]] <- rhdf5::h5read(file, file.path(path, k)) obj[[k]] <- .convert_element( obj[[k]], file.path(path, k), - file, recursive=TRUE + file, + recursive = TRUE ) } } @@ -352,7 +358,8 @@ readH5AD <- function(file, X_name = NULL, use_hdf5 = FALSE, vec <- .convert_element( vec, file.path(path, col_name), - file, recursive=FALSE + file, + recursive = FALSE ) if (!is.factor(vec)) { @@ -406,7 +413,7 @@ readH5AD <- function(file, X_name = NULL, use_hdf5 = FALSE, .read_dim_mats <- function(file, path, fields) { all.contents <- list() for (field in names(fields)) { - # because everything's transposed. + # Because everything's transposed. all.contents[[field]] <- t(rhdf5::h5read(file, file.path(path, field))) } all.contents diff --git a/R/reticulate.R b/R/reticulate.R index 737e328..b0b4f4b 100644 --- a/R/reticulate.R +++ b/R/reticulate.R @@ -36,9 +36,11 @@ py_to_r.numpy.ndarray <- function(x) { { x <- pandas$DataFrame(x)$to_numpy() py_to_r(x) - }, error = function(err) { + }, + error = function(err) { stop("Failed to convert recarray with error: ", err$message, - call. = FALSE) + call. = FALSE + ) } ) return(out) @@ -61,7 +63,7 @@ py_to_r.pandas.core.arrays.masked.BaseMaskedArray <- function(x) { } else if (is(x, "pandas.core.arrays.floating.FloatingArray")) { dtype <- "float" fill <- 0.0 - } else if (is(x , "pandas.core.arrays.string_.StringArray")) { + } else if (is(x, "pandas.core.arrays.string_.StringArray")) { dtype <- "str" fill <- "" } else { diff --git a/R/ui.R b/R/ui.R index 4600b87..5e85aaf 100644 --- a/R/ui.R +++ b/R/ui.R @@ -27,7 +27,6 @@ setZellkonverterVerbose <- function(verbose = TRUE) { } .get_verbose <- function(envir) { - verbose <- envir$verbose if (is.null(verbose)) { @@ -38,7 +37,6 @@ setZellkonverterVerbose <- function(verbose = TRUE) { } .ui_rule <- function(msg, ...) { - envir <- parent.frame() if (.get_verbose(envir)) { @@ -47,7 +45,6 @@ setZellkonverterVerbose <- function(verbose = TRUE) { } .ui_info <- function(msg, ...) { - envir <- parent.frame() if (.get_verbose(envir)) { @@ -56,7 +53,6 @@ setZellkonverterVerbose <- function(verbose = TRUE) { } .ui_warn <- function(msg, warn = TRUE, ...) { - envir <- parent.frame() msg <- cli::format_message(msg, .envir = envir) @@ -71,7 +67,6 @@ setZellkonverterVerbose <- function(verbose = TRUE) { } .ui_step <- function(msg, ...) { - envir <- parent.frame() if (.get_verbose(envir)) { @@ -80,7 +75,6 @@ setZellkonverterVerbose <- function(verbose = TRUE) { } .ui_process <- function(msg, ...) { - envir <- parent.frame() if (.get_verbose(envir)) { @@ -89,7 +83,6 @@ setZellkonverterVerbose <- function(verbose = TRUE) { } .ui_process_done <- function(...) { - envir <- parent.frame() if (.get_verbose(envir)) { @@ -98,7 +91,6 @@ setZellkonverterVerbose <- function(verbose = TRUE) { } .trim_path <- function(path, n = 40) { - path_split <- .split_path(path) for (level in seq_along(path_split)) { diff --git a/R/validation.R b/R/validation.R index cc7746e..c318932 100644 --- a/R/validation.R +++ b/R/validation.R @@ -38,7 +38,6 @@ #' #' @author Luke Zappia validateH5ADSCE <- function(sce, names, missing) { - if ("varm" %in% colnames(SummarizedExperiment::rowData(sce))) { varm <- SummarizedExperiment::rowData(sce)$varm SummarizedExperiment::rowData(sce)$varm <- NULL @@ -131,8 +130,7 @@ validateH5ADSCE <- function(sce, names, missing) { } .names_validator <- function(label, actual_names, correct_names, - missing_names) { - + missing_names) { if (!is.null(correct_names)) { testthat::expect_identical( actual_names, diff --git a/R/write.R b/R/write.R index 644327a..42e5dcd 100644 --- a/R/write.R +++ b/R/write.R @@ -73,9 +73,8 @@ #' @importFrom Matrix sparseMatrix #' @importFrom DelayedArray is_sparse writeH5AD <- function(sce, file, X_name = NULL, skip_assays = FALSE, - compression = c("none", "gzip", "lzf"), version = NULL, - verbose = NULL, ...) { - + compression = c("none", "gzip", "lzf"), version = NULL, + verbose = NULL, ...) { compression <- match.arg(compression) if (compression == "none") { @@ -124,7 +123,8 @@ writeH5AD <- function(sce, file, X_name = NULL, skip_assays = FALSE, if (!is_sparse(mat)) { HDF5Array::writeHDF5Array( - mat, filepath = file, name = curp, with.dimnames = FALSE + mat, + filepath = file, name = curp, with.dimnames = FALSE ) } else { .write_CSR_matrix(file, name = curp, mat = mat) @@ -136,10 +136,13 @@ writeH5AD <- function(sce, file, X_name = NULL, skip_assays = FALSE, } #' @importFrom reticulate import -.H5ADwriter <- function(sce, file, X_name, skip_assays, compression, - verbose = NULL, ...) { - adata <- SCE2AnnData(sce, X_name = X_name, skip_assays = skip_assays, - verbose = verbose, ...) +.H5ADwriter <- function( + sce, file, X_name, skip_assays, compression, + verbose = NULL, ...) { + adata <- SCE2AnnData( + sce, + X_name = X_name, skip_assays = skip_assays, verbose = verbose, ... + ) .ui_step( "Writing {.file { .trim_path(file)} }", msg_done = "Wrote {.file { .trim_path(file)} }", diff --git a/tests/testthat/test-read.R b/tests/testthat/test-read.R index 6bd75fa..381a445 100644 --- a/tests/testthat/test-read.R +++ b/tests/testthat/test-read.R @@ -16,8 +16,10 @@ test_that("Reading example H5AD works", { names <- list( assays = c("X", "counts"), colData = "louvain", - rowData = c("n_counts", "highly_variable", "means", "dispersions", - "dispersions_norm"), + rowData = c( + "n_counts", "highly_variable", "means", "dispersions", + "dispersions_norm" + ), metadata = c("louvain", "neighbors", "pca", "rank_genes_groups", "umap"), redDim = c("X_pca", "X_umap"), varm = "PCs", @@ -132,7 +134,7 @@ test_that("Reading H5AD works with native reader", { test_that("Reading v0.8 H5AD works with native reader", { sce_py <- readH5AD(file_v08) - sce_r <- readH5AD(file_v08, reader="R") + sce_r <- readH5AD(file_v08, reader = "R") expect_identical(rownames(sce_py), rownames(sce_r)) expect_identical(colnames(sce_py), colnames(sce_r)) @@ -146,8 +148,10 @@ test_that("Reading v0.8 H5AD works with native reader", { expect_identical(assays(sce_py), assays(sce_r)) # check the easy metadata columns - for (key in c("dummy_category", "dummy_int", "dummy_int2", "highlight", - "iroot")) { + for (key in c( + "dummy_category", "dummy_int", "dummy_int2", "highlight", + "iroot" + )) { expect_equal(metadata(sce_py)[[key]], metadata(sce_r)[[key]]) } @@ -158,8 +162,10 @@ test_that("Reading v0.8 H5AD works with native reader", { }) test_that("Skipping slot conversion works", { - sce <- readH5AD(file, layers = FALSE, uns = FALSE, var = FALSE, obs = FALSE, - varm = FALSE, obsm = FALSE, varp = FALSE, obsp = FALSE) + sce <- readH5AD(file, + layers = FALSE, uns = FALSE, var = FALSE, obs = FALSE, + varm = FALSE, obsm = FALSE, varp = FALSE, obsp = FALSE + ) expect_identical(assayNames(sce), "X") expect_identical(metadata(sce), list()) @@ -194,21 +200,29 @@ test_that("Conversion of raw works", { names <- list( assays = c("X"), - colData = c("n_genes", "n_genes_by_counts", "total_counts", - "total_counts_mt", "pct_counts_mt", "leiden"), - rowData = c("gene_ids", "n_cells", "mt", "n_cells_by_counts", - "mean_counts", "pct_dropout_by_counts", "total_counts", - "highly_variable", "means", "dispersions", - "dispersions_norm", "mean", "std"), - metadata = c("hvg", "leiden", "neighbors", "pca", "rank_genes_groups", - "umap"), + colData = c( + "n_genes", "n_genes_by_counts", "total_counts", + "total_counts_mt", "pct_counts_mt", "leiden" + ), + rowData = c( + "gene_ids", "n_cells", "mt", "n_cells_by_counts", + "mean_counts", "pct_dropout_by_counts", "total_counts", + "highly_variable", "means", "dispersions", + "dispersions_norm", "mean", "std" + ), + metadata = c( + "hvg", "leiden", "neighbors", "pca", "rank_genes_groups", + "umap" + ), redDim = c("X_pca", "X_umap"), varm = c("PCs"), colPairs = c("connectivities", "distances"), - raw_rowData = c("gene_ids", "n_cells", "mt", "n_cells_by_counts", - "mean_counts", "pct_dropout_by_counts", "total_counts", - "highly_variable", "means", "dispersions", - "dispersions_norm") + raw_rowData = c( + "gene_ids", "n_cells", "mt", "n_cells_by_counts", + "mean_counts", "pct_dropout_by_counts", "total_counts", + "highly_variable", "means", "dispersions", + "dispersions_norm" + ) ) missing <- list() @@ -228,25 +242,32 @@ test_that("Conversion of raw works with use_hdf5 = TRUE", { names <- list( assays = c("X"), - colData = c("n_genes", "n_genes_by_counts", "total_counts", - "total_counts_mt", "pct_counts_mt", "leiden"), - rowData = c("gene_ids", "n_cells", "mt", "n_cells_by_counts", - "mean_counts", "pct_dropout_by_counts", "total_counts", - "highly_variable", "means", "dispersions", - "dispersions_norm", "mean", "std"), - metadata = c("hvg", "leiden", "neighbors", "pca", "rank_genes_groups", - "umap"), + colData = c( + "n_genes", "n_genes_by_counts", "total_counts", + "total_counts_mt", "pct_counts_mt", "leiden" + ), + rowData = c( + "gene_ids", "n_cells", "mt", "n_cells_by_counts", + "mean_counts", "pct_dropout_by_counts", "total_counts", + "highly_variable", "means", "dispersions", + "dispersions_norm", "mean", "std" + ), + metadata = c( + "hvg", "leiden", "neighbors", "pca", "rank_genes_groups", + "umap" + ), redDim = c("X_pca", "X_umap"), varm = c("PCs"), colPairs = c("connectivities", "distances"), - raw_rowData = c("gene_ids", "n_cells", "mt", "n_cells_by_counts", - "mean_counts", "pct_dropout_by_counts", "total_counts", - "highly_variable", "means", "dispersions", - "dispersions_norm") + raw_rowData = c( + "gene_ids", "n_cells", "mt", "n_cells_by_counts", + "mean_counts", "pct_dropout_by_counts", "total_counts", + "highly_variable", "means", "dispersions", + "dispersions_norm" + ) ) missing <- list() validateH5ADSCE(sce, names, missing) }) - diff --git a/tests/testthat/test-validation.R b/tests/testthat/test-validation.R index ae09067..dcbf4fd 100644 --- a/tests/testthat/test-validation.R +++ b/tests/testthat/test-validation.R @@ -1,12 +1,15 @@ file <- system.file("extdata", "example_anndata.h5ad", - package = "zellkonverter") + package = "zellkonverter" +) sce <- readH5AD(file) names <- list( assays = c("X", "counts"), colData = "louvain", - rowData = c("n_counts", "highly_variable", "means", "dispersions", - "dispersions_norm"), + rowData = c( + "n_counts", "highly_variable", "means", "dispersions", + "dispersions_norm" + ), metadata = c("louvain", "neighbors", "pca", "rank_genes_groups", "umap"), redDim = c("X_pca", "X_umap"), varm = "PCs", diff --git a/tests/testthat/test-write.R b/tests/testthat/test-write.R index 009bab7..921e76e 100644 --- a/tests/testthat/test-write.R +++ b/tests/testthat/test-write.R @@ -377,9 +377,11 @@ test_that("writeH5AD works with lzf compression", { test_that("Skipping slot conversion works", { temp <- tempfile(fileext = ".h5ad") - writeH5AD(sce, temp, assays = FALSE, colData = FALSE, rowData = FALSE, - varm = FALSE, reducedDims = FALSE, metadata = FALSE, - colPairs = FALSE, rowPairs = FALSE) + writeH5AD(sce, temp, + assays = FALSE, colData = FALSE, rowData = FALSE, + varm = FALSE, reducedDims = FALSE, metadata = FALSE, + colPairs = FALSE, rowPairs = FALSE + ) out <- readH5AD(temp, X_name = "X") diff --git a/tests/testthat/test-zzz-anndata.R b/tests/testthat/test-zzz-anndata.R index b4ae20d..e03388a 100644 --- a/tests/testthat/test-zzz-anndata.R +++ b/tests/testthat/test-zzz-anndata.R @@ -9,7 +9,8 @@ test_that("Reading is compatible with R anndata", { withr::with_package("anndata", { file <- system.file("extdata", "krumsiek11.h5ad", - package = "zellkonverter") + package = "zellkonverter" + ) sce <- readH5AD(file) expect_s4_class(sce, "SingleCellExperiment") @@ -25,21 +26,29 @@ test_that("Reading is compatible with R anndata", { names <- list( assays = c("X"), - colData = c("n_genes", "n_genes_by_counts", "total_counts", - "total_counts_mt", "pct_counts_mt", "leiden"), - rowData = c("gene_ids", "n_cells", "mt", "n_cells_by_counts", - "mean_counts", "pct_dropout_by_counts", "total_counts", - "highly_variable", "means", "dispersions", - "dispersions_norm", "mean", "std"), - raw_rowData = c("gene_ids", "n_cells", "mt", "n_cells_by_counts", - "mean_counts", "pct_dropout_by_counts", - "total_counts", "highly_variable", "means", - "dispersions", "dispersions_norm"), + colData = c( + "n_genes", "n_genes_by_counts", "total_counts", + "total_counts_mt", "pct_counts_mt", "leiden" + ), + rowData = c( + "gene_ids", "n_cells", "mt", "n_cells_by_counts", + "mean_counts", "pct_dropout_by_counts", "total_counts", + "highly_variable", "means", "dispersions", + "dispersions_norm", "mean", "std" + ), + raw_rowData = c( + "gene_ids", "n_cells", "mt", "n_cells_by_counts", + "mean_counts", "pct_dropout_by_counts", + "total_counts", "highly_variable", "means", + "dispersions", "dispersions_norm" + ), redDim = c("X_pca", "X_umap"), varm = c("PCs"), colPairs = c("connectivities", "distances"), - metadata = c("hvg", "leiden", "neighbors", "pca", - "rank_genes_groups", "umap") + metadata = c( + "hvg", "leiden", "neighbors", "pca", + "rank_genes_groups", "umap" + ) ) missing <- list() diff --git a/vignettes/zellkonverter.Rmd b/vignettes/zellkonverter.Rmd index 14d5353..a7809d6 100644 --- a/vignettes/zellkonverter.Rmd +++ b/vignettes/zellkonverter.Rmd @@ -42,8 +42,10 @@ H5AD file. This can be manipulated in the usual way as described in the library(zellkonverter) # Obtaining an example H5AD file. -example_h5ad <- system.file("extdata", "krumsiek11.h5ad", - package = "zellkonverter") +example_h5ad <- system.file( + "extdata", "krumsiek11.h5ad", + package = "zellkonverter" +) readH5AD(example_h5ad) ``` @@ -78,14 +80,14 @@ library(scRNAseq) seger <- SegerstolpePancreasData() roundtrip <- basiliskRun(fun = function(sce) { - # Convert SCE to AnnData: - adata <- SCE2AnnData(sce) + # Convert SCE to AnnData: + adata <- SCE2AnnData(sce) - # Maybe do some work in Python on 'adata': - # BLAH BLAH BLAH + # Maybe do some work in Python on 'adata': + # BLAH BLAH BLAH - # Convert back to an SCE: - AnnData2SCE(adata) + # Convert back to an SCE: + AnnData2SCE(adata) }, env = zellkonverterAnnDataEnv(), sce = seger) ``` @@ -119,7 +121,7 @@ If you would like to see progress messages for all functions by default you can turn this on using the `setZellkonverterVerbose()` function. ```{r verbose-set, eval = FALSE} -# This is not run here +# This is not run here setZellkonverterVerbose(TRUE) ```