-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5_run_ldsc.py
153 lines (95 loc) · 3.78 KB
/
5_run_ldsc.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
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import sys
import os
import glob
import subprocess as sp
import multiprocessing as mp
import pandas as pd
import numpy as np
from basic_tools import *
debug=False
# In[2]:
def run_ldsc(pheno_code,ld,output,mode='original',samp_prev=np.nan,pop_prev=np.nan):
if os.path.exists(ldsc_path.format(pheno_code)+'.log'):
print("Congratulations!. ldsc result of",pheno_code,"exists. passed.")
return
if mode=='original':
script=['ldsc.py','--h2',sumstats_path.format(pheno_code)+'.sumstats.gz',
'--ref-ld-chr',ld_path.format(ld,''),
'--w-ld-chr',wld_path,
'--out',ldsc_path.format(output)]
elif mode=='my':
script=['ldsc_my.py','--h2',sumstats_path.format(pheno_code)+'.sumstats.gz',
'--ref-ld-chr',ld_path.format(ld,''),
'--w-ld-chr',wld_path,
'--out',ldsc_path.format(output)]
else:
print("run_ldsc mode Error!!!!!!!")
if np.isnan(samp_prev)==False and np.isnan(pop_prev)==False:
script+=['--samp-prev',str(samp_prev),'--pop-prev',str(pop_prev)]
print('Started:',' '.join(script))
sp.call(script)
print('Finished:',' '.join(script))
# In[3]:
def run_ldsc_wrapper(prefix,scale,pheno_code,samp_prev=np.nan,pop_prev=np.nan):
run_ldsc(pheno_code,prefix,'{}.{}'.format(prefix,pheno_code),mode='original' if mode=='uni' else 'my',samp_prev=samp_prev,pop_prev=pop_prev)
# In[3]:
sys.argv#uni 0 20 x x
# In[4]:
mode=sys.argv[1]
scale=int(sys.argv[2])
cores=int(sys.argv[3])
start=int(sys.argv[4])
end=int(sys.argv[5])
if mode=='uni':
prefix=mode
else:
prefix=mode+str(scale)
# In[25]:
#start,end,prefix=0,1000,'bp300'
# In[3]:
phenotypes_uni_filtered['prevalence']=phenotypes_uni_filtered['n_cases']/phenotypes_uni_filtered['n_non_missing']
# In[4]:
phenotypes_uni_filtered.shape
# In[5]:
pheno_code_list_todo=[]
for idx,row in phenotypes_uni_filtered.iloc[start:end].iterrows():
if os.path.exists(ldsc_path.format('{}.{}'.format(prefix,idx))+'.log'):
#print(ldsc_path.format('{}.{}'.format(prefix,idx))+'.log','exists')
continue
print(idx,end=' ')
pheno_code_list_todo.append((idx,row['prevalence']))
# In[11]:
"""
phenotypes_filtered['prevalence']=phenotypes_filtered['n_cases']/phenotypes_filtered['n_non_missing']
phenotypes_filtered.shape
pheno_code_list_todo=[]
for idx,row in phenotypes_filtered.iloc[start:end].iterrows():
if os.path.exists(ldsc_path.format('{}.{}'.format(prefix,idx))+'.log'):
continue
print(idx,end=' ')
pheno_code_list_todo.append((idx,row['prevalence']))
"""
# ```
# jupyter nbconvert 5_run_ldsc.ipynb --to script
#
# export SCREENDIR=$HOME/.screen
#
# start=0;end=600;mode=uni
# python 5_run_ldsc.py $mode 0 10 $start $end
#
# start=0;end=600;mode=bp
# python 5_run_ldsc.py $mode 300 10 $start $end && python 5_run_ldsc.py $mode 128 10 $start $end && python 5_run_ldsc.py $mode 64 5 $start $end && python 5_run_ldsc.py $mode 32 5 $start $end && python 5_run_ldsc.py $mode 16 5 $start $end && python 5_run_ldsc.py $mode 8 2 $start $end
#
#
# python 5_run_ldsc.py bp 128 10 $start $end && python 5_run_ldsc.py bp 64 5 $start $end && python 5_run_ldsc.py bp 32 5 $start $end && python 5_run_ldsc.py bp 16 5 $start $end && python 5_run_ldsc.py bp 8 2 $start $end
# ```
# In[ ]:
#pool = mp.Pool(processes=15)
#pool.starmap(run_ldsc_wrapper,[(mode,scale,pheno_code,prevelence,prevelence) for (pheno_code,prevelence) in pheno_code_list_todo])
# In[ ]:
pool = mp.Pool(processes=cores)
#pool.starmap(run_ldsc_wrapper,[(mode,scale,pheno_code) for pheno_code in pheno_code_list_todo])
pool.starmap(run_ldsc_wrapper,[(prefix,scale,pheno_code,prevelence,prevelence) for (pheno_code,prevelence) in pheno_code_list_todo])