diff --git a/R/read_h5ad.R b/R/read_h5ad.R new file mode 100644 index 00000000..1d133930 --- /dev/null +++ b/R/read_h5ad.R @@ -0,0 +1,33 @@ +#' Read H5AD +#' +#' Read data from a H5AD file +#' +#' @param path Path to the H5AD file to read +#' @param to The type of object to return. Must be one of: "SingleCellExperiment", +#' "Seurat", "HDF5AnnData", "InMemoryAnnData" +#' +#' @return The object specified by `to` +#' @export +#' +#' @examples +#' h5ad_file <- system.file("extdata", "example.h5ad", package = "anndataR") +#' # Read the H5AD as a SingleCellExperiment object +#' if (requireNamespace("SingleCellExperiment", quietly = TRUE)) { +#' sce <- read_h5ad(h5ad_file, to = "SingleCellExperiment") +#' } +#' # Read the H5AD as a Seurat object +#' if (requireNamespace("SeuratObject", quietly = TRUE)) { +#' seurat <- read_h5ad(h5ad_file, to = "Seurat") +#' } +read_h5ad <- function(path, to = c("SingleCellExperiment", "Seurat", "HDF5AnnData", "InMemoryAnnData")) { + to <- match.arg(to) + + adata <- HDF5AnnData$new(path) + + switch(to, + "SingleCellExperiment" = to_SingleCellExperiment(adata), + "Seurat" = to_Seurat(adata), + "HDF5AnnData" = adata, + "InMemoryAnnData" = to_InMemoryAnnData(adata) + ) +} \ No newline at end of file diff --git a/R/HDF5-read.R b/R/read_h5ad_helpers.R similarity index 92% rename from R/HDF5-read.R rename to R/read_h5ad_helpers.R index 35584fc0..0cdcaa11 100644 --- a/R/HDF5-read.R +++ b/R/read_h5ad_helpers.R @@ -430,37 +430,3 @@ read_h5ad_collection <- function(file, name, column_order) { } columns } - -#' Read H5AD -#' -#' Read data from a H5AD file -#' -#' @param path Path to the H5AD file to read -#' @param to The type of object to return. Must be one of: "SingleCellExperiment", -#' "Seurat", "HDF5AnnData", "InMemoryAnnData" -#' -#' @return The object specified by `to` -#' @export -#' -#' @examples -#' h5ad_file <- system.file("extdata", "example.h5ad", package = "anndataR") -#' # Read the H5AD as a SingleCellExperiment object -#' if (requireNamespace("SingleCellExperiment", quietly = TRUE)) { -#' sce <- read_h5ad(h5ad_file, to = "SingleCellExperiment") -#' } -#' # Read the H5AD as a Seurat object -#' if (requireNamespace("SeuratObject", quietly = TRUE)) { -#' seurat <- read_h5ad(h5ad_file, to = "Seurat") -#' } -read_h5ad <- function(path, to = c("SingleCellExperiment", "Seurat", "HDF5AnnData", "InMemoryAnnData")) { - to <- match.arg(to) - - adata <- HDF5AnnData$new(path) - - switch(to, - "SingleCellExperiment" = to_SingleCellExperiment(adata), - "Seurat" = to_Seurat(adata), - "HDF5AnnData" = adata, - "InMemoryAnnData" = to_InMemoryAnnData(adata) - ) -} diff --git a/R/write_h5ad.R b/R/write_h5ad.R new file mode 100644 index 00000000..efd95668 --- /dev/null +++ b/R/write_h5ad.R @@ -0,0 +1,83 @@ +#' Write H5AD +#' +#' Write an H5AD file +#' +#' @param object The object to write, either a "SingleCellExperiment" or a +#' "Seurat" object +#' @param path Path of the file to write to +#' +#' @return `path` invisibly +#' @export +#' +#' @examples +#' adata <- AnnData( +#' X = matrix(1:15, 3L, 5L), +#' layers = list( +#' A = matrix(15:1, 3L, 5L), +#' B = matrix(letters[1:15], 3L, 5L) +#' ), +#' obs = data.frame(cell = 1:3), +#' var = data.frame(gene = 1:5), +#' obs_names = LETTERS[1:3], +#' var_names = letters[1:5] +#' ) +#' h5ad_file <- tempfile(fileext = ".h5ad") +#' write_h5ad(adata, h5ad_file) +#' +#' # Write a SingleCellExperiment as a H5AD +#' h5ad_file <- tempfile(fileext = ".h5ad") +#' if (requireNamespace("SingleCellExperiment", quietly = TRUE)) { +#' sce <- SingleCellExperiment( +#' assays = list( +#' counts = matrix(1:15, 3L, 5L), +#' A = matrix(15:1, 3L, 5L), +#' B = matrix(letters[1:15], 3L, 5L) +#' ), +#' rowData = data.frame(gene = 1:5), +#' colData = data.frame(cell = 1:3), +#' ) +#' write_h5ad(sce, h5ad_file) +#' } +#' +#' # Write a Seurat as a H5AD +#' h5ad_file <- tempfile(fileext = ".h5ad") +#' if (requireNamespace("SeuratObject", quietly = TRUE)) { +#' 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")) { + from_SingleCellExperiment( + object, + output_class = "HDF5AnnData", + file = path + ) + } else if (inherits(object, "Seurat")) { + from_Seurat( + object, + output_class = "HDF5AnnData", + file = path + ) + } else if (inherits(object, "AbstractAnnData")) { + to_HDF5AnnData(object, path) + } else { + stop("Unable to write object of class: ", class(object)) + } + + invisible(path) +} diff --git a/R/HDF5-write.R b/R/write_h5ad_helpers.R similarity index 86% rename from R/HDF5-write.R rename to R/write_h5ad_helpers.R index 7f5b5b39..b78dbfdd 100644 --- a/R/HDF5-write.R +++ b/R/write_h5ad_helpers.R @@ -468,88 +468,4 @@ hdf5_path_exists <- function(file, target_path) { paths <- gsub("//", "/", paths) # Remove double slash for root paths target_path %in% paths -} - -#' Write H5AD -#' -#' Write an H5AD file -#' -#' @param object The object to write, either a "SingleCellExperiment" or a -#' "Seurat" object -#' @param path Path of the file to write to -#' -#' @return `path` invisibly -#' @export -#' -#' @examples -#' adata <- AnnData( -#' X = matrix(1:15, 3L, 5L), -#' layers = list( -#' A = matrix(15:1, 3L, 5L), -#' B = matrix(letters[1:15], 3L, 5L) -#' ), -#' obs = data.frame(cell = 1:3), -#' var = data.frame(gene = 1:5), -#' obs_names = LETTERS[1:3], -#' var_names = letters[1:5] -#' ) -#' h5ad_file <- tempfile(fileext = ".h5ad") -#' write_h5ad(adata, h5ad_file) -#' -#' # Write a SingleCellExperiment as a H5AD -#' h5ad_file <- tempfile(fileext = ".h5ad") -#' if (requireNamespace("SingleCellExperiment", quietly = TRUE)) { -#' sce <- SingleCellExperiment( -#' assays = list( -#' counts = matrix(1:15, 3L, 5L), -#' A = matrix(15:1, 3L, 5L), -#' B = matrix(letters[1:15], 3L, 5L) -#' ), -#' rowData = data.frame(gene = 1:5), -#' colData = data.frame(cell = 1:3), -#' ) -#' write_h5ad(sce, h5ad_file) -#' } -#' -#' # Write a Seurat as a H5AD -#' h5ad_file <- tempfile(fileext = ".h5ad") -#' if (requireNamespace("SeuratObject", quietly = TRUE)) { -#' 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")) { - from_SingleCellExperiment( - object, - output_class = "HDF5AnnData", - file = path - ) - } else if (inherits(object, "Seurat")) { - from_Seurat( - object, - output_class = "HDF5AnnData", - file = path - ) - } else { - ( - stop("Unable to write object of class: ", class(object)) - ) - } - - invisible(path) -} +} \ No newline at end of file