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
{{ message }}
Repository navigation
No p-value and adjusted p-value in adata.uns['cosg'] #6
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 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 Falsepvalue_fdr_method='fdr_bh') # or 'fdr_by'adata.uns['cosg']['pvals'] # raw padata.uns['cosg']['pvals_adj'] # Benjamini-Hochberg, within each groupadata.uns['cosg']['neg_log10_pvals'] # -log10 p, no flooradata.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.
Activity
genecell commentedon Sep 4, 2026
Thank you for the request, and for the patience — this is in COSG 1.2.0.
The default is off, so existing output is unchanged.
On the choice of test
We did not add
t-test/wilcoxonoptions, 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_groupsalready 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_ccells, the raw cosine isT / (||x|| · sqrt(n_c))withTthe gene's sum over the group. Under the null that the gene is independent of the grouping,||x||andn_care fixed, so testing the cosine is testingT— 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_varcame 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 torank_genes_groupsin 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.