-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move functions. allow writing anndata objects to file
- Loading branch information
Showing
4 changed files
with
117 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
) | ||
} | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters