Skip to content
Merged
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
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
(PR #309)
- Remove deprecated functions and arguments (PR #311, PR #313)
- Minor cleanups and improvements (PR #313).
- Add more tests to increase coverage (PR #315)

# anndataR 0.2.0

Expand Down
Binary file modified inst/extdata/example.h5ad
Binary file not shown.
20 changes: 13 additions & 7 deletions inst/scripts/example_h5ad.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# python v3.10.10
import anndata # anndata v0.8.0
import scanpy # scanpy v1.9.3
import numpy # numpy v1.23.5
import pandas # pandas v2.0.0
import scipy.sparse # scipy v1.10.1
# python v3.13.5
import anndata # anndata v0.11.4
import scanpy # scanpy v1.11.4
import numpy # numpy v2.2.6
import pandas # pandas v2.3.0
import scipy.sparse # scipy v1.14.1

# This script uses Python to create an example H5AD file for testing
# interoperability between languages. It is designed to be a small but
Expand All @@ -22,6 +22,9 @@
#
# CHANGELOG
#
# v0.3.0 (2025-08-04)
# - Add adata.varp["test_varp"] to test reading of varp
# - Update package versions to latest stable versions
# v0.2.0 (2023-05-11)
# - Add 1D sparse matrix to `adata.uns["Sparse1D"]
# - Reduce the size of `adata.uns["String2D"]` and add columns to values
Expand Down Expand Up @@ -83,5 +86,8 @@
scanpy.tl.leiden(adata)
scanpy.tl.rank_genes_groups(adata, "leiden")

# add varp to test reading of varp
adata.varp["test_varp"] = numpy.random.rand(adata.n_vars, adata.n_vars)

# Write the H5AD file
adata.write("example.h5ad")
adata.write_h5ad("inst/extdata/example.h5ad", compression="gzip")
88 changes: 88 additions & 0 deletions tests/testthat/test-HDF5AnnData.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,26 @@ test_that("reading varm works", {
)
})

# trackstatus: class=HDF5AnnData, feature=test_get_obsp, status=done
test_that("reading obsp works", {
obsp <- adata$obsp
expect_true(is.list(obsp), "list")
expect_equal(
names(obsp),
c("connectivities", "distances")
)
})

# trackstatus: class=HDF5AnnData, feature=test_get_varp, status=done
test_that("reading varp works", {
varp <- adata$varp
expect_true(is.list(varp), "list")
expect_equal(
names(varp),
c("test_varp")
)
})

# trackstatus: class=HDF5AnnData, feature=test_get_obs, status=done
test_that("reading obs works", {
obs <- adata$obs
Expand Down Expand Up @@ -187,3 +207,71 @@ test_that("writing var names works", {
h5ad$var_names <- LETTERS[1:20]
expect_identical(h5ad$var_names, LETTERS[1:20])
})

# trackstatus: class=HDF5AnnData, feature=test_set_obsm, status=done
test_that("writing obsm works", {
h5ad_file <- withr::local_tempfile(fileext = ".h5ad")
obs <- data.frame(row.names = 1:10)
var <- data.frame(row.names = 1:20)
h5ad <- HDF5AnnData$new(h5ad_file, obs = obs, var = var)

obsm_x <- matrix(rnorm(10 * 5), nrow = 10, ncol = 5)
h5ad$obsm <- list(X = obsm_x)
expect_identical(h5ad$obsm$X, obsm_x)
})

# trackstatus: class=HDF5AnnData, feature=test_set_varm, status=done
test_that("writing varm works", {
h5ad_file <- withr::local_tempfile(fileext = ".h5ad")
obs <- data.frame(row.names = 1:10)
var <- data.frame(row.names = 1:20)
h5ad <- HDF5AnnData$new(h5ad_file, obs = obs, var = var)
varm_x <- matrix(rnorm(20 * 5), nrow = 20, ncol = 5)
h5ad$varm <- list(PCs = varm_x)
expect_identical(h5ad$varm$PCs, varm_x)
})

# trackstatus: class=HDF5AnnData, feature=test_set_obsp, status=done
test_that("writing obsp works", {
h5ad_file <- withr::local_tempfile(fileext = ".h5ad")
obs <- data.frame(row.names = 1:10)
var <- data.frame(row.names = 1:20)
h5ad <- HDF5AnnData$new(h5ad_file, obs = obs, var = var)

obsp_x <- matrix(rnorm(10 * 10), nrow = 10, ncol = 10)
h5ad$obsp <- list(connectivities = obsp_x)
expect_identical(h5ad$obsp$connectivities, obsp_x)
})

# trackstatus: class=HDF5AnnData, feature=test_set_varp, status=done
test_that("writing varp works", {
h5ad_file <- withr::local_tempfile(fileext = ".h5ad")
obs <- data.frame(row.names = 1:10)
var <- data.frame(row.names = 1:20)
h5ad <- HDF5AnnData$new(h5ad_file, obs = obs, var = var)

varp_x <- matrix(rnorm(20 * 20), nrow = 20, ncol = 20)
h5ad$varp <- list(connectivities = varp_x)
expect_identical(h5ad$varp$connectivities, varp_x)
})

# trackstatus: class=HDF5AnnData, feature=test_set_uns, status=done
test_that("writing uns works", {
h5ad_file <- withr::local_tempfile(fileext = ".h5ad")
obs <- data.frame(row.names = 1:10)
var <- data.frame(row.names = 1:20)
h5ad <- HDF5AnnData$new(h5ad_file, obs = obs, var = var)

h5ad$uns <- list(
foo = "bar",
baz = c(1, 2, 3),
nested = list(
nested_foo = "nested_bar",
nested_baz = c(4L, 5L, 6L)
)
)
expect_identical(h5ad$uns$foo, "bar")
expect_equal(h5ad$uns$baz, c(1, 2, 3), ignore_attr = TRUE)
expect_identical(h5ad$uns$nested$nested_foo, "nested_bar")
expect_equal(h5ad$uns$nested$nested_baz, c(4L, 5L, 6L), ignore_attr = TRUE)
})
Loading