Skip to content

Commit

Permalink
Support non-standard metadata objects when reading from H5AD files. (#41
Browse files Browse the repository at this point in the history
)

Fixes an issue where `uns` is not a dictionary. Update tests and documentation.
  • Loading branch information
jkanche authored May 5, 2024
1 parent ce552d5 commit e7a1e6b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/singlecellexperiment/SingleCellExperiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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,
Expand Down
8 changes: 8 additions & 0 deletions tests/test_sce_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit e7a1e6b

Please sign in to comment.