Skip to content

No p-value and adjusted p-value in adata.uns['cosg'] #6

Closed

Description

@chc1234567890

Hi,

I am using the cosg package for differentially expressed genes identification in my research. However, I noticed that the output does not include p-values and adjusted p-values, which are crucial for subsequent analysis, e.g. pathway over-representation analysis. It seems that they haven't been implemented in the source code. Also, It would greatly enhance the usability of the package if multiple methods for p-value and adjusted p-value calculation can be added (like those in scanpy.tl.rank_genes_groups, e.g. t-test, wilcoxon for p-value and benjamini-hochberg for adjusted p-value).

Thank you for considering this feature request!

sc.logging.print_header()
# scanpy==1.10.3 anndata==0.11.1 umap==0.5.7 numpy==1.24.3 scipy==1.13.1 pandas==2.2.3 scikit-learn==1.5.2
# statsmodels==0.14.4 igraph==0.11.5 pynndescent==0.5.13

cosg.cosg(adata,
    key_added='cosg',
        mu=1,
        n_genes_user=2000,
               groupby=group_var)

adata.uns['cosg']['pvals'] # Not implemented yet

adata.uns['cosg']['pvals_adj'] # Not implemented yet

Activity

  1. genecell commented on Sep 4, 2026

    @genecell
    Owner

    Thank you for the request, and for the patience — this is in COSG 1.2.0.

    cosg.cosg(adata, groupby=group_var,
              key_added='cosg', mu=1, n_genes_user=2000,
              calculate_pvalues=True,          # default False
              pvalue_fdr_method='fdr_bh')      # or 'fdr_by'
    
    adata.uns['cosg']['pvals']              # raw p
    adata.uns['cosg']['pvals_adj']          # Benjamini-Hochberg, within each group
    adata.uns['cosg']['neg_log10_pvals']    # -log10 p, no floor
    adata.uns['cosg']['zscores']            # standardised effect

    The default is off, so existing output is unchanged.

    On the choice of test

    We did not add t-test / wilcoxon options, and the reason is worth stating rather than leaving as a gap. Those tests answer a different question from the one COSG's score asks: they compare a gene's mean (or rank) between one group and the rest, whereas COSG ranks by the cosine specificity of the gene to the group. Attaching a t-test p-value to a cosine-ranked table would give you a number that does not calibrate the statistic it sits next to — the gene at the top of the COSG table would carry significance from a quantity that did not put it there. If you want a t-test or a Wilcoxon p-value, scanpy.tl.rank_genes_groups already computes exactly that, and running both is a reasonable thing to do.

    What 1.2.0 adds instead is a p-value for the statistic COSG actually ranks by. For gene g and a group of n_c cells, the raw cosine is T / (||x|| · sqrt(n_c)) with T the gene's sum over the group. Under the null that the gene is independent of the grouping, ||x|| and n_c are fixed, so testing the cosine is testing T — a sum over a random subset of cells without replacement, whose distribution has exact moments. Nothing is sampled, so there is no permutation floor, and the tail is computed with a conditional saddlepoint (with exact enumeration where the gene is sparse enough for it) rather than a normal approximation, which is anti-conservative by 3–36× exactly on the sparse, specific genes that make good markers.

    BH is computed within each group, over all genes tested in that group, before the top-N selection — adjusting after selection would divide by the size of the top-N rather than the size of the screen.

    One caveat, since you mention pathway over-representation downstream

    If group_var came from clustering the same expression matrix, these p-values are anti-conservative: the labels were chosen to separate the data they are then tested against. That is a property of the design, not of our approximation — it applies to rank_genes_groups in the same situation, and sampling permutations would not fix it.

    They are valid when the labels carry independent information (curated annotation, another modality, a reference mapping), or with count splitting, where you derive the labels on one split and pass the other via layer=. For an ORA input this usually matters less than it sounds, since you are ranking genes rather than making per-gene claims, but it is worth knowing which of the two you are doing.

    Full details, calibration numbers and cost are in the v1.2.0 release notes.

    Closing this as implemented — please reopen or open a new issue if the behaviour is not what you need.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions