Skip to main content

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 at log(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 of MASS::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 of qpois_reg IRLS + MASS::theta.ml
  • Pearson & deviance residuals, min_variance ("umi_median"), residual clipping
  • Cell/gene subsampling (density-based gene sampling, as in R)
  • batch_var support — 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

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

sctransform-0.2.0.tar.gz (7.4 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sctransform-0.2.0-py3-none-any.whl (30.7 kB view details)

Uploaded Python 3

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

Hashes for sctransform-0.2.0.tar.gz
Algorithm Hash digest
SHA256 3fe3dbdc1731fc94a8aefeb7be0319b5d6747171c21f1138d2a179d803413315
MD5 0ebe869da7d7d3109edc696b2c2807ee
BLAKE2b-256 7c25c14386a6dff0d8b358aa5752ea832a03562c775cbf5b3e94fd8d22b5cb11

See more details on using hashes here.

Provenance

The following attestation bundles were made for sctransform-0.2.0.tar.gz:

Publisher: release.yml on Zikun-Yang/SCTransform

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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

Hashes for sctransform-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a99141b779d2c4d80c828416b24c901f1a89b4b59092d752efb6f7b362bee574
MD5 e229a3fb021e8946b37f8dd24a92856f
BLAKE2b-256 ef78b8271e47454f06618a872d4fe9c0eb19f85ae22dcd6d22103bb35279cff8

See more details on using hashes here.

Provenance

The following attestation bundles were made for sctransform-0.2.0-py3-none-any.whl:

Publisher: release.yml on Zikun-Yang/SCTransform

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 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