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.
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 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9e1b4799a0e372c4a7cc53333f3b62ae103ab6dd2271d2af1b2edb28973fd54d
|
|
| MD5 |
975db1a44fb77dfcbe5f25f90165d107
|
|
| BLAKE2b-256 |
3a8655760584e87e81c590c5b4131f6156511afd2dae23ac26f3e66defc4ddf0
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7d0115e3a1d4f2fd067bb56e74c87f443de2d117356a96996f8db0a3061a1597
|
|
| MD5 |
658b3f732fa629c8176d1792b8dd9d00
|
|
| BLAKE2b-256 |
d0be2073b8c6651b4d199ff1037fcd9f4aa04fce085e2394fbae6a1b6901053d
|