diff --git a/R/HDF5AnnData.R b/R/HDF5AnnData.R index 9d0e2127..2efcac64 100644 --- a/R/HDF5AnnData.R +++ b/R/HDF5AnnData.R @@ -395,9 +395,8 @@ HDF5AnnData <- R6::R6Class("HDF5AnnData", # nolint #' obs_names = LETTERS[1:3], #' var_names = letters[1:5] #' ) -#' to_HDF5AnnData(ad, "test.h5ad") -#' # remove file -#' file.remove("test.h5ad") +#' h5ad_file <- tempfile(fileext = ".h5ad") +#' to_HDF5AnnData(ad, h5ad_file) to_HDF5AnnData <- function(adata, file, compression = c("none", "gzip", "lzf")) { # nolint stopifnot( inherits(adata, "AbstractAnnData") diff --git a/R/write_h5ad.R b/R/write_h5ad.R index 438ea4ef..02ad911d 100644 --- a/R/write_h5ad.R +++ b/R/write_h5ad.R @@ -8,6 +8,7 @@ #' @param compression The compression algorithm to use when writing the #' HDF5 file. Can be one of `"none"`, `"gzip"` or `"lzf"`. Defaults to #' `"none"`. +#' @param overwrite Whether or not to overwrite `path` if it already exists #' #' @return `path` invisibly #' @export @@ -67,7 +68,13 @@ #' # h5ad_file <- tempfile(fileext = ".h5ad") #' # write_h5ad(obj, h5ad_file) #' } -write_h5ad <- function(object, path, compression = c("none", "gzip", "lzf")) { +write_h5ad <- function(object, path, compression = c("none", "gzip", "lzf"), + overwrite = FALSE) { + + if (file.exists(path) && !overwrite) { + stop("'", path, "' already exists, set `overwrite = TRUE` to overwrite this file") + } + if (inherits(object, "SingleCellExperiment")) { from_SingleCellExperiment( object, diff --git a/man/to_HDF5AnnData.Rd b/man/to_HDF5AnnData.Rd index e14aa376..eaa73c2e 100644 --- a/man/to_HDF5AnnData.Rd +++ b/man/to_HDF5AnnData.Rd @@ -35,7 +35,6 @@ ad <- AnnData( obs_names = LETTERS[1:3], var_names = letters[1:5] ) -to_HDF5AnnData(ad, "test.h5ad") -# remove file -file.remove("test.h5ad") +h5ad_file <- tempfile(fileext = ".h5ad") +to_HDF5AnnData(ad, h5ad_file) } diff --git a/man/write_h5ad.Rd b/man/write_h5ad.Rd index a88142ed..f63e5f5d 100644 --- a/man/write_h5ad.Rd +++ b/man/write_h5ad.Rd @@ -4,7 +4,12 @@ \alias{write_h5ad} \title{Write H5AD} \usage{ -write_h5ad(object, path, compression = c("none", "gzip", "lzf")) +write_h5ad( + object, + path, + compression = c("none", "gzip", "lzf"), + overwrite = FALSE +) } \arguments{ \item{object}{The object to write, either a "SingleCellExperiment" or a @@ -15,6 +20,8 @@ write_h5ad(object, path, compression = c("none", "gzip", "lzf")) \item{compression}{The compression algorithm to use when writing the HDF5 file. Can be one of \code{"none"}, \code{"gzip"} or \code{"lzf"}. Defaults to \code{"none"}.} + +\item{overwrite}{Whether or not to overwrite \code{path} if it already exists} } \value{ \code{path} invisibly