-
Notifications
You must be signed in to change notification settings - Fork 0
/
3_munge_sumstats.py
188 lines (117 loc) · 4.24 KB
/
3_munge_sumstats.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import sys
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import os
import glob
import time
import subprocess as sp
import multiprocessing as mp
import psutil
from basic_tools import *
debug=False
# # functions used for loading gwas result files
# In[2]:
def save_sumstats(gwas_result,pheno_code):
dest=premunge_path.format(pheno_code)
if os.path.exists(dest) or os.path.exists(sumstats_path.format(pheno_code)+'.sumstats.gz'):
print("Congratulations!. premunged sumstat of",pheno_code,"exists. passed.")
return
elif os.path.exists(sumstats_path.format(pheno_code)+'.sumstats.gz'):
print("Congratulations!. munged sumstat of",pheno_code,"exists. passed.")
return
print("Saving sumstats:",dest)
tosave=gwas_result
tosave.to_csv(dest,sep=' ',columns=['SNP','A1','A2','N','BETA','P','MAF','INFO'],index=False)
print("Finished saving sumstats:",dest)
def munge_sumstats(pheno_code):
if os.path.exists(sumstats_path.format(pheno_code)+'.sumstats.gz'):
print("Congratulations!. munged sumstats of",pheno_code,"exists. passed.")
return
script=['munge_sumstats.py','--sumstats',premunge_path.format(pheno_code),'--merge-alleles',snplist_path,'--out',sumstats_path.format(pheno_code)]
print('Started:',' '.join(script))
sp.call(script)
print('Finished:',' '.join(script))
#sp.call(['munge_sumstats.py','--sumstats',premunge_path.format(pheno_code),'--merge-alleles',snplist_path,'--out',sumstats_path.format(pheno_code)])
# In[3]:
if debug:
gwas_result=read_gwas2('23115')
gwas_result.to_pickle('23115.pickle')
# In[4]:
def for_mp(pheno_code):
gwas_result=read_gwas2(pheno_code)
save_sumstats(gwas_result,pheno_code)
munge_sumstats(pheno_code)
# In[6]:
#pheno_code_list=ukbb_table_filtered['Phenotype Code'].str.replace('_irnt','')
# In[4]:
start=int(sys.argv[1])
end=int(sys.argv[2])
# In[23]:
#phenotypes_filtered
# ```
# #!/bin/sh
# chrN=$1
#
# echo $chrN "parallel started"
#
# qctool -g "/data/UKbiobank/ukb_genetic_info/imp/ukb_imp_chr${chrN}_v3.bgen" -s "/data/UKbiobank/ukb_genetic_info/imp/ukb46263_imp_chr${chrN}_v3_s487320.sample" -og "/data01/ch6845/ldsc_data/ukb_imp_plink/ukb_imp_chr${chrN}_v3.bed" -incl-samples "/data01/ch6845/ldsc_data/ukb_sub.sample"
# ```
# ```
# jupyter nbconvert 3_munge_sumstats.ipynb --to script && mkdir ~/premunge &&python 3_munge_sumstats.py 0 50
# ```
# In[6]:
#start,end=383,384
# In[9]:
#phenotypes_filtered['phenotype']
# In[21]:
phenotypes_filtered.shape
# In[ ]:
for idx,row in phenotypes_filtered.iloc[start:end].iterrows():
pheno_code=idx
if os.path.exists(sumstats_path.format(pheno_code)+'.sumstats.gz'):
print("Congratulations!. passed",pheno_code,"exists. passed.")
continue
print(pheno_code)
gwas_result=read_gwas2(pheno_code)
save_sumstats(gwas_result,pheno_code)
munge_sumstats(pheno_code)
run_command('rm {}'.format(premunge_path.format(pheno_code)))
# print(pheno_code)
# In[23]:
idx_list=[]
for idx,row in phenotypes_filtered.iterrows():
pheno_code=idx
if os.path.exists(sumstats_path.format(pheno_code)+'.sumstats.gz'):
#print("Congratulations!. passed",row['phenotype'],"exists. passed.")
continue
idx_list.append(idx)
print(idx)
continue
gwas_result=read_gwas2(pheno_code)
save_sumstats(gwas_result,pheno_code)
munge_sumstats(pheno_code)
run_command('rm {}'.format(premunge_path.format(pheno_code)))
# print(pheno_code)
# In[18]:
for i in range(0, len(idx_list), int(len(idx_list)/25)):
print(idx_list[i:i + int(len(idx_list)/25)])
# In[5]:
"""
cnt=0
for idx,pheno_code in ukbb_table_filtered['Phenotype Code'].str.replace('_irnt','').iteritems():
if os.path.exists(sumstats_path.format(pheno_code)+'.sumstats.gz'):
continue
print(idx,pheno_code)
cnt+=1
"""
# In[ ]:
"""
a=pd.read_csv('/home/ch6845/premunge/20426.premunge',sep=' ')
a.shape,a[np.logical_not(a['BETA']=='Infinity')].shape,a[np.logical_not(a['BETA']=='-Infinity')].shape
a[np.logical_not(a['BETA']=='Infinity')].to_csv('/home/ch6845/premunge/20426.premunge',sep=' ',index=None)
"""