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

Compatibility with non-dict types in uns when reading from H5AD files. #41

Merged
merged 5 commits into from
May 5, 2024
Merged
Show file tree
Hide file tree
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
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
Loading