You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In Seurat, there is an option of selecting a list of genes in the pre-processing regressOut function. I was wondering if there was similar functionality in Scanpy. Doing something like below does not work for me as a lot of the cells have 0 expression, giving me a PerfectSeparationError.
Hi,
I'm not entirely sure how Seurat does it, but I assume you could take the mean expression level (or mean z-score) of a couple of genes, store that in a .var column, and regress that out by sc.pp.regress_out(adata, var_col)?
If you want to ensure an equal contribution of all the genes to the gene score without weighting by mean gene expression, you could first use sc.pp.scale() on a copy of the adata object like this:
This looks covered now. Since 1.13.0b1 (#4359), regress_out's keys became covariates, which takes anndata.acc references, so genes can be mixed with obs columns:
fromanndata.accimportAsc.pp.regress_out(adata, [A.obs["n_counts"], A.X[:, "MALAT1"]])
sc.pp.regress_out(adata, [A.obs["n_counts"], A.layers["counts"][:, "MALAT1"]]) # from a layer
The reference says which matrix it reads, so a name that is both an obs column and a gene isn't ambiguous, and an unknown gene raises a KeyError. I checked this on 1.13.0b1 (Python 3.13): regressing on A.X[:, "MALAT1"] gives the same result as copying that gene into .obs first and regressing on the copy, which is still the workaround on 1.12.x.
Could this be closed? If it helps, I can open a small PR adding a gene example to the covariates docstring and a test for it.
I used an AI coding assistant to help prepare this; I reviewed and tested it.
Activity
LuckyMD commentedon Feb 19, 2019
Hi,
I'm not entirely sure how Seurat does it, but I assume you could take the mean expression level (or mean z-score) of a couple of genes, store that in a
.varcolumn, and regress that out bysc.pp.regress_out(adata, var_col)?Something like this:
If you want to ensure an equal contribution of all the genes to the gene score without weighting by mean gene expression, you could first use
sc.pp.scale()on a copy of theadataobject like this:Note that I have not tested this code... so no guarantees ;).
ivirshup commentedon Jan 24, 2021
This would be very easy to implement with:
sc.get.obs_df(adata, keys).That would not solve the problem of (I believe) constant genes giving errors.
chenyangsu commentedon Nov 10, 2026
This looks covered now. Since 1.13.0b1 (#4359),
regress_out'skeysbecamecovariates, which takesanndata.accreferences, so genes can be mixed with obs columns:The reference says which matrix it reads, so a name that is both an obs column and a gene isn't ambiguous, and an unknown gene raises a
KeyError. I checked this on 1.13.0b1 (Python 3.13): regressing onA.X[:, "MALAT1"]gives the same result as copying that gene into.obsfirst and regressing on the copy, which is still the workaround on 1.12.x.Could this be closed? If it helps, I can open a small PR adding a gene example to the
covariatesdocstring and a test for it.I used an AI coding assistant to help prepare this; I reviewed and tested it.