在python中使用超几何检验

问题描述

我有两个基因列表,并计算它们之间的交集。
我需要计算以下假设的p值-这些列表的交集是偶然发生的。
我尝试使用费舍尔的精确测试(scipy function)来实现这一点。
请注意,我需要一个单边p值。

我的代码

def main(gene_path1,gene_path2,pop_size):
    genes1 = pd.read_csv(gene_path1,sep='\n',header=None)
    genes2 = pd.read_csv(gene_path2,header=None)

    intersection = pd.merge(genes1,genes2,how='inner').drop_duplicates([0])

    len_genes1 = genes1[0].count()
    len_genes2 = genes2[0].count()
    len_intersection = intersection[0].count()

    oddsratio,pvalue = stats.fisher_exact([[len_genes1 - len_intersection,len_genes1],[len_genes2 - len_intersection,len_genes2]],alternative='less')

    print(f'Genes1 len: {len_genes1},Genes2 len: {len_genes2},Intersection: {len_intersection},pvalue: {pvalue}')

为简单起见,我使用了一个数字列表(而非基因)。

由于时间太长,我不会复制整个文件,但可以想象两个文件,其中有许多由换行符分隔的随机数。
例如:

1
2
3
246
51451
...

问题是-如何确定为Fisher的确切函数正确指定了参数?我要检查的假设是否正确?
我怀疑我做错了,但是我不确定为什么。 可能是出了什么问题的暗示-我知道人口数量应该是相关的,但是我不确定在哪里使用以及如何使用。
任何线索或见解将不胜感激。

更新:
我试图以其他方式实现它。

from scipy.stats import hypergeom as hg
import pandas as pd
def main(gene_path1,how='inner').drop_duplicates([0])

    len_genes1 = genes1[0].count()
    len_genes2 = genes2[0].count()
    len_intersection = intersection[0].count()
    pvalue = hg.cdf(int(len_intersection)-1,int(pop_size),int(len_genes1),int(len_genes2))
    print(f'Genes1 len: {len_genes1},p value: {pvalue})

我只是想知道我是否将论点放在正确的位置,我该如何验证呢?

解决方法

这也应该有帮助: http://pedagogix-tagc.univ-mrs.fr/courses/ASG1/practicals/go_statistics_td/go_statistics_td_2015.html

g = 75 ## Number of submitted genes
k = 59 ## Size of the selection,i.e. submitted genes with at least one annotation in GO biological processes
m = 611 ## Number of "marked" elements,i.e. genes associated to this biological process
N = 13588 ## Total number of genes with some annotation in GOTERM_BP_FAT.
n = N - m ## Number of "non-marked" elements,i.e. genes not associated to this biological process
x = 19 ## Number of "marked" elements in the selection,i.e. genes of the group of interest that are associated to this biological process

# Python
stats.hypergeom(M=N,n=m,N=k).sf(x-1)
# 4.989682834451419e-12

# R
phyper(q=x -1,m=m,n=n,k=k,lower.tail=FALSE)
# [1] 4.989683e-12
,

不知道你是否还有同样的问题。但是,我发现 this link 对确保超几何测试结果非常有用。关于您的计算,您的结果必须等于累积概率:P(X