-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlca.R
161 lines (125 loc) · 4.72 KB
/
lca.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
#!/usr/bin/Rscript
library(R2jags)
library(getopt)
library(reshape2)
discard = runif(1) # fixes this error: 'Error in set.seed() : argument "seed" is missing, with no default'
# get options, using the spec as defined by the enclosed list.
# we read the options from the default: commandArgs(TRUE).
spec = matrix(c(
'verbose', 'v', 2, "logical",
'countsFile', 'c', 1, "character",
'statsOutFile', 's', 1, "character",
'help' , 'h', 0, "logical"
), byrow=TRUE, ncol=4);
opt = getopt(spec);
# if help was asked for print a friendly message and exit with a non-zero error code
if ( !is.null(opt$help) ) {
cat(getopt(spec, usage=TRUE));
q(status=1);
}
if ( is.null(opt$countsFile ) ) {
cat("please specify a --countsFile\n")
cat(getopt(spec, usage=TRUE));
q(status=1);
}
if ( is.null(opt$statsOutFile ) ) {
cat("please specify a --statsOutFile\n")
cat(getopt(spec, usage=TRUE));
q(status=1);
}
########################################
########################################
# This function generates a JAGS file, to be written out and used by R2Jags
# The JAGS file changes depending on the number of SNP calls
generateJagsFile <- function(numberOfSnpCallSets, outJagsFile) {
# numberOfSnpCallSets is the number of VCF files the user gives us
jagCode =
"model {
theta ~ dbeta(1,2);
for (i in 1:N) {
fp[i] ~ dbeta(1,2);
fn[i] ~ dbeta(1,2);
tp[i] <- 1-fn[i];
tn[i] <- 1-fp[i];
}
";
jagCode = paste(jagCode, "for (j in 1:M) {\n\tmargprobs[j] <- theta * \n" )
for (i in 1:numberOfSnpCallSets){
if ( i == numberOfSnpCallSets ){
jagCode = paste(jagCode, "\t(fn[", i, "]^(1-x[j,", i, "])) * (tp[", i, "]^x[j,", i, "]) \n", sep="")
}
else {
jagCode = paste(jagCode, "\t(fn[", i, "]^(1-x[j,", i, "])) * (tp[", i, "]^x[j,", i, "]) *\n", sep="")
}
}
jagCode = paste(jagCode, "\t+ (1-theta) * \n")
for (i in 1:numberOfSnpCallSets){
if ( i == numberOfSnpCallSets ){
jagCode = paste(jagCode, "\t(tn[", i, "]^(1-x[j,", i, "])) * (fp[", i, "]^x[j,", i, "]);\n\n", sep="")
}
else {
jagCode = paste(jagCode, "\t(tn[", i, "]^(1-x[j,", i, "])) * (fp[", i, "]^x[j,", i, "]) *\n", sep="")
}
}
jagCode = paste(jagCode, "\tpostprobs[j] <- theta * \n", sep="")
for (i in 1:numberOfSnpCallSets){
if ( i == numberOfSnpCallSets ){
jagCode = paste(jagCode, "\t(fn[", i, "]^(1-x[j,", i, "])) * (tp[", i, "]^x[j,", i, "])\n\t/ margprobs[j];\n", sep="")
}
else {
jagCode = paste(jagCode, "\t(fn[", i, "]^(1-x[j,", i, "])) * (tp[", i, "]^x[j,", i, "]) *\n", sep="")
}
}
jagCode = paste(jagCode, "}
counts ~ dmulti(margprobs, total);
}", sep="")
fileConn<-file(outJagsFile)
writeLines(jagCode, fileConn)
close(fileConn)
}
########################################
########################################
# read in counts file
raw.data = read.table(opt$countsFile, header=F, skip=1 )
# counts <- c(2942808473, 17491655, 21576, 23189, 339805, 89159,
# 168214, 76044, 43138288, 530963, 22682, 22169,
# 462052, 129472, 2804257, 3454104);
counts = raw.data[,length(raw.data)]
M <- length(counts)
N <- log(M, base=2)
total <- sum(counts);
# make matrix of all possible 0/1 values for each SNP caller
cmdstring = paste( "expand.grid(", paste( rep("c(0,1)", N), collapse = ", " ), ")", sep="")
x <- eval( parse(text = cmdstring ) )
jags.data <- c("N", "M", "x", "counts", "total");
jags.params <- c("theta", "fp", "fn", "margprobs", "postprobs");
jags.inits <- NULL;
# write out lca.jags file
tempJagFile = tempfile();
generateJagsFile(N, tempJagFile);
jagsfit1 <- jags(data=jags.data, inits=jags.inits, param=jags.params, DIC=FALSE,
n.chains=1, n.iter=120000, n.thin=10, n.burnin=10000,
model.file=tempJagFile)
mcmc <- as.mcmc(jagsfit1);
# below is what Aaron was doing before, but I'm just going to write out the whole summary statistics file
# print(params <- summary(mcmc)$statistics[c(1:8,41),1, drop=F])
# print(pp <- summary(mcmc)$statistics[25:40,1, drop=F])
# print(cbind(x, pp, pp >= 0.5))
# write out summary statistics
summaryStats = summary(mcmc)$statistics
idx <- 2*N + 1 # skip the fp/fn parameters
data <- cbind(x, cts = counts, margprobs = summary(mcmc)$statistics[idx:(idx+M-1), 1])
bvrs <- matrix(NA, ncol=N, nrow=N);
for (i in 2:N) {
for (j in 1:(i-1)) {
obs <- dcast(data[,i]+data[,j] ~ ., sum, value.var="cts", data=data)[,3]
exp <- dcast(data[,i]+data[,j] ~ ., sum, value.var="margprobs", data=data)[,3]
bvrs[i,j] <- chisq.test(obs, p=exp, rescale.p=T)$statistic / 3 # BVR == Chi-square/d.f.
}
}
sink(opt$statsOutFile)
cat("==== Summary Statistics ====\n")
summaryStats
cat("==== Bivariate residuals ====\n")
bvrs
sink()