Split donors / regions based on shapes from xenium explorer

Split donors / regions based on shapes from xenium explorer#

import spatialdata as sd
import sparty as spt

# import spatialdata_io
# spatialdata_io.__version__
# '0.7.0'

Read xenium output#

xenium_output = "DIR_PATH"
sdata = spt.io.load_xenium(xenium_output,  n_jobs = 15)
sdata
SpatialData object
├── Images
│     └── 'morphology_focus': DataTree[cyx] (4, 88331, 28440), (4, 44165, 14220), (4, 22082, 7110), (4, 11041, 3555), (4, 5520, 1777)
├── Labels
│     ├── 'cell_labels': DataTree[yx] (88331, 28440), (44165, 14220), (22082, 7110), (11041, 3555), (5520, 1777)
│     └── 'nucleus_labels': DataTree[yx] (88331, 28440), (44165, 14220), (22082, 7110), (11041, 3555), (5520, 1777)
├── Points
│     └── 'transcripts': DataFrame with shape: (<Delayed>, 13) (3D points)
├── Shapes
│     ├── 'cell_boundaries': GeoDataFrame shape: (33853, 1) (2D shapes)
│     └── 'nucleus_boundaries': GeoDataFrame shape: (34204, 2) (2D shapes)
└── Tables
      └── 'table': AnnData (33853, 5101)
with coordinate systems:
    ▸ 'global', with elements:
        morphology_focus (Images), cell_labels (Labels), nucleus_labels (Labels), transcripts (Points), cell_boundaries (Shapes), nucleus_boundaries (Shapes)
sdata['table'].uns['spatialdata_attrs']
{'region': 'cell_boundaries',
 'region_key': 'region',
 'instance_key': 'cell_id',
 'feature_key': 'feature_name'}

Import shapes from xenium explorer#

