Spatial transcriptomics foundation model
Project description
TERRA is a foundation model for spatial transcriptomics. It uses a Joint-Embedding Predictive Architecture (JEPA): cells are tokenized together with their spatial neighbors, parts of the input are masked, and the model learns by predicting the latent representations of the masked cell and neighborhood tokens. The resulting embeddings capture both a cell's own expression and its tissue microenvironment.
Pretrained on HST-Corpus-112M (>100M cells at single-cell resolution spanning human spatial-transcriptomics datasets), TERRA produces cell- and neighborhood-level embeddings that transfer to downstream tasks such as niche and cell-type identification, batch-integrated atlasing, spatial gene-pair scoring, and in-silico perturbation — without task-specific retraining.
- 📖 Documentation: https://terra-st.readthedocs.io
- 🤗 Pretrained models: https://huggingface.co/Lotfollahi-lab (
TERRA-96M,TERRA-112M) - 📓 Tutorial: end-to-end walkthrough
Key features
- Spatially-aware embeddings — cell and neighborhood representations learned in latent space via JEPA.
- Pretrained and ready to use — download a model from the Hugging Face Hub and embed your own
AnnDatain a few lines. - Self-contained model bundles — each release ships the checkpoint, tokenizer, and gene-reference files needed to reproduce its training-time harmonization.
- Downstream analyses — niche/cell-type clustering, gene-pair spatial scoring, EMD-based spatial structure, and perturbation.
Installation
TERRA is published on PyPI as terra-st (the import name is terra) and
requires an NVIDIA GPU. Install PyTorch first (so it matches your GPU), then
TERRA — we recommend uv.
1. Install PyTorch for your hardware. Run nvidia-smi, read the "CUDA
Version" in the top-right, and install the matching CUDA build (see the
PyTorch install guide), e.g.:
uv pip install torch --index-url https://download.pytorch.org/whl/cu124
2. Install TERRA.
uv pip install terra-st
Plain pip install terra-st works too. For a development install from a clone
of this repository (after step 1): uv pip install -e ".[dev,test,doc]".
Verify the install:
python -c "import torch; print(torch.__version__, torch.version.cuda, torch.cuda.is_available())"
Quickstart
Download a pretrained model and embed your own spatial AnnData with the
end-to-end pipeline. Each downloaded bundle contains the gene-reference files
needed for harmonization, so no external paths are required:
import anndata as ad
from terra import download_pretrained, harmonize_tokenize_embed_pipeline
adata = ad.read_h5ad("my_spatial_data.h5ad") # raw counts in adata.X
model_dir = download_pretrained("Lotfollahi-lab/TERRA-96M")
adata = harmonize_tokenize_embed_pipeline(
adata=adata,
sample_key="sample", # column in adata.obs identifying samples
batch_key="batch", # column to store the batch identifier
model_folder_path=model_dir,
cache_directory_path="./terra_cache",
)
# Cell- and neighborhood-level embeddings are now in adata.obsm.
See the documentation for the step-by-step pipeline, downstream analyses (niche identification, gene-pair scoring, perturbation), and the full tutorial.
Citation
If you use TERRA in your research, please cite the manuscript (in preparation). A BibTeX entry and DOI will be added here on publication.
License
The TERRA code is released under the BSD 3-Clause License. Pretrained model weights distributed on the Hugging Face Hub are released under CC-BY-NC-4.0 (non-commercial use).
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file terra_st-0.1.1.tar.gz.
File metadata
- Download URL: terra_st-0.1.1.tar.gz
- Upload date:
- Size: 1.5 MB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6a72ae5b5ed6b91a519774ad533f57faf4bb331f28b255defdb56dac010a0820
|
|
| MD5 |
db4baca10a18b58018074a1c2ba91734
|
|
| BLAKE2b-256 |
db22d9de566f57edf21259c42ba33bc872e6ac4189c00d41cd86a36ee2096c13
|
Provenance
The following attestation bundles were made for terra_st-0.1.1.tar.gz:
Publisher:
release.yaml on Lotfollahi-lab/terra
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
terra_st-0.1.1.tar.gz -
Subject digest:
6a72ae5b5ed6b91a519774ad533f57faf4bb331f28b255defdb56dac010a0820 - Sigstore transparency entry: 1970957250
- Sigstore integration time:
-
Permalink:
Lotfollahi-lab/terra@470f4d737fef9c782973e66ef04066dad4cfd418 -
Branch / Tag:
refs/tags/0.1.1 - Owner: https://github.com/Lotfollahi-lab
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yaml@470f4d737fef9c782973e66ef04066dad4cfd418 -
Trigger Event:
release
-
Statement type:
File details
Details for the file terra_st-0.1.1-py3-none-any.whl.
File metadata
- Download URL: terra_st-0.1.1-py3-none-any.whl
- Upload date:
- Size: 245.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
df6631565cb6b2938f6c87b8eb6f3e564d1a05c83fbc4c53ad56c3bd4cf6c86d
|
|
| MD5 |
27152718a11123dc1a8b3355fb5b681a
|
|
| BLAKE2b-256 |
2603d911d873ca2a9cb5586ab3f1888a8feab0e555680205b4ceae4e2b060b72
|
Provenance
The following attestation bundles were made for terra_st-0.1.1-py3-none-any.whl:
Publisher:
release.yaml on Lotfollahi-lab/terra
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
terra_st-0.1.1-py3-none-any.whl -
Subject digest:
df6631565cb6b2938f6c87b8eb6f3e564d1a05c83fbc4c53ad56c3bd4cf6c86d - Sigstore transparency entry: 1970957344
- Sigstore integration time:
-
Permalink:
Lotfollahi-lab/terra@470f4d737fef9c782973e66ef04066dad4cfd418 -
Branch / Tag:
refs/tags/0.1.1 - Owner: https://github.com/Lotfollahi-lab
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yaml@470f4d737fef9c782973e66ef04066dad4cfd418 -
Trigger Event:
release
-
Statement type: