Skip to content
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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
22 changes: 18 additions & 4 deletions src/singlecellexperiment/SingleCellExperiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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),
Expand All @@ -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,
)

###############################
Expand Down
10 changes: 10 additions & 0 deletions tests/test_sce_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Loading