-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAFpredictoR.R
executable file
·146 lines (116 loc) · 4.41 KB
/
AFpredictoR.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
#!/usr/bin/env Rscript
library(data.table)
library(VarianceGamma)
read.frqx = function(frqx.file){
frqx = fread(frqx.file)
setnames(frqx, c(5:10), c("homa1", "het", "homa2", "hap1", "hap2", "missing"))
frqx[,c("frqx_ac", "frqx_an", "frqx_a1f"):=list((2*homa1+het), (2*homa1+2*het+2*homa2+2*missing), (2*homa1+het)/(2*homa1+2*het+2*homa2+2*missing))]
# setnames(frqx, c('A1', 'A2', 'SNP'), c('frqx_A1', 'frqx_A2', 'frqx_SNP'))
frqx[,c("A1", "A2", "homa1", "het", "homa2", "hap1", "hap2", "missing", 'CHR'):=NULL]
return(frqx)
}
read.bim = function(bim.file){
bim = fread(bim.file)
bim[,V3:=NULL]
setnames(bim, c('frqx_chrom', 'SNP', 'frqx_pos', 'frqx_A1', 'frqx_A2'))
return(bim)
}
# Assumes columns: SNP, chrom, pos, A1, A2, AF, N
read.sumstats = function(sumstats.file){
sumstats = fread(sumstats.file)
sumstats[,an:=N*2]
sumstats[,ac:=as.integer(an*AF)] # This will round down
sumstats[,id_a1_a2:=paste0('chr', chrom, ':', pos, ':', A1, ':', A2)]
sumstats[,id_a2_a1:=paste0('chr', chrom, ':', pos, ':', A2, ':', A1)]
return(sumstats)
}
main = function(sumstats.file,
bim.file,
frqx.file,
output,
threshold.pdiff=0.05,
threshold.pmax=1e-10,
debug=FALSE) {
cat('Running AFpredictoR on', as.character(Sys.time()), '\n')
cat('Reading & processing input files\n')
output.file = paste0(output, '.tsv')
output.debug.file = paste0(output, '.debug.tsv')
sumstats = read.sumstats(sumstats.file)
bim = read.bim(bim.file)
frqx = read.frqx(frqx.file)
frqx2 = merge(bim, frqx, by = 'SNP')
frqx2[,var_id:=paste0('chr', frqx_chrom, ':', frqx_pos, ':', frqx_A1, ':', frqx_A2)]
setnames(frqx2, 'SNP', 'frqx_SNP')
m1=merge(frqx2, sumstats, by.x="var_id", by.y="id_a1_a2")
m1[,id_a2_a1:=NULL]
m2=merge(frqx2, sumstats, by.x="var_id", by.y="id_a2_a1")
m2[,id_a1_a2:=NULL]
m = rbind(m1, m2)
m = m[!duplicated(m)] # There were some sumstats with duplicate rows.
m[,var_id:=NULL]
cat('Running proportions test\n')
# Run proportions test and add test statistics to the table
binomp=apply(m[,.(ac, frqx_ac, an, frqx_an)], 1, function(x){
te=prop.test(x=x[1:2], n=x[3:4])
return(list(p=te$p.value, chisq=te$statistic))
})
binomp2=apply(m[,.(ac, frqx_ac, an, frqx_an)], 1, function(x){
x2=x[4]-x[2]
te=prop.test(x=c(x[1],x2), n=x[3:4])
return(list(p=te$p.value, chisq=te$statistic))
})
addendum=cbind(rbindlist(binomp), rbindlist(binomp2))
setnames(addendum, c("p1", "chi1", "p2", "chi2"))
addendum[,c("pmax", "chidiff"):=list(pmax(p1,p2), -abs(chi1-chi2))]
addendum[,AF_is_for_frqx_A1:=p1>p2]
addendum[,pdiff:=pvg(chidiff, vgC=0, theta=0, nu=2, sigma=2)]
m_addendum=cbind(m, addendum)
if (debug==TRUE) {
cat('Debug mode enabled. Creating', output.debug.file, '\n')
fwrite(m_addendum, output.debug.file, sep = '\t')
}
cat('Filtering variants according to thresholds\n')
cat(paste0('pdiff<', threshold.pdiff, '\n'))
cat(paste0('pmax>', threshold.pmax, '\n'))
m_addendum2 = m_addendum[pdiff<threshold.pdiff
& pmax>threshold.pmax,
.(SNP, chrom, pos, A1, A2, AF, N, AF_is_for_frqx_A1, frqx_A1, frqx_A2, pdiff, pmax)]
filtered.rows = nrow(m_addendum) - nrow(m_addendum2)
cat(filtered.rows, 'variants filtered out\n')
cat('Assigning AF to A1\n')
# Case 1 and 7
m_addendum2[AF_is_for_frqx_A1==TRUE
& A1==frqx_A2
& A2==frqx_A1, AF:=1-AF]
# Case 2 and 8
m_addendum2[AF_is_for_frqx_A1==FALSE
& A1==frqx_A1
& A2==frqx_A2, AF:=1-AF]
out = m_addendum2[,.(SNP, chrom, pos, A1, A2, AF, N)]
cat('Generating', output.file, '\n')
fwrite(out, output.file, sep = '\t')
cat('Done\n')
}
args = commandArgs(trailingOnly=TRUE)
args.length = length(args)
if (args.length==4){
main(
sumstats.file = args[1],
bim.file = args[2],
frqx.file = args[3],
output = args[4],
debug=TRUE
)
} else if (args.length==6){
main(
sumstats.file = args[1],
bim.file = args[2],
frqx.file = args[3],
output = args[4],
pdiff = args[5],
pmax = args[6],
debug = TRUE
)
} else {
print('incorrect run option')
}