-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathevalprofile.R
More file actions
executable file
·136 lines (114 loc) · 3.32 KB
/
evalprofile.R
File metadata and controls
executable file
·136 lines (114 loc) · 3.32 KB
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
#!/usr/bin/env Rscript
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Copyright (C) 2011-2012 Gad Abraham and National ICT Australia (NICTA).
# All rights reserved.
#
usage <- paste("usage: evalprofile.R",
"model=<MODEL>",
"[prev=<PREVALENCE>]",
"[name=results]",
"[indir=discovery]",
"[outdir=predict]"
)
args <- commandArgs(TRUE)
s <- strsplit(args, "=")
for(m in s) {
eval(parse(text=sprintf("%s=\"%s\"", m[1], m[2])))
}
if(!exists("model", mode="character") || !model %in% c("linear", "sqrhinge") ) {
stop("model not specified, must be one of: sqrhinge, linear")
}
if(!exists("outdir", mode="character")) {
outdir <- "predict"
}
if(!exists("indir", mode="character")) {
indir <- "discovery"
}
if(!exists("name")) {
name <- "results"
}
if(!exists("prev")) {
prev <- NULL
cat("warning: prevalence not supplied, PPV/NPV will not be calculated\n")
} else {
prev <- as.numeric(prev)
if(is.na(prev) || prev < 0 || prev > 1) {
stop("invalid prevalence: ", prev)
}
}
library(ROCR)
cat("indir:", indir, "\n")
cat("outdir:", outdir, "\n")
R2 <- function(pr, y)
{
pr <- cbind(pr)
y <- cbind(y)
if(ncol(y) != ncol(pr))
stop("ncol(y) doesn't match ncol(pr)")
s <- sapply(1:ncol(y), function(k) {
1 - sum((pr[,k] - y[,k])^2) / sum((y[,k] - mean(y[,k]))^2)
})
s[is.nan(s)] <- 0
s
}
lf <- list.files(path=outdir, pattern="[[:digit:]]+\\.profile$", full.names=TRUE)
cat("found", length(lf), "profile files\n")
nums <- sapply(sapply(strsplit(lf, "\\.profile"), strsplit, split="_"), tail, n=1)
lf <- lf[order(as.integer(nums))]
res <- lapply(seq(along=lf), function(i) {
cat("reading", lf[i], "\n")
prof <- read.table(lf[i], header=TRUE)
intf <- sprintf("%s/intercept_path_%s.txt", indir, i)
cat("reading intercept file", intf, "\n")
intercept <- scan(intf, quiet=TRUE)
# PLINK divides the predicted score by the number of alleles, we don't want
# that to we multiply to get original score
score <- prof$SCORE * max(prof$CNT) + intercept
nz <- max(prof$CNT) / 2 # CNT is alleles not SNPs
if(model == "sqrhinge") {
pred <- prediction(labels=prof$PHENO, predictions=score)
perf <- performance(pred, "sens", "spec")
auc <- performance(pred, "auc")
sens <- perf@y.values
spec <- perf@x.values
cutoffs <- pred@cutoffs
ppv <- npv <- NULL
if(!is.na(prev)) {
ppv <- sapply(1:length(cutoffs), function(j) {
(sens[[j]] * prev) / (
sens[[j]] * prev + (1 - spec[[j]]) * (1 - prev))
})
npv <- sapply(1:length(cutoffs), function(j) {
(spec[[j]] * (1 - prev)) / (
(1 - sens[[j]]) * prev + spec[[j]] * (1 - prev))
})
}
list(
score=score,
pred=pred,
perf=perf,
sens=sens,
spec=spec,
cutoffs=cutoffs,
ppv=ppv,
npv=npv,
auc=auc,
nz=nz
)
} else {
list(
pred=score,
observed=prof$PHENO,
R2=R2(score, prof$PHENO),
nz=nz
)
}
})
f <- sprintf("%s/%s.RData", outdir, name)
cat("saving results to file", f, "\n")
save(res, file=f)