sctransform
Pure-python implementation of R sctransform — variance-stabilizing transformation for single-cell UMI count data. No R, no rpy2 required.
Implements both model flavors of R sctransform:
- v2 model (
vst.flavor="v2"in R): negative-binomial GLM with library-size offset (slope fixed atlog(10)), per-gene intercept + overdispersion estimation, Poisson-gene exclusion. - v1 model (original sctransform): per-gene Poisson GLM with free slope on
log10_umi, theta via a port ofMASS::theta.ml.
Both use kernel-smoothed regularization of parameters — all validated end-to-end against the R reference implementation (with glmGamPoi).
Why
Existing python ports either wrap R via rpy2 (defeats the purpose) or delegate the v2 model to R's glmGamPoi. This package re-implements everything natively and numerically matches R:
| Component | Validation vs R |
|---|---|
| Step-1 NB offset GLM (theta, intercept) | log10 θ corr 0.994, intercept corr 0.9994 |
bw.SJ bandwidth selector |
exact to 7 significant digits |
ksmooth (Nadaraya-Watson, normal kernel) |
max diff 2e-4 |
is_outlier |
identical flags on real data |
| v2 end-to-end Pearson residuals (pbmc3k) | corr 0.99999 |
| v2 UMAP workflow vs Seurat | HVG Jaccard 0.993, KNN label transfer 0.97 |
| v1 end-to-end Pearson residuals (pbmc3k) | corr 1.000000 (regularized log10 θ corr 1.0000) |
See benchmark/ for the full executed notebook and reproduction scripts.
Installation
pip install sctransform
# or with the scanpy interface:
pip install sctransform[scanpy]
Quickstart
import scanpy as sc
from sctransform import SCTransform
# adata with raw counts in adata.layers["counts"]
residuals = SCTransform(adata, layer="counts", vst_flavor="v2", var_features_n=3000)
# downstream, Seurat-style: PCA on Pearson residuals of variable features
adata.obsm["X_pca"] = sc.pp.pca(residuals.values, n_comps=30)
sc.pp.neighbors(adata, use_rep="X_pca")
sc.tl.umap(adata)
Low-level API (R-like, genes x cells matrix in, full vst output out):
from sctransform import vst
vst_out = vst(umi_matrix, gene_names=genes, vst_flavor="v2")
vst_out["y"] # residuals (genes x cells DataFrame)
vst_out["model_parameters_fit"] # regularized per-gene theta / intercept / slope
vst_out["gene_attr"] # residual_variance etc. (for HVG ranking)
Scope
- v2 model (
glmGamPoi_offset): fixed slope, intercept + theta learned,exclude_poisson - v1 model (
poisson/theta_ml, free slope) — exact port ofqpois_regIRLS +MASS::theta.ml - Pearson & deviance residuals,
min_variance("umi_median"), residual clipping - Cell/gene subsampling (density-based gene sampling, as in R)
-
batch_varsupport — roadmap - corrected counts (
return_corrected_umi) — roadmap
Relationship to Seurat
SCTransform(adata, vst_flavor="v2") replaces NormalizeData + FindVariableFeatures + ScaleData:
use the returned residuals as scale.data for PCA (as shown above). HVG flags and
regularized model parameters are stored in adata.var / adata.uns["sct"].
Tutorials
tutorials/pbmc3k_sctransform_vs_lognormalize.ipynb- end-to-end pbmc3k analysis, side-by-side vs the traditional log-normalize workflow (variance-stabilization QC, UMAP, cluster agreement, markers, depth effects).
License & attribution
GPL-3.0. Contains code ported from R (GPL-2+, R Core Team) and algorithms re-implemented from R sctransform (GPL-3, Christoph Hafemeister / Rahul Satija lab).
If you use this package, please cite the original publications:
- Hafemeister & Satija, Normalization and variance stabilization of single-cell RNA-seq data using regularized negative binomial regression, Genome Biology 2019
- Choudhary & Satija, Comparison and evaluation of statistical error models for scRNA-seq, Genome Biology 2022
- Ahlmann-Eltze & Huber, glmGamPoi: Fitting Gamma-Poisson Generalized Linear Models on Single Cell Count Data, Bioinformatics 2021
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 sctransform-0.2.0.tar.gz.
File metadata
- Download URL: sctransform-0.2.0.tar.gz
- Upload date:
- Size: 7.4 MB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3fe3dbdc1731fc94a8aefeb7be0319b5d6747171c21f1138d2a179d803413315
|
|
| MD5 |
0ebe869da7d7d3109edc696b2c2807ee
|
|
| BLAKE2b-256 |
7c25c14386a6dff0d8b358aa5752ea832a03562c775cbf5b3e94fd8d22b5cb11
|
Provenance
The following attestation bundles were made for sctransform-0.2.0.tar.gz:
Publisher:
release.yml on Zikun-Yang/SCTransform
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
sctransform-0.2.0.tar.gz -
Subject digest:
3fe3dbdc1731fc94a8aefeb7be0319b5d6747171c21f1138d2a179d803413315 - Sigstore transparency entry: 2582815806
- Sigstore integration time:
-
Permalink:
Zikun-Yang/SCTransform@8ade6eded351689ff53e37958a8190d70279fe31 -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/Zikun-Yang
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@8ade6eded351689ff53e37958a8190d70279fe31 -
Trigger Event:
push
-
Statement type:
File details
Details for the file sctransform-0.2.0-py3-none-any.whl.
File metadata
- Download URL: sctransform-0.2.0-py3-none-any.whl
- Upload date:
- Size: 30.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a99141b779d2c4d80c828416b24c901f1a89b4b59092d752efb6f7b362bee574
|
|
| MD5 |
e229a3fb021e8946b37f8dd24a92856f
|
|
| BLAKE2b-256 |
ef78b8271e47454f06618a872d4fe9c0eb19f85ae22dcd6d22103bb35279cff8
|
Provenance
The following attestation bundles were made for sctransform-0.2.0-py3-none-any.whl:
Publisher:
release.yml on Zikun-Yang/SCTransform
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
sctransform-0.2.0-py3-none-any.whl -
Subject digest:
a99141b779d2c4d80c828416b24c901f1a89b4b59092d752efb6f7b362bee574 - Sigstore transparency entry: 2582815816
- Sigstore integration time:
-
Permalink:
Zikun-Yang/SCTransform@8ade6eded351689ff53e37958a8190d70279fe31 -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/Zikun-Yang
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@8ade6eded351689ff53e37958a8190d70279fe31 -
Trigger Event:
push
-
Statement type: