Skip to content

Commit

Permalink
Merge branch 'main' into issue-60-from-seurat
Browse files Browse the repository at this point in the history
  • Loading branch information
csangara authored Sep 19, 2023
2 parents d713e7f + bffaf33 commit f4784f3
Show file tree
Hide file tree
Showing 49 changed files with 542 additions and 907 deletions.
5 changes: 3 additions & 2 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
^LICENSE\.md$
^doc$
^.*\.Rproj$
^\.Rproj\.user$
^\.github$
Expand All @@ -10,4 +9,6 @@
^_pkgdown\.yml$
^docs$
^pkgdown$
^vignettes/features.Rmd$
^vignettes/diagrams/*\.mmd$
^vignettes/diagrams/*\.svg$
^vignettes/design\.Rmd$
46 changes: 36 additions & 10 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,41 @@ Package: anndataR
Title: AnnData interoperability in R
Version: 0.0.0.9000
Authors@R: c(
person("Robrecht", "Cannoodt", , "[email protected]", role = c("aut", "cre"),
comment = c(ORCID = "0000-0003-3641-729X", github = "rcannood")),
person("Luke", "Zappia", , "[email protected]", role = "aut",
comment = c(ORCID = "0000-0001-7744-8565", github = "lazappi")),
person("Martin", "Morgan", , "[email protected]", role = "aut",
comment = c(ORCID = "0000-0002-5874-8148", github = "mtmorgan")),
person("Louise", "Deconinck", , "[email protected]", role = "aut",
comment = c(ORCID = "0000-0001-8100-6823", github = "LouiseDck")),
person("Danila", "Bredikhin", , "[email protected]", role = "aut",
comment = c(ORCID = "0000-0001-8089-6983", github = "gtca"))
person(
"Robrecht",
"Cannoodt",
email = "[email protected]",
role = c("aut", "cre"),
comment = c(ORCID = "0000-0003-3641-729X", github = "rcannood")
),
person(
"Luke",
"Zappia",
email = "[email protected]",
role = "aut",
comment = c(ORCID = "0000-0001-7744-8565", github = "lazappi")
),
person(
"Martin",
"Morgan",
email = "[email protected]",
role = "aut",
comment = c(ORCID = "0000-0002-5874-8148", github = "mtmorgan")
),
person(
"Louise",
"Deconinck",
email = "[email protected]",
role = "aut",
comment = c(ORCID = "0000-0001-8100-6823", github = "LouiseDck")
),
person(
"Danila",
"Bredikhin",
email = "[email protected]",
role = "ctb",
comment = c(ORCID = "0000-0001-8089-6983", github = "gtca")
)
)
Description: Bring the power and flexibility of AnnData to the R
ecosystem, allowing you to effortlessly manipulate and analyze your
Expand All @@ -29,6 +54,7 @@ Imports:
R6
Suggests:
anndata,
BiocStyle,
knitr,
reticulate,
rhdf5,
Expand Down
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

export(AnnData)
export(InMemoryAnnData)
export(dummy_data)
export(from_Seurat)
export(from_SingleCellExperiment)
export(read_h5ad)
Expand Down
2 changes: 1 addition & 1 deletion R/AbstractAnnData.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ AbstractAnnData <- R6::R6Class("AbstractAnnData", # nolint
#' computationally expensive.
#' @param ... Optional arguments to print method.
print = function(...) {
cat("AnnData object with n_obs \u00D7 n_vars = ", self$n_obs(), " × ", self$n_vars(), "\n", sep = "")
cat("AnnData object with n_obs \u00D7 n_vars = ", self$n_obs(), " \u00D7 ", self$n_vars(), "\n", sep = "")

for (attribute in c(
"obs",
Expand Down
13 changes: 6 additions & 7 deletions R/AnnData.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,12 @@
#'
#' adata
AnnData <- function(
obs_names = NULL,
var_names = NULL,
X = NULL,
obs = NULL,
var = NULL,
layers = NULL
) {
obs_names = NULL,
var_names = NULL,
X = NULL,
obs = NULL,
var = NULL,
layers = NULL) {
InMemoryAnnData$new(
obs_names = obs_names,
var_names = var_names,
Expand Down
25 changes: 12 additions & 13 deletions R/HDF5AnnData.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ HDF5AnnData <- R6::R6Class("HDF5AnnData", # nolint
#' @field X The X slot
X = function(value) {
if (missing(value)) {
# trackstatus: class=HDF5AnnData, feature=get_X, status=wip
# trackstatus: class=HDF5AnnData, feature=get_X, status=done
read_h5ad_element(private$.h5obj, "/X")
} else {
# trackstatus: class=HDF5AnnData, feature=set_X, status=wip
# trackstatus: class=HDF5AnnData, feature=set_X, status=done
value <- private$.validate_matrix(value, "X")
write_h5ad_element(value, private$.h5obj, "/X")
}
Expand All @@ -28,21 +28,21 @@ HDF5AnnData <- R6::R6Class("HDF5AnnData", # nolint
#' `obs` and `var`.
layers = function(value) {
if (missing(value)) {
# trackstatus: class=HDF5AnnData, feature=get_layers, status=wip
# trackstatus: class=HDF5AnnData, feature=get_layers, status=done
read_h5ad_element(private$.h5obj, "layers")
} else {
# trackstatus: class=HDF5AnnData, feature=set_layers, status=wip
# trackstatus: class=HDF5AnnData, feature=set_layers, status=done
value <- private$.validate_layers(value)
write_h5ad_element(value, private$.h5obj, "/layers")
}
},
#' @field obs The obs slot
obs = function(value) {
if (missing(value)) {
# trackstatus: class=HDF5AnnData, feature=get_obs, status=wip
# trackstatus: class=HDF5AnnData, feature=get_obs, status=done
read_h5ad_element(private$.h5obj, "/obs", include_index = FALSE)
} else {
# trackstatus: class=HDF5AnnData, feature=set_obs, status=wip
# trackstatus: class=HDF5AnnData, feature=set_obs, status=done
value <- private$.validate_obsvar_dataframe(value, "obs")
write_h5ad_element(
value,
Expand All @@ -55,10 +55,10 @@ HDF5AnnData <- R6::R6Class("HDF5AnnData", # nolint
#' @field var The var slot
var = function(value) {
if (missing(value)) {
# trackstatus: class=HDF5AnnData, feature=get_var, status=wip
# trackstatus: class=HDF5AnnData, feature=get_var, status=done
read_h5ad_element(private$.h5obj, "/var", include_index = FALSE)
} else {
# trackstatus: class=HDF5AnnData, feature=set_var, status=wip
# trackstatus: class=HDF5AnnData, feature=set_var, status=done
value <- private$.validate_obsvar_dataframe(value, "var")
write_h5ad_element(
value,
Expand All @@ -70,17 +70,16 @@ HDF5AnnData <- R6::R6Class("HDF5AnnData", # nolint
},
#' @field obs_names Names of observations
obs_names = function(value) {
# TODO: directly write to and read from /obs/_index
if (missing(value)) {
# trackstatus: class=HDF5AnnData, feature=get_obs_names, status=wip
# trackstatus: class=HDF5AnnData, feature=get_obs_names, status=done
# obs names are cached to avoid reading all of obs whenever they are
# accessed
if (is.null(private$.obs_names)) {
private$.obs_names <- read_h5ad_data_frame_index(private$.h5obj, "obs")
}
private$.obs_names
} else {
# trackstatus: class=HDF5AnnData, feature=set_obs_names, status=wip
# trackstatus: class=HDF5AnnData, feature=set_obs_names, status=done
value <- private$.validate_obsvar_names(value, "obs")
write_h5ad_data_frame_index(value, private$.h5obj, "obs", "_index")
private$.obs_names <- value
Expand All @@ -90,15 +89,15 @@ HDF5AnnData <- R6::R6Class("HDF5AnnData", # nolint
var_names = function(value) {
# TODO: directly write to and read from /var/_index
if (missing(value)) {
# trackstatus: class=HDF5AnnData, feature=get_var_names, status=wip
# trackstatus: class=HDF5AnnData, feature=get_var_names, status=done
# var names are cached to avoid reading all of var whenever they are
# accessed
if (is.null(private$.var_names)) {
private$.var_names <- read_h5ad_data_frame_index(private$.h5obj, "var")
}
private$.var_names
} else {
# trackstatus: class=HDF5AnnData, feature=set_var_names, status=wip
# trackstatus: class=HDF5AnnData, feature=set_var_names, status=done
value <- private$.validate_obsvar_names(value, "var")
write_h5ad_data_frame_index(value, private$.h5obj, "var", "_index")
private$.var_names <- value
Expand Down
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)
)
}
Loading

0 comments on commit f4784f3

Please sign in to comment.