-
Notifications
You must be signed in to change notification settings - Fork 6
/
QTLClumpanator.py
executable file
·205 lines (175 loc) · 7.22 KB
/
QTLClumpanator.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/usr/bin/python
print "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
print " QTLTools Clumpanator"
print ""
print ""
print "* Written by : Jacco Schaap | jacco_schaap@hotmail.com"
print "* Suggested for by : Sander W. van der Laan | s.w.vanderlaan-2@umcutrecht.nl"
print "* Last update : 2018-07-26"
print "* Name : QTLClumpanator"
print "* Version : v1.1.1"
print ""
print "* Description : In case of a QTL analysis this script will collect all "
print " variants in LD with index variants based on a specific GWAS"
print " dataset (e.g. CARDIoGRAMplusC4D), and according to p-value, "
print " r^2, and range thresholds (in kb)."
print ""
print "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
from subprocess import call
from datetime import datetime
from os.path import isfile, isdir
import gzip
import sys
plink = sys.argv[1] #'/hpc/local/CentOS7/dhl_ec/software/plink_v1.9'
id = sys.argv[2]
chr = sys.argv[3]
nominal_data_file = sys.argv[4]
ld_nominal_data_file = sys.argv[5]
output = sys.argv[6] # clumps directory in qtl folder
data = sys.argv[7]
clump = sys.argv[8]
metalfile = sys.argv[15]
exclusion = sys.argv[16] # exclude variants that are duplicated because they are multi-allelic - such as rs114643911.
try:
clump_tresh = sys.argv[9]
except IndexError:
print 'No clump threshold given, set to 0.8.'
clump_tresh = 0.8
try:
clump_p1 = sys.argv[10]
except IndexError:
print 'No clump p1 threshold given, set to 5e-8 (GWAS threshold).'
clump_p1 = 5e-8
try:
clump_p2 = sys.argv[11]
except IndexError:
print 'No clump p2 threshold given, set to 0.05.'
clump_p2 = 0.05
try:
clump_kb = sys.argv[12]
except IndexError:
print 'No clump range threshold given, set to 1000kb.'
clump_kb = 1000
try:
clump_gwas_snpfield = sys.argv[13]
except IndexError:
print 'The clump varianID field.'
clump_gwas_snpfield = "SNP"
try:
clump_gwas_pval = sys.argv[14]
except IndexError:
print 'The clump p-value field.'
clump_gwas_pval = "P"
if not isfile(sys.argv[0]):
print 'Script not found'
def main():
print 'Start-time: ' + datetime.now().strftime('%Y-%m-%d %H:%M:%S') + '\n'
# check_qtlnom()
read_clumped(id, chr)
print '\n' + 'End-time: ' + datetime.now().strftime('%Y-%m-%d %H:%M:%S')
def create_clumped_file(chr):
# creates clumped file with minimum p-value of 0.05
if clump == 'Y':
call([plink,
"--bfile", data,
"--exclude", exclusion,
"--clump-verbose",
"--clump", metalfile,
"--clump-r2", clump_tresh, "--clump-p1", clump_p1, "--clump-p2", clump_p2, "--clump-kb", clump_kb,
"--clump-snp-field", clump_gwas_snpfield, "--clump-field", clump_gwas_pval,
"--memory", "4000",
"--out", output+"/chr"+chr+"."+id
])
if clump == 'N':
call([plink,
"--bfile", data,
"--exclude", exclusion,
"--clump-verbose",
"--clump", metalfile,
"--clump-r2", "0.8", "--clump-p1", "5e-8", "--clump-p2", "0.05", "--clump-kb", "1000",
"--clump-snp-field", "SNP", "--clump-field", "P",
"--memory", "4000",
"--out", output+"/chr"+chr+"."+id
])
def read_clumped(id, chr):
# print id
range = ''
ldbuddies = []
if isfile(output + '/highlight_ranges_' + id + '.list'):
with open(output + '/highlight_ranges_' + id + '.list', 'r') as file:
for line in file.readlines():
line = line.split('\t')
if id in line[0]:
open(output + '/highlight_ranges_' + id + '.list', 'w')
# id = 'rs10953541'
if not isfile(output + '/chr' + chr + '.' + id + '.clumped'):
print "\n\t***Chromosome " + chr + " isn't clumped, mr Bourne will do it right away...***\n"
create_clumped_file(chr)
print "\nDone clumping chr" + chr + ", fast ain't it???\n"
if not isfile(output + '/chr' + chr + '.' + id + '.clumped'):
print 'no LD buddies found, created empty file\n'
open(output+'/chr'+chr+'.' + id + '.clumped', 'w')
elif isfile(output + '/chr' + chr + '.' + id + '.clumped'):
print "\n\t***Chromosome " + chr + " for " + id + " is clumped before, skipping this part.***\n"
with open(output + '/chr' + chr + '.' + id + '.clumped', 'r') as file, open(output + '/ldbuddies_' + id + '.list',
'w') as outfile \
, open(output + '/rsquared.txt', 'w') as rfile, open(output + '/only_ldbuddies_' + id + '.list', 'w') as ldfile:
print 'Outputfile: ' + output + '/ldbuddies_' + id + '.list'
outfile.write(id + '\t' + id + '\t' + '1' + '\n')
ldfile.write(id + '\n')
teller = 0
chr_bp = ''
for line in file.readlines():
line = line.strip('\n')
line = line.split()
# print line
try:
try:
if id == line[2]:
# print line
# bp = line[3]
chr_bp = chr + ':' + line[3]
except IndexError:
pass
if id == line[1]:
# print line
teller = 1
if teller == 1 and 'RANGE:' not in line and '(INDEX)' not in line:
# print line
ldbuddies.append(line[0])
ldfile.write(line[0] + '\n')
outfile.writelines(id + '\t' + line[0] + '\t' + line[2] + '\n')
if "RANGE:" in line and teller == 1:
range = line[1]
teller = 0
# print line
except IndexError:
continue
ldbuddies.append(id)
try:
range = range.split(':')[1]
range = range.split('..')
# write range file, comma separated
with open(output + '/highlight_ranges_' + id + '.list', 'a') as rangefile:
rangefile.writelines(id + ',' + range[0] + ',' + range[1] + '\n')
print 'High-light range: ' + range[0] + '..' + range[1]
print 'Number of LD buddies found: ' + str(len(ldbuddies))
return ldbuddies
except IndexError:
print 'No LD buddies found for ' + id + ', created empty files.\n'
with open(output + '/highlight_ranges_' + id + '.list', 'a') as rangefile:
rangefile.writelines(id + ',' + '' + ',' + '' + '\n')
def check_qtlnom():
ldbuddies = read_clumped(id, chr)
print 'File: ' + nominal_data_file
with gzip.open(nominal_data_file, 'r') as file, \
gzip.open(ld_nominal_data_file + '.txt.gz', 'w') as outfile:
for regel in file.readlines():
regel = regel.strip('\n')
line = regel.split(' ')
# print line
for buddie in ldbuddies:
if buddie == line[1]:
# print line
outfile.writelines(regel + '\n')
main()