Note

This page was generated from 1_multi-slices_alignment.ipynb. Interactive online version: Colab badge. Some tutorial content may look better in light mode.

1.slices alignment#

This notebook demonstrates the process of Aligning spatial transcriptome data. This is done in the following two steps.

  1. Align spatial transcriptome data from a set of multi-slices;

  2. Align spatial transcriptome data from two sets of multi-slices simultaneously.

  3. Reduce the amount of data by sampling to improve alignment speed.

Here we modified a published method PASTE, which utilized Fused Gromov-Wasserstein Optimal Transport (FGW-OT) algorithm, to leverages both gene expression similarity and spatial distances between spots to align and integrate spatial transcriptomics data.

Reference: Ron Zeira, Max Land, Benjamin J. Raphael. Alignment and Integration of Spatial Transcriptomics Data. bioRxiv, 2021.03.16.435604. doi: https://doi.org/10.1101/2021.03.16.435604

Packages#

[1]:
import os
import spateo as st
|-----> setting visualization default mode in dynamo. Your customized matplotlib settings might be overritten.
/home/yao/.local/lib/python3.8/site-packages/nxviz/__init__.py:18: UserWarning:
nxviz has a new API! Version 0.7.3 onwards, the old class-based API is being
deprecated in favour of a new API focused on advancing a grammar of network
graphics. If your plotting code depends on the old API, please consider
pinning nxviz at version 0.7.3, as the new API will break your old code.

To check out the new API, please head over to the docs at
https://ericmjl.github.io/nxviz/ to learn more. We hope you enjoy using it!

(This deprecation message will go away in version 1.0.)

  warnings.warn(
/home/yao/anaconda3/envs/BGIpy38_tf2/lib/python3.8/site-packages/requests/__init__.py:102: RequestsDependencyWarning: urllib3 (1.26.9) or chardet (5.0.0)/charset_normalizer (2.0.12) doesn't match a supported version!
  warnings.warn("urllib3 ({}) or chardet ({})/charset_normalizer ({}) doesn't match a supported "
/home/yao/anaconda3/envs/BGIpy38_tf2/lib/python3.8/site-packages/spaghetti/network.py:36: FutureWarning:

The next major release of pysal/spaghetti (2.0.0) will drop support for all ``libpysal.cg`` geometries. This change is a first step in refactoring ``spaghetti`` that is expected to result in dramatically reduced runtimes for network instantiation and operations. Users currently requiring network and point pattern input as ``libpysal.cg`` geometries should prepare for this simply by converting to ``shapely`` geometries.

Data source#

[2]:
# cellbin data
cellbin_slices = st.sample_data.drosophila(filename="E7-9h_cellbin_h5ad.zip")[3:7]

# binning data
binning_slices = st.sample_data.drosophila(filename="E7-9h_bin20_h5ad.zip")[3:7]

Align spatial transcriptome data from a set of multi-slices#

Slices alignment#

[3]:
cellbin_slices_sample = [slice.copy() for slice in cellbin_slices]
align_cellbin_slices = st.tl.models_align(
    models=cellbin_slices_sample,
    spatial_key="spatial",
    key_added="align_spatial",
    numItermax=200,
    numItermaxEmd=500000,
    device="0"
)
|-----> [Models alignment] in progress: 33.3333%|-----------> Filtered all samples for common genes. There are 7776 common genes.
|-----> [Models alignment] in progress: 66.6667%|-----------> Filtered all samples for common genes. There are 8334 common genes.
|-----> [Models alignment] in progress: 100.0000%|-----------> Filtered all samples for common genes. There are 8257 common genes.
|-----> [Models alignment] in progress: 100.0000%
|-----> [Models alignment] finished [24.5754s]

Visualize slices based on aligned coordinates#

[10]:
st.pl.multi_slices(
    slices=align_cellbin_slices.copy(),
    slices_key="slices",
    spatial_key="align_spatial",
    point_size=5,
    ax_height=0.6,
    ncols=2,
)
../../../_images/tutorials_notebooks_5_3d_reconstruction_1_multi-slices_alignment_9_0.png

Align spatial transcriptome data from two sets of multi-slices simultaneously#

Slices alignment#

[11]:
cellbin_slices_sample = [slice.copy() for slice in cellbin_slices]
binning_slices_sample = [slice.copy() for slice in binning_slices]
align_cellbin_slices, align_binning_slices = st.tl.models_align_ref(
    models=cellbin_slices_sample,
    models_ref=binning_slices_sample,
    spatial_key="spatial",
    key_added="align_spatial",
    numItermax=500,
    numItermaxEmd=100000,
    device="0"
)
|-----> [Models alignment] in progress: 33.3333%|-----------> Filtered all samples for common genes. There are 7776 common genes.
|-----> [Models alignment] in progress: 66.6667%|-----------> Filtered all samples for common genes. There are 8334 common genes.
|-----> [Models alignment] in progress: 100.0000%|-----------> Filtered all samples for common genes. There are 8257 common genes.
|-----> [Models alignment] in progress: 100.0000%
|-----> [Models alignment] finished [3.9251s]

Visualize slices based on aligned coordinates#

[12]:
st.pl.multi_slices(
    slices=align_cellbin_slices.copy(),
    slices_key="slices",
    spatial_key="align_spatial",
    point_size=5,
    ax_height=0.6,
    ncols=2,
)
../../../_images/tutorials_notebooks_5_3d_reconstruction_1_multi-slices_alignment_14_0.png
[14]:
st.pl.multi_slices(
    slices=align_binning_slices.copy(),
    slices_key="slices",
    spatial_key="align_spatial",
    point_size=5,
    ax_height=0.6,
    ncols=2,
)
../../../_images/tutorials_notebooks_5_3d_reconstruction_1_multi-slices_alignment_15_0.png

Reduce the amount of data by sampling to improve alignment speed#

Slices alignment#

[15]:
cellbin_slices_sample = [slice.copy() for slice in cellbin_slices]
align_cellbin_slices, _ = st.tl.models_align_ref(
    models=cellbin_slices_sample,
    models_ref=None,
    n_sampling=500,
    sampling_method="trn",
    spatial_key="spatial",
    key_added="align_spatial",
    numItermax=500,
    numItermaxEmd=100000,
    device="0"
)
|-----> [Running TRN] in progress: 100.0000%
|-----> [Running TRN] finished [24.1933s]
|-----> [Running TRN] in progress: 100.0000%
|-----> [Running TRN] finished [24.2045s]
|-----> [Running TRN] in progress: 100.0000%
|-----> [Running TRN] finished [24.0666s]
|-----> [Running TRN] in progress: 100.0000%
|-----> [Running TRN] finished [23.8935s]
|-----> [Models alignment] in progress: 33.3333%|-----------> Filtered all samples for common genes. There are 7776 common genes.
|-----> [Models alignment] in progress: 66.6667%|-----------> Filtered all samples for common genes. There are 8334 common genes.
|-----> [Models alignment] in progress: 100.0000%|-----------> Filtered all samples for common genes. There are 8257 common genes.
|-----> [Models alignment] in progress: 100.0000%
|-----> [Models alignment] finished [2.8459s]

Visualize slices based on aligned coordinates#

[16]:
st.pl.multi_slices(
    slices=align_cellbin_slices.copy(),
    slices_key="slices",
    spatial_key="align_spatial",
    point_size=5,
    ax_height=0.6,
    ncols=2,
)
../../../_images/tutorials_notebooks_5_3d_reconstruction_1_multi-slices_alignment_20_0.png
[ ]: