Skip to content

Regressing out genes #492

Open

Description

@joshuahwu

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.

adata.obs[gene] = adata[:, adata.var_names==gene].X
sc.pp.regress_out(adata,gene)

Any help would be appreciated. Thank you!

Activity

  1. LuckyMD commented on Feb 19, 2019

    @LuckyMD
    Contributor

    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)?

    Something like this:

    adata.var['genes_of_interest'] = adata.X[:,gene_list].mean(0)
    sc.pp.regress_out(adata, genes_of_interest)
    

    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:

    adata_tmp = adata.copy()
    sc.pp.scale(adata_tmp)
    adata.var['genes_of_interest'] = adata_tmp.X[:,gene_list].mean(0)
    sc.pp.regress_out(adata, genes_of_interest)
    

    Note that I have not tested this code... so no guarantees ;).

  2. ivirshup commented on Jan 24, 2021

    @ivirshup
    Member

    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.

  3. added
    good first issueeasy first issue to get started in OSS community contribution!
    on Jan 24, 2021
  4. chenyangsu commented on Nov 10, 2026

    @chenyangsu

    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:

    from anndata.acc import A
    sc.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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    good first issueeasy first issue to get started in OSS community contribution!

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions