# Analyze Nanostring data

In this tutorial we show how we can use Squidpy and Scanpy for the analysis of Nanostring data.

[1]:
import scanpy as sc
import squidpy as sq
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from pathlib import Path

scanpy==1.9.0.dev183+gd74a0e68 anndata==0.8.0rc2.dev27+ge524389 umap==0.5.1 numpy==1.19.5 scipy==1.7.0 pandas==1.3.0 scikit-learn==0.24.2 statsmodels==0.12.2 python-igraph==0.9.6 pynndescent==0.5.4

Download the data from Nanostring FFPE Dataset. Unpack the .tar.gz file. Load the unpacked dataset into an anndata.AnnData object. The dataset used here consists of a non-small-cell lung cancer (NSCLC) tissue which represents the largest single-cell and sub-cellular analysis on Formalin-Fixed Paraffin-Embedded (FFPE) samples.

[2]:
# !mkdir tutorial_data
# !mkdir tutorial_data/nanostring_data
# !wget -P tutorial_data/nanostring_data https://nanostring-public-share.s3.us-west-2.amazonaws.com/SMI-Compressed/Lung5_Rep2/Lung5_Rep2+SMI+Flat+data.tar.gz
# !tar -xzf tutorial_data/nanostring_data/Lung5_Rep2+SMI+Flat+data.tar.gz -C tutorial_data/nanostring_data/.
[3]:
nanostring_dir = Path().resolve() / "tutorial_data" / "nanostring_data"
sample_dir = nanostring_dir / "Lung5_Rep2" / "Lung5_Rep2-Flat_files_and_images"

path=sample_dir,
counts_file="Lung5_Rep2_exprMat_file.csv",
fov_file="Lung5_Rep2_fov_positions_file.csv",
)
WARNING: FOV 31 does not exist, skipping it.
WARNING: FOV 32 does not exist, skipping it.

Make the variable names unique using the method anndata.var_names_make_unique. Obtain the mitochondrial genes using their names prefixed with “mt-”. Calculate the quality control metrics on the anndata.AnnData using scanpy.pp.calculate_qc_metrics.

[4]:

## Calculate quality control metrics

Plot the “total_counts” and “n_genes_by_counts” from adata.obs. The first subplot shows adata.obs["total_counts"], the second adata.obs["total_counts"] less than 10000. The third subplot displays adata.obs["n_genes_by_counts"] while the fourth displays the adata.obs["n_genes_by_counts"] less than 4000.

[5]:
fig, axs = plt.subplots(1, 4, figsize=(15, 4))
sns.distplot(
kde=False,
ax=axs[0],
)
sns.distplot(
kde=False,
bins=40,
ax=axs[1],
)
sns.distplot(
kde=False,
bins=60,
ax=axs[2],
)
sns.distplot(
kde=False,
bins=60,
ax=axs[3],
)
/Users/giovanni.palla/miniconda3/envs/squidpy/lib/python3.8/site-packages/seaborn/distributions.py:2557: FutureWarning: distplot is a deprecated function and will be removed in a future version. Please adapt your code to use either displot (a figure-level function with similar flexibility) or histplot (an axes-level function for histograms).
warnings.warn(msg, FutureWarning)
[5]:
<AxesSubplot:xlabel='n_genes_by_counts'>

Filter the cells based on the minimum number of counts required using scanpy.pp.filter_cells. Filter the genes based on the minimum number of cells required with scanpy.pp.filter_genes. The parameters for both were specified based on the plots above. They were set to filter out nearly 10% of the cells and genes with minimum counts and minimum cells respectively. It’s a conservative filtering, more relaxed ones could be also useful.

[6]:

Annotate the highly variable genes based on the count data by using scanpy.pp.highly_variable_genes with flavor="seurat_v3". Normalize counts per cell using scanpy.pp.normalize_total.

Logarithmize, do principal component analysis, compute a neighborhood graph of the observations using scanpy.pp.log1p, scanpy.pp.pca and scanpy.pp.neighbors respectively.

Use scanpy.tl.umap to embed the neighborhood graph of the data and cluster the cells into subgroups employing scanpy.tl.leiden.

You may have to install scikit-misc package for highly variable genes identification.

[8]:
# !pip install scikit-misc
[9]:

## Visualize annotation on UMAP and spatial coordinates

Subplot with scatter plot in UMAP (Uniform Manifold Approximation and Projection) basis. The embedded points were colored, respectively, according to the total counts, number of genes by counts and leiden clusters in each of the subplots. This gives us some idea of what the data looks like.

