Skip to content

Commit

Permalink
Support for delayed and file-backed array when coercing to anndata (#34
Browse files Browse the repository at this point in the history
…).

Also update tests.
  • Loading branch information
jkanche authored Feb 14, 2024
1 parent 1f5a035 commit c7c28d9
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 20 deletions.
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ optional =
anndata
h5py
mudata
delayedarray

# Add here test requirements (semicolon/line-separated)
testing =
Expand All @@ -72,6 +73,7 @@ testing =
anndata
h5py
mudata
delayedarray

[options.entry_points]
# Add here console scripts like:
Expand Down
28 changes: 8 additions & 20 deletions src/singlecellexperiment/SingleCellExperiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import biocframe
import biocutils as ut
from genomicranges import GenomicRanges
from summarizedexperiment._combineutils import (
check_assays_are_equal,
merge_assays,
Expand Down Expand Up @@ -984,25 +983,16 @@ def to_anndata(self, include_alternative_experiments: bool = False):
Returns:
A tuple with ``AnnData`` main experiment and a list of alternative experiments.
"""
from anndata import AnnData
obj = super().to_anndata()

layers = OrderedDict()
for asy, mat in self.assays.items():
layers[asy] = mat.transpose()
if self.reduced_dims is not None:
obj.obsm = self.reduced_dims

trows = self.row_data
if isinstance(self.row_data, GenomicRanges):
trows = self.row_data.to_pandas()

obj = AnnData(
obs=self.col_data,
var=trows,
uns=self.metadata,
obsm=self.reduced_dims,
layers=layers,
varp=self.row_pairs,
obsp=self.column_pairs,
)
if self.row_pairs is not None:
obj.varp = self.row_pairs

if self.column_pairs is not None:
obj.obsp = self.column_pairs

if include_alternative_experiments is True:
adatas = None
Expand All @@ -1014,8 +1004,6 @@ def to_anndata(self, include_alternative_experiments: bool = False):
) in self.alternative_experiments.items():
adatas[alt_name] = alternative_experiment.to_anndata()

return obj, adatas

return obj, None

@classmethod
Expand Down
2 changes: 2 additions & 0 deletions tests/test_sce_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ def test_SCE_to_anndata():
adata = tse.to_anndata()
assert adata is not None
assert isinstance(adata[0], anndata.AnnData)
assert adata[0].shape[0] == counts.shape[1]
assert adata[0].shape[1] == counts.shape[0]


def test_SCE_fromH5AD():
Expand Down

0 comments on commit c7c28d9

Please sign in to comment.