-
Notifications
You must be signed in to change notification settings - Fork 1
/
gen_regression.R
328 lines (269 loc) · 9.68 KB
/
gen_regression.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
## Usage: Rscript ../benchmark.R -i closed_reference_otus_1234.txt -f WATER_CONTENT_SOIL
library(optparse)
source('~/softwares/my/ml_util.R')
opt <- interface_generalize()
if (opt$debug) save.image('debug.Rdata')
if (opt$verbose) {
cat("Running command with args:\n",
paste(commandArgs(), collapse = " "),
'\n')
}
if (opt$split <= 0 & opt$split > 1) {
stop("The split arg should be greater than 0 and not greater than 1.")
}
if(is.null(opt$models)) {
models <- regression
} else {
models <- strsplit(opt$models, ',')[[1]]
}
library(caret)
if (opt$cores > 1) {
library(doMC)
registerDoMC(opt$cores)
}
meta <- read.table.x(opt$metadata)
meta.col <- colnames(meta)
if (is.null(opt$fields)) {
stop("No field is provided to do regression on.")
}
outcome.col <- strsplit(opt$fields, ',')[[1]]
x <- which(! outcome.col %in% meta.col)
if (length(x) > 0) {
stop("Field(s) ", paste(outcome.col[x], collapse=','), " do not exist in meta data")
}
meta.2 <- read.table.x(opt$metadata_2)
meta.col.2 <- colnames(meta.2)
if (is.null(opt$fields)) {
stop("No field is provided to do regression on.")
}
x <- which(! outcome.col %in% meta.col.2)
if (length(x) > 0) {
stop("Field(s) ", paste(outcome.col[x], collapse=','), " do not exist in meta data 2")
}
## extract part of the samples by their meta data
if (! is.null(opt$category)) {
## e.g. "SITE::nostril,skin;SEX::male"
extract <- strsplit(opt$category, ':_:')[[1]]
extract <- strsplit(extract, '::')
for (x in extract) {
if (! x[1] %in% meta.col)
stop("The field ", x[1], " does not exist in meta data")
i <- meta[[ x[1] ]]
j <- strsplit(x[2], ',')[[1]]
if (! all(j %in% i)) {
## insanity check to avoid typos
stop("You specified non-existing values for field ", x[1], " in meta data")
}
meta <- meta[ i %in% j, ]
if (! x[1] %in% meta.col.2)
stop("The field ", x[1], " does not exist in meta data 2")
i <- meta.2[[ x[1] ]]
j <- strsplit(x[2], ',')[[1]]
if (! all(j %in% i)) {
## insanity check to avoid typos
stop("You specified non-existing values for field ", x[1], " in meta data 2")
}
meta.2 <- meta.2[ i %in% j, ]
}
}
if (! is.null(opt$numeric)) {
## e.g. "PH::6,12;TEMP::,32"
extract <- strsplit(opt$numeric, ':_:')[[1]]
extract <- strsplit(extract, '::')
for (x in extract) {
if (! x[1] %in% meta.col)
stop("The field ", x[1], " does not exist in meta data")
## in case there is None, NA, etc in the column (R will read it into character
## instead of numerical)
i <- as.numeric(as.character(meta[[ x[1] ]]))
j <- as.numeric(strsplit(x[2], ',')[[1]])
## NA & TRUE -> NA
## NA & FALSE -> FALSE
n <- rep(T, nrow(meta))
if (! is.na(j[1]))
n <- n & i >= j[1]
if (! is.na(j[2]))
n <- n & i <= j[2]
meta <- meta[n, ]
}
}
if (! is.null(opt$numeric2)) {
## e.g. "PH::6,12;TEMP::,32"
extract <- strsplit(opt$numeric2, ':_:')[[1]]
extract <- strsplit(extract, '::')
for (x in extract) {
if (! x[1] %in% meta.col.2)
stop("The field ", x[1], " does not exist in meta.2 data")
## in case there is None, NA, etc in the column (R will read it into character
## instead of numerical)
i <- as.numeric(as.character(meta.2[[ x[1] ]]))
j <- as.numeric(strsplit(x[2], ',')[[1]])
## NA & TRUE -> NA
## NA & FALSE -> FALSE
n <- rep(T, nrow(meta.2))
if (! is.na(j[1]))
n <- n & i >= j[1]
if (! is.na(j[2]))
n <- n & i <= j[2]
meta.2 <- meta.2[n, ]
}
}
otus <- read.table.x(opt$input_otu_table)
tax.16s <- otus[, length(otus)]
tax.16s <- gsub("^Root; ", "", tax.16s)
## insert a newline for every three levels of taxonomy
tax.16s <- gsub("([^;]*); ([^;]*); ([^;]*); ", '\\1; \\2; \\3\n', tax.16s)
names(tax.16s) <- otus[[1]]
## remove the 6-digit suffix of the sample IDs in the mapping file.
## meta.sid <- gsub(".[0-9]{6}$", "", as.character(meta[[1]]))
meta.sid <- as.character(meta[[1]])
rownames(meta) <- meta.sid
sample.ids <- intersect(meta.sid, colnames(otus))
meta <- meta[sample.ids, ]
rownames(otus) <- otus[[1]]
otus <- data.frame(t(otus[, sample.ids]), check.names=FALSE)
otus.2 <- read.table.x(opt$input_otu_table_2)
meta.sid <- as.character(meta.2[[1]])
rownames(meta.2) <- meta.sid
sample.ids <- intersect(meta.sid, colnames(otus.2))
meta.2 <- meta.2[sample.ids, ]
rownames(otus.2) <- otus.2[[1]]
otus.2 <- data.frame(t(otus.2[, sample.ids]), check.names=FALSE)
## add numeric fields as predictors
if (! is.null(opt$add_numeric)) {
add.pred <- strsplit(opt$add_numeric, ',', fixed=TRUE)[[1]]
not.in <- which(! add.pred %in% colnames(meta))
if (length(not.in) > 0) {
stop(paste(c(add.pred[not.in], "not in the meta data!!!"), collapse=' '))
}
otus <- cbind(as.numeric(meta[, add.pred]), otus)
colnames(otus)[1:length(add.pred)] <- add.pred
not.in <- which(! add.pred %in% colnames(meta.2))
if (length(not.in) > 0) {
stop(paste(c(add.pred[not.in], "not in the meta data 2!!!"), collapse=' '))
}
otus.2 <- cbind(as.numeric(meta.2[, add.pred]), otus.2)
colnames(otus.2)[1:length(add.pred)] <- add.pred
}
## add the categorical fields in the meta data as predictors
if (! is.null(opt$add_category)) {
add.pred <- strsplit(opt$add_category, ',', fixed=TRUE)[[1]]
not.in <- which(! add.pred %in% colnames(meta))
if (length(not.in) > 0) {
stop(paste(c(add.pred[not.in], "not in the meta data!!!"), collapse=' '))
}
if (opt$debug) save.image('debug.Rdata')
x <- meta[, add.pred, drop=FALSE]
dummy <- dummyVars(~., data=x)
otus <- cbind(predict(dummy, x), otus)
not.in <- which(! add.pred %in% colnames(meta.2))
if (length(not.in) > 0) {
stop(paste(c(add.pred[not.in], "not in the meta data 2!!!"), collapse=' '))
}
if (opt$debug) save.image('debug.Rdata')
x <- meta.2[, add.pred, drop=FALSE]
dummy <- dummyVars(~., data=x)
otus.2 <- cbind(predict(dummy, x), otus.2)
}
otu.ids <- intersect(colnames(otus), colnames(otus.2))
otus <- otus[, otu.ids]
otus.2 <- otus.2[, otu.ids]
pdf(sprintf("%s.pdf", opt$output))
for(label in outcome.col) {
if(opt$verbose)
cat("=========", label, ":\n")
outcome <- as.numeric(as.character(meta[[label]]))
if(opt$verbose) {
cat("---- a glimpse of outcome:\n")
print(head(outcome, n=30))
}
## remove NA values
outcome.na <- is.na(outcome)
## if more than half of the samples are not numeric
if(sum(outcome.na) > 0.5 * length(outcome)) {
if(opt$verbose)
cat("outcome has less than half of numeric values. skip it.\n")
next
}
outcome <- outcome[! outcome.na]
## if there less than 5 uniq values in this category
if(length(unique(outcome)) < 3) {
if(opt$verbose)
cat("outcome has less than 3 distinctive values. skip it.\n")
next
}
train.set <- otus[! outcome.na, ]
if (opt$split < 1) {
set.seed(1)
training.rows <- createDataPartition(outcome, p=opt$split, list=F)
} else {
training.rows <- 1:length(outcome)
}
train.full <- train.set[training.rows, ]
test.full <- train.set[-training.rows, ]
train.outcome <- outcome[training.rows]
test.outcome <- outcome[-training.rows]
nzv <- nearZeroVar(train.full)
if (length(nzv) > 0) {
train.full <- train.full[, -nzv]
test.full <- test.full[, -nzv]
}
tooHigh <- findCorrelation(cor(train.full), .9)
if (length(tooHigh) > 0) {
train.full <- train.full[, -tooHigh]
test.full <- test.full[, -tooHigh]
}
## save the test set results in a data.frame
if (length(test.outcome) > 0)
testResults <- data.frame(obs=test.outcome)
gen.testX <- otus.2[, colnames(train.full), drop=FALSE]
gen.testY <- as.numeric(as.character(meta.2[[label]]))
gen.results <- data.frame(obs=gen.testY)
if (opt$debug) save.image('debug.Rdata')
## benchmark the specified models
tuned.list <- list()
accuracies <- data.frame()
for (model in models) {
tuned <- regression.tune(train.full, train.outcome, model)
## if(is.na(tuned) | is.null(tuned)) next
if (class(tuned) != 'train') {
cat("Warning message:\nModel ", model, " failed.\n")
next
}
tuned.list[[model]] <- tuned
accu <- accuracy(tuned)
if (opt$verbose) {
print(tuned)
print(accu)
}
## add a new column - Model
tuned$resample$Model <- model
accuracies <- rbind(accuracies, tuned$resample)
if (length(test.outcome) > 0)
testResults[model] <- predict(tuned, test.full)
imp <- varImp(tuned)
pimp <- plot.imp(imp, tax.16s, main=model)
print(pimp, position=c(0, 0, 0.56, 1))
gen.results[model] <- predict(tuned, gen.testX)
if (opt$debug) save.image(sprintf("%s.Rdata", opt$output))
}
if (opt$debug) save.image('debug.Rdata')
if (ncol(gen.results) > 1)
y.yhat(gen.results)
if (opt$diagnostic) {
diagn <- diagnostics(train.full, train.outcome, gen.testX, gen.testY)
}
if (length(tuned.list) > 1) {
## compare the model performances
resamp <- resamples(tuned.list)
m.diff <- diff(resamp)
if (opt$verbose) print(summary(m.diff))
print(dotplot(m.diff))
}
## plot yhat vs. obs
if (length(test.outcome) > 0) {
y.yhat(testResults)
}
}
dev.off()
save.image(sprintf("%s.Rdata", opt$output))