-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest_analysis.R
executable file
·314 lines (247 loc) · 11.5 KB
/
test_analysis.R
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
module load fusion_twas/github
## Using summary stats from http://walters.psycm.cf.ac.uk/
## Choose chr
chr="3"
mkdir -p test_psycm
## gotta debug chr 3
for chr in {1..22}
do
echo "*************************"
echo ""
echo "processing chromosome ${chr}"
date
echo ""
## Create summarized analysis
Rscript /jhpce/shared/jhpce/libd/fusion_twas/github/fusion_twas/FUSION.assoc_test.R \
--sumstats /dcl01/lieber/ajaffe/lab/brainseq_phase2/twas/psycm/clozuk_pgc2.meta.reformatted.sumstats_hg38_ourname \
--weights /dcl01/lieber/ajaffe/lab/brainseq_phase2/twas/HIPPO/gene/HIPPO_gene.pos \
--weights_dir /dcl01/lieber/ajaffe/lab/brainseq_phase2/twas/HIPPO/gene \
--ref_ld_chr /dcl01/lieber/ajaffe/lab/brainseq_phase2/twas/reference_hg38/LDREF_hg38/1000G.EUR. \
--chr ${chr} \
--out test_psycm/test_psycm.${chr}.dat
echo ""
echo "making plots for chromosome ${chr}"
date
echo ""
## companion plotting step
Rscript /jhpce/shared/jhpce/libd/fusion_twas/github/fusion_twas/FUSION.post_process.R \
--sumstats /dcl01/lieber/ajaffe/lab/brainseq_phase2/twas/psycm/clozuk_pgc2.meta.reformatted.sumstats_hg38_ourname \
--input test_psycm/test_psycm.${chr}.dat \
--out test_psycm/test_psycm.${chr}.analysis \
--ref_ld_chr /dcl01/lieber/ajaffe/lab/brainseq_phase2/twas/reference_hg38/LDREF_hg38/1000G.EUR. \
--chr ${chr} \
--plot --locus_win 100000 --verbose 2 --plot_individual --plot_eqtl --plot_corr
done
## Choose chr
chr="21"
for chr in {1..22}
do
echo "*************************"
echo ""
echo "processing chromosome ${chr}"
date
echo ""
## Create summarized analysis
Rscript /jhpce/shared/jhpce/libd/fusion_twas/github/fusion_twas/FUSION.assoc_test.R \
--sumstats /dcl01/lieber/ajaffe/lab/brainseq_phase2/twas/pgc_scz2_sumstats/PGC2.SCZ.sumstats_hg38_ourname \
--weights /dcl01/lieber/ajaffe/lab/brainseq_phase2/twas/HIPPO/gene/HIPPO_gene.pos \
--weights_dir /dcl01/lieber/ajaffe/lab/brainseq_phase2/twas/HIPPO/gene \
--ref_ld_chr /dcl01/lieber/ajaffe/lab/brainseq_phase2/twas/reference_hg38/LDREF_hg38/1000G.EUR. \
--chr ${chr} \
--out test_PGC2.SCZ.${chr}.dat
echo ""
echo "making plots for chromosome ${chr}"
date
echo ""
## companion plotting step
Rscript /jhpce/shared/jhpce/libd/fusion_twas/github/fusion_twas/FUSION.post_process.R \
--sumstats /dcl01/lieber/ajaffe/lab/brainseq_phase2/twas/pgc_scz2_sumstats/PGC2.SCZ.sumstats_hg38_ourname \
--input test_PGC2.SCZ.${chr}.dat \
--out test_PGC2.SCZ.${chr}.analysis \
--ref_ld_chr /dcl01/lieber/ajaffe/lab/brainseq_phase2/twas/reference_hg38/LDREF_hg38/1000G.EUR. \
--chr ${chr} \
--plot --locus_win 100000 --verbose 2 --plot_individual --plot_eqtl --plot_corr
done
cd test_psycm
R
library('readr')
# start_patt <- 'test_PGC2.SCZ.'
start_patt <- 'test_psycm.'
dir_path <- gsub('\\.$', '', start_patt)
## Weird issue with chr 6
info_all <- lapply(c(1:5, 7:22), function(chr) {
message(paste(Sys.time(), 'reading chromosome', chr))
patt <- paste0(start_patt, chr, '\\..*dat')
files <- dir(dir_path, pattern = patt, full.names = TRUE)
if(length(files) == 0) {
warning(paste('no files for chromosome', chr))
return(NULL)
}
if(length(files) < 3) {
warning(paste('not all files for chromosome', chr, 'are present'))
return(NULL)
}
info <- lapply(files, read_tsv)
names(info) <- gsub(paste0(start_patt, chr, '.|analysis.'), '', dir(dir_path, pattern = patt))
# table(is.na(info[['dat']]$EQTL.ID))
# sapply(info, dim)
# chr <- 5
# ids <- unlist(sapply(info[grepl('joint', names(info))], function(x) x$ID))
# non_na_id <- info[['dat']]$ID[!is.na(info[['dat']]$EQTL.ID)]
# s(subset(info[['dat']], ID %in% non_na_id[which(!non_na_id %in% ids)]))
# non_na_id[133]
stopifnot(sum(sapply(info[grepl('joint', names(info))], nrow)) == sum(!is.na(info[['dat']]$EQTL.ID)) - sum(info[['dat']]$TWAS.P == 1, na.rm = TRUE))
return(info)
})
names(info_all) <- c(1:5, 7:22)
info_all <- info_all[!sapply(info_all, is.null)]
library('purrr')
info_all <- purrr::map(purrr::transpose(info_all), function(x) do.call(rbind, x))
## Missing chrs:
which(!1:22 %in% unique(info_all[['dat']]$CHR))
# [1] 6 18
library('GenomicRanges')
load('/dcl01/lieber/ajaffe/lab/brainseq_phase2/eQTL_GWAS_riskSNPs/eqtl_tables/mergedEqtl_output_hippo_raggr_4features.rda', verbose = TRUE)
dim(allEqtl)
summary(allEqtl$FDR)
# 0.0000 0.6892 0.8731 0.7734 0.9528 1.0000
eGene <- subset(allEqtl, Type == 'Gene' & FDR < 0.01)
length(unique(eGene$gene))
# [1] 123
length(unique(allEqtl$gene[allEqtl$Type == 'Gene']))
# [1] 1678
table(info_all[['joint_dropped.dat']]$ID %in% unique(eGene$gene))
# FALSE TRUE
# 3589 52
table(info_all[['joint_included.dat']]$ID %in% unique(eGene$gene))
# FALSE TRUE
# 59 18
table(info_all[['dat']]$ID %in% unique(eGene$gene))
# FALSE TRUE
# 5317 100
table(info_all[['joint_dropped.dat']]$ID %in% unique(allEqtl$gene[allEqtl$Type == 'Gene']))
# FALSE TRUE
# 3430 211
table(info_all[['joint_included.dat']]$ID %in% unique(allEqtl$gene[allEqtl$Type == 'Gene']))
# FALSE TRUE
# 51 26
table(info_all[['dat']]$ID %in% unique(allEqtl$gene))
# FALSE TRUE
# 5070 347
addmargins(table(
'in eGene' = info_all[['joint_included.dat']]$ID %in% unique(eGene$gene),
'in tested gene set' = info_all[['joint_included.dat']]$ID %in% unique(allEqtl$gene[allEqtl$Type == 'Gene'])
))
# in tested gene set
# in eGene FALSE TRUE Sum
# FALSE 51 8 59
# TRUE 0 18 18
# Sum 51 26 77
# > 18/26
# [1] 0.6923077
#info_all[['joint_included.dat']]$ID[which(!info_all[['joint_included.dat']]$ID %in% unique(allEqtl$gene))]
summary(info_all[['joint_included.dat']])
summary(subset(info_all[['joint_included.dat']], ID %in% unique(eGene$gene)))
x <- subset(info_all[['joint_included.dat']], ID %in% unique(eGene$gene))
x
y <- subset(info_all[['dat']], ID %in% x$ID)
as.data.frame(y)
as.data.frame(y)[which.min(y$TWAS.P), ]
## Original goal
Rscript /jhpce/shared/jhpce/libd/fusion_twas/github/fusion_twas/FUSION.assoc_test.R \
--sumstats /dcl01/lieber/ajaffe/lab/brainseq_phase2/twas/pgc_scz2_sumstats/PGC2.SCZ.sumstats \
--weights /dcl01/lieber/ajaffe/lab/brainseq_phase2/twas/HIPPO/gene/HIPPO_gene.pos \
--weights_dir /dcl01/lieber/ajaffe/lab/brainseq_phase2/twas/HIPPO/gene \
--ref_ld_chr /dcl01/lieber/ajaffe/lab/brainseq_phase2/twas/reference_hg38/LDREF_hg38/1000G.EUR. \
--chr 22 \
--out test_PGC2.SCZ.22.dat
## Removed similar warnings for all the other chr22 genes...
# WARNING : out_files/gene_9997.wgt.RDat ENSG00000272940.1 22 50597152 50597599 had 0 overlapping SNPs, but none with non-zero expression weights, try more SNPS or a different model
# WARNING : out_files/gene_9997.wgt.RDat ENSG00000272940.1 22 50597152 50597599 had no overlapping SNPs
# Analysis completed.
# NOTE: 146 / 146 genes were skipped
# If a large number of genes were skipped, verify that your GWAS Z-scores, expression weights, and LDREF data use the same SNPs (or nearly)
# Or consider pre-imputing your summary statistics to the LDREF markers using summary-imputation software such as [http://bogdan.bioinformatics.ucla.edu/software/impg/]
## companion plotting step (doesn't work for another reason)
Rscript /jhpce/shared/jhpce/libd/fusion_twas/github/fusion_twas/FUSION.post_process.R \
--sumstats /dcl01/lieber/ajaffe/lab/brainseq_phase2/twas/pgc_scz2_sumstats/PGC2.SCZ.sumstats \
--input test_PGC2.SCZ.22.dat \
--out test_PGC2.SCZ.22.analysis \
--ref_ld_chr /dcl01/lieber/ajaffe/lab/brainseq_phase2/twas/reference_hg38/LDREF_hg38/1000G.EUR. \
--chr 22 \
--plot --locus_win 100000
## Use filtered PGC2 sumstats to those snps present in the ported hg38 reference
Rscript /jhpce/shared/jhpce/libd/fusion_twas/github/fusion_twas/FUSION.assoc_test.R \
--sumstats /dcl01/lieber/ajaffe/lab/brainseq_phase2/twas/pgc_scz2_sumstats/PGC2.SCZ.sumstats_filt \
--weights /dcl01/lieber/ajaffe/lab/brainseq_phase2/twas/HIPPO/gene/HIPPO_gene.pos \
--weights_dir /dcl01/lieber/ajaffe/lab/brainseq_phase2/twas/HIPPO/gene \
--ref_ld_chr /dcl01/lieber/ajaffe/lab/brainseq_phase2/twas/reference_hg38/LDREF_hg38/1000G.EUR. \
--chr 22 \
--out test_PGC2.SCZ.22.dat
## Try with another chromosome (also doesn't work)
Rscript /jhpce/shared/jhpce/libd/fusion_twas/github/fusion_twas/FUSION.assoc_test.R \
--sumstats /dcl01/lieber/ajaffe/lab/brainseq_phase2/twas/pgc_scz2_sumstats/PGC2.SCZ.sumstats \
--weights /dcl01/lieber/ajaffe/lab/brainseq_phase2/twas/HIPPO/gene/HIPPO_gene.pos \
--weights_dir /dcl01/lieber/ajaffe/lab/brainseq_phase2/twas/HIPPO/gene \
--ref_ld_chr /dcl01/lieber/ajaffe/lab/brainseq_phase2/twas/reference_hg38/LDREF_hg38/1000G.EUR. \
--chr 10 \
--out test_PGC2.SCZ.10.dat
## Try with the original hg19 reference SNPs (although gene coords are in hg38)
Rscript /jhpce/shared/jhpce/libd/fusion_twas/github/fusion_twas/FUSION.assoc_test.R \
--sumstats /dcl01/lieber/ajaffe/lab/brainseq_phase2/twas/pgc_scz2_sumstats/PGC2.SCZ.sumstats \
--weights /dcl01/lieber/ajaffe/lab/brainseq_phase2/twas/HIPPO/gene/HIPPO_gene.pos \
--weights_dir /dcl01/lieber/ajaffe/lab/brainseq_phase2/twas/HIPPO/gene \
--ref_ld_chr /dcl01/lieber/ajaffe/lab/brainseq_phase2/twas/filter_data/LDREF/1000G.EUR. \
--chr 22 \
--out test_PGC2.SCZ.22.dat #--force_model "lasso"
load('out_files/gene_12892.wgt.RDat', verbose = TRUE)
library(data.table)
pgc2 <- fread('/dcl01/lieber/ajaffe/lab/brainseq_phase2/twas/pgc_scz2_sumstats/PGC2.SCZ.sumstats')
present <- snps$V2 %in% pgc2$SNP
table(present)
# present
# FALSE TRUE
# 348 601
head(snps$V2[!present])
their_bims <- dir('/dcl01/lieber/ajaffe/lab/brainseq_phase2/twas/filter_data/LDREF', '.*bim$', full.names = TRUE)
names(their_bims) <- dir('/dcl01/lieber/ajaffe/lab/brainseq_phase2/twas/filter_data/LDREF', '.*bim$')
ldref_bim <- do.call(rbind, lapply(their_bims, function(input_bim) {
message(paste(Sys.time(), 'reading file', input_bim))
res <- fread(input_bim,
col.names = c('chr', 'snp', 'position', 'basepair', 'allele1', 'allele2'),
colClasses = c('character', 'character', 'numeric', 'integer', 'character', 'character')
)
#setkey(res, 'chr', 'basepair')
return(res)
}))
present_ref <- snps$V2 %in% ldref_bim$snp
table(present_ref)
# present_ref
# FALSE TRUE
# 289 660
their_bims_hg38 <- dir('/dcl01/lieber/ajaffe/lab/brainseq_phase2/twas/reference_hg38/LDREF_hg38', '.*bim$', full.names = TRUE)
names(their_bims_hg38) <- dir('/dcl01/lieber/ajaffe/lab/brainseq_phase2/twas/reference_hg38/LDREF_hg38', '.*bim$')
ldref_bim_hg38 <- do.call(rbind, lapply(their_bims_hg38, function(input_bim) {
message(paste(Sys.time(), 'reading file', input_bim))
res <- fread(input_bim,
col.names = c('chr', 'snp', 'position', 'basepair', 'allele1', 'allele2'),
colClasses = c('character', 'character', 'numeric', 'integer', 'character', 'character')
)
#setkey(res, 'chr', 'basepair')
return(res)
}))
present_ref_hg38 <- snps$V2 %in% ldref_bim_hg38$snp
table(present_ref_hg38)
# FALSE TRUE
# 338 611
present_z_ref <- pgc2$SNP %in% ldref_bim$snp
table(present_z_ref)
# TRUE
# 1083014
present_z_ref_hg38 <- pgc2$SNP %in% ldref_bim_hg38$snp
table(present_z_ref_hg38)
# FALSE TRUE
# 114105 968909
## Ok, filter the PGC2 sumstats to the SNPs we have in the ported hg38 reference
pgc2_filt <- pgc2[present_z_ref_hg38, ]
fwrite(pgc2_filt, file = '/dcl01/lieber/ajaffe/lab/brainseq_phase2/twas/pgc_scz2_sumstats/PGC2.SCZ.sumstats_filt', sep = '\t')