[10]:
sc.pl.umap(
color=[
"total_counts",
"n_genes_by_counts",
"leiden",
],
wspace=0.4,
)

Plot segmentation masks using squidpy.pl.spatial_segment. Specifically, the key library_id in adata.obs contains the same unique values contained in adata.uns["spatial"]. The "cell_ID" column is used to spot individual cells. Here, the images were colored in accordance with the intensity of the maximum pan-cytokeratin (CK) staining.

[12]:
sq.pl.spatial_segment(
color="Max.PanCK",
library_key="fov",
seg_cell_id="cell_ID",
)

The argument library_id can also be altered to obtain specific field(s) of view.

[13]:
sq.pl.spatial_segment(
color="Max.PanCK",
library_key="fov",
library_id=["12", "16"],
seg_cell_id="cell_ID",
)

There are several parameters that can be controlled. For instance, it is possible to plot segmentation masks as “contours”, in order to visualize the underlying image. The co-ordinates of the FOV can be specified using the argument crop_coord, to get the view of required section.

[14]:
sq.pl.spatial_segment(
color="Max.PanCK",
library_key="fov",
library_id="12",
seg_cell_id="cell_ID",
seg_contourpx=10,
crop_coord=[(0, 0, 700, 700)],
)

If groups of observations are plotted (as above), it’s possible to modify whether to “visualize” the segmentation masks that do not belong to any selected group. It is set as “transparent” by default (see above) but in cases where e.g. no image is present it can be useful to visualize them nonetheless.

A scale bar can also be added, where size and pixel units must be passed. The sizes of the scalebars for these examples are not real values and are purely for visualization purposes.

[15]:
sq.pl.spatial_segment(
color="Area",
library_key="fov",
library_id=["12", "16"],
seg_cell_id="cell_ID",
seg_outline=True,
cmap="plasma",
img=False,
scalebar_dx=1.0,
scalebar_kwargs={"scale_loc": "bottom", "location": "lower right"},
)

Plot the image, with an overlay of the Leiden clusters. Use squidpy.pl.spatial_segment for the same. The image is not visualized by specifying img=False.

One or multiple groups can also be used to overlay, by specifying the groups argument in squidpy.pl.spatial_segment as shown in the second subplot.

[20]:
fig, ax = plt.subplots(1, 2, figsize=(15, 7))
sq.pl.spatial_segment(
shape="hex",
color="leiden",
library_key="fov",
library_id="12",
seg_cell_id="cell_ID",
img=False,
size=60,
ax=ax[0],
)

sq.pl.spatial_segment(
color="leiden",
seg_cell_id="cell_ID",
library_key="fov",
library_id="16",
img=False,
size=60,
ax=ax[1],
)

## Building the spatial neighbors graphs

Spatial graph is a graph of spatial neighbors with observations as nodes and neighborhood relations between observations as edges. We use spatial coordinates of spots/cells to identify neighbors among them. Different approaches of defining a neighborhood relation among observations are used for different types of spatial datasets. We use squidpy.gr.spatial_neighbors to compute the spatial neighbors graph. We use this function for a non-grid dataset with coord_type = 'generic'.

Depending on the coord_type, n_neighs specifies the number of neighboring tiles if coord_type='grid' and when the coord_type is not a grid, n_neighs represents the number of neighborhoods. Moreover, radius is only available when coord_type='generic'.

Alternatively, delaunay = True can be used, for a Delaunay triangulation graph. This way, we can observe the difference in using K-nearest neighbors and Delaunay triangulation. You can appreciate that the neighbor graph is different than before.

[21]:
fig, ax = plt.subplots(1, 2, figsize=(15, 15))
sq.gr.spatial_neighbors(
n_neighs=10,
coord_type="generic",
)
idx = np.append(idx, 420)
sq.pl.spatial_scatter(
library_id="16",
color="leiden",
connectivity_key="spatial_connectivities",
size=3,
edges_width=1,
edges_color="black",
img=False,
title="K-nearest neighbors",
ax=ax[0],
)

sq.gr.spatial_neighbors(
n_neighs=10,
coord_type="generic",
delaunay=True,
)
idx = np.append(idx, 420)
sq.pl.spatial_scatter(
library_id="16",
color="leiden",
connectivity_key="spatial_connectivities",
size=3,
edges_width=1,
edges_color="black",
img=False,
title="Delaunay triangulation",
ax=ax[1],
)
/Users/giovanni.palla/Projects/anndata/anndata/compat/_overloaded_dict.py:106: ImplicitModificationWarning: Trying to modify attribute ._uns of view, initializing view as actual.
self.data[key] = value
/Users/giovanni.palla/Projects/anndata/anndata/compat/_overloaded_dict.py:106: ImplicitModificationWarning: Trying to modify attribute ._uns of view, initializing view as actual.
self.data[key] = value

