Skip to main content

decontx-scanpy

Scanpy-native Python port of DecontX (Yang et al., Genome Biology 2020; Bioconductor package campbio/decontX) — estimation and removal of ambient RNA contamination in droplet-based single-cell RNA-seq data. Import name: pydecontx.

  • Faithful: the variational-EM kernels are a line-for-line vectorized port of the upstream src/DecontX.cpp, validated against the actual upstream kernels (compiled from source and driven by the original R EM loop) to machine precision — max θ difference ~4e-16 with a shared initialization; Pearson r > 0.99 end-to-end with independent RNGs.
  • Complete: automatic broad-cluster estimation (PCA → UMAP → DBSCAN with k-means fallback, as in R), empty-droplet background support incl. per-batch backgrounds and barcode-overlap checks, multi-batch mode, in-place AnnData results following scanpy conventions.
  • Fast, no compiler: pure numpy/scipy; nonzeros are grouped by cluster once so each EM pass streams cache-resident columns (~0.5 s/iteration at 20k cells × 12k genes × 22M nonzeros, single core).
  • No R required. Only DecontX is ported (not DecontPro).

Install

pip install decontx-scanpy

Usage

import scanpy as sc
from pydecontx import decontx

adata = sc.read_h5ad("filtered_cells.h5ad")   # raw counts, cells x genes

# recommended: use your own annotation / clustering
decontx(adata, z="cell_type", batch="sample")           # modifies in place

# or let DecontX estimate broad clusters (PCA -> UMAP -> DBSCAN)
decontx(adata)

# with empty droplets as the ambient profile
decontx(adata, background=raw_droplets_adata)

adata.layers["decontX_counts"]        # decontaminated counts (non-integer; round if needed)
adata.obs["decontX_contamination"]    # per-cell contamination fraction
adata.obs["decontX_clusters"]         # clusters used
adata.uns["decontX"]["estimates"]     # theta/phi/eta/delta/log-likelihood per batch

A plain cells × genes matrix (numpy or scipy sparse) is also accepted; a result dict is then returned, mirroring the R list return. A simulate_contamination() generator (port of the R one) is included for testing.

Key parameters (same semantics as R): delta=(10, 10) with estimate_delta=True controls the prior on per-cell contamination — set estimate_delta=False with a larger second value for more aggressive removal; max_iter=500, convergence=0.001.

The model

Each cell's observed counts are a two-component multinomial mixture: a native gene distribution phi[:, k] of the cell's population k, and a contamination distribution eta[:, k] aggregating every other population's expression, mixed by a per-cell native proportion theta ~ Beta(delta). Variational EM yields per-transcript native probabilities; the decontaminated matrix is counts × P(native).

Validation

The test suite (tests/, 30 tests) has three layers:

  1. R parity: the upstream DecontX.cpp is compiled via Rcpp::sourceCpp and driven by a verbatim copy of the R EM loop on identical inputs. With a shared initial θ and fixed δ the Python kernels match to ~4e-16 (θ), ~5e-13 (decontaminated matrix); with δ re-estimated, ~2e-4; end-to-end r > 0.99. Cached reference outputs ship in the sdist, so pytest needs no R installation.
  2. Kernel units: EM step vs a literal dense transcription of the C++ loops; Dirichlet-MLE (Minka fixed point) recovery.
  3. Functional: ground-truth recovery on simulations (r = 0.998, MAE 0.004 at ~10% contamination), ~99% removal of marker-gene cross-contamination, background / batch / error paths.

Citation

If you use this package, please cite the DecontX paper:

Yang, S., Corbett, S.E., Koga, Y. et al. Decontamination of ambient RNA in single-cell RNA-seq with DecontX. Genome Biology 21, 57 (2020).

License

MIT. Ported from the MIT-licensed Bioconductor package decontX; see LICENSE for attribution.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

decontx_scanpy-0.1.1.tar.gz (414.4 kB view details)

Uploaded Source

Built Distribution

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

decontx_scanpy-0.1.1-py3-none-any.whl (13.2 kB view details)

Uploaded Python 3

File details

Details for the file decontx_scanpy-0.1.1.tar.gz.

File metadata

  • Download URL: decontx_scanpy-0.1.1.tar.gz
  • Upload date:
  • Size: 414.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.12.1

File hashes

Hashes for decontx_scanpy-0.1.1.tar.gz
Algorithm Hash digest
SHA256 9e1b4799a0e372c4a7cc53333f3b62ae103ab6dd2271d2af1b2edb28973fd54d
MD5 975db1a44fb77dfcbe5f25f90165d107
BLAKE2b-256 3a8655760584e87e81c590c5b4131f6156511afd2dae23ac26f3e66defc4ddf0

See more details on using hashes here.

File details

Details for the file decontx_scanpy-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: decontx_scanpy-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 13.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.12.1

File hashes

Hashes for decontx_scanpy-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7d0115e3a1d4f2fd067bb56e74c87f443de2d117356a96996f8db0a3061a1597
MD5 658b3f732fa629c8176d1792b8dd9d00
BLAKE2b-256 d0be2073b8c6651b4d199ff1037fcd9f4aa04fce085e2394fbae6a1b6901053d

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 files

0.1.0

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