From e7a1e6b6abca77b1dfd2103f34e059c809712dcc Mon Sep 17 00:00:00 2001 From: Jayaram Kancherla Date: Sun, 5 May 2024 12:41:59 -0700 Subject: [PATCH] Support non-standard metadata objects when reading from H5AD files. (#41) Fixes an issue where `uns` is not a dictionary. Update tests and documentation. --- src/singlecellexperiment/SingleCellExperiment.py | 7 +++++-- tests/test_sce_io.py | 8 ++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/singlecellexperiment/SingleCellExperiment.py b/src/singlecellexperiment/SingleCellExperiment.py index 0df4ff9..47df2ee 100644 --- a/src/singlecellexperiment/SingleCellExperiment.py +++ b/src/singlecellexperiment/SingleCellExperiment.py @@ -1015,7 +1015,9 @@ def from_anndata(cls, input: "anndata.AnnData") -> "SingleCellExperiment": Input data. Returns: - A ``SingleCellExperiment`` object. + A ``SingleCellExperiment`` object. If the input contains any data + in the ``uns`` attribute, the `metadata` slot of the ``SingleCellExperiment`` + will contain a key ``uns``. """ layers = OrderedDict() @@ -1028,12 +1030,13 @@ def from_anndata(cls, input: "anndata.AnnData") -> "SingleCellExperiment": obsm = _to_normal_dict(input.obsm) varp = _to_normal_dict(input.varp) obsp = _to_normal_dict(input.obsp) + _metadata = {"uns": input.uns} if input.uns is not None else None return cls( assays=layers, row_data=biocframe.BiocFrame.from_pandas(input.var), column_data=biocframe.BiocFrame.from_pandas(input.obs), - metadata=input.uns, + metadata=_metadata, reduced_dims=obsm, row_pairs=varp, column_pairs=obsp, diff --git a/tests/test_sce_io.py b/tests/test_sce_io.py index 75bd505..a744e55 100644 --- a/tests/test_sce_io.py +++ b/tests/test_sce_io.py @@ -129,6 +129,14 @@ def test_SCE_randomAnnData(): assert tse is not None assert isinstance(tse, SingleCellExperiment) + # to avoid unknown mapping types; + # ran into an issue with anndata.compat._overloaded_dict.OverloadedDict when loading a h5ad + adata.uns = {".internal": [f"obs_{i+1}" for i in range(n)]} + tse = singlecellexperiment.SingleCellExperiment.from_anndata(adata) + + assert tse is not None + assert isinstance(tse, SingleCellExperiment) + def test_SCE_to_mudata(): tse = SingleCellExperiment(