From 5f2ea294721ce7ab92b4a24655b8a59af0c2bd4d Mon Sep 17 00:00:00 2001 From: Jayaram Kancherla Date: Wed, 24 Jun 2026 14:57:01 -0700 Subject: [PATCH 1/2] Access the `raw` slot when initializing form anndata/h5ad objects --- CHANGELOG.md | 3 ++- .../SingleCellExperiment.py | 22 +++++++++++++++---- tests/test_sce_io.py | 10 +++++++++ 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 73b6fb3..8d17b17 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,11 +1,12 @@ # Changelog -## Version 0.6.0 - 0.6.2 +## Version 0.6.0 - 0.6.3 - Changed related to SummarizedExperiment and implementation of `CompressedGenomicRangesList` in the genomic ranges package. - Update versions of relevant dependency packages. - Rename `reduced_dims` to `reduced_dimensions`. - Implement coercions to/from RSE/SE. +- Access data stored in `raw` (if available) as `alternative_experiments`, when initializing `SingleCellExperiment` objects from anndata/h5ad files. ## Version 0.5.8 - 0.5.9 diff --git a/src/singlecellexperiment/SingleCellExperiment.py b/src/singlecellexperiment/SingleCellExperiment.py index e35330a..745e206 100644 --- a/src/singlecellexperiment/SingleCellExperiment.py +++ b/src/singlecellexperiment/SingleCellExperiment.py @@ -1195,14 +1195,18 @@ def to_anndata(self, include_alternative_experiments: bool = False): def from_anndata(cls, input: "anndata.AnnData") -> SingleCellExperiment: """Create a ``SingleCellExperiment`` from :py:class:`~anndata.AnnData`. - Args: + If the input contains any data in the ``uns`` attribute, the + `metadata` slot of the ``SingleCellExperiment`` will contain a key ``uns``. + + If the input contains ``raw`` data, the ``SingleCellExperiment`` + will contain an alternative experiment called ``raw``. + + Args: input: Input data. Returns: - A ``SingleCellExperiment`` object. If the input contains any data - in the ``uns`` attribute, the `metadata` slot of the ``SingleCellExperiment`` - will contain a key ``uns``. + A ``SingleCellExperiment`` object. """ layers = OrderedDict() @@ -1217,6 +1221,15 @@ def from_anndata(cls, input: "anndata.AnnData") -> SingleCellExperiment: obsp = _to_normal_dict(input.obsp) _metadata = {"uns": input.uns} if input.uns is not None else None + alt_expts = None + if input.raw is not None: + raw_se = SummarizedExperiment( + assays={"X": input.raw.X.transpose()}, + row_data=biocframe.BiocFrame.from_pandas(input.raw.var), + column_data=biocframe.BiocFrame.from_pandas(input.obs), + ) + alt_expts = {"raw": raw_se} + return cls( assays=layers, row_data=biocframe.BiocFrame.from_pandas(input.var), @@ -1225,6 +1238,7 @@ def from_anndata(cls, input: "anndata.AnnData") -> SingleCellExperiment: reduced_dimensions=obsm, row_pairs=varp, column_pairs=obsp, + alternative_experiments=alt_expts, ) ############################### diff --git a/tests/test_sce_io.py b/tests/test_sce_io.py index e7eba44..0b15379 100644 --- a/tests/test_sce_io.py +++ b/tests/test_sce_io.py @@ -198,6 +198,16 @@ def test_SCE_randomAnnData(): assert tse is not None assert isinstance(tse, SingleCellExperiment) + # set raw + adata.raw = adata.copy() + tse = singlecellexperiment.SingleCellExperiment.from_anndata(adata) + + assert tse is not None + assert isinstance(tse, SingleCellExperiment) + assert tse.alternative_experiments is not None + assert "raw" in tse.alternative_experiments + assert isinstance(tse.alternative_experiments["raw"], SummarizedExperiment) + assert tse.alternative_experiments["raw"].shape == (d, n) def test_SCE_to_mudata(): tse = SingleCellExperiment( From 1914130d613e9885fdea50b3b36e3c7cfccb5c09 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 24 Jun 2026 21:57:32 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/test_sce_io.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_sce_io.py b/tests/test_sce_io.py index 0b15379..424b0cd 100644 --- a/tests/test_sce_io.py +++ b/tests/test_sce_io.py @@ -205,7 +205,7 @@ def test_SCE_randomAnnData(): assert tse is not None assert isinstance(tse, SingleCellExperiment) assert tse.alternative_experiments is not None - assert "raw" in tse.alternative_experiments + assert "raw" in tse.alternative_experiments assert isinstance(tse.alternative_experiments["raw"], SummarizedExperiment) assert tse.alternative_experiments["raw"].shape == (d, n)