soupx-python
A Python port of SoupX (Young & Behjati, 2020) for removing ambient RNA contamination from droplet-based single-cell RNA-seq data. It follows SoupX 1.6.2 function by function and reproduces its results numerically (see Validation).
SoupX estimates the ambient ("soup") expression profile from empty droplets, estimates the contamination fraction ρ from genes that are clearly not expressed in some clusters, and removes the expected soup counts from each cell. It needs the raw (unfiltered) droplet matrix.
Installation
pip install soupx-python # numpy, scipy, pandas only
pip install "soupx-python[scanpy]" # adds scanpy for load10X
Usage with scanpy
import scanpy as sc
import soupx
raw = sc.read_10x_h5("raw_feature_bc_matrix.h5") # all droplets
adata = sc.read_10x_h5("filtered_feature_bc_matrix.h5") # cells; same genes in the same order
# ... cluster adata as usual (on a normalized copy), giving adata.obs["leiden"]
s = soupx.SoupChannel(raw.X.T.tocsr(), adata.X.T.tocsr(), # genes x droplets / genes x cells, raw counts
gene_names=list(adata.var["gene_ids"])) # unique names
s.setClusters(adata.obs["leiden"].to_numpy())
soupx.autoEstCont(s) # sets rho, details in s.fit
adata.layers["soupx"] = soupx.adjustCounts(s).T.tocsr() # corrected counts, cells x genes
The R function names are kept (autoEstCont, adjustCounts, quickMarkers, ...); snake_case aliases exist
for the main ones. load10X(path) reads a Cell Ranger outs folder.
Manual estimation with genes that some cells should not express (e.g. haemoglobin):
genes = {"HB": ["HBA1", "HBA2", "HBB"]} # names as in gene_names
use = soupx.estimateNonExpressingCells(s, genes) # cells x gene sets
soupx.calculateContaminationFraction(s, genes, use) # rho with 95% CI in s.metaData
or set ρ directly with s.set_contamination_fraction(0.1).
Correction methods (adjustCounts(method=...)), characterised on a human/mouse mixture below:
"subtraction"(default): removes ρ·nUMIs counts per cell, distributed according to the soup profile."multinomial": maximum-likelihood allocation of the same number of counts; very similar results, slower."soupOnly": only removes whole genes that are consistent with being pure soup; removes less, but almost only ambient counts.
Validation
Compared against R SoupX 1.6.2 (R 4.6.1) on the 10x Genomics dataset 10k 1:1 Mixture of Human HEK293T and
Mouse NIH3T3 cells, 3' v3.1, Chromium Controller (9,588 single-species cells after removing human/mouse
doublets, 68,886 genes, 14 Leiden clusters), with identical inputs and default parameters. Scripts and
instructions: validation/.
| R SoupX | soupx-python | |
|---|---|---|
| soup profile | identical | |
marker table (quickMarkers, 124,792 gene/cluster pairs) |
identical, incl. order | |
ρ (autoEstCont, 100 markers, 739 estimates) |
0.017 | 0.017, identical posterior |
| corrected counts, max. abs. difference | subtraction 1e-12, soupOnly 0, multinomial 7e-13 |
Runtime on the same Windows machine (seconds; R / Python): autoEstCont 14 / 20, subtraction 62 / 13,
soupOnly 19 / 6, multinomial 576 / 78.
Cross-species counts (mouse genes in human cells and vice versa) are known ambient RNA. Scaled by the share
of the other species in the soup profile they give ρ ≈ 0.022, so autoEstCont underestimates slightly on
this dataset (0.017). subtraction and multinomial remove 71% of the cross-species counts, soupOnly 52%;
of the counts removed by soupOnly, 99% are cross-species, versus 45% for the other two (49% would be
expected if everything removed were ambient). These are properties of SoupX itself, as R gives the same
numbers.
tests/ checks R parity on a small synthetic channel with reference values computed in R, so this is
re-tested on every change.
Known differences to R
- Random parts (stochastic rounding with
roundToInt=True, tie breaking in"multinomial") cannot follow R's random number stream; useseed=for reproducibility. On the data above this did not change results. calculateContaminationFraction: ρ is identical; the confidence interval is the exact profile-likelihood interval, R interpolates it (relative difference ~1e-6).- Plotting functions and
autoEstCont(doPlot=...)are not ported;s.fitcontains the posterior.
Citation
Please cite the original method:
Young, M.D., Behjati, S. SoupX removes ambient RNA contamination from droplet-based single-cell RNA sequencing data. GigaScience 9, giaa151 (2020). https://doi.org/10.1093/gigascience/giaa151
and, if you use this port, the software itself (see CITATION.cff).
Related: decontx-python, a validated port of DecontX, which does not need empty droplets.
License
Metadata
Release files for soupx-python 0.4.0
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 | Actions |
|---|---|---|---|---|
| soupx_python-0.4.0.tar.gz | 26.2 kB | Without provenance | Details for soupx_python-0.4.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| soupx_python-0.4.0-py3-none-any.whl | Python 3 | none | any | Details for soupx_python-0.4.0-py3-none-any.whl |
Total release size: 49.9 kB
Release files / soupx_python-0.4.0.tar.gz
| Download URL | soupx_python-0.4.0.tar.gz |
|---|---|
| Size | 26.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3bb7256accba951f507fa0107866064f82cf0eea34eefe2256cf48e511c6ea35
|
|
BLAKE2b-256 checksum How to use checksums |
b5be179bb4d911016925816cdf4e7461559fc3b40516609bb358995de10c18fe
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.16
|
Release files / soupx_python-0.4.0-py3-none-any.whl
| Download URL | soupx_python-0.4.0-py3-none-any.whl |
|---|---|
| Size | 23.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
bb998fdc7aa1e706ac252c3b6fe150de1d6386562768be403fe9bd119e7a0054
|
|
BLAKE2b-256 checksum How to use checksums |
78c716517d5bdc9c671e2ba6f93549c10d44febc248382221ee4670952b51131
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.16
|
Security for soupx-python 0.4.0
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.
To learn more about protecting yourself from malicious actors, visit our package security documentation.