Skip to main content

TCRi

Information-theoretic analysis of paired single-cell RNA + TCR sequencing.

Documentation · Tutorials · Paper · Issues

tests docs license: MIT python 3.10+ built on scverse


TCRi quantifies how T-cell clonotypes relate to transcriptional phenotypes in paired single-cell data. It fits a hierarchical Bayesian model (on top of scvi-tools / Pyro) that ties each clone's phenotype distribution to gene expression, then reads out information-theoretic summaries — clonotypic and phenotypic entropy, clone↔phenotype mutual information, and phenotypic flux across conditions — over the learned clone × phenotype joint distribution.

It is built on the scverse stack (AnnData, scanpy, scvi-tools) and works on a standard AnnData carrying both a clonotype label and a gene-expression matrix.

Key capabilities

  • Joint model of expression + repertoire — a hierarchical VAE (TCRIModel) that learns each clone's phenotype distribution while reconstructing counts with a ZINB decoder.
  • Information-theoretic metrics — clonotypic entropy H(c|φ), phenotypic entropy H(φ|c), normalized mutual information I(c;φ), and phenotypic flux between conditions, all in bits over the posterior joint.
  • Uncertainty-aware — every metric can be reported as a posterior mean ± HDI by drawing from the fitted Dirichlet posterior (n_samples > 0).
  • Group comparisons — paired/unpaired contrasts across cohorts (e.g. disease vs control), with n counting replicates rather than items.
  • Diagnostics — posterior-predictive checks, phenotype calibration, and permutation nulls for the metrics.
  • Contract-governed — the public API, the generative model, the metric definitions, and the training plan are each frozen by a machine-checked contract.

Installation

TCRi targets Python ≥ 3.10.

pip install tcri

The heavy scientific stack (PyTorch, Pyro, scvi-tools, scanpy) is pulled in automatically. A GPU is optional but speeds up model fitting.

For the development version:

git clone https://github.com/nceglia/tcri.git
cd tcri
pip install -e ".[test]"

See the installation guide for details.

Quickstart

import tcri

# 1. register clonotype / phenotype / covariate / batch columns on your AnnData
tcri.ml.TCRIModel.setup_anndata(
    adata,
    layer="counts",
    clonotype_key="clone_id",
    phenotype_key="phenotype",
    covariate_key="condition",
    batch_key="patient",
    replicate="patient",
)

# 2. fit the model and write learned quantities back onto the AnnData
model = tcri.ml.TCRIModel(adata)
model.train(max_epochs=200, batch_size=512)
model.to_anndata(adata)

# 3. fit the permutation references, once: every metric is read against one
tcri.null.all(model, adata)

# 4. read out information-theoretic metrics (bits), each with its own floor
#    -> value, null_value, excess = value - null_value
mi = tcri.tl.mutual_information(adata, covariate="pre", normalize_mode="average")
ce = tcri.tl.clonotypic_entropy(adata, covariate="pre")
flux = tcri.tl.phenotypic_flux(adata, cov_from="pre", cov_to="post")

# 5. ask the fitted model which genes the phenotype calls rest on
tcri.perturb.gene_importance(model, adata, splitby="disease_status")
tcri.pl.gene_importance(adata, quantity="excess")

No paired data yet? tcri.datasets.simulate_cohort() returns a synthetic AnnData with patients as replicates, an ordered condition axis within each, and a disease_status label between them — the basis for the tutorials. tcri.datasets.simulate_tcri() is the single-sample alternative, with a mutual information known in closed form.

Framework

TCRi models the joint distribution of clonotype and phenotype hierarchically: a clonotype-level prior p_c over phenotypes, a covariate-specific p_ct anchored to it, and a per-cell phenotype that fuses a classifier on the latent embedding z with the clone's local prior. The metrics are then pure functions of the learned clone × phenotype joint. The full generative model and its plate diagram are documented under Concepts.

TCRi framework

Citation

If you use TCRi in your research, please cite:

TCRi: An Information Theoretic Framework for Paired Single Cell Gene Expression and TCR Sequencing

Nicholas Ceglia, Sohrab Salehi, et al.

bioRxiv 2022. doi: 10.1101/2022.10.01.510457

BibTeX
@article{ceglia2022tcri,
  title   = {TCRi: An Information Theoretic Framework for Paired Single Cell Gene Expression and TCR Sequencing},
  author  = {Ceglia, Nicholas and Salehi, Sohrab and others},
  journal = {bioRxiv},
  year    = {2022},
  doi     = {10.1101/2022.10.01.510457}
}

License

TCRi is released under the MIT License.

Metadata

Release files for tcri 0.12.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tcri 0.12.0
File Size Uploaded
tcri-0.12.0.tar.gz 240.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for tcri 0.12.0
File Interpreter ABI Platform
tcri-0.12.0-py3-none-any.whl Python 3 none any Details

Total release size: 409.2 kB

Release files / tcri-0.12.0.tar.gz

Download URL tcri-0.12.0.tar.gz
Size 240.1 kB
Tags Source
SHA-256 checksum
How to use checksums
b159a5ee2097ba4d612ba6c776d91cdf6db16d55e8565e0454605922c8102ce9
BLAKE2b-256 checksum
How to use checksums
3c4ee5653b400c8969b4a4accbe7e6e739076782cc14af5ed42f2f7a53d2afcf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 15, 2026.

Transparency log

Release files / tcri-0.12.0-py3-none-any.whl

Download URL tcri-0.12.0-py3-none-any.whl
Size 169.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
027848c9d5f8011486885aaaadae1504c1de458779d1312d7945b56bc48bffb0
BLAKE2b-256 checksum
How to use checksums
0b3c67f0296b5872c4159e9399b573795cf48ddbae6a0799d571605204923ebf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 15, 2026.

Transparency log

Release history Release notifications | RSS feed

0.14.0

2 release files

0.13.1

2 release files

0.13.0

2 release files

This release

0.12.0 This release

2 release files

0.9.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page