The function builds a spatial graph and saves its adjacency matrix adata.obsp['spatial_connectivities'] and distances to adata.obsp['spatial_distances'] by default.

[22]:
[22]:
<89246x89246 sparse matrix of type '<class 'numpy.float64'>'
with 534344 stored elements in Compressed Sparse Row format>
[23]:
[23]:
<89246x89246 sparse matrix of type '<class 'numpy.float64'>'
with 534344 stored elements in Compressed Sparse Row format>

In order to get all spots within a specified radius (in units of the spatial coordinates) from each spot as neighbors, the parameter radius should be used. We can observe how this plot is unlike the above one.

[24]:
sq.gr.spatial_neighbors(
coord_type="generic",
)

idx = np.append(idx, 420)
sq.pl.spatial_scatter(
library_id="16",
color="leiden",
connectivity_key="spatial_connectivities",
size=3,
edges_width=1,
edges_color="black",
img=False,
)
/Users/giovanni.palla/Projects/anndata/anndata/compat/_overloaded_dict.py:106: ImplicitModificationWarning: Trying to modify attribute ._uns of view, initializing view as actual.
self.data[key] = value
[25]:
[25]:
<89246x89246 sparse matrix of type '<class 'numpy.float64'>'
with 1101570 stored elements in Compressed Sparse Row format>

## Compute centrality scores

This example shows how to compute centrality scores, given a spatial graph and cell type annotation.

The scores calculated are closeness centrality, degree centrality and clustering coefficient with the following properties: * closeness centrality - measure of how close the group is to other nodes. * clustering coefficient - measure of the degree to which nodes cluster together. * degree centrality - fraction of non-group members connected to group members.

All scores are descriptive statistics of the spatial graph.

This dataset contains Leiden cluster groups’ annotations in anndata.AnnData.obs, which are used for calculation of centrality scores.

First, we need to compute a connectivity matrix from spatial coordinates to calculate the centrality scores. We can use squidpy.gr.spatial_neighbors for this purpose. We use the coord_type="generic" based on the data and the neighbors are classified with Delaunay triangulation by specifying delaunay=True.

[26]:
)

Centrality scores are calculated with squidpy.gr.centrality_scores, with the Leiden clusters.

[27]:

The results were visualized by plotting the average centrality, closeness centrality, and degree centrality using squidpy.pl.centrality_scores.

[28]:

## Compute co-occurrence probability

This example shows how to compute the co-occurrence probability.

The co-occurrence score is defined as:

$$\frac{p(exp|cond)}{p(exp)}$$

where $$p(exp|cond)$$ is the conditional probability of observing a cluster $$exp$$ conditioned on the presence of a cluster $$cond$$, whereas $$p(exp)$$ is the probability of observing $$exp$$ in the radius size of interest. The score is computed across increasing radii size around each cell in the tissue.

We can compute the co-occurrence score with squidpy.gr.co_occurrence. Results of co-occurrence probability ratio can be visualized with squidpy.pl.co_occurrence. The ‘3’ in the $$\frac{p(exp|cond)}{p(exp)}$$ represents a Leiden clustered group.

[29]:

We can further visualize tissue organization in spatial coordinates with squidpy.pl.spatial_segment, with an overlay of the expressed genes which were colored in consonance with the Leiden clusters.

[30]:
sq.gr.co_occurrence(
cluster_key="leiden",
)
sq.pl.co_occurrence(
cluster_key="leiden",
clusters="3",
)

sq.pl.spatial_segment(
shape="hex",
color="leiden",
library_id="16",
library_key="fov",
seg_cell_id="cell_ID",
img=False,
size=60,
)
100%|████████████████████████████████████████████████| 1/1 [00:11<00:00, 11.68s/]

## Neighbors enrichment analysis

This example shows how to run the neighbors enrichment analysis routine.

It calculates an enrichment score based on proximity on the connectivity graph of cell clusters. The number of observed events is compared against $$N$$ permutations and a z-score is computed.

This dataset contains cell type annotations in anndata.Anndata.obs which are used for calculation of the neighborhood enrichment. We calculate the neighborhood enrichment score with squidpy.gr.nhood_enrichment.

