-
Notifications
You must be signed in to change notification settings - Fork 0
/
enrichment_analysis.py
130 lines (108 loc) · 4.09 KB
/
enrichment_analysis.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import os
import glob
from utils import read_pbz2
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pylab as plt
dfs = []
# Read the regression results:
for trait_file in glob.glob("../results/regression/EUR/M_chi2filt/*/S-*.pbz2"):
trait_name = os.path.basename(os.path.dirname(trait_file))
trait_method = os.path.basename(trait_file).replace('.pbz2', '')
res = read_pbz2(trait_file)
res_comp = read_pbz2(os.path.join(os.path.dirname(trait_file), 'S-D2_0.0.pbz2'))
maf_bin_filt = np.array(['MAFbin' in n for n in res['Annotations']['Names']])
univar_prop_hg2 = res['Annotation Sum'][0][maf_bin_filt] / (res['Annotation Sum'][0][0])
var_prop = res_comp['Annotation Sum'][0][maf_bin_filt] / res_comp['Annotation Sum'][0][0]
univar_enrich = univar_prop_hg2 / var_prop
df = pd.DataFrame({
'MAF bin': [int(x.replace('MAFbin', '')) for x in np.array(res['Annotations']['Names'])[maf_bin_filt]],
'Score': res['Annotations']['hg2'][maf_bin_filt],
'Enrichment': res['Annotations']['hg2'][maf_bin_filt] / (res['Counts'][0][maf_bin_filt] / res['Counts'][0][0]),
'M Enrichment': res['Annotations']['hg2'][maf_bin_filt] / var_prop,
'Univar Enrichment': univar_enrich
})
df.loc[len(df)] = [0, 1. - df['Score'].sum(),
(1. - df['Score'].sum()) / (
(res['Counts'][0][0] - res['Counts'][0][maf_bin_filt].sum()) / res['Counts'][0][0]),
(1. - df['Score'].sum()) / (1. - var_prop.sum()),
(1. - univar_prop_hg2.sum()) / (1. - var_prop.sum())]
df['Trait'] = trait_name
df['Method'] = trait_method
dfs.append(df)
maf_hg = pd.concat(dfs)
maf_hg['MAF bin'] = maf_hg['MAF bin'].astype(np.int)
# Plot the analysis results:
ld_scores_colors = {
'D2_0.0': '#E15759',
'D2_0.25': '#C66F6F',
'D2_0.5': '#AC8786',
'D2_0.75': '#919F9C',
'D2_1.0': '#76B7B2',
'R2_0.0': '#B07AA1',
'R2_0.25': '#C17F84',
'R2_0.5': '#D18466',
'R2_0.75': '#E28949',
'R2_1.0': '#F28E2B',
'S-D2_0.0': '#E15759',
'S-D2_0.25': '#C66F6F',
'S-D2_0.5': '#AC8786',
'S-D2_0.75': '#919F9C',
'S-D2_1.0': '#76B7B2',
'S-R2_0.0': '#B07AA1',
'S-R2_0.25': '#C17F84',
'S-R2_0.5': '#D18466',
'S-R2_0.75': '#E28949',
'S-R2_1.0': '#F28E2B'
}
ld_scores_names = {
'D2_0.0': '$\\alpha=0$',
'D2_0.25': '$\\alpha=0.25$',
'D2_0.5': '$\\alpha=0.5$',
'D2_0.75': '$\\alpha=0.75$',
'D2_1.0': '$\\alpha=1$',
'R2_0.0': '$\\alpha=0$',
'R2_0.25': '$\\alpha=0.25$',
'R2_0.5': '$\\alpha=0.5$',
'R2_0.75': '$\\alpha=0.75$',
'R2_1.0': '$\\alpha=1$',
'S-D2_0.0': '$\\alpha=0$',
'S-D2_0.25': '$\\alpha=0.25$',
'S-D2_0.5': '$\\alpha=0.5$',
'S-D2_0.75': '$\\alpha=0.75$',
'S-D2_1.0': '$\\alpha=1$',
'S-R2_0.0': '$\\alpha=0$',
'S-R2_0.25': '$\\alpha=0.25$',
'S-R2_0.5': '$\\alpha=0.5$',
'S-R2_0.75': '$\\alpha=0.75$',
'S-R2_1.0': '$\\alpha=1$',
}
sns.set_context('talk')
stratified = True
methods = ['D2_0.0', 'D2_0.25', 'D2_0.5', 'D2_0.75', 'D2_1.0']
if stratified:
methods = ['S-' + m for m in methods]
data_df = maf_hg.loc[maf_hg['Method'].isin(methods)].reset_index()
plt.subplots(figsize=(12, 8))
sns.barplot(x='MAF bin', y='Enrichment', hue='Method',
data=data_df, ci=None,
hue_order=methods,
palette=ld_scores_colors)
plt.xlabel('MAF bin')
plt.ylabel('Heritability Enrichment')
plt.legend(labels=[ld_scores_names[m] for m in methods], loc='center left', bbox_to_anchor=(1, 0.5))
plt.tight_layout()
plt.savefig("figures/analysis/enrichment/heritability_enrichment.png")
plt.close()
plt.subplots(figsize=(12, 8))
sns.barplot(x='MAF bin', y='M Enrichment', hue='Method',
data=data_df, ci=None,
hue_order=methods,
palette=ld_scores_colors)
plt.xlabel('MAF bin')
plt.ylabel('Functional Enrichment')
plt.legend(labels=[ld_scores_names[m] for m in methods], loc='center left', bbox_to_anchor=(1, 0.5))
plt.tight_layout()
plt.savefig("figures/analysis/enrichment/functional_enrichment.png")
plt.close()