-
Notifications
You must be signed in to change notification settings - Fork 1
/
make.gene.bslmm.stwas.R
executable file
·148 lines (109 loc) · 4.4 KB
/
make.gene.bslmm.stwas.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
#!/usr/bin/env Rscript
argv <- commandArgs(trailingOnly = TRUE)
if(length(argv) < 4) q()
bslmm.weights <- argv[1] # e.g., bslmm.weights <- 'result/bslmm/1/bslmm-2-weights.gz'
data.dir <- argv[2] # e.g., data.dir <- 'scratch/data/2/'
gwas.dir <- argv[3] # e.g., gwas.dir <- 'IGAP'
out.file <- argv[4] # e.g., 'out.file.gz'
source('Util.R')
if(file.exists(out.file)){
log.msg('File exists: %s\n\n', out.file)
q()
}
pip.cutoff <- 0.95
n.cutoff <- 10
n.perm <- 5e7
n.blk <- 1024
n.round <- ceiling(n.perm/n.blk)
source('NWAS.R')
library(dplyr)
library(fqtl)
library(reshape2)
options(stringsAsFactors = FALSE)
plink.hdr <- glue(data.dir, '/plink')
gene.info <- read.table(glue(data.dir, '/data.gene'),
col.names = c('idx', 'ensg', 'chr', 'tss', 'tss.2'))
gene.info <- gene.info[2:4]
log.msg('Running on %s [%d:%d]...\n', gene.info$ensg, gene.info$chr, gene.info$tss)
## 1. read BSLMM weights
tis.info <- read.table(glue(data.dir,'/tissues.txt.gz'), col.names = c('tis', 'tis.name'))
snp.info <- read.table(glue(plink.hdr, '.bim'), col.names = c('chr', 'rs', '.', 'loc', 'qtl.a1', 'qtl.a2'))
snp.info <- snp.info %>% mutate(x.loc = 1:nrow(snp.info))
bslmm.tab <- read.table(bslmm.weights, header = TRUE)
bslmm.tab <- bslmm.tab %>% mutate(tis.name = tis.info[tis, 2]) %>%
mutate(tis = tis.info[tis, 1]) %>%
mutate(tis.name = gsub(tis.name, pattern = '_Analysis', replacement = ''))
## Use tissues with max PIP > 0.95
tissues <- bslmm.tab %>% filter(pip >= pip.cutoff) %>% select(tis) %>% unique()
tissues <- tissues[, 1]
if(length(tissues) < 1){
system(paste('printf \"\" | gzip >', out.file))
log.msg('No valid tissue in %s\n\n', paste(gene.info[1, ], collapse = ' '))
q()
}
bslmm.tab <- bslmm.tab %>% filter(tis %in% tissues)
## 2. read GWAS tab
gwas.file <- glue(gwas.dir, '/chr', gene.info$chr, '.txt.gz')
plink.bim.file <- glue(plink.hdr, '.bim')
system.cmd <- paste('./make_gene_fqtl_gwas.sh', gwas.file, plink.bim.file)
gwas.txt <- system(system.cmd, intern=TRUE, ignore.stderr = TRUE)
gwas.cols <- c('chr', '.', 'loc', 'rs', 'gwas.a1', 'gwas.a2', 'gwas.z', 'gwas.theta', 'gwas.se')
gwas.tab <- read.table(text = gwas.txt, sep = '\t', col.names = gwas.cols) %>%
mutate(chr = sapply(chr, gsub, pattern = 'chr', replacement = '')) %>%
select(chr, loc, rs, gwas.a1, gwas.a2, gwas.z)
matched.tab <- bslmm.tab %>%
left_join(snp.info %>% select(-chr, -loc), by = 'rs') %>%
rename(qtl.name = rs) %>%
left_join(gwas.tab %>% select(-chr), by = c('loc')) %>%
na.omit() %>%
mutate(gwas.z.flip = if_else(qtl.a1 != gwas.a1, -gwas.z, gwas.z))
gwas.matched <- matched.tab %>% select(x.loc, gwas.z.flip) %>% unique()
gwas.z <- gwas.matched[, 'gwas.z.flip']
plink <- read.plink(plink.hdr)
LD.svd <- func.LD.svd(plink$BED, normalize = TRUE)
rm(plink)
## Select high PIP QTL
tissues <- matched.tab %>% filter(pip >= pip.cutoff) %>%
select(tis) %>% unique()
tissues <- tissues[, 1]
if(length(tissues) < 1){
system(paste('printf \"\" | gzip >', out.file))
log.msg('No valid factor in %s\n\n', paste(gene.info[1, ], collapse = ' '))
q()
}
## Permutation test
out <- NULL
for(tt in tissues) {
obs.tab <- matched.tab %>% filter(pip >= pip.cutoff, tis == tt)
null.tab <- matched.tab %>% filter(tis == tt)
n.qtl <- nrow(obs.tab %>% select(qtl.name) %>% unique())
qtl.z <- obs.tab$sparse
blk.ind <- func.blk.ind(n.qtl, n.blk = 1024)
vt <- LD.svd$V.t %c% obs.tab$x.loc
dd <- LD.svd$d
vt.null <- LD.svd$V.t %c% null.tab$x.loc
gwas.null <- null.tab$gwas.z.flip
obs.stat <- func.NWAS(qtl.z, obs.tab$gwas.z.flip, vt, dd)
z.obs.abs <- abs(obs.stat[, 'z'])
## adaptive permutation
c.tot <- 0
p.tot <- 0
for(b in seq(1, n.round)){
set.seed(b)
perm.stat <- func.NWAS.qtl.perm(qtl.z, gwas.null, vt.null, dd, blk.ind)
stat.z <- perm.stat[,'z']
c.tot <- c.tot + sum(abs(stat.z) > z.obs.abs)
p.tot <- p.tot + length(stat.z)
if(c.tot > n.cutoff){
break;
}
cat('\n', c.tot, '/', p.tot, '... ')
cat(signif(range(stat.z), 2), '\n')
}
p.val <- (1 + c.tot)/(1 + p.tot)
log.msg('Done : p-value = %.2e\n', p.val)
out <- rbind(out, data.frame(obs.stat, p.val, tis = tt))
}
out <- data.frame(gene.info, gwas.dir, out) %>%
left_join(tis.info, by = 'tis')
write.tsv(out, file = gzfile(out.file))