Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[r][python] Create an AnnData -> SOMA -> Seurat test #1263

Merged
merged 3 commits into from
Apr 25, 2023
Merged
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
66 changes: 66 additions & 0 deletions apis/system/tests/test_write_anndata_read_seurat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import anndata as ad
import pytest

from tiledbsoma import io

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"
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("SeuratObject")
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})))
"""
)