|
1 | 1 | from pathlib import Path |
2 | | -from typing import Any, Dict, List, Optional, Sequence, Union |
| 2 | +from typing import Any, Dict, List, Optional, Sequence, Union, Tuple |
3 | 3 | from urllib.parse import urlparse |
4 | 4 | from warnings import warn |
5 | 5 |
|
@@ -1019,15 +1019,37 @@ def to_spatial_experiment(): |
1019 | 1019 | ######>> AnnData interop <<##### |
1020 | 1020 | ################################ |
1021 | 1021 |
|
1022 | | - def to_anndata(self, include_alternative_experiments: bool = False) -> "anndata.AnnData": |
| 1022 | + def to_anndata( |
| 1023 | + self, include_alternative_experiments: bool = False |
| 1024 | + ) -> Tuple["anndata.AnnData", Dict[str, "anndata.AnnData"]]: |
1023 | 1025 | """Transform :py:class:`~SpatialExperiment`-like into a :py:class:`~anndata.AnnData` representation. |
1024 | 1026 |
|
| 1027 | + This method converts the main experiment data, spatial coordinates, |
| 1028 | + and image data into an AnnData structure. Image data, including the |
| 1029 | + image arrays and their scale factors, are stored within the ``obj.uns["spatial"]`` |
| 1030 | + dictionary, adhering to a common convention for spatial single-cell data. |
| 1031 | +
|
| 1032 | + The ``obj.uns["spatial"]`` dictionary is structured as follows: |
| 1033 | + - It is a dictionary where each key is a unique ``library_id``. |
| 1034 | + The ``library_id`` is generated by combining ``sample_id`` and ``image_id`` |
| 1035 | + from the input image data (e.g., "sample1-image01"). |
| 1036 | + - Each ``library_id`` entry is itself a dictionary containing: |
| 1037 | + - ``"images"``: A dictionary to store image arrays. |
| 1038 | + Currently, images are stored under the key ``"hires"`` by default |
| 1039 | + (e.g., ``obj.uns["spatial"][library_id]["images"]["hires"] = image_numpy_array``). |
| 1040 | + - ``"scalefactors"``: A dictionary to store scale factors associated |
| 1041 | + with the images. Currently, scale factors are stored under the key |
| 1042 | + ``"tissue_hires_scalef"`` by default (e.g., |
| 1043 | + ``obj.uns["spatial"][library_id]["scalefactors"]["tissue_hires_scalef"] = scale_factor_value``). |
| 1044 | +
|
| 1045 | + Spatial coordinates are stored in ``obj.obsm["spatial"]``. |
| 1046 | +
|
1025 | 1047 | Args: |
1026 | 1048 | include_alternative_experiments: |
1027 | 1049 | Whether to transform alternative experiments. |
1028 | 1050 |
|
1029 | 1051 | Returns: |
1030 | | - An ``AnnData`` representation of the experiment. |
| 1052 | + A tuple containing an ``AnnData`` object with spatial information and a list of alternative experiments. |
1031 | 1053 | """ |
1032 | 1054 | obj, alt_exps = super().to_anndata(include_alternative_experiments=include_alternative_experiments) |
1033 | 1055 |
|
|
0 commit comments