Skip to content

Clean file when writing H5AD #156

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions R/HDF5AnnData.R
Original file line number Diff line number Diff line change
Expand Up @@ -445,9 +445,8 @@ HDF5AnnData <- R6::R6Class(
#' obs = data.frame(row.names = LETTERS[1:3], cell = 1:3),
#' var = data.frame(row.names = letters[1:5], gene = 1:5),
#' )
#' to_HDF5AnnData(ad, "test.h5ad")
#' # remove file
#' file.remove("test.h5ad")
#' h5ad_file <- tempfile(fileext = ".h5ad")
#' to_HDF5AnnData(ad, h5ad_file)
# nolint start: object_name_linter
to_HDF5AnnData <- function(
# nolint end: object_name_linter
Expand Down
19 changes: 18 additions & 1 deletion R/write_h5ad.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#' * `r+` opens an existing file for read/write.
#' * `w` creates a file, truncating any existing ones
#' * `w-`/`x` are synonyms creating a file and failing if it already exists.
#' @param overwrite Whether or not to overwrite `path` if it already exists
#'
#' @return `path` invisibly
#' @export
Expand Down Expand Up @@ -78,9 +79,24 @@ write_h5ad <- function(
object,
path,
compression = c("none", "gzip", "lzf"),
mode = c("w-", "r", "r+", "a", "w", "x")
mode = c("w-", "r", "r+", "a", "w", "x"),
overwrite = FALSE
) {
mode <- match.arg(mode)

if (file.exists(path) && !overwrite) {
cli_abort(
"{.path {path}} already exists. Set {.arg overwrite} to {.val {TRUE}} to overwrite this file."
)
}

if (file.exists(path) && overwrite) {
cli::cli_alert_warning(
"{.path {path}} already exists. It will be overwritten."
)
file.remove(path)
}

adata <-
if (inherits(object, "SingleCellExperiment")) {
from_SingleCellExperiment(
Expand All @@ -107,6 +123,7 @@ write_h5ad <- function(
} else {
cli_abort("Unable to write object of class {.cls {class(object)}}")
}

adata$close()
rm(adata)
gc()
Expand Down
5 changes: 4 additions & 1 deletion man/write_h5ad.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading