Skip to content

Commit

Permalink
move functions. allow writing anndata objects to file
Browse files Browse the repository at this point in the history
  • Loading branch information
rcannood committed Sep 18, 2023
1 parent d1cb95b commit b915995
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 119 deletions.
33 changes: 33 additions & 0 deletions R/read_h5ad.R
Original file line number Diff line number Diff line change
@@ -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)
)
}

Check warning on line 33 in R/read_h5ad.R

View workflow job for this annotation

GitHub Actions / lint

file=R/read_h5ad.R,line=33,col=2,[trailing_blank_lines_linter] Missing terminal newline.
34 changes: 0 additions & 34 deletions R/HDF5-read.R → R/read_h5ad_helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
}
83 changes: 83 additions & 0 deletions R/write_h5ad.R
Original file line number Diff line number Diff line change
@@ -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)
}
86 changes: 1 addition & 85 deletions R/HDF5-write.R → R/write_h5ad_helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Check warning on line 471 in R/write_h5ad_helpers.R

View workflow job for this annotation

GitHub Actions / lint

file=R/write_h5ad_helpers.R,line=471,col=2,[trailing_blank_lines_linter] Missing terminal newline.

0 comments on commit b915995

Please sign in to comment.