Analyse MibiTOF in Napari-SpatialData

Analyse MibiTOF in Napari-SpatialData#

This tutorial shows how to load and analyse the MibiTOF dataset with the Napari-SpatialData plugin.

Import packages and data#

There are two options to install napari-spatialdata:

(1) Run pip install napari-spatialdata

or,

(2) Clone this repo and run pip install -e .

%matplotlib inline
import matplotlib.pyplot as plt
from napari_spatialdata import Interactive
from spatialdata import SpatialData
import squidpy as sq
import scanpy as sc

plt.rcParams['figure.figsize'] = (20, 20)

Next, we will download the Nanostring dataset (cosmx_io) for analysis. Uncomment the lines below to download the dataset.

!mkdir tutorial_data
!mkdir tutorial_data/mibitof_data
!wget -P tutorial_data/mibitof_data https://s3.embl.de/spatialdata/spatialdata-sandbox/mibitof.zip
!tar -xzf tutorial_data/mibitof_data/mibitof.zip -C tutorial_data/mibitof_data/.
mkdir: tutorial_data: File exists
mkdir: tutorial_data/mibitof_data: File exists
--2023-05-04 12:28:27--  https://s3.embl.de/spatialdata/spatialdata-sandbox/mibitof.zip
Resolving s3.embl.de (s3.embl.de)... 194.94.45.80
Connecting to s3.embl.de (s3.embl.de)|194.94.45.80|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 8108095 (7,7M) [application/zip]
Saving to: ‘tutorial_data/mibitof_data/mibitof.zip.2’

mibitof.zip.2       100%[===================>]   7,73M  7,01MB/s    in 1,1s    

2023-05-04 12:28:29 (7,01 MB/s) - ‘tutorial_data/mibitof_data/mibitof.zip.2’ saved [8108095/8108095]

We will load the dataset from the filepath and create a spatialdata.SpatialData object. We’ll use this object with the class Interactive to visualise this dataset in Napari.

sdata = SpatialData.read("tutorial_data/mibitof_data/data.zarr")

Visualise in napari#

To make it easier to analyse our data, we will filter the SpatialData object by the coordinate system “point8”.

sdata = sdata.filter_by_coordinate_system("point8")
adata = sdata.table

The scikit-misc package could be required for highly variable genes identification.

Next, we will use the Scatter Widget offered by Napari SpatialData to visualise the UMAP coordinates.

First, we instantiate the Interactive class with our spatialdata.SpatialData object, and view it in Napari.

interactive = Interactive(sdata)
interactive.run()
plt.imshow(interactive.screenshot())
plt.axis('off')
(-0.5, 2559.5, 1601.5, -0.5)
../_images/5f6d477a2189898146d6e7007bdace45b8e8fa9a6b7e15075242f184313553a9.png

We select the coordinate system “point8” and load the elements “point8_image” and “point8_labels” into the viewer.

plt.imshow(interactive.screenshot())
plt.axis('off')
(-0.5, 2559.5, 1601.5, -0.5)
../_images/f5849d8203ab0fa758d57909013b354267baa3144e701fe0d520e4bb6bf74c5f.png

Select “point8_labels” in the list of layers. Then, we open the “Scatter” Widget by using the menu bar and going to Plugins > napari-spatialdata > Scatter. This loads the AnnData object associated with that layer into the “Scatter” Widget.

plt.imshow(interactive.screenshot())
plt.axis('off')
(-0.5, 2559.5, 1601.5, -0.5)
../_images/d26ceef3c4b6dd0a5b726b6a076483a54853cad529fd6cf1d7a1b1e71d60012b.png

Then, as previously, we can now pick specific x-axis, y-axis and color values to visualise in the scatterplot. In the example below, we’re visualising the UMAP (Uniform Manifold Approximation and Projection) coordinates of two different axes and coloring it by cell size.

plt.imshow(interactive.screenshot())
plt.axis('off')
(-0.5, 2559.5, 1601.5, -0.5)
../_images/99b88b8e035094e9046355ff30f785df9d6bffd27b9cb7a5b2807e3a41175944.png

After plotting, as mentioned in the previous sections, it is possible to interactively select clusters and export it to AnnData.

In the example below, we used the cursor to select a cluster on the left half of the scatterplot. We can export it into AnnData by clicking on the “Export” button.

plt.imshow(interactive.screenshot())
plt.axis('off')
(-0.5, 2559.5, 1601.5, -0.5)
../_images/1c85a82cacad35f681212cde92ce61c447440f44ab04474da254b09b93908d65.png

After exporting, we can now view the selected points with the View widget. Close the Scatter Widget and from the menu bar, go to Plugins > napari-spatialdata > View. You should be able to observe “point8_labels_LASSO_SELECTED” under “Observations:”. This is selected points we exported in the previous step.

plt.imshow(interactive.screenshot())
plt.axis('off')
(-0.5, 2559.5, 1857.5, -0.5)
../_images/4869a3a662bf8cf2e162714110bded6a60ec5e1da49d85fb2f2264591cc402b9.png

Double clicking on “point8_labels_LASSO_SELECTED” loads it as a layer in the Napari viewer.

plt.imshow(interactive.screenshot())
plt.axis('off')
(-0.5, 2559.5, 1857.5, -0.5)
../_images/71f80cedeb89ed0a2b4cfc03acef65460895738cac894e379797347913ddc852.png