[31]:
100%|██████████████████████████████████████████| 1000/1000 [00:15<00:00, 64.85/s]

The same can be done for a specific FOV, by creating a subset of the anndata.AnnData.

[32]:
100%|█████████████████████████████████████████| 1000/1000 [00:08<00:00, 123.89/s]

And visualize the results with squidpy.pl.nhood_enrichment.

[33]:
fig, ax = plt.subplots(1, 3, figsize=(22, 22))
sq.pl.nhood_enrichment(
cluster_key="leiden",
figsize=(3, 3),
ax=ax[0],
)
sq.pl.nhood_enrichment(
cluster_key="leiden",
figsize=(3, 3),
ax=ax[1],
)
sq.pl.spatial_segment(
shape="hex",
color="leiden",
library_id="16",
library_key="fov",
seg_cell_id="cell_ID",
img=False,
size=60,
ax=ax[2],
)

## Compute Ripley’s statistics

This example shows how to compute the Ripley’s L function.

The Ripley’s L function is a descriptive statistics generally used to determine whether points have a random, dispersed or clustered distribution pattern at certain scale. The Ripley’s L is a variance-normalized version of the Ripley’s K statistic. There are also 2 other Ripley’s statistics available (that are closely related): ‘G’ and ‘F’.

Ripley’s G monitors the portion of points for which the nearest neighbor is within a given distance threshold, and plots that cumulative percentage against the increasing distance radii.

For increasing separation range, Ripley’s F function assembles the percentage of points which can be found in the aforementioned range from an arbitrary point pattern spawned in the expanse of the noticed pattern.

We can compute the Ripley’s L function with squidpy.gr.ripley. Results can be visualized with squidpy.pl.ripley. The same was plotted for adata_subset. Other Ripley’s statistics can be specified using mode = 'G' or mode = 'F'.

[34]:
mode = "L"
fig, ax = plt.subplots(1, 2, figsize=(20, 6))

sq.pl.ripley(
cluster_key="leiden",
mode=mode,
ax=ax[0],
)

sq.pl.spatial_segment(
shape="hex",
color="leiden",
library_id="16",
library_key="fov",
seg_cell_id="cell_ID",
img=False,
size=60,
ax=ax[1],
)

## Compute Moran’s I score

This example shows how to compute the Moran’s I global spatial auto-correlation statistics.

The Moran’s I global spatial auto-correlation statistics evaluates whether features (i.e. genes) shows a pattern that is clustered, dispersed or random in the tissue are under consideration.

We can compute the Moran’s I score with squidpy.gr.spatial_autocorr and mode = 'moran'. We first need to compute a spatial graph with squidpy.gr.spatial_neighbors. We will also subset the number of genes to evaluate.

[42]:
sq.gr.spatial_autocorr(
mode="moran",
n_perms=100,
n_jobs=1,
)
100%|██████████| 100/100 [04:26<00:00,  2.67s/]
[42]:
I pval_norm var_norm pval_z_sim pval_sim var_sim pval_norm_fdr_bh pval_z_sim_fdr_bh pval_sim_fdr_bh
KRT19 0.774879 0.0 0.000076 0.0 0.009901 0.000193 0.0 0.0 0.01929
OLFM4 0.752305 0.0 0.000076 0.0 0.009901 0.000204 0.0 0.0 0.01929
CEACAM6 0.749981 0.0 0.000076 0.0 0.009901 0.000179 0.0 0.0 0.01929
KRT17 0.665890 0.0 0.000076 0.0 0.009901 0.000172 0.0 0.0 0.01929
S100A6 0.624956 0.0 0.000076 0.0 0.009901 0.000132 0.0 0.0 0.01929
TM4SF1 0.586031 0.0 0.000076 0.0 0.009901 0.000196 0.0 0.0 0.01929
EPCAM 0.556131 0.0 0.000076 0.0 0.009901 0.000171 0.0 0.0 0.01929
KRT8 0.528026 0.0 0.000076 0.0 0.009901 0.000132 0.0 0.0 0.01929
ANXA2 0.511835 0.0 0.000076 0.0 0.009901 0.000163 0.0 0.0 0.01929
PIGR 0.498941 0.0 0.000076 0.0 0.009901 0.000155 0.0 0.0 0.01929

We can visualize some of those genes with squidpy.pl.spatial_segment. We could also pass mode = 'geary' to compute a closely related auto-correlation statistic, Geary’s C. See squidpy.gr.spatial_autocorr for more information.

[43]:
sq.pl.spatial_segment(