From c7c28d9dfa9d24ba806363efe55b468d23d26870 Mon Sep 17 00:00:00 2001 From: Jayaram Kancherla Date: Wed, 14 Feb 2024 08:39:24 -0800 Subject: [PATCH] Support for delayed and file-backed array when coercing to anndata (#34). Also update tests. --- setup.cfg | 2 ++ .../SingleCellExperiment.py | 28 ++++++------------- tests/test_sce_io.py | 2 ++ 3 files changed, 12 insertions(+), 20 deletions(-) diff --git a/setup.cfg b/setup.cfg index edce69a..44aa670 100644 --- a/setup.cfg +++ b/setup.cfg @@ -63,6 +63,7 @@ optional = anndata h5py mudata + delayedarray # Add here test requirements (semicolon/line-separated) testing = @@ -72,6 +73,7 @@ testing = anndata h5py mudata + delayedarray [options.entry_points] # Add here console scripts like: diff --git a/src/singlecellexperiment/SingleCellExperiment.py b/src/singlecellexperiment/SingleCellExperiment.py index aac38cb..0df4ff9 100644 --- a/src/singlecellexperiment/SingleCellExperiment.py +++ b/src/singlecellexperiment/SingleCellExperiment.py @@ -4,7 +4,6 @@ import biocframe import biocutils as ut -from genomicranges import GenomicRanges from summarizedexperiment._combineutils import ( check_assays_are_equal, merge_assays, @@ -984,25 +983,16 @@ def to_anndata(self, include_alternative_experiments: bool = False): Returns: A tuple with ``AnnData`` main experiment and a list of alternative experiments. """ - from anndata import AnnData + obj = super().to_anndata() - layers = OrderedDict() - for asy, mat in self.assays.items(): - layers[asy] = mat.transpose() + if self.reduced_dims is not None: + obj.obsm = self.reduced_dims - trows = self.row_data - if isinstance(self.row_data, GenomicRanges): - trows = self.row_data.to_pandas() - - obj = AnnData( - obs=self.col_data, - var=trows, - uns=self.metadata, - obsm=self.reduced_dims, - layers=layers, - varp=self.row_pairs, - obsp=self.column_pairs, - ) + if self.row_pairs is not None: + obj.varp = self.row_pairs + + if self.column_pairs is not None: + obj.obsp = self.column_pairs if include_alternative_experiments is True: adatas = None @@ -1014,8 +1004,6 @@ def to_anndata(self, include_alternative_experiments: bool = False): ) in self.alternative_experiments.items(): adatas[alt_name] = alternative_experiment.to_anndata() - return obj, adatas - return obj, None @classmethod diff --git a/tests/test_sce_io.py b/tests/test_sce_io.py index 8a2c102..75bd505 100644 --- a/tests/test_sce_io.py +++ b/tests/test_sce_io.py @@ -61,6 +61,8 @@ def test_SCE_to_anndata(): adata = tse.to_anndata() assert adata is not None assert isinstance(adata[0], anndata.AnnData) + assert adata[0].shape[0] == counts.shape[1] + assert adata[0].shape[1] == counts.shape[0] def test_SCE_fromH5AD():