Metadata-Version: 2.4
Name: terra-st
Version: 0.1.5
Summary: Spatial transcriptomics foundation model
Project-URL: Documentation, https://terra-st.readthedocs.io/
Project-URL: Source, https://github.com/Lotfollahi-lab/terra
Project-URL: Homepage, https://github.com/Lotfollahi-lab/terra
Project-URL: Changelog, https://github.com/Lotfollahi-lab/terra/blob/main/CHANGELOG.md
Project-URL: Issues, https://github.com/Lotfollahi-lab/terra/issues
Author: Sebastian Birk, Mohammad Vali Sanian
Maintainer-email: Sebastian Birk <sebastian.birk@helmholtz-munich.de>, Mohammad Vali Sanian <mohammad.sanian@helsinki.fi>
License: BSD 3-Clause License
        
        Copyright (c) 2026, Lotfollahi Lab
        
        Redistribution and use in source and binary forms, with or without
        modification, are permitted provided that the following conditions are met:
        
        1. Redistributions of source code must retain the above copyright notice, this
           list of conditions and the following disclaimer.
        
        2. Redistributions in binary form must reproduce the above copyright notice,
           this list of conditions and the following disclaimer in the documentation
           and/or other materials provided with the distribution.
        
        3. Neither the name of the copyright holder nor the names of its
           contributors may be used to endorse or promote products derived from
           this software without specific prior written permission.
        
        THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
        AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
        IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
        DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
        FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
        DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
        SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
        CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
        OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
        OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
License-File: LICENSE
Keywords: JEPA,deep learning,foundation model,representation learning,single-cell,spatial transcriptomics
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: BSD License
Classifier: Natural Language :: English
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Topic :: Scientific/Engineering :: Bio-Informatics
Requires-Python: >=3.10
Requires-Dist: anndata>=0.10
Requires-Dist: datasets>=2.18
Requires-Dist: leidenalg>=0.9
Requires-Dist: peft>=0.10
Requires-Dist: pyensembl>=2.2
Requires-Dist: pyyaml>=6.0
Requires-Dist: scib-metrics>=0.5
Requires-Dist: scikit-misc>=0.3
Requires-Dist: session-info>=1.0
Requires-Dist: squidpy>=1.4
Requires-Dist: torch>=2.1
Requires-Dist: transformers>=4.44
Provides-Extra: dev
Requires-Dist: pre-commit; extra == 'dev'
Requires-Dist: twine>=4.0.2; extra == 'dev'
Provides-Extra: doc
Requires-Dist: docutils!=0.18.*,!=0.19.*,>=0.8; extra == 'doc'
Requires-Dist: ipykernel; extra == 'doc'
Requires-Dist: ipython; extra == 'doc'
Requires-Dist: myst-nb; extra == 'doc'
Requires-Dist: pandas; extra == 'doc'
Requires-Dist: sphinx-autodoc-typehints; extra == 'doc'
Requires-Dist: sphinx-book-theme>=1.0.0; extra == 'doc'
Requires-Dist: sphinx-copybutton; extra == 'doc'
Requires-Dist: sphinx-design; extra == 'doc'
Requires-Dist: sphinx>=4; extra == 'doc'
Requires-Dist: sphinxcontrib-bibtex>=1.0.0; extra == 'doc'
Requires-Dist: sphinxext-opengraph; extra == 'doc'
Provides-Extra: eval
Requires-Dist: cellphonedb; extra == 'eval'
Requires-Dist: omnipath; extra == 'eval'
Provides-Extra: hub
Requires-Dist: huggingface-hub>=0.20; extra == 'hub'
Provides-Extra: notebook
Requires-Dist: ipykernel>=6; extra == 'notebook'
Requires-Dist: jupyterlab>=4; extra == 'notebook'
Provides-Extra: test
Requires-Dist: coverage; extra == 'test'
Requires-Dist: pytest; extra == 'test'
Provides-Extra: train
Requires-Dist: wandb>=0.16; extra == 'train'
Description-Content-Type: text/markdown

<p align="center">
  <img src="https://raw.githubusercontent.com/Lotfollahi-lab/terra/main/docs/_static/terra_logo.png" width="300" alt="TERRA logo">
</p>

<p align="center">
  <a href="https://pypi.org/project/terra-st/"><img src="https://img.shields.io/pypi/v/terra-st.svg" alt="PyPI"></a>
  <a href="https://terra-st.readthedocs.io/"><img src="https://readthedocs.org/projects/terra-st/badge/?version=latest" alt="Documentation"></a>
  <a href="LICENSE"><img src="https://img.shields.io/badge/License-BSD%203--Clause-blue.svg" alt="License: BSD-3-Clause"></a>
</p>

**TERRA** is a self-supervised foundation model for spatial transcriptomics. It
serializes each cell together with its spatial neighbors into a sequence of gene
tokens, then trains with a Joint-Embedding Predictive Architecture (JEPA): some
tokens are masked and the model predicts their representations in *latent space* —
rather than reconstructing raw expression — to infer the molecular and spatial
context of the neighboring cells. This yields hierarchical embeddings at the gene,
cell, and neighborhood scales, capturing 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](https://terra-st.readthedocs.io/en/latest/tutorials.html)

## 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 `AnnData` in 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](https://docs.astral.sh/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](https://pytorch.org/get-started/locally/)), e.g.:

```shell
uv pip install torch --index-url https://download.pytorch.org/whl/cu124
```

**2. Install TERRA.**

```shell
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:

```shell
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:

```python
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](https://terra-st.readthedocs.io) for the step-by-step
pipeline, downstream analyses (niche identification, gene-pair scoring,
perturbation), and the full [tutorial](https://terra-st.readthedocs.io/en/latest/tutorials.html).

## 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](LICENSE).
Pretrained **model weights** distributed on the Hugging Face Hub are released
under [CC-BY-NC-4.0](https://creativecommons.org/licenses/by-nc/4.0/)
(non-commercial use).
