-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdownstream_gwas.py
394 lines (341 loc) · 10.7 KB
/
downstream_gwas.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
import os
import subprocess
from os.path import join
import numpy as np
import pandas as pd
import toml
import torch
from qmplot import manhattanplot
from scipy import stats
from sklearn.decomposition import PCA
from sklearn.linear_model import LinearRegression
from torch.utils.data import DataLoader
from tqdm import tqdm
from data.data_ukb import get_tfms, get_indiv_split, UKBRetina
from models.model_loading import GeneralModel
config = toml.load("paths.toml")
# need both: plink1's association computations are super slow; plink2 doesn't implement clumping
PLINK1 = config["PLINK1"]
PLINK2 = config["PLINK2"]
PRJ = config["CHECKPOINTS_BASE_PATH"]
BFILE = join(config["BASE_GEN"], "ukb_chr{chromo}_v2")
TEMPL = "gwas_results_chr{chromo}.PC{pc}.glm.linear"
# significance thresholds for clumping
P1 = 5e-8
P2 = 1e-7
SIZE = 448
COVARIATES = ["sex", "age"] + [f"genet_pc_{i}" for i in range(1, 16)]
WEIGHT_PATHS = {
# baselines
"barlow": join(PRJ, "barlow_r50_proj128/epoch_99-step_170399.ckpt"),
"byol": join(PRJ, "byol_r50_proj128/epoch_99-step_170399.ckpt"),
"nnclr": join(PRJ, "nnclr_r50_proj128/epoch_99-step_170399.ckpt"),
"simclr": join(PRJ, "simclr_r50_proj128/epoch_99-step_170399.ckpt"),
"simsiam": join(PRJ, "simsiam_r50_proj128/epoch_99-step_170399.ckpt"),
# ContIG
"rpb": join(PRJ, "cm_r50_raw_risks_burdens_outer_h1/checkpoints/last.ckpt"),
"rpb-inner": join(PRJ, "cm_r50_raw_risks_burdens_inner_h1/last.ckpt"),
"gen": join(PRJ, "cm_r50_raw_snps_h1/last.ckpt"),
"pgs": join(PRJ, "cm_r50_risk_scores_gen_h1/last.ckpt"),
"burdens": join(PRJ, "cm_r50_burden_scores_gen_h1/last.ckpt"),
}
def run_all_gwas(
split="test",
dev="cuda:0",
bs=10,
threads=20,
main_dir="gwas_results",
use_INT=True,
subset=None,
):
for key in WEIGHT_PATHS:
print("starting model", key)
run_transfer_gwas(
out_dir=join(main_dir, key),
weights_path=WEIGHT_PATHS[key],
split=split,
subset=subset,
dev=dev,
bs=bs,
threads=threads,
use_INT=use_INT,
)
def compare_models(main_dir="gwas_results"):
results = dict()
for key in WEIGHT_PATHS:
fn = join(main_dir, key, "final_clumps.csv")
if os.path.isfile(fn):
if os.path.getsize(fn) > 1:
res = pd.read_csv(fn)
results[key] = len(res)
else:
results[key] = 0
return results
def run_transfer_gwas(
out_dir="gwas_results",
weights_path=WEIGHT_PATHS["rpb"],
pheno_fn="transfer_embeddings.txt",
cov_fn="transfer_cov.txt",
size=SIZE,
split="valid",
comp=10,
dev="cuda:0",
threads=20,
seed=42,
use_INT=True,
bs=10,
subset=None,
):
os.makedirs(out_dir, exist_ok=True)
pheno_fn = join(out_dir, pheno_fn)
cov_fn = join(out_dir, cov_fn)
print("loading model & data...")
model = GeneralModel(checkpoint_path=weights_path, device=dev).eval()
tl, vl, ttl = get_gwas_data(
size=size,
batch_size=bs,
return_iid=True,
normalize_features=False,
seed=seed,
subset=subset,
)
loader = {"train": tl, "valid": vl, "test": ttl}[split]
print(f"computing {split} embeddings")
export_embeddings(
loader,
model,
pheno_fn=pheno_fn,
cov_fn=cov_fn,
comp=comp,
dev=dev,
use_INT=use_INT,
)
print(f"running GWAS")
run_plink(
pheno_fn=pheno_fn,
covar_fn=cov_fn,
out=join(out_dir, "gwas_results_chr{chromo}"),
threads=threads,
)
plot_gwas(
out_fn=join(out_dir, "mhat_plot"),
templ=join(out_dir, "gwas_results_chr{chromo}.PC{pc}.glm.linear"),
clip_min=1e-99,
)
clump_results(direc=out_dir)
def plot_gwas(
out_fn=join("gwas_results", "mhat_plot"),
templ=join("gwas_results", "gwas_results_chr{chromo}.PC{pc}.glm.linear"),
clip_min=1e-99,
):
df = get_plink_results(templ=templ)
df.P = df.P.clip(clip_min, 1)
manhattanplot(
data=df.dropna(how="any", axis=0),
figname=out_fn + ".png",
xticklabel_kws={"rotation": "vertical"},
)
manhattanplot(
data=df.dropna(how="any", axis=0),
figname=out_fn + ".pdf",
xticklabel_kws={"rotation": "vertical"},
)
def clump_results(
direc,
p1=P1,
p2=P2,
r2=0.1,
kb=150,
threads=20,
):
merged_fn = join(direc, "gwas_merged.txt")
merge_plink(
templ=join(direc, TEMPL),
out_fn=merged_fn,
)
for chromo in range(1, 23):
bfile = BFILE.format(chromo=chromo)
out_fn = join(direc, f"clumped_{chromo}")
cmd = f"{PLINK1} --bfile {bfile} --clump {merged_fn} --clump-p1 {p1} --clump-p2 {p2} --clump-r2 {r2} --clump-kb {kb} --threads {threads} --out {out_fn}"
process = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE)
output, error = process.communicate()
print("errors:", error, flush=True)
print("output:", output, flush=True)
results = read_clumps(direc=direc)
results.to_csv(join(direc, "final_clumps.csv"), index=False)
n_clumps = len(results)
return results, n_clumps
def read_clumps(direc):
full_df = None
for chromo in range(1, 23):
clump_fn = join(direc, f"clumped_{chromo}.clumped")
if os.path.isfile(clump_fn):
print(f"reading file {clump_fn}")
df = pd.read_csv(join(direc, f"clumped_{chromo}.clumped"), sep="\s+")
if full_df is None:
full_df = df
else:
full_df = pd.concat([full_df, df])
if full_df is None:
full_df = pd.DataFrame()
return full_df
def merge_plink(
out_fn=join("gwas_results", "gwas_merged.txt"), templ=join("gwas_results", TEMPL)
):
df = get_plink_results(templ=templ)
df["SNP"] = [line["ID"] for _, line in df.iterrows()]
df[["SNP", "P"]].to_csv(out_fn, header=True, index=False, sep=" ")
def get_plink_results(templ):
cols = ["#CHROM", "POS", "ID"]
for chromo in tqdm(range(1, 23)):
for pc in range(10):
sub_df = pd.read_csv(templ.format(chromo=chromo, pc=pc), sep="\t")
if pc == 0:
df = sub_df[cols]
df[f"P_PC{pc}"] = sub_df.P
if chromo == 1:
full_df = df
else:
full_df = pd.concat([full_df, df])
full_df["P"] = (10 * full_df.loc[:, [f"P_PC{i}" for i in range(10)]].min(1)).clip(
1e-320, 1
)
return full_df
def run_plink(
pheno_fn,
covar_fn,
out="gwas_results_chr{chromo}",
threads=20,
):
for chromo in range(1, 23):
bfile = BFILE.format(chromo=chromo)
out_fn = out.format(chromo=chromo)
print(f"running GWAS on chromo {chromo}", flush=True)
cmd = f"{PLINK2} --bfile {bfile} --linear hide-covar --covar {covar_fn} --pheno {pheno_fn} --threads {threads} --allow-no-sex --out {out_fn}"
process = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE)
output, error = process.communicate()
print("errors:", error, flush=True)
print("output:", output, flush=True)
@torch.no_grad()
def export_embeddings(
loader,
model, # ModelCLR
pheno_fn="tmp.txt",
cov_fn="cov.txt",
comp=10,
dev="cuda:0",
use_INT=False,
):
feats = []
covs = []
cov_inds = [loader.dataset.cov_columns.index(trait) for trait in COVARIATES]
iids = []
for imgs, cov, iid in tqdm(loader):
batch_embedding = model(imgs.to(dev)).cpu()
feats.append(batch_embedding)
covs.append(cov[:, cov_inds])
iids.append(iid)
covs = torch.cat(covs).double().numpy()
covs[:, COVARIATES.index("sex")] += 1
feats = torch.cat(feats).double().numpy()
iids = torch.cat(iids).numpy()
pca = PCA(n_components=comp)
feats = pca.fit_transform(feats)
cov = pd.DataFrame(
{
"FID": iids,
"IID": iids,
**dict((covariate, covs[:, i]) for i, covariate in enumerate(COVARIATES)),
}
)
cov.sex = cov.sex.astype(int)
cov.to_csv(cov_fn, header=True, index=False, sep="\t")
df = pd.DataFrame(
{
"FID": iids,
"IID": iids,
**dict((f"PC{i}", feats[:, i]) for i in range(comp)),
}
)
if use_INT:
df = inverse_rank_transform(df, cov, method="adjusted")
df.to_csv(pheno_fn, header=True, index=False, sep="\t")
return feats, iids
def get_gwas_data(
seed=42,
num_workers=8,
size=256,
normalize_features=True,
batch_size=50,
train_pct=0.7,
val_pct=0.2,
cov_fillna="mean",
return_iid=False,
eye="left",
subset=None,
):
t_iids, v_iids, tt_iids = get_indiv_split(
train_pct=train_pct, val_pct=val_pct, seed=seed
)
loaders = []
tfms = get_tfms(size=size, augmentation=False)
for iids, mode in [(t_iids, "train"), (v_iids, "valid"), (tt_iids, "test")]:
dset = UKBRetina(
eye=eye,
iid_selection=iids,
tfms=tfms,
normalize_features=normalize_features,
cov_fillna=cov_fillna,
return_iid=return_iid,
subset=subset,
)
dset = prune_iids(dset)
loader = DataLoader(
dset,
batch_size=batch_size,
shuffle=False,
num_workers=num_workers,
pin_memory=True,
)
loaders.append(loader)
return loaders
def prune_iids(dset):
"""make sure each iid only occurs once"""
unique_iids = set(dset.iids)
used_iids = set()
paths = []
iids = []
for iid, path in tqdm(zip(dset.iids, dset.paths)):
if not iid in used_iids:
paths.append(path)
iids.append(iid)
used_iids.add(iid)
dset.iids = iids
dset.paths = paths
return dset
def inverse_rank_transform(df, cov, covars=None, qcovars=None, method="adjusted"):
pcs = range(df.shape[1] - 2)
if method == "adjusted":
cov.index = cov.IID
cov = cov.loc[df.IID]
cov = cov.drop(["IID", "FID"], 1)
df.index = df.IID
ind = np.intersect1d(cov.index, df.index)
cov = cov.loc[ind]
df = df.loc[ind]
df_adj = df.copy()
for pc in tqdm(pcs):
col = f"PC{pc}"
lr = LinearRegression()
df_adj[col] = df[col] - lr.fit(cov, df[col]).predict(cov)
df = df_adj
for pc in tqdm(pcs):
col = f"PC{pc}"
df[col] = INT(df[col])
return df
def INT(x, method="average", c=3.0 / 8):
"""perform rank-based inverse normal transform"""
r = stats.rankdata(x, method=method)
x = (r - c) / (len(x) - 2 * c + 1)
norm = stats.norm.ppf(x)
return norm