[go: nahoru, domu]

Skip to content

Commit

Permalink
support reading multi-polygon selection files from the Xenium Explorer
Browse files Browse the repository at this point in the history
  • Loading branch information
quentinblampey committed Apr 11, 2024
1 parent 4bd5636 commit 94fb593
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ and this project adheres to [Semantic Versioning][].

## [0.1.3] - xxxx-xx-xx

### Added

- (Xenium) support reading multi-polygon selection files from the Xenium Explorer

## [0.1.2] - 2024-03-30

Expand Down
4 changes: 4 additions & 0 deletions src/spatialdata_io/_constants/_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ class XeniumKeys(ModeEnum):
NUCLEUS_COUNT = "nucleus_count"
Z_LEVEL = "z_level"

EXPLORER_SELECTION_X = "X"
EXPLORER_SELECTION_Y = "Y"
EXPLORER_SELECTION_KEY = "Selection"


@unique
class VisiumKeys(ModeEnum):
Expand Down
20 changes: 18 additions & 2 deletions src/spatialdata_io/readers/xenium.py
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,14 @@ def xenium_aligned_image(
)


def xenium_explorer_selection(path: str | Path, pixel_size: float = 0.2125) -> Polygon:
def _selection_to_polygon(df: pd.DataFrame, pixel_size: float) -> Polygon:
xy_keys = [XeniumKeys.EXPLORER_SELECTION_X, XeniumKeys.EXPLORER_SELECTION_Y]
return Polygon(df[xy_keys].values / pixel_size)


def xenium_explorer_selection(
path: str | Path, pixel_size: float = 0.2125, return_list: bool = False
) -> Polygon | list[Polygon]:
"""Read the coordinates of a selection `.csv` file exported from the `Xenium Explorer <https://www.10xgenomics.com/support/software/xenium-explorer/latest>`_.
This file can be generated by the "Freehand Selection" or the "Rectangular Selection".
Expand All @@ -624,19 +631,28 @@ def xenium_explorer_selection(path: str | Path, pixel_size: float = 0.2125) -> P
If `spatialdata_xenium_explorer <https://github.com/quentinblampey/spatialdata_xenium_explorer>`_ was used,
the `pixel_size` argument must be set to the one used during conversion with `spatialdata_xenium_explorer`.
In case multiple polygons were selected on the Explorer and exported into a single file, it will return a list of polygons.
Parameters
----------
path
Path to the `.csv` file containing the selection coordinates
pixel_size
Size of a pixel in microns. By default, the Xenium pixel size is used.
return_list
If `True`, returns a list of Polygon even if only one polygon was selected
Returns
-------
:class:`shapely.geometry.polygon.Polygon`
"""
df = pd.read_csv(path, skiprows=2)
return Polygon(df.values / pixel_size)

if XeniumKeys.EXPLORER_SELECTION_KEY not in df:
polygon = _selection_to_polygon(df, pixel_size)
return [polygon] if return_list else polygon

return [_selection_to_polygon(sub_df, pixel_size) for _, sub_df in df.groupby(XeniumKeys.EXPLORER_SELECTION_KEY)]


def _parse_version_of_xenium_analyzer(
Expand Down

0 comments on commit 94fb593

Please sign in to comment.