在Python中使用超几何测试

Eli*_*man 1 python statistics p-value

我有两个基因列表,我计算它们之间的交集。
我需要计算假设的 p 值 - 这些列表的交集是偶然发生的。
我尝试使用 Fisher 的精确测试(scipy 函数)来实现这一点。
请注意,我需要一个单边 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, sep='\n', 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}')
Run Code Online (Sandbox Code Playgroud)

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

由于它太长,我不会复制整个文件,而是想象两个文件,其中包含大量由换行符分隔的随机数。
例如:

1
2
3
246
51451
...
Run Code Online (Sandbox Code Playgroud)

问题是 - 我如何确定我正确指定了渔夫精确函数的参数?根据我试图检查的假设是否正确?
我怀疑我做错了,但我不确定为什么。可能是错误的提示 - 我知道人口规模应该是相关的,但我不确定在哪里使用它以及如何使用它。
任何线索或见解将不胜感激。

更新:
我尝试以不同的方式实现它。

from scipy.stats import hypergeom as hg
import pandas as pd
def main(gene_path1, gene_path2, pop_size):
    genes1 = pd.read_csv(gene_path1, sep='\n', header=None)
    genes2 = pd.read_csv(gene_path2, sep='\n', 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()
    pvalue = hg.cdf(int(len_intersection)-1, int(pop_size), int(len_genes1), int(len_genes2))
    print(f'Genes1 len: {len_genes1}, Genes2 len: {len_genes2}, Intersection: {len_intersection}, p value: {pvalue})
Run Code Online (Sandbox Code Playgroud)

我只是想知道我的论点是否在正确的位置,我如何验证这一点?

O.r*_*rka 5

这也应该有帮助: 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
Run Code Online (Sandbox Code Playgroud)