Working with scanpy Package

Scanpy is a one of the best toolkit for single-cell RNA sequencing data analysis in Python. stLearn is created based on a part of scanpy and also our tool is partially compatible with scanpy.

We recommend to use both scanpy and stLearn for the analysis.

Convert AnnData from scanpy to work with stLearn function

stLearn also uses the AnnData as the core object like scanpy. However, we store the spatial data in different way.

Then if you read data by scanpy, you need to convert to stLearn AnnData format. But if you read the data by stLearn, you can use almost of all functions from scanpy.

[1]:
import stlearn as st
import scanpy as sc
import pathlib as pathlib
/Users/andrew/conda/stlearn/lib/python3.10/site-packages/louvain/__init__.py:54: UserWarning: pkg_resources is deprecated as an API. See https://setuptools.pypa.io/en/latest/pkg_resources.html. The pkg_resources package is slated for removal as early as 2025-11-30. Refrain from using this package or pin to Setuptools<81.
  from pkg_resources import get_distribution, DistributionNotFound
/Users/andrew/conda/stlearn/lib/python3.10/site-packages/stlearn/tl/cci/het.py:206: NumbaDeprecationWarning: The keyword argument 'nopython=False' was supplied. From Numba 0.59.0 the default is being changed to True and use of 'nopython=False' will raise a warning as the argument will have no effect. See https://numba.readthedocs.io/en/stable/reference/deprecation.html#deprecation-of-object-mode-fall-back-behaviour-when-using-jit for details.
  @jit(parallel=True, nopython=False)
[2]:
# Read Visium data by scanpy
st.settings.datasetdir =  pathlib.Path.cwd().parent / "data"
adata = sc.datasets.visium_sge()
/Users/andrew/conda/stlearn/lib/python3.10/site-packages/anndata/_core/anndata.py:1758: UserWarning: Variable names are not unique. To make them unique, call `.var_names_make_unique`.
  utils.warn_names_duplicates("var")
/Users/andrew/conda/stlearn/lib/python3.10/site-packages/anndata/_core/anndata.py:1758: UserWarning: Variable names are not unique. To make them unique, call `.var_names_make_unique`.
  utils.warn_names_duplicates("var")
[3]:
adata
[3]:
AnnData object with n_obs × n_vars = 3798 × 36601
    obs: 'in_tissue', 'array_row', 'array_col'
    var: 'gene_ids', 'feature_types', 'genome'
    uns: 'spatial'
    obsm: 'spatial'
[4]:
# Convert AnnData object to work with stLearn
adata = st.convert_scanpy(adata)
[5]:
adata
[5]:
AnnData object with n_obs × n_vars = 3798 × 36601
    obs: 'in_tissue', 'array_row', 'array_col', 'imagecol', 'imagerow'
    var: 'gene_ids', 'feature_types', 'genome'
    uns: 'spatial'
    obsm: 'spatial'

After that you can work with all functions in scanpy and please check their tutorials for the detail processes.