Extract images of individual samples from whole slide image#

Per run of technologies such as Xenium In Situ or Merscope, multiple tissue sections can be measured. While the spatial transcriptomics measurements of the tissue sections are often done per tissue sections, scans of subsequent H&E stainings often contain all tissue sections at once. This makes it necessary to extract the individual H&E scans from the whole slide image before registering them to the spatial transcriptomics datasets. Here, we present a InSituPy-based approach to achieve this.

## The following code ensures that all functions and init files are reloaded before executions.
%load_ext autoreload
%autoreload 2
# import packages
from insitupy import InSituData, ImageData, CACHE
from insitupy import InSituExperiment

Load images and create an InSituData object#

# setup empty InSituData object
xd = InSituData()
xd.images = ImageData()

# add image
he_path = CACHE / "demo_datasets/hbreastcancer/unregistered_images/slide_id__hbreastcancer__HE__histo.ome.tif"
xd.images.add_image(image=he_path, name="HE")
xd
InSituData
Method:		unknown
Slide ID:	None
Sample ID:	None
Path:		None
Metadata file:	None
    ➤ images
       HE:	(24241, 30786, 3)

Visualize the data to select the individual samples#

xd.show()

Add regions to image#

To add regions, one can use the “Add geometries” widget on the right side of the napari window:

  1. Select Type = “Regions”

  2. Add a Key value and a Class name

  3. Press Add geometry layer

  4. Add one rectangle per region layer. !!! Important: Per region layer only one shape is allowed.

  5. Repeat this for all regions.

../../_images/select_sample_region.jpg

Import the regions from the napari viewer into the InSituData object#

# import the selected regions
xd.store_geometries()
Added 1 new regions to key 'TestKey'
Added 1 new regions to existing key 'TestKey'
Added 1 new regions to existing key 'TestKey'
xd
InSituData
Method:		unknown
Slide ID:	None
Sample ID:	None
Path:		None
Metadata file:	None
    ➤ images
       HE:	(24241, 30786, 3)
    ➤ annotationsregions
       TestKey:	3 regions, 3 classes ('Region 1','Region 2','Region 3') 

Create an InSituExperiment object using the annotated regions#

exp = InSituExperiment.from_regions(
    data=xd, region_key="TestKey"
)
Region 1
Region 2
Region 3
exp
InSituExperiment with 3 samples:
           uid  CITAR slide_id sample_id region_key region_name
0     878e9d5c  -+-++     None      None    TestKey    Region 1
1     717f3107  -+-++     None      None    TestKey    Region 2
2     c58f37ab  -+-++     None      None    TestKey    Region 3

Save the individual images#

for m, d in exp.iterdata():
    d.images.save(output_folder=f"out/{m['region_name']}", as_zarr=False)

After saving the individual images, one can now continue by registering the images to the spatial transcriptomics data as shown in notebook 01_InSituPy_demo_register_images.ipynb.