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                # core (numpy + scipy)
pip install "decontx-scanpy[clustering]"  # + scanpy/sklearn for auto-clustering

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 (requires [clustering] extra)
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.0.tar.gz (414.6 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.0-py3-none-any.whl (13.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: decontx_scanpy-0.1.0.tar.gz
  • Upload date:
  • Size: 414.6 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.0.tar.gz
Algorithm Hash digest
SHA256 55312f0040f465f22a956b8f5197e5065500b61c1e24eb7abd2c709056809aee
MD5 208b5bb8fbe178bdd528d17563471657
BLAKE2b-256 ab516dd611b0ec8d99446efe49a530be1c019a1a0097e7d84aff82432a4406aa

See more details on using hashes here.

File details

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

File metadata

  • Download URL: decontx_scanpy-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 13.3 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c3d1a4b917d13c956b3b71514138bad902867f89ef6c1b5762e1724ad1406d6b
MD5 8c59b5fe1f35572f4b03a3f66edc8595
BLAKE2b-256 d7252d8d4a21e0d1cc25901fccf4dcf002a16f3be52c18ef3d212306155c6805

See more details on using hashes here.

Release history Release notifications | RSS feed

0.1.1

2 files

This release

0.1.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