file_sample_shapes = 'xenium_shapes.csv'
gdf = spt.io.shapes_from_xe(
    file=file_sample_shapes,
    plot_fig = False,
    # return_gdf=False
)
gdf
name geometry
0 D655 POLYGON ((968.46 2166.936, 2179.671 2003.013, ...
1 D661 POLYGON ((22197.707 13394.094, 22896.337 13483...
2 D652 POLYGON ((6720.018 43235.62, 7150.272 42882.04...
3 D651 POLYGON ((3467.867 54694.143, 3679.379 53094.5...
4 D657 POLYGON ((17771.5 68096.6, 18538.232 67224.112...
5 D649 POLYGON ((3741.384 80733.102, 4526.719 80448.6...
# sdata.pl.render_shapes(
#     "cell_boundaries",
#     color='grey',
#     method='matplotlib',
#     ).pl.render_shapes(
#     "sample_shapes",
#     fill_alpha=0.5,
#     method='matplotlib',
#     outline_color='grey',
#     outline_width=1,
#     ).pl.show()

Subset each shape and save as zarr#

DIR = "OUT_DIR"
for _, row in gdf.iterrows():
    name = row["name"]
    geometry = row["geometry"]
    print(name)

    cropped_sdata = sd.polygon_query(
        sdata,
        polygon=geometry,
        target_coordinate_system="global",
    )
    print(cropped_sdata)

    # scanpy prepro 
    # spt.pp.prepro_qc_scanpy(
    #     cropped_sdata, 
    #     min_counts = 10,
    #     min_genes = 5,
    #     pct_negative = 5.0)

    print(cropped_sdata['table'].n_obs)   
    cropped_sdata.write(f"{DIR}outs/sdata/sd_{name}.zarr")
    cropped_sdata['table'].write(f"{DIR}outs/adata/{name}.h5ad")
D655
SpatialData object
├── Images
│     └── 'morphology_focus': DataTree[cyx] (4, 6912, 8345), (4, 3456, 4173), (4, 1728, 2086), (4, 864, 1043), (4, 432, 521)
├── Labels
│     ├── 'cell_labels': DataTree[yx] (6912, 8345), (3456, 4173), (1728, 2086), (864, 1043), (432, 521)
│     └── 'nucleus_labels': DataTree[yx] (6912, 8345), (3456, 4173), (1728, 2086), (864, 1043), (432, 521)
├── Points
│     └── 'transcripts': DataFrame with shape: (<Delayed>, 13) (3D points)
├── Shapes
│     ├── 'cell_boundaries': GeoDataFrame shape: (5083, 1) (2D shapes)
│     └── 'nucleus_boundaries': GeoDataFrame shape: (5158, 2) (2D shapes)
└── Tables
      └── 'table': AnnData (5083, 5101)
with coordinate systems:
    ▸ 'global', with elements:
        morphology_focus (Images), cell_labels (Labels), nucleus_labels (Labels), transcripts (Points), cell_boundaries (Shapes), nucleus_boundaries (Shapes)
5083
D661
SpatialData object
├── Images
│     └── 'morphology_focus': DataTree[cyx] (4, 3591, 3783), (4, 1796, 1891), (4, 898, 945), (4, 449, 473), (4, 224, 236)
├── Labels
│     ├── 'cell_labels': DataTree[yx] (3591, 3783), (1796, 1891), (898, 945), (449, 473), (224, 236)
│     └── 'nucleus_labels': DataTree[yx] (3591, 3783), (1796, 1891), (898, 945), (449, 473), (224, 236)
├── Points
│     └── 'transcripts': DataFrame with shape: (<Delayed>, 13) (3D points)
├── Shapes
│     ├── 'cell_boundaries': GeoDataFrame shape: (217, 1) (2D shapes)
│     └── 'nucleus_boundaries': GeoDataFrame shape: (204, 2) (2D shapes)
└── Tables
      └── 'table': AnnData (217, 5101)
with coordinate systems:
    ▸ 'global', with elements:
        morphology_focus (Images), cell_labels (Labels), nucleus_labels (Labels), transcripts (Points), cell_boundaries (Shapes), nucleus_boundaries (Shapes)
217
D652
SpatialData object
├── Images
│     └── 'morphology_focus': DataTree[cyx] (4, 4354, 4153), (4, 2177, 2077), (4, 1089, 1038), (4, 544, 520), (4, 272, 260)
├── Labels
│     ├── 'cell_labels': DataTree[yx] (4354, 4153), (2177, 2077), (1089, 1038), (544, 520), (272, 260)
│     └── 'nucleus_labels': DataTree[yx] (4354, 4153), (2177, 2077), (1089, 1038), (544, 520), (272, 260)
├── Points
│     └── 'transcripts': DataFrame with shape: (<Delayed>, 13) (3D points)
├── Shapes
│     ├── 'cell_boundaries': GeoDataFrame shape: (3840, 1) (2D shapes)
│     └── 'nucleus_boundaries': GeoDataFrame shape: (3969, 2) (2D shapes)
└── Tables
      └── 'table': AnnData (3840, 5101)
with coordinate systems:
    ▸ 'global', with elements:
        morphology_focus (Images), cell_labels (Labels), nucleus_labels (Labels), transcripts (Points), cell_boundaries (Shapes), nucleus_boundaries (Shapes)
3840
D651
SpatialData object
├── Images
│     └── 'morphology_focus': DataTree[cyx] (4, 8329, 8289), (4, 4164, 4144), (4, 2082, 2072), (4, 1041, 1036), (4, 520, 518)
├── Labels
│     ├── 'cell_labels': DataTree[yx] (8329, 8289), (4164, 4144), (2082, 2072), (1041, 1036), (520, 518)
│     └── 'nucleus_labels': DataTree[yx] (8329, 8289), (4164, 4144), (2082, 2072), (1041, 1036), (520, 518)
├── Points
│     └── 'transcripts': DataFrame with shape: (<Delayed>, 13) (3D points)
├── Shapes
│     ├── 'cell_boundaries': GeoDataFrame shape: (6917, 1) (2D shapes)
│     └── 'nucleus_boundaries': GeoDataFrame shape: (6890, 2) (2D shapes)
└── Tables
      └── 'table': AnnData (6917, 5101)
with coordinate systems:
    ▸ 'global', with elements:
        morphology_focus (Images), cell_labels (Labels), nucleus_labels (Labels), transcripts (Points), cell_boundaries (Shapes), nucleus_boundaries (Shapes)
6917
D657
SpatialData object
├── Images
│     └── 'morphology_focus': DataTree[cyx] (4, 9280, 9359), (4, 4640, 4680), (4, 2320, 2340), (4, 1160, 1170), (4, 580, 585)
├── Labels
│     ├── 'cell_labels': DataTree[yx] (9280, 9359), (4640, 4680), (2320, 2340), (1160, 1170), (580, 585)
│     └── 'nucleus_labels': DataTree[yx] (9280, 9359), (4640, 4680), (2320, 2340), (1160, 1170), (580, 585)
├── Points
│     └── 'transcripts': DataFrame with shape: (<Delayed>, 13) (3D points)
├── Shapes
│     ├── 'cell_boundaries': GeoDataFrame shape: (11917, 1) (2D shapes)
│     └── 'nucleus_boundaries': GeoDataFrame shape: (11992, 2) (2D shapes)
└── Tables
      └── 'table': AnnData (11917, 5101)
with coordinate systems:
    ▸ 'global', with elements:
        morphology_focus (Images), cell_labels (Labels), nucleus_labels (Labels), transcripts (Points), cell_boundaries (Shapes), nucleus_boundaries (Shapes)
11917
D649
SpatialData object
├── Images
│     └── 'morphology_focus': DataTree[cyx] (4, 6418, 6931), (4, 3210, 3466), (4, 1604, 1733), (4, 803, 866), (4, 401, 433)
├── Labels
│     ├── 'cell_labels': DataTree[yx] (6418, 6931), (3210, 3466), (1604, 1733), (803, 866), (401, 433)
│     └── 'nucleus_labels': DataTree[yx] (6418, 6931), (3210, 3466), (1604, 1733), (803, 866), (401, 433)
├── Points
│     └── 'transcripts': DataFrame with shape: (<Delayed>, 13) (3D points)
├── Shapes
│     ├── 'cell_boundaries': GeoDataFrame shape: (5807, 1) (2D shapes)
│     └── 'nucleus_boundaries': GeoDataFrame shape: (5921, 2) (2D shapes)
└── Tables
      └── 'table': AnnData (5807, 5101)
with coordinate systems:
    ▸ 'global', with elements:
        morphology_focus (Images), cell_labels (Labels), nucleus_labels (Labels), transcripts (Points), cell_boundaries (Shapes), nucleus_boundaries (Shapes)
5807