-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLDpred2-auto.R
More file actions
516 lines (411 loc) · 14.5 KB
/
LDpred2-auto.R
File metadata and controls
516 lines (411 loc) · 14.5 KB
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
args <- commandArgs(trailingOnly = TRUE)
print(args)
# Argument Descriptions
#1. Argument one is the directory. Example: `SampleData1`
#2. Argument two is the file name. Example: `SampleData1\\Fold_0`
#3. Argument three is the output file name. Example: `train_data`
#4. Argument four is the specific function to be called. Example: `train_data.QC.clumped.pruned`
#5. Argument five is LDpred-2 option. Example: `LDpred-2_full` or `LDpred-2_hapmap`
#6. Argument six is the size parameter. Example: `200`
#7. Argument seven is the alpha parameter. Example: `1`
#8. Argument eight is the thr_r2 parameter. Example: `0.1`
#9. Argument nine is burn_in. Example: `100`
#10 Argument ten is num_iter. Example: `10`
#11 Argument 11 is shrink_corr = 0.7`
#12 Argument 12 is use_MLE = c(FALSE, TRUE)`
#13 Argument 13 is sparse = c(FALSE, TRUE)`
#14 Argument 14 is allow_jump_sign = c(FALSE, TRUE)`
if (args[5]=="1"){
cran_mirror_url <- "https://cran.r-project.org"
install.packages("remotes", repos = cran_mirror_url)
library(remotes)
#remotes::install_github("https://github.com/privefl/bigsnpr.git")
library(bigsnpr)
options(bigstatsr.check.parallel.blas = FALSE)
options(default.nproc.blas = NULL)
library(data.table)
library(magrittr)
info <- readRDS(runonce::download_file(
"https://ndownloader.figshare.com/files/25503788",
fname = "map_hm3_ldpred2.rds"))
library(bigsnpr)
options(bigstatsr.check.parallel.blas = FALSE)
options(default.nproc.blas = NULL)
library(data.table)
library(magrittr)
help(snp_cor)
}
if (args[5]=="2"){
library(bigsnpr)
options(bigstatsr.check.parallel.blas = FALSE)
options(default.nproc.blas = NULL)
library(data.table)
library(magrittr)
result <-paste(".",args[2],paste(args[3],toString(".PHENO"), sep = ""),sep="//")
phenotype <- fread(result)
result <-paste(".",args[2],paste(args[3],toString(".cov"), sep = ""),sep="//")
covariate <- fread(result)
result <-paste(".",args[2],paste(args[3],toString(".eigenvec"), sep = ""),sep="//")
pcs <- fread(result)
# rename columns
colnames(pcs) <- c("FID","IID", paste0("PC",1:as.numeric(args[9])))
# generate required table
pheno <- merge(phenotype, covariate) %>%
merge(., pcs)
info <- readRDS(runonce::download_file(
"https://ndownloader.figshare.com/files/25503788",
fname = "map_hm3_ldpred2.rds"))
# Read in the summary statistic file
result <-paste(".",args[1],paste(args[1],toString(".txt"), sep = ""),sep="//")
sumstats <- bigreadr::fread2(result)
# LDpred 2 require the header to follow the exact naming
names(sumstats) <-
c("chr",
"pos",
"rsid",
"a1",
"a0",
"n_eff",
"beta_se",
"p",
"BETA",
"INFO",
"MAF")
# Transform the OR into log(OR)
sumstats$beta <- sumstats$BETA
# Filter out hapmap SNPs
sumstats <- sumstats[sumstats$rsid%in% info$rsid,]
# Get maximum amount of cores
NCORES <- nb_cores()
# Open a temporary file
result <-paste(".",args[2],"tmp-data",sep="//")
if (dir.exists(result)) {
# Delete the directory and its contents
system(paste("rm -r", shQuote(result)))
print(paste("Directory", result, "deleted."))
}
tmp <- tempfile(tmpdir = result)
on.exit(file.remove(paste0(tmp, ".sbk")), add = TRUE)
corr <- NULL
ld <- NULL
# We want to know the ordering of samples in the bed file
fam.order <- NULL
# preprocess the bed file (only need to do once for each data set)
result <-paste(".",args[2],paste(args[4],toString(".rds"), sep = ""),sep="//")
if (file.exists(result)) {
file.remove(result)
print(paste("File", result, "deleted."))
}
result <-paste(".",args[2],paste(args[4],toString(".bk"), sep = ""),sep="//")
if (file.exists(result)) {
file.remove(result)
print(paste("File", result, "deleted."))
}
result <-paste(".",args[2],paste(args[4],toString(".bed"), sep = ""),sep="//")
snp_readBed(result)
# now attach the genotype object
result <-paste(".",args[2],paste(args[4],toString(".rds"), sep = ""),sep="//")
obj.bigSNP <- snp_attach(result)
# extract the SNP information from the genotype
map <- obj.bigSNP$map[-3]
names(map) <- c("chr", "rsid", "pos", "a1", "a0")
# perform SNP matching
info_snp <- snp_match(sumstats, map)
help(snp_match)
info_snp
# Assign the genotype to a variable for easier downstream analysis
genotype <- obj.bigSNP$genotypes
# Rename the data structures
CHR <- map$chr
POS <- map$pos
# get the CM information from 1000 Genome
# will download the 1000G file to the current directory (".")
#help(snp_asGeneticPos)
POS2 <- snp_asGeneticPos(CHR, POS, dir = ".")
check <-TRUE
for (chr in 1:22) {
# Extract SNPs that are included in the chromosome
ind.chr <- which(info_snp$chr == chr)
print(length(ind.chr))
ind.chr2 <- info_snp$`_NUM_ID_`[ind.chr]
ind.chr2
if (length(ind.chr2) == 0) {
next
}
else{
corr0 <- snp_cor(
genotype,
ind.col = ind.chr2,
ncores = NCORES,
infos.pos = POS2[ind.chr2],
#size = 200,
#thr_r2=0.1,
#alpha = 1
size = as.numeric(args[6]),
alpha = as.numeric(args[7]),
thr_r2=as.numeric(args[8]),
)
if (check==TRUE) {
check <-FALSE
ld <- Matrix::colSums(corr0^2)
corr <- as_SFBM(corr0, tmp)
} else {
ld <- c(ld, Matrix::colSums(corr0^2))
corr$add_columns(corr0, nrow(corr))
}
}
}
# We assume the fam order is the same across different chromosomes
fam.order <- as.data.table(obj.bigSNP$fam)
# Rename fam order
setnames(fam.order,
c("family.ID", "sample.ID"),
c("FID", "IID"))
df_beta <- info_snp[,c("beta", "beta_se", "n_eff", "_NUM_ID_")]
length(df_beta$beta)
length(ld)
help(snp_ldsc)
ldsc <- snp_ldsc(ld,
length(ld),
chi2 = (df_beta$beta / df_beta$beta_se)^2,
sample_size = df_beta$n_eff,
blocks = NULL)
h2_est <- ldsc[["h2"]]
h2_est
result <-paste(".",args[2],"ldpred_h2_hapmap.txt",sep="//")
if (file.exists(result)) {
file.remove(result)
print(paste("File", result, "deleted."))
}
write.table(h2_est, file = result, col.names = FALSE)
result <-paste(".",args[2],"ldpred_h2_variants.txt",sep="//")
if (file.exists(result)) {
file.remove(result)
print(paste("File", result, "deleted."))
}
write.table(length(ld), file = result, col.names = FALSE)
help(snp_ldsc)
# Here we have to specify the p and h2_seq
multi_auto <- snp_ldpred2_auto(
corr,
df_beta,
h2_init = h2_est,
vec_p_init = seq_log(1e-4, 1, length.out = 10),
burn_in = as.numeric(args[9]),
num_iter = as.numeric(args[10]),
sparse = tolower(args[11]) == "true",
allow_jump_sign = tolower(args[12]) == "true",
#shrink_corr = as.numeric(args[13]),
use_MLE = tolower(args[14]) == "true",
# Here we have specify the alpha bounds.
alpha_bounds = c(-1.5, 0.5),
ind.corr = cols_along(corr)
)
print("Sex")
beta_auto <- sapply(multi_auto, function(auto) auto$beta_est)
beta_auto
print("Sex")
p_seq <- signif(seq_log(1e-4, 1, length.out = 10), 2)
grid.param <-
expand.grid(p = p_seq
)
# Save the grid paramters.
gridparamters <- grid.param[,c("p" )]
result <-paste(".",args[2],paste(args[3],toString(".ldpred_auto_parameters"), sep = ""),sep="//")
write.table(gridparamters, file = result, row.names = FALSE,sep=",", quote = FALSE)
result <-paste(".",args[2],paste(args[3],toString(".ldpred_auto_betas"), sep = ""),sep="//")
write.table(beta_auto, file = result, row.names = FALSE,sep=",", quote = FALSE)
newgwas <- info_snp[,c("rsid.ss", "a0", "beta")]
result <-paste(".",args[2],paste(args[3],toString(".ldpred_auto_gwas"), sep = ""),sep="//")
write.table(newgwas, file = result, row.names = FALSE,sep=",", quote = FALSE)
print("Sex")
}
if (args[5]=="3"){
library(bigsnpr)
options(bigstatsr.check.parallel.blas = FALSE)
options(default.nproc.blas = NULL)
library(data.table)
library(magrittr)
result <-paste(".",args[2],paste(args[3],toString(".PHENO"), sep = ""),sep="//")
phenotype <- fread(result)
result <-paste(".",args[2],paste(args[3],toString(".cov"), sep = ""),sep="//")
covariate <- fread(result)
result <-paste(".",args[2],paste(args[3],toString(".eigenvec"), sep = ""),sep="//")
pcs <- fread(result)
# rename columns
colnames(pcs) <- c("FID","IID", paste0("PC",1:as.numeric(args[9])))
# generate required table
pheno <- merge(phenotype, covariate) %>%
merge(., pcs)
info <- readRDS(runonce::download_file(
"https://ndownloader.figshare.com/files/25503788",
fname = "map_hm3_ldpred2.rds"))
# Read in the summary statistic file
result <-paste(".",args[1],paste(args[1],toString(".txt"), sep = ""),sep="//")
sumstats <- bigreadr::fread2(result)
# LDpred 2 require the header to follow the exact naming
names(sumstats) <-
c("chr",
"pos",
"rsid",
"a1",
"a0",
"n_eff",
"beta_se",
"p",
"BETA",
"INFO",
"MAF")
# Transform the OR into log(OR)
sumstats$beta <- sumstats$BETA
# Filter out hapmap SNPs
# Turn off this line to ensure that all the SNPs from
# the sumstats are included.
#sumstats <- sumstats[sumstats$rsid%in% info$rsid,]
# Get maximum amount of cores
NCORES <- nb_cores()
# Open a temporary file
result <-paste(".",args[2],"tmp-data",sep="//")
if (dir.exists(result)) {
# Delete the directory and its contents
system(paste("rm -r", shQuote(result)))
print(paste("Directory", result, "deleted."))
}
tmp <- tempfile(tmpdir = result)
on.exit(file.remove(paste0(tmp, ".sbk")), add = TRUE)
# Initialize variables for storing the LD score and LD matrix
corr <- NULL
ld <- NULL
# We want to know the ordering of samples in the bed file
fam.order <- NULL
# preprocess the bed file (only need to do once for each data set)
result <-paste(".",args[2],paste(args[4],toString(".rds"), sep = ""),sep="//")
if (file.exists(result)) {
file.remove(result)
print(paste("File", result, "deleted."))
}
result <-paste(".",args[2],paste(args[4],toString(".bk"), sep = ""),sep="//")
if (file.exists(result)) {
file.remove(result)
print(paste("File", result, "deleted."))
}
result <-paste(".",args[2],paste(args[4],toString(".bed"), sep = ""),sep="//")
snp_readBed(result)
# now attach the genotype object
result <-paste(".",args[2],paste(args[4],toString(".rds"), sep = ""),sep="//")
obj.bigSNP <- snp_attach(result)
# extract the SNP information from the genotype
map <- obj.bigSNP$map[-3]
names(map) <- c("chr", "rsid", "pos", "a1", "a0")
# perform SNP matching
info_snp <- snp_match(sumstats, map)
help(snp_match)
info_snp
# Assign the genotype to a variable for easier downstream analysis
genotype <- obj.bigSNP$genotypes
# Rename the data structures
CHR <- map$chr
POS <- map$pos
# get the CM information from 1000 Genome
# will download the 1000G file to the current directory (".")
help(snp_asGeneticPos)
POS2 <- snp_asGeneticPos(CHR, POS, dir = ".")
check <-TRUE
for (chr in 1:22) {
# Extract SNPs that are included in the chromosome
ind.chr <- which(info_snp$chr == chr)
print(length(ind.chr))
ind.chr2 <- info_snp$`_NUM_ID_`[ind.chr]
print(length(ind.chr2))
if (length(ind.chr2) == 0) {
next
}
else{
corr0 <- snp_cor(
genotype,
ind.col = ind.chr,
ncores = NCORES,
infos.pos = POS2[ind.chr2],
#size = 200,
#thr_r2=0.1,
#alpha = 1
size = as.numeric(args[6]),
alpha = as.numeric(args[7]),
thr_r2=as.numeric(args[8]),
)
if (check==TRUE) {
check <-FALSE
ld <- Matrix::colSums(corr0^2)
help(as_SFBM)
corr <- as_SFBM(corr0, tmp)
} else {
ld <- c(ld, Matrix::colSums(corr0^2))
corr$add_columns(corr0, nrow(corr))
}
}
}
# We assume the fam order is the same across different chromosomes
fam.order <- as.data.table(obj.bigSNP$fam)
# Rename fam order
setnames(fam.order,
c("family.ID", "sample.ID"),
c("FID", "IID"))
df_beta <- info_snp[,c("beta", "beta_se", "n_eff", "_NUM_ID_")]
length(df_beta$beta)
length(ld)
help(snp_ldsc)
ldsc <- snp_ldsc(ld,
length(ld),
chi2 = (df_beta$beta / df_beta$beta_se)^2,
sample_size = df_beta$n_eff,
blocks = NULL)
h2_est <- ldsc[["h2"]]
# Here we have to specify the p and h2_seq
# Here we have to specify the p and h2_seq
multi_auto <- snp_ldpred2_auto(
corr,
df_beta,
h2_init = h2_est,
vec_p_init = seq_log(1e-4, 1, length.out = 10),
burn_in = as.numeric(args[9]),
num_iter = as.numeric(args[10]),
sparse = tolower(args[11]) == "true",
allow_jump_sign = tolower(args[12]) == "true",
#shrink_corr = as.numeric(args[13]),
use_MLE = tolower(args[14]) == "true",
# Here we have specify the alpha bounds.
alpha_bounds = c(-1.5, 0.5),
ind.corr = cols_along(corr)
)
print("Sex")
beta_auto <- sapply(multi_auto, function(auto) auto$beta_est)
beta_auto
print("Sex")
p_seq <- signif(seq_log(1e-4, 1, length.out = 10), 2)
grid.param <-
expand.grid(p = p_seq
)
# Save the grid paramters.
gridparamters <- grid.param[,c("p" )]
result <-paste(".",args[2],paste(args[3],toString(".ldpred_auto_parameters"), sep = ""),sep="//")
write.table(gridparamters, file = result, row.names = FALSE,sep=",", quote = FALSE)
result <-paste(".",args[2],paste(args[3],toString(".ldpred_auto_betas"), sep = ""),sep="//")
write.table(beta_auto, file = result, row.names = FALSE,sep=",", quote = FALSE)
newgwas <- info_snp[,c("rsid.ss", "a0", "beta")]
result <-paste(".",args[2],paste(args[3],toString(".ldpred_auto_gwas"), sep = ""),sep="//")
write.table(newgwas, file = result, row.names = FALSE,sep=",", quote = FALSE)
result <-paste(".",args[2],"ldpred_h2_full.txt",sep="//")
if (file.exists(result)) {
file.remove(result)
print(paste("File", result, "deleted."))
}
write.table(h2_est, file = result, col.names = FALSE)
result <-paste(".",args[2],"ldpred_h2_variants.txt",sep="//")
if (file.exists(result)) {
file.remove(result)
print(paste("File", result, "deleted."))
}
write.table(length(ld), file = result, col.names = FALSE)
#print(ldsc)
#exit(0)
}