-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter_sentinel_SNPs.py
259 lines (220 loc) · 10.3 KB
/
filter_sentinel_SNPs.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import os, json
from sys import platform, float_info
from transform_gwas_to_long import file_sep, DATA_FOLDER, NUM_COORDS, NUM_COORDS_VALID, NUM_SLICES, NUM_SLICES_VALID, \
SNIP_COUNTS, NUM_SNIPS, CHROMOSOMES, SLICE_NUMS, COORD_NUMS, safe_snipID
def parse_sentinelSNPs():
loci_summary_file = os.path.join(DATA_FOLDER, "gwSigLociSummary.csv")
loci_out_json = os.path.join(DATA_FOLDER, "gwSigLociSummary.json")
if os.path.isfile(loci_out_json):
loci_dict = json.loads(open(loci_out_json).read())
return loci_dict
loci_dict = {}
counter = 0
with open(loci_summary_file, "r") as fin:
for l in fin:
counter +=1
l_toks = l.rstrip().split(",")
if counter==1:
header= l_toks
else:
cur_dict = dict(zip(header, l_toks))
locusID = cur_dict["locusID"]
loci_dict[locusID] = cur_dict
fin.close()
with open(loci_out_json, "w") as fout:
json.dump(loci_dict, fout)
fout.close()
return loci_dict
def parse_lociSNPs():
# loci_dict = parse_sentinelSNPs()
locSNPs_file = os.path.join(DATA_FOLDER, "gwSigLociSNPs.csv")
locSNPIDs_out = os.path.join(DATA_FOLDER, "gwSigLociSNP_IDs.csv")
counter = 0
with open(locSNPIDs_out, "w") as fout:
with open(locSNPs_file, "r") as fin:
for l in fin:
counter +=1
l_toks = l.rstrip().split(",")
if counter==1:
header = l_toks
else:
cur_dict = dict(zip(header, l_toks))
# locusID = cur_dict["locusID"]
# sentinelSNP = cur_dict["sentinelSNPID"]
snipID = cur_dict["locusSNPID"]
fout.write("{}\n".format(snipID))
fin.close()
fout.close()
return
def filter_SNPs(chromosomes=CHROMOSOMES):
locSNPIDs_out = os.path.join(DATA_FOLDER, "gwSigLociSNP_IDs.csv")
locSNPIDs = []
with open(locSNPIDs_out, "r") as fin:
for l in fin:
locSNPIDs.append(l.rstrip())
fin.close()
locSNPIDs_set = set(locSNPIDs)
for cdx, chrom in enumerate(chromosomes):
chr_folder = os.path.join(DATA_FOLDER, "chr{}".format(chrom))
outdir = os.path.join(chr_folder, 'sigSNPs')
if not os.path.isdir(outdir):
os.makedirs(outdir)
for sdx, slice_num in enumerate(SLICE_NUMS):
print("processing:", cdx, chrom, sdx, slice_num)
fname = "slice{}_result.txt".format(slice_num)
fpath = os.path.join(chr_folder, fname)
outpath = os.path.join(outdir, fname)
if not os.path.isfile(fpath):
print("slice data not found: ", slice_num, fpath)
continue
with open(outpath, "w") as fout:
counter = 0
with open(fpath, 'r') as fin:
for l in fin:
counter+=1
l_toks = l.rstrip().split(",")
if counter==1:
header = l_toks
fout.write("{}\n".format(l))
else:
cur_dict = dict(zip(header, l_toks)) # line dict
snipID_raw = cur_dict["ID"]
# pos = cur_dict["POS"]
# snipID = safe_snipID(snipID_raw)
# snip_index = counter-1 # track which subfolder for netlify
if snipID_raw in locSNPIDs_set:
fout.write("{}".format(l))
fin.close()
fout.close()
print("processed:", cdx, chrom, sdx, slice_num)
return
def make_combined_SNPs_info_file():
data_folder_html = os.path.join("html", "data", "gwas")
data_folder_raw = os.path.join("data_raw", "gwas")
# locSNPIDs_file = os.path.join(data_folder_html, "gwSigLociSNP_IDs.csv")
# locSNPIDs = []
# with open(locSNPIDs_file, "r") as fin:
# for l in fin:
# locSNPIDs.append(l.rstrip())
# fin.close()
# locSNPIDs_set = set(locSNPIDs) # NB - non-unique by just rs_name!
sentinelSNPs_file = os.path.join(data_folder_html, "gwSigLociSummary.csv")
sentinelDict = {}
with open(sentinelSNPs_file, "r") as fin:
counter = 0
for l in fin:
counter +=1
l_toks = l.rstrip().split(",")
if counter==1:
header=l_toks
else:
cur_dict = dict(zip(header, l_toks))
curID = cur_dict["ID"]
sentinelDict[curID] = cur_dict
fin.close()
locSNP_file = os.path.join(data_folder_html, "gwSigLociSNPs.csv")
SNP_IDs = []
chr_SNP_keys = []
sigSNPs_dict = {}
with open(locSNP_file, "r") as fin:
counter = 0
for l in fin:
counter +=1
l_toks = l.rstrip().split(",")
if counter==1:
header=l_toks # locusID,sentinelSNPID,locusSNPID,r2withSentinel
else:
cur_dict = dict(zip(header, l_toks))
curID = cur_dict["locusSNPID"]
SNP_IDs.append(curID)
chr_SNP_keys.append("{}; {}".format(sentinelDict[cur_dict["sentinelSNPID"]]["CHR"], curID))
sigSNPs_dict[curID] = cur_dict
fin.close()
locSNPIDs_set = set(sigSNPs_dict.keys())
# sanity checks
print([len(SNP_IDs), len(set(SNP_IDs))])
print([len(chr_SNP_keys), len(set(chr_SNP_keys))])
# go through slice1_result.txt for each chromosome
sigSNPS_outfile = os.path.join(data_folder_html, "info_sigSNPs.csv")
SNPS_outfile = os.path.join(data_folder_html, "info_SNPs.csv")
SNPS_outfile = os.path.join(data_folder_html, "info_SNPs_v2.csv")
with open(sigSNPS_outfile, "w") as fout_sig:
fout_sig.write("SNP,chr,pos,A1,num_sig,bonf,sentinel\n")
with open(SNPS_outfile, "w") as fout:
fout.write("SNP,chr,pos\n")
# fout.write("SNP,chr\n") # bare minimum
for cdx, chromosome in enumerate(CHROMOSOMES):
fpath = os.path.join(data_folder_raw, "chr{}".format(chromosome), "slice1_result.txt")
print("processing ", cdx, chromosome, fpath)
with open(fpath, "r") as fin:
counter = 0
for l in fin:
counter += 1
l_toks = l.rstrip().split(",")
if counter==1:
header = l_toks
else:
cur_dict = dict(zip(header, l_toks))
curID = cur_dict["ID"]
if curID in locSNPIDs_set:
sentinel_data = sentinelDict[sigSNPs_dict[curID]["sentinelSNPID"]]
is_sentinel = int(curID in sentinelDict)
out_vals = [curID, chromosome, cur_dict["POS"], cur_dict["A1"],
sentinel_data["nPixelsLocus"], sentinel_data["BonferroniSig"], is_sentinel]
fout_sig.write("{}\n".format(",".join([str(x) for x in out_vals])))
else:
# out_vals = [curID, chromosome] # bare minimum
out_vals = [curID, chromosome, cur_dict["POS"]] # bare minimum
fout.write("{}\n".format(",".join([str(x) for x in out_vals])))
fin.close()
fout.close()
fout_sig.close()
return
def make_combined_location_files():
data_folder_html = os.path.join("html", "data", "gwas")
location_folder = os.path.join(data_folder_html, "location_summary")
if not os.path.isdir(location_folder):
os.makedirs(location_folder)
for cdx, chromosome in enumerate(CHROMOSOMES):
slice_coord_dict = {}
chr_folder = os.path.join(data_folder_html, "chr{}".format(chromosome), "sigSNPs", "long")
for sdx, slice_num in enumerate(range(1,NUM_SLICES_VALID+1)):
fpath = os.path.join(chr_folder, "slice{}.txt".format(slice_num))
print("processing: ", cdx, chromosome, sdx, slice_num, fpath)
with open(fpath, "r") as fin:
counter = 0
for l in fin:
counter+=1
l_toks = l.rstrip().split(",")
if counter==1:
header = l_toks
header = ["SNP","slice_num","coord_num","beta","pval","Bonf"]
else:
cur_dict = dict(zip(header, l_toks))
slice_coord = "{},{}".format(cur_dict["slice_num"],cur_dict["coord_num"])
if slice_coord not in slice_coord_dict:
slice_coord_dict[slice_coord] = []
cur_vals = [cur_dict["SNP"],chromosome,cur_dict["beta"],cur_dict["pval"]]
slice_coord_dict[slice_coord].append(cur_vals)
fin.close()
# write location_summaries at chromosome level to avoid RAM issues
for slice_coord, sdata in slice_coord_dict.items():
stoks = slice_coord.split(",")
slice_num, coord_num = stoks
outpath = os.path.join(location_folder, "loc_summary_{}_{}.csv".format(slice_num, coord_num))
print("writing: ", cdx, chromosome, slice_num, coord_num, outpath)
if not os.path.isfile(outpath): # add header if first write
with open(outpath, "w") as fout:
fout.write("SNP,chr,beta,pval\n")
fout.close()
with open(outpath, "a") as fout: # otherwise append
for ddx, d in enumerate(sdata):
fout.write("{}\n".format(",".join([str(x) for x in d])))
fout.close()
return
if __name__ == "__main__":
# parse_sentinelSNPs()
# parse_lociSNPs()
# filter_SNPs()
make_combined_SNPs_info_file()
# make_combined_location_files()