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
backgroundsupport 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:
- R parity: the upstream
DecontX.cppis compiled viaRcpp::sourceCppand 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, sopytestneeds no R installation. - Kernel units: EM step vs a literal dense transcription of the C++ loops; Dirichlet-MLE (Minka fixed point) recovery.
- 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.
Metadata
Release files for decontx-scanpy 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| decontx_scanpy-0.1.1.tar.gz | 414.4 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| decontx_scanpy-0.1.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 427.6 kB
Release files / decontx_scanpy-0.1.1.tar.gz
| Download URL | decontx_scanpy-0.1.1.tar.gz |
|---|---|
| Size | 414.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9e1b4799a0e372c4a7cc53333f3b62ae103ab6dd2271d2af1b2edb28973fd54d
|
|
BLAKE2b-256 checksum How to use checksums |
3a8655760584e87e81c590c5b4131f6156511afd2dae23ac26f3e66defc4ddf0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.1
|
Release files / decontx_scanpy-0.1.1-py3-none-any.whl
| Download URL | decontx_scanpy-0.1.1-py3-none-any.whl |
|---|---|
| Size | 13.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
7d0115e3a1d4f2fd067bb56e74c87f443de2d117356a96996f8db0a3061a1597
|
|
BLAKE2b-256 checksum How to use checksums |
d0be2073b8c6651b4d199ff1037fcd9f4aa04fce085e2394fbae6a1b6901053d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.1
|
Security for decontx-scanpy 0.1.1
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.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.