1. 理論
例として、全タンパクを化合物
全タンパク ( | 化合物 | 合計 | ||
含まれない | 含まれる | |||
Pathway | 含まれる | |||
含まれない | ||||
合計 |
合計部分を固定した場合、上記のような表が得られる確率は超幾何分布に従います(詳しくはこちらの記事)。
具体的には以下のようにして
2. 分割表を作成する関数
Python
x
27
27
1
# library
2
import pandas as pd
3
4
5
# 関数
6
def make_contingency_table(
7
col_positives,
8
row_positives,
9
N # a+b+c+d
10
):
11
_b = set(col_positives) & set(row_positives) # double positives (b, 右上)
12
_a = set(row_positives) - _b # only row positives (a, 左上)
13
_d = set(col_positives) - _b # only col positives (d, 右下)
14
15
a, b, d = len(list(_a)), len(list(_b)), len(list(_d))
16
c = n_proteins - (a + b + d) # double negatives (c, 左下)
17
18
df_table = pd.DataFrame([[a, b], [c, d]])
19
df_table.columns = ['col_neg', 'col_pos']
20
df_table.index = ['row_pos', 'rpw_neg']
21
df_table = df_table.T
22
23
return df_table
24
25
26
# 使用例
27
table = make_contingency_table(ink_syms[ik], ptm_syms[pt], n_proteins)
3. 分割表から正確確率検定のp値/オッズ比を返す関数
Python
1
13
13
1
# library
2
from scipy.stats import fisher_exact
3
4
5
# fisher's exact testの関数 (alternativeに注意)
6
def calc_p(table):
7
odds_ratio, p = fisher_exact(table, alternative='less')
8
return p
9
10
11
# 使用例
12
table = make_contingency_table(ink_syms[ik], ptm_syms[pt], n_proteins)
13
calc_p(table)
4. 使用例
任意の組合せに対してFisherの正確確率検定を実施。
Python
1
15
15
1
# make table & tests for all inputs
2
def enrichment_analysis(
3
inkList, ptmList, n_proteins,
4
ink_syms, ptm_syms
5
):
6
pvals = []
7
8
for i, ik in enumerate(inkList):
9
pvals.append([])
10
11
for pt in ptmList:
12
table = make_contingency_table(ink_syms[ik], ptm_syms[pt], n_proteins)
13
pvals[-1].append(calc_p(table))
14
15
return pvals
コメント