-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpqtlMR.sh
executable file
·145 lines (135 loc) · 5.44 KB
/
pqtlMR.sh
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
#/usr/bin/bash
module load gcc/6
if [ ! -d ${INF}/mr/pQTLs ]; then mkdir -p ${INF}/mr/pQTLs; fi
function iv()
{
(
echo SNP Phenotype effect_allele other_allele eaf beta se pval N
# rsid prot Allele1 Allele2 Freq1 Effect StdErr log.P. cis.trans
cut -f2,3,6,7,8-11,17,21 ${INF}/work/INF1.METAL | \
awk -vtype=${type} '$10==type {print $1,$2,toupper($3),toupper($4),$5,$6,$7,10^$8,$9}'
) > ${INF}/mr/pQTLs/INF1_${type}.ins
}
function INF1_efo()
{
R --no-save -q <<\ \ END
INF <- Sys.getenv("INF")
outcomes <- c("ieu-a-7","ebi-a-GCST007432")
ieugwasr::gwasinfo(id = outcomes)
type <- Sys.getenv("type")
ivs <- read.table(file.path(INF,"mr","pQTLs",paste0("INF1_",type,".ins")),as.is=TRUE,header=TRUE)
Ins <- TwoSampleMR::format_data(ivs,snp_col="SNP",samplesize_col="N")
prefix <- file.path(INF,"mr","pQTLs",paste0("INF1_pQTL-combined-",type,"-"))
pQTLtools::pqtlMR(Ins,outcomes,prefix=prefix)
prefix <- file.path(INF,"mr","pQTLs",paste0("INF1_rev_pQTL-combined-",type,"-"))
pQTLtools::pqtlMR(Ins,outcomes,prefix=prefix,reverse=TRUE)
END
R --no-save -q <<\ \ END
INF <- Sys.getenv("INF")
outcomes <- with(read.delim(file.path(INF,"rsid","efo.txt")),MRBASEID)
type <- Sys.getenv("type")
ivs <- read.table(file.path(INF,"mr","pQTLs",paste0("INF1_",type,".ins")),as.is=TRUE,header=TRUE)
Ins <- TwoSampleMR::format_data(ivs,snp_col="SNP",samplesize_col="N")
Ids <- TwoSampleMR::extract_outcome_data(snps=with(Ins,SNP),outcomes=outcomes)
harmonise <- TwoSampleMR::harmonise_data(Ins,Ids)
prefix <- file.path(INF,"mr","pQTLs",paste0("efo_pQTL-combined-",type,"-"))
pQTLtools::pqtlMR(harmonise,prefix=prefix)
prefix <- file.path(INF,"mr","pQTLs",paste0("efo_rev_pQTL-combined-",type,"-"))
pQTLtools::pqtlMR(harmonise,prefix=prefix,reverse=TRUE)
END
}
function collect()
{
echo ${prefix} -- ${id} -- ${trait}
(
cat ${prefix}result.txt | head -1
grep -w ${id} ${prefix}result.txt | grep -e "Wald ratio" -e Inverse | awk '$NF!="NA"'
) | grep -v _rev_ > ${prefix}${id}.result
(
cat ${prefix}single.txt | head -1
grep -w ${id} ${prefix}single.txt | awk '$NF!="NA"'
) | grep -v _rev_ > ${prefix}${id}.single
}
function collect_rev()
{
echo ${prefix} -- ${id} -- ${trait}
(
cat ${prefix}result.txt | head -1
grep -w ${id} ${prefix}result.txt | grep -e "Wald ratio" -e Inverse | awk '$NF!="NA"'
) | awk -v FS="\t" -v id=${id} 'NR==1||$1==id' > ${prefix}${id}.result
(
cat ${prefix}single.txt | head -1
grep -w ${id} ${prefix}single.txt | awk '$NF!="NA"'
) | awk -v FS="\t" -v id=${id} 'NR==1||$3==id' > ${prefix}${id}.single
}
function collect_all()
{
for i in ieu-a-7 ebi-a-GCST007432
do
export id=${i}
if [ "${i}" == "ieu-a-7" ]; then
export trait="CHD || ${i}"
else
export trait="FEV1 || ${i}"
fi
export prefix=${INF}/mr/pQTLs/INF1_pQTL-combined-${type}-
collect
export prefix=${INF}/mr/pQTLs/INF1_rev_pQTL-combined-${type}-
collect_rev
done
export nrows=$(sed '1d' ${INF}/rsid/efo.txt | wc -l | cut -d' ' -f1)
for i in $(seq ${nrows})
do
export trait=$(sed '1d' ${INF}/rsid/efo.txt | awk -vFS="\t" -vnr=${i} 'NR==nr{print $2}')
export id=$(sed '1d' ${INF}/rsid/efo.txt | awk -vFS="\t" -vnr=${i} 'NR==nr{print $4}')
export prefix=${INF}/mr/pQTLs/efo_pQTL-combined-${type}-
collect
export prefix=${INF}/mr/pQTLs/efo_rev_pQTL-combined-${type}-
collect_rev
done
}
for type in cis trans
do
export type=${type}
iv
INF1_efo
collect_all
done
export dir=${INF}/mr/pQTLs/
(
awk 'NR==1' ${dir}INF1_pQTL-combined-cis-result.txt | cut -f1,2,5,6 --complement | awk -v OFS="\t" '{print $0, "cistrans"}'
awk '/ieu/' ${dir}INF1_pQTL-combined-cis-result.txt | cut -f1,2,5,6 --complement | awk -v OFS="\t" '{print $0, "cis"}'
awk '/ieu/' ${dir}INF1_pQTL-combined-trans-result.txt | cut -f1,2,5,6 --complement | awk -v OFS="\t" '{print $0, "trans"}'
awk '/FEV1/' ${dir}INF1_pQTL-combined-cis-result.txt | cut -f1,2,5,6 --complement | awk -v OFS="\t" '{print $0, "cis"}'
awk '/FEV1/' ${dir}INF1_pQTL-combined-trans-result.txt | cut -f1,2,5,6 --complement | awk -v OFS="\t" '{print $0, "trans"}'
) > ${dir}/pQTL-ieu-FEV1.txt
(
awk 'NR==1' ${dir}efo_pQTL-combined-cis-result.txt | cut -f1,2,5,6 --complement | awk -v OFS="\t" '{print $0, "cistrans"}'
awk 'NR>1' ${dir}efo_pQTL-combined-cis-result.txt | cut -f1,2,5,6 --complement | awk -v OFS="\t" '{print $0, "cis"}'
awk 'NR>1' ${dir}efo_pQTL-combined-trans-result.txt | cut -f1,2,5,6 --complement | awk -v OFS="\t" '{print $0, "trans"}'
) > ${dir}/pQTL-efo.txt
# Bidirectionality test for FGF.5
function dummy()
{
R --no-save -q <<END
options(width=200)
INF1_cis <- read.delim("INF1_cis.ins",sep=" ")
pQTLtools::pqtlMR(subset(INF1_cis,Phenotype=="FGF.5"),"ieu-a-7",prefix="test")
h <- read.delim("test-harmonise.txt")
require(TwoSampleMR)
info <- ieugwasr::gwasinfo("ieu-a-7")
# binary outcomes
lor <- with(h,beta.outcome)
af <- with(h,eaf.outcome)
ncase <- with(info,ncase)
ncontrol <- with(info,ncontrol)
prevalence <- 0.1
pval.exposure <- with(h,pval.exposure)
samplesize.exposure <- 11787
outcome <- with(info,sample_size)
r.exposure <- get_r_from_pn(pval.exposure,samplesize.exposure)
r.outcome <- get_r_from_lor(lor, af, ncase, ncontrol, prevalence, model = "logit", correction = FALSE)
h <- data.frame(h,samplesize.exposure=11787,r.exposure=r.exposure,r.outcome=r.outcome)
directionality_test(h)
END
}