From 849f9da6f8bca7064ede60bc4282421afa716eaa Mon Sep 17 00:00:00 2001 From: Emanuele Bezzi Date: Tue, 18 Apr 2023 17:23:13 -0400 Subject: [PATCH 1/3] add test --- .../tests/test_write_anndata_read_seurat.py | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 apis/system/tests/test_write_anndata_read_seurat.py diff --git a/apis/system/tests/test_write_anndata_read_seurat.py b/apis/system/tests/test_write_anndata_read_seurat.py new file mode 100644 index 0000000000..f50be8feb1 --- /dev/null +++ b/apis/system/tests/test_write_anndata_read_seurat.py @@ -0,0 +1,66 @@ +import anndata as ad +import pytest + +import tiledbsoma as soma + +from .common import TestWritePythonReadR, embed_python_list_into_R_code + + +class TestAnndataSOMASeurat(TestWritePythonReadR): + """ + This test will perform an AnnData->SOMA->Seurat conversion and verify that the final artifact (Seurat) + has the same properties of the initial one (AnnData) + """ + + @pytest.fixture(scope="class") + def h5ad(self): + """ + Fixture that will load an h5ad, convert it to SOMA, deploy it to `self.uri` and return a parsed version of it. + """ + h5ad_path = "apis/python/testdata/pbmc-small.h5ad" + soma.io.from_h5ad(self.uri, input_path=h5ad_path, measurement_name="RNA") + return ad.read_h5ad(h5ad_path) + + # Prepares an R script with the dependencies and loads the object in seuratObject + # Future assertions can be done by appending to this script + def base_R_script(self): + return f""" + library("tiledbsoma") + library("Seurat") + soma <- SOMAOpen("{self.uri}") + ms <- soma$ms$get("RNA") + query <- SOMAExperimentAxisQuery$new( + experiment = soma, + measurement_name = "RNA" + ) + seuratObject <- query$to_seurat(c(data = "data"), var_index="var_id", obs_index="obs_id") + """ + + # tests + def test_seurat_dim_matches(self, h5ad): + n_var = len(h5ad.var) + n_obs = len(h5ad.obs) + self.r_assert( + f""" + stopifnot(ncol(seuratObject) == {n_obs}) + stopifnot(nrow(seuratObject) == {n_var}) + """ + ) + + def test_seurat_var_names_match(self, h5ad): + var_names = h5ad.var.index.tolist() + var_names_list = embed_python_list_into_R_code(var_names) + self.r_assert( + f""" + stopifnot(all.equal(rownames(x = seuratObject), c({var_names_list}))) + """ + ) + + def test_seurat_obs_names_match(self, h5ad): + obs_names = h5ad.obs.index.tolist() + obs_names_list = embed_python_list_into_R_code(obs_names) + self.r_assert( + f""" + stopifnot(all.equal(colnames(x = seuratObject), c({obs_names_list}))) + """ + ) From 6ca320a934fe4e46d3f950beb44d830c46da33f7 Mon Sep 17 00:00:00 2001 From: Emanuele Bezzi Date: Wed, 19 Apr 2023 10:53:18 -0400 Subject: [PATCH 2/3] import --- apis/system/tests/test_write_anndata_read_seurat.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apis/system/tests/test_write_anndata_read_seurat.py b/apis/system/tests/test_write_anndata_read_seurat.py index f50be8feb1..f4b2eb9df3 100644 --- a/apis/system/tests/test_write_anndata_read_seurat.py +++ b/apis/system/tests/test_write_anndata_read_seurat.py @@ -1,7 +1,7 @@ import anndata as ad import pytest -import tiledbsoma as soma +from tiledbsoma import io from .common import TestWritePythonReadR, embed_python_list_into_R_code @@ -18,7 +18,7 @@ def h5ad(self): Fixture that will load an h5ad, convert it to SOMA, deploy it to `self.uri` and return a parsed version of it. """ h5ad_path = "apis/python/testdata/pbmc-small.h5ad" - soma.io.from_h5ad(self.uri, input_path=h5ad_path, measurement_name="RNA") + io.from_h5ad(self.uri, input_path=h5ad_path, measurement_name="RNA") return ad.read_h5ad(h5ad_path) # Prepares an R script with the dependencies and loads the object in seuratObject From 9a7dae14f2a9f139d6fee96cb579c1e942c6a0eb Mon Sep 17 00:00:00 2001 From: Emanuele Bezzi Date: Wed, 19 Apr 2023 11:47:51 -0400 Subject: [PATCH 3/3] replace Seurat -> SeuratObject --- apis/system/tests/test_write_anndata_read_seurat.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apis/system/tests/test_write_anndata_read_seurat.py b/apis/system/tests/test_write_anndata_read_seurat.py index f4b2eb9df3..50153f20b6 100644 --- a/apis/system/tests/test_write_anndata_read_seurat.py +++ b/apis/system/tests/test_write_anndata_read_seurat.py @@ -26,7 +26,7 @@ def h5ad(self): def base_R_script(self): return f""" library("tiledbsoma") - library("Seurat") + library("SeuratObject") soma <- SOMAOpen("{self.uri}") ms <- soma$ms$get("RNA") query <- SOMAExperimentAxisQuery$new(