Spatial plotting functionalities#

This notebook shows the insitupy plotting functionalities. It assumes that the previous steps of the demo notebooks such as preprocessing, import of annotations and regions and cell type annotation have been run.

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

Load data#

insitupy_project = Path(CACHE / "out/demo_insitupy_project")
xd = InSituData.read(insitupy_project)
xd.load_all()
xd
InSituData
Method:		Xenium
Slide ID:	0001879
Sample ID:	Replicate 1
Path:		C:\Users\ge37voy\.cache\InSituPy\out\demo_insitupy_project
Metadata file:	.ispy
    ➤ images
       nuclei:	(25778, 35416)
       CD20:	(25778, 35416)
       HER2:	(25778, 35416)
       HE:	(25778, 35416, 3)
    ➤ cells
       MultiCellData with main layer 'main'
           matrix
               AnnData object with n_obs × n_vars = 156447 × 297
               obs: 'transcript_counts', 'control_probe_counts', 'control_codeword_counts', 'total_counts', 'cell_area', 'nucleus_area', 'n_genes_by_counts', 'n_genes', 'leiden', 'cell_type_dc', 'cell_type_dc_sub', 'cell_type_tacco', 'cell_type_publ'
               var: 'gene_ids', 'feature_types', 'genome', 'n_cells_by_counts', 'mean_counts', 'pct_dropout_by_counts', 'total_counts', 'n_cells'
               uns: 'cell_type_dc_colors', 'cell_type_dc_sub', 'cell_type_dc_sub_colors', 'cell_type_publ_colors', 'cell_type_tacco_colors', 'counts_location', 'leiden', 'leiden_colors', 'log1p', 'neighbors', 'pca', 'umap'
               obsm: 'OT', 'X_pca', 'X_umap', 'annotations', 'ora_estimate', 'ora_pvals', 'regions', 'spatial'
               varm: 'OT', 'PCs'
               layers: 'counts', 'norm_counts'
               obsp: 'connectivities', 'distances'
           boundaries
               BoundariesData object with 2 entries:
                   cells
                   nuclei transcripts
       DataFrame with shape <dask_expr.expr.Scalar: expr=ReadParquetFSSpec(184a915).size() // 8, dtype=int64> x 8
    ➤ annotations
       TestKey:	9 annotations, 2 classes ('TestClass','points') ✔
       demo:	4 annotations, 1 class ('None') ✔
       demo2:	5 annotations, 1 class ('None') ✔
       demo3:	7 annotations, 1 class ('None') ✔
       Demo:	28 annotations, 2 classes ('Tumor cells','Stroma') ✔
    ➤ regions
       demo_regions:	3 regions, 3 classes ('Region1','Region2','Region3') ✔
       TMA:	6 regions, 6 classes ('B-2','A-3','B-1','B-3','A-1','A-2') ✔
       Demo:	3 regions, 3 classes ('Region 1','Region 3','Region 2') ✔

Sample-level or experimental level plotting of expression data#

from insitupy.plotting import plot_spatial

Using the keys argument, one or multiple keys can be selected to be displayed.

plot_spatial(xd, keys="cell_type_dc_sub")
../../_images/f6c89af97e87011107605b1f33b09cc886dfe0295a6208a44644938835783c14.png
plot_spatial(xd, keys=["cell_type_dc_sub", "ERBB2"])
../../_images/5188cde442af7f5480f6dbdc9a535a303760f0b9997ec99d94dd5e374eb290b7.png

The savepath argument can be used to save the plot in a certain path. The filename extension determines the format of the output image. When saving as .pdf long saving times can occur due to the large number of data points.

plot_spatial(xd, keys=["cell_type_dc_sub", "ERBB2"],
             savepath="figures/spatial-demo.png")
Saving figure to file figures/spatial-demo.png
Saved.
../../_images/5188cde442af7f5480f6dbdc9a535a303760f0b9997ec99d94dd5e374eb290b7.png

The image_key argument can be used to select a image to be displayed in the background.

plot_spatial(xd, keys=["cell_type_dc_sub", "ERBB2"],
             image_key="HE",
             )
../../_images/a70b8172344154b00226e29cc4d219c000ecbc0467aa628f59e13687082e4ca5.png

The xlim/ylim parameters can be used to zoom into the images and plot only selected regions and the spot_size argument to adapt the size of the spots. Further, the opacity of the spots can be adapted using alpha.

plot_spatial(xd, keys=["cell_type_dc_sub", "ERBB2"],
             spot_size=8, alpha=0.7,
             xlim=(1800, 2200), ylim=(2500, 3500),
             image_key="HE",
             )
../../_images/b9626efd49989b329ad34bfb3150bb988239e38d3a7ba10b20f64c7d84ef7d9e.png
plot_spatial(xd, keys=["cell_type_dc_sub", "ERBB2"],
             spot_size=8, alpha=0.7,
             xlim=(0, 7000), ylim=(0, 7000),
             image_key="HE",
             )
../../_images/86e5111b7065b9f28ad220367b69b3f75f55c921b25c8d3a01de8dbbe75d103d.png

When working with an InSituExperiment object for multi-sample analysis, one can directly plot all datasets within the InSituExperiment object. For details on generating InSituExperiment objects and working with them see this notebook.

from insitupy import InSituExperiment

First, we generate an InSituExperiment object.

exp = InSituExperiment.from_regions(
    data=xd, region_key="TMA"
)
A-1
A-2
A-3
B-1
B-2
B-3

And then we can use the plot_spatial function directly on the InSituExperiment object. The name_column argument can be used to determine the column in the .metadata dataframe to be used for naming the plot.

exp.metadata
uid slide_id sample_id region_key region_name
0 39abd0f9 0001879 Replicate 1 TMA A-1
1 a4b97164 0001879 Replicate 1 TMA A-2
2 bb7d859e 0001879 Replicate 1 TMA A-3
3 b2580297 0001879 Replicate 1 TMA B-1
4 ba9b5f9e 0001879 Replicate 1 TMA B-2
5 80290f88 0001879 Replicate 1 TMA B-3

When using both multiple datasets and multiple keys the plots are represented in a grid layout.

plot_spatial(exp, keys=["cell_type_dc_sub", "ERBB2", "ACTA2"], image_key="HE",
             name_column="region_name")
../../_images/f8592408f6d06288c40dac9f646dd1d8f665054bedef6458082f74f0fea74fc3.png

When using only multiple datasets but only one key, they are listed next to each other.

plot_spatial(exp, keys=["cell_type_dc_sub"], image_key="HE",
             name_column="region_name")
../../_images/2a1ebf98b1df72b9984b87f47bcdaffc6ead7421850583ad09651d555c2a9415.png