forked from getian107/PRScs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_genet.py
196 lines (151 loc) · 7.19 KB
/
parse_genet.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
#!/usr/bin/env python
"""
Parse the reference panel, summary statistics, and validation set.
"""
import os
import scipy as sp
from scipy.stats import norm
from scipy import linalg
import h5py
def parse_ref(ref_file, chrom):
print('... parse reference file: %s ...' % ref_file)
ref_dict = {'CHR':[], 'SNP':[], 'BP':[], 'A1':[], 'A2':[], 'MAF':[]}
with open(ref_file) as ff:
header = next(ff)
for line in ff:
ll = (line.strip()).split()
if int(ll[0]) == chrom:
ref_dict['CHR'].append(chrom)
ref_dict['SNP'].append(ll[1])
ref_dict['BP'].append(int(ll[2]))
ref_dict['A1'].append(ll[3])
ref_dict['A2'].append(ll[4])
ref_dict['MAF'].append(float(ll[5]))
print('... %d SNPs on chromosome %d read from %s ...' % (len(ref_dict['SNP']), chrom, ref_file))
return ref_dict
def parse_bim(bim_file, chrom):
print('... parse bim file: %s ...' % (bim_file + '.bim'))
vld_dict = {'SNP':[], 'A1':[], 'A2':[]}
with open(bim_file + '.bim') as ff:
for line in ff:
ll = (line.strip()).split()
if int(ll[0]) == chrom:
vld_dict['SNP'].append(ll[1])
vld_dict['A1'].append(ll[4])
vld_dict['A2'].append(ll[5])
print('... %d SNPs on chromosome %d read from %s ...' % (len(vld_dict['SNP']), chrom, bim_file + '.bim'))
return vld_dict
def parse_sumstats(ref_dict, vld_dict, sst_file, n_subj):
print('... parse sumstats file: %s ...' % sst_file)
ATGC = ['A', 'T', 'G', 'C']
sst_dict = {'SNP':[], 'A1':[], 'A2':[]}
with open(sst_file) as ff:
header = next(ff)
for line in ff:
ll = (line.strip()).split()
if ll[1] in ATGC and ll[2] in ATGC:
sst_dict['SNP'].append(ll[0])
sst_dict['A1'].append(ll[1])
sst_dict['A2'].append(ll[2])
print('... %d SNPs read from %s ...' % (len(sst_dict['SNP']), sst_file))
mapping = {'A': 'T', 'T': 'A', 'C': 'G', 'G': 'C'}
vld_snp = set(zip(vld_dict['SNP'], vld_dict['A1'], vld_dict['A2']))
ref_snp = set(zip(ref_dict['SNP'], ref_dict['A1'], ref_dict['A2'])) | set(zip(ref_dict['SNP'], ref_dict['A2'], ref_dict['A1'])) | \
set(zip(ref_dict['SNP'], [mapping[aa] for aa in ref_dict['A1']], [mapping[aa] for aa in ref_dict['A2']])) | \
set(zip(ref_dict['SNP'], [mapping[aa] for aa in ref_dict['A2']], [mapping[aa] for aa in ref_dict['A1']]))
sst_snp = set(zip(sst_dict['SNP'], sst_dict['A1'], sst_dict['A2'])) | set(zip(sst_dict['SNP'], sst_dict['A2'], sst_dict['A1'])) | \
set(zip(sst_dict['SNP'], [mapping[aa] for aa in sst_dict['A1']], [mapping[aa] for aa in sst_dict['A2']])) | \
set(zip(sst_dict['SNP'], [mapping[aa] for aa in sst_dict['A2']], [mapping[aa] for aa in sst_dict['A1']]))
comm_snp = vld_snp & ref_snp & sst_snp
print('... %d common SNPs in the reference, sumstats, and validation set ...' % len(comm_snp))
n_sqrt = sp.sqrt(n_subj)
sst_eff = {}
with open(sst_file) as ff:
header = (next(ff).strip()).split()
header = [col.upper() for col in header]
for line in ff:
ll = (line.strip()).split()
snp = ll[0]; a1 = ll[1]; a2 = ll[2]
if a1 not in ATGC or a2 not in ATGC:
continue
if (snp, a1, a2) in comm_snp or (snp, mapping[a1], mapping[a2]) in comm_snp:
if 'BETA' in header:
beta = float(ll[3])
elif 'OR' in header:
beta = sp.log(float(ll[3]))
if 'SE' in header:
se = float(ll[4])
beta_std = beta/se/n_sqrt
elif 'P' in header:
p = max(float(ll[4]), 1e-323)
beta_std = sp.sign(beta)*abs(norm.ppf(p/2.0))/n_sqrt
sst_eff.update({snp: beta_std})
elif (snp, a2, a1) in comm_snp or (snp, mapping[a2], mapping[a1]) in comm_snp:
if 'BETA' in header:
beta = float(ll[3])
elif 'OR' in header:
beta = sp.log(float(ll[3]))
if 'SE' in header:
se = float(ll[4])
beta_std = -1*beta/se/n_sqrt
elif 'P' in header:
p = max(float(ll[4]), 1e-323)
beta_std = -1*sp.sign(beta)*abs(norm.ppf(p/2.0))/n_sqrt
sst_eff.update({snp: beta_std})
sst_dict = {'CHR':[], 'SNP':[], 'BP':[], 'A1':[], 'A2':[], 'MAF':[], 'BETA':[], 'FLP':[]}
for (ii, snp) in enumerate(ref_dict['SNP']):
if snp in sst_eff:
sst_dict['SNP'].append(snp)
sst_dict['CHR'].append(ref_dict['CHR'][ii])
sst_dict['BP'].append(ref_dict['BP'][ii])
sst_dict['BETA'].append(sst_eff[snp])
a1 = ref_dict['A1'][ii]; a2 = ref_dict['A2'][ii]
if (snp, a1, a2) in comm_snp:
sst_dict['A1'].append(a1)
sst_dict['A2'].append(a2)
sst_dict['MAF'].append(ref_dict['MAF'][ii])
sst_dict['FLP'].append(1)
elif (snp, a2, a1) in comm_snp:
sst_dict['A1'].append(a2)
sst_dict['A2'].append(a1)
sst_dict['MAF'].append(1-ref_dict['MAF'][ii])
sst_dict['FLP'].append(-1)
elif (snp, mapping[a1], mapping[a2]) in comm_snp:
sst_dict['A1'].append(mapping[a1])
sst_dict['A2'].append(mapping[a2])
sst_dict['MAF'].append(ref_dict['MAF'][ii])
sst_dict['FLP'].append(1)
elif (snp, mapping[a2], mapping[a1]) in comm_snp:
sst_dict['A1'].append(mapping[a2])
sst_dict['A2'].append(mapping[a1])
sst_dict['MAF'].append(1-ref_dict['MAF'][ii])
sst_dict['FLP'].append(-1)
return sst_dict
def parse_ldblk(ldblk_dir, sst_dict, chrom):
print('... parse reference LD on chromosome %d ...' % chrom)
if '1kg' in os.path.basename(ldblk_dir):
chr_name = ldblk_dir + '/ldblk_1kg_chr' + str(chrom) + '.hdf5'
elif 'ukbb' in os.path.basename(ldblk_dir):
chr_name = ldblk_dir + '/ldblk_ukbb_chr' + str(chrom) + '.hdf5'
hdf_chr = h5py.File(chr_name, 'r')
n_blk = len(hdf_chr)
ld_blk = [sp.array(hdf_chr['blk_'+str(blk)]['ldblk']) for blk in range(1,n_blk+1)]
snp_blk = []
for blk in range(1,n_blk+1):
snp_blk.append([bb.decode("UTF-8") for bb in list(hdf_chr['blk_'+str(blk)]['snplist'])])
blk_size = []
mm = 0
for blk in range(n_blk):
idx = [ii for (ii, snp) in enumerate(snp_blk[blk]) if snp in sst_dict['SNP']]
blk_size.append(len(idx))
if idx != []:
idx_blk = range(mm,mm+len(idx))
flip = [sst_dict['FLP'][jj] for jj in idx_blk]
ld_blk[blk] = ld_blk[blk][sp.ix_(idx,idx)]*sp.outer(flip,flip)
_, s, v = linalg.svd(ld_blk[blk])
h = sp.dot(v.T, sp.dot(sp.diag(s), v))
ld_blk[blk] = (ld_blk[blk]+h)/2
mm += len(idx)
else:
ld_blk[blk] = sp.array([])
return ld_blk, blk_size