Skip to content
QihuangZhangPublic

About

CeLEry: cell location recovery in single-cell RNA sequencing

Resources

Stars

38 stars

Watchers

6 watching

Forks

Repository files navigation

CeLEry

tests docs PyPI license

CeLEry (Cell Location recovEry) is a supervised deep learning method that recovers the spatial origin of cells in single-cell RNA-seq (scRNA-seq) data by learning how gene expression relates to location in spatial transcriptomics data. It predicts the 2D coordinates, the tissue layer or the spatial domain of each cell together with an estimate of uncertainty, and it can enlarge a small spatial reference with replicates generated by a variational autoencoder.

Qihuang Zhang*, Shunzhou Jiang, Amelia Schroeder, Jian Hu, Kejie Li, Baohong Zhang, David Dai, Edward B. Lee, Rui Xiao and Mingyao Li* (*corresponding authors)

Documentation: https://qihuangzhang.github.io/CeLEry/

Maintenance status. CeLEry is maintained for compatibility with current dependencies and was last tested on 2026-10-28 with the versions listed under Installation.

Overview of the CeLEry workflow

A deep neural network learns, from the spots of a spatial transcriptomics section, how expression relates to location, and then places the cells of an scRNA-seq dataset on that section. With CeLEry you can:

  • predict the 2D coordinates of each cell on the section;
  • give each predicted location a confidence score, 1 minus the size of a region that contains the true location with a chosen probability;
  • assign each cell to a tissue layer or a spatial domain, with the probability of each;
  • enlarge a small spatial reference with replicates generated by a variational autoencoder;
  • assign single-nucleus data to the eight spatial domains of a mouse brain section with a pretrained model.

This repository contains the Python package that accompanies the CeLEry paper (Zhang et al., Nature Communications, 2023), its tutorials and the code of the analyses in the paper.


Repository layout

  • CeLEry_package/: the Python package CeLEry, published on PyPI as CeLEryPy.
  • examples/: the quick start and its data, which make_quickstart_data.py draws from the tutorial data.
  • tests/: the smoke tests and the tests of the documentation pages, run by GitHub Actions on every change.
  • tutorial/: the original tutorials as Markdown files and Jupyter notebooks, with their data.
  • pretrainmodel/Biogen/: a model pretrained on a mouse brain spatial transcriptomics section from Biogen (section 075B), used by the pretrained-model tutorial.
  • code_paper/: the code of the analyses in the paper, one folder per case study (see code_paper/README.md).
  • docs/: the documentation website (MkDocs); the logo is in docs/assets/.
  • environment.yml: the tested environment.
  • RENOVATION_LOG.md, FUTURE_WORK.md: the record of the 2026 maintenance release and the changes left open.
  • CHANGELOG.md, CITATION.cff, LICENSE: release history, citation metadata and the MIT license.

API at a glance

The steps of the Methods of the paper map to the following functions, all called as cel.<function> after import CeLEry as cel.

Manuscript step (Methods) Functions
Data preprocessing: gene filtering and log transformation of the counts, genes shared by the two datasets make_annData_spatial(), make_annData_query(), get_common_genes()
Data preprocessing: standardization of each gene across the spots or cells of each dataset get_zscore(), drop_NaN()
Data augmentation for the training ST data: replicates generated by a variational autoencoder DataAugmentation(), AugFit_domain()
Location prediction model: 2D coordinates (Eq. 3) Fit_cord(), Predict_cord()
Location prediction model: uncertainty region of the predicted location (Eq. 4) Fit_region(), Predict_region()
Location prediction model: spatial domains, with a logistic regression loss (Eq. 5) Fit_domain(), Predict_domain()
Location prediction model: tissue layers, with a rank-consistent logistic regression loss (Eq. 6) Fit_layer(), Predict_layer()
Evaluation of the predictions against known locations or layers (Results) get_Layer_LIBD(), plot_confusion_matrix(), RelocationPlot()

The reference documents the arguments, return values and output files of the main functions.


Quick start data

The quick start uses as the reference 1,000 super-pixels of a 10x Visium section of the mouse posterior brain, whose resolution was enhanced with TESLA, as in the paper; one unit of their coordinates is 50 pixels of the histology image. The query is 300 cells of a mouse brain scRNA-seq dataset, with 352 genes in common. One super-pixel in five is held out from training, and its known position is the answer that the predictions are checked against. examples/make_quickstart_data.py draws both files from the tutorial data with a fixed seed.


Installation

CeLEry needs no GPU, so the environment specifies the CPU build of PyTorch, which conda-forge provides for Linux, macOS and Windows. Clone the repository, create the environment with conda, mamba or micromamba, and install CeLEry from the clone:

git clone https://github.com/QihuangZhang/CeLEry.git
cd CeLEry
conda env create -f environment.yml     # or: micromamba create -f environment.yml
conda activate celery
pip install --no-deps ./CeLEry_package

Creating the environment took about five minutes on the test machine. On Windows, micromamba needs a short root folder or long-path support (see Installation in the documentation). pip install CeLEryPy installs release 1.2.2, which contains the same code but lets pip choose the dependency versions. Release 1.2.1 (April 2024) fails on sparse expression matrices with SciPy 1.14 or later.

Python PyTorch NumPy SciPy pandas anndata scanpy scikit-learn scikit-image matplotlib
3.12.15 2.14.1 (CPU) 2.5.4 1.18.1 2.3.3 0.13.4 1.12.4 1.9.1 0.26.0 3.11.2

These versions were tested on 2026-10-28 on Windows 11, with the quick start, the smoke tests and every code block of tasks 1 to 3 of tutorial/tutorial.md (3 training epochs per model), and on Ubuntu under WSL2, with the quick start and the smoke tests. pandas stays below 3.0 because pandas 3 breaks get_Layer_LIBD. RENOVATION_LOG.md gives the reason for each pin and compares the results with those obtained with the dependencies of 2024. The authors recommend 16 GB of RAM and 4 CPU cores, with 2 GB of RAM as the minimum.


Quick start

python examples/quickstart.py

The script trains the coordinate model on 800 spots, reports the error on the 200 held-out spots and writes the predicted coordinates of the 300 single cells to examples/output/. It ran in under a minute on Windows and in under 15 s on Linux. The tests run with python -m pytest -q tests.

A shorter version in Python, without the held-out check, run from the root of the clone:

import scanpy as sc
import CeLEry as cel

ref = sc.read_h5ad("examples/data/MousePosteriorQuick.h5ad")   # reference, coordinates in obs x and y
query = sc.read_h5ad("examples/data/MouseSCQuick.h5ad")        # scRNA-seq cells, same genes
cel.get_zscore(ref)
cel.get_zscore(query)
cel.Fit_cord(data_train=ref, hidden_dims=[30, 25, 15], num_epochs_max=50, num_workers=0,
             path="output/readme", filename="cord")
pred = cel.Predict_cord(data_test=query, path="output/readme", filename="cord")
print(pred[:5])   # coordinates rescaled to (0, 1)

In a script on Windows or macOS, put this code inside a main guard (see Reading the output), because Predict_cord starts a worker process. The Quick start page of the documentation walks through each step, checks the predictions on held-out spots and converts them back to grid units.


Step by step

The documentation has one tutorial per task. All but the last run on data that ship with the repository:

Reading the output explains each output, the settings that matter and the common problems. The original tutorials remain in tutorial/ as Markdown files and Jupyter notebooks.


Reproducibility

The Fit_* functions seed Python, NumPy and PyTorch through seednum (default 2021), so a repeated run with the same versions on the same platform gives bitwise identical weights. Results still vary in four situations:

  • Predict_layer and Predict_domain keep dropout active at prediction, and calling torch.manual_seed() before each call makes a prediction repeatable.
  • Gene lists built with set() change order between Python sessions, which changes the trained model unless the list is sorted or PYTHONHASHSEED is fixed.
  • PyTorch releases and operating systems differ in rounding and in their random streams. In a 20-epoch test, the upgrade from the PyTorch of 2024 to that of 2026 moved predicted coordinates by up to 8.1 grid units, against 15.6 for a change of seed (see RENOVATION_LOG.md).
  • Since scikit-learn 1.4, k-means runs one initialization instead of ten, so DataAugmentation clusters the genes differently from the environment of the paper.

Reproducing results from the paper

The analyses of the paper ran in 2022 and 2023 with the package versions of that time. The scripts and notebooks under code_paper/ hold the analysis code as it was run, one folder per case study, and code_paper/README.md maps each folder to its section and figures of the paper and to its data sources. Their file paths are written for the authors' machines and have to be edited before a run. Some figures have no code in the repository, and some files call functions that the current package no longer provides; code_paper/README.md lists both.


Citation

If you use CeLEry, please cite (see also CITATION.cff, which GitHub shows as a "Cite this repository" button):

Zhang, Q., Jiang, S., Schroeder, A., Hu, J., Li, K., Zhang, B., Dai, D., Lee, E. B., Xiao, R. and Li, M. Leveraging spatial transcriptomics data to recover cell locations in single-cell RNA-seq with CeLEry. Nature Communications 14, 4050 (2023). https://doi.org/10.1038/s41467-023-39895-3


License

MIT (see LICENSE).

About

CeLEry: cell location recovery in single-cell RNA sequencing

Resources

Stars

38 stars

Watchers

6 watching

Forks

Releases

Packages

Used by

Contributors

Languages