-
Notifications
You must be signed in to change notification settings - Fork 0
/
Retinue_SVM.R
290 lines (247 loc) · 12.1 KB
/
Retinue_SVM.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
#!/usr/bin/env Rscript
args <- commandArgs(trailingOnly=TRUE)
if(identical(args, character(0))){args = c("TEMP.RData",600,"NA",getwd())}
print(args)
library(e1071)
library(DESeq2)
library(limma)
library(pracma)
library(sva)
library(tidyverse)
library(doParallel)
setwd(args[[4]])
cores = detectCores()
print(paste0(cores," cores detected"))
registerDoParallel(cores=cores)
if(!file.exists(args[[1]])){
##### Load data
counts = read.csv("datExpr.csv",row.names=1)
metadata = read.csv("metadata.csv")
counts = as.matrix(counts[metadata$ID])
##### Correct counts for batch effect of study
counts_adj <- ComBat_seq(counts, batch=metadata$block, group=metadata$Role)
#### Perform initial classification
##### Divide data into training (control) set and test (treatment) set
treatment = metadata[sample(nrow(metadata), 6), "ID"]
metadata[metadata$ID%in%treatment,"Role"] =
paste(metadata[metadata$ID%in%treatment,"Role"],"test",sep="_")
svm.data = metadata[,c("ID","Role")]
svm.data.train = subset(svm.data, Role %in% c("responsive","unresponsive"))
svm.data.test = subset(svm.data, !(Role %in% c("responsive","unresponsive")))
###### best model: radial kernel, gamma = 0.01, cost = 0.5 | best performance: 0.2638667
#### Define SVM function
svm.train = function(readcounts, traindata, testdata = NA, referencelevel = "queen", kerneltype = "radial", crossfold = 5, vstCheck = T){
svm.counts.test=NA
# normalise data
svm.counts = readcounts
# perform DESeq's variance stabilizing tranformation, which is preferable to logging for gene expression data
if(vstCheck){svm.counts.vst = vst(svm.counts)}else{svm.counts.vst = varianceStabilizingTransformation(svm.counts)}
# scale counts and remove zero-variance features
svm.counts.vst.quantiles.scale = t(scale(t(svm.counts.vst)))
svm.counts.vst.quantiles.scale = na.omit(svm.counts.vst.quantiles.scale)
# Divide transcriptomic data into training set (queens and workers from control) and test set (individuals from treatment)
svm.counts.train = svm.counts.vst.quantiles.scale[,which(colnames(svm.counts.vst.quantiles.scale) %in% traindata$ID)]
if(length(testdata)>1){
svm.counts.test = svm.counts.vst.quantiles.scale[,which((colnames(svm.counts.vst.quantiles.scale) %in% testdata$ID))]
}
# Perform a grid search to optimise SVM parameters
svm.counts.tuneResult = tune("svm",
train.x = t(svm.counts.train),
train.y = as.numeric(traindata$Role == referencelevel),
probability = TRUE,
scale = FALSE,
kernel = kerneltype,
tunecontrol = tune.control(sampling = "cross",
cross = crossfold),
ranges = list(gamma = 10^(-3:-1),
cost = 2^(-2:0))
)
# Final classifier
svm.counts.classifier = svm.counts.tuneResult$best.model
svm.counts.prediction = NULL
if(length(testdata)>1){
# Make predictions for the test data, if test data were provided.
svm.counts.prediction = predict(svm.counts.classifier,
t(svm.counts.test),
type = "class",
probability = TRUE)
}
#output prediction for test data and cross-validation error for training data
svm.result = list("prediction" = svm.counts.prediction,
"validation_error" = signif(svm.counts.tuneResult$best.performance,4),
"traincounts" = svm.counts.train,
"testcounts" = svm.counts.test)
#return results
return(svm.result)
}
# apply svm to entire set of genes
svm.full = svm.train(counts_adj,
svm.data.train,
svm.data.test,
crossfold = 3,
vstCheck = F,
referencelevel = "responsive")
print(paste0("Root mean cross-validation error rate for full model: ",svm.full$validation_error))
### Perform feature selection
# create copy of training data that we can subject to repeated trimming while preserving original frame
svm.counts.train.iterate = svm.full$traincounts
#record original number of features
nfeatures = nrow(svm.counts.train.iterate)
#target number of features
nfeatures_target = 100
traindata = svm.data.train
#instantiate data frame to hold data on the error of each model
iterations = data.frame(feature = character(),
error_before_removal = numeric())
}
if(file.exists(args[[1]])){
load(args[[1]])
print(paste0("Restarting from feature # ", nfeatures))
}
time.start = strptime(Sys.time(), "%Y-%m-%d %H:%M:%S")
print(paste0("Start time: ",time.start))
#iteratively remove features until target number is reached
while(nfeatures > nfeatures_target){
#Check if elapsed time is less than time limit in seconds (set at beginning of script as args[[1]])
#If true, and if currently running on the cluster (providing submit script path in args[[3]]),
#Restart R script from submit script and continue
time.diff = as.numeric(difftime(strptime(Sys.time(),"%Y-%m-%d %H:%M:%S"),time.start,units="sec"))
if(time.diff > as.numeric(args[[2]])){
save.image(args[[1]])
if(!args[[3]]=="NA"){
system(paste0("qsub ",args[[3]]))
quit()
}
}
print(paste(nfeatures,Sys.time(),sep=" "))
#run repeatedly to account for stochasticity in cross-validation
tunelist = c()
tunelist = foreach(i=1:10) %dopar% {
#Perform a grid search to optimise SVM parameters
svm.counts.tuneResult = tune("svm",
train.x = t(svm.counts.train.iterate),
train.y = as.numeric(traindata$Role == "responsive"),
probability = TRUE,
scale = FALSE,
kernel = "radial",
tunecontrol = tune.control(sampling = "cross",
cross = 3),
ranges = list(gamma = 10^(-3:-1),
cost = 2^(-2:0)))
#record error
error = svm.counts.tuneResult$best.performance
iterlist = c(svm.counts.tuneResult,error=error)
return(iterlist)
}
#sample classifier
svm.counts.classifier = tunelist[length(tunelist)][[1]][["best.model"]]
#return mean error value
error = signif(mean(sapply(tunelist, get, x="error")),4)
#extract feature weights
weights = (t(svm.counts.classifier$coefs) %*% svm.counts.classifier$SV)
#calculate feature with lowest weight (for ties, choose arbitrarily)
weakfeature = colnames(weights)[which(abs(weights) == min(abs(weights)))[1]]
#remove lowest-weight feature from data frame
svm.counts.train.iterate = subset(svm.counts.train.iterate, !(rownames(svm.counts.train.iterate) %in% c(weakfeature)))
#in a dataframe, store removed feature name and error value before removing that feature
iterations = rbind(iterations, tibble(feature = weakfeature,
error_before_removal = error))
#tick down
nfeatures = (nfeatures-1)
#output every 100 runs to track progress
if((nfeatures/100)%%1==0){print(paste0("Features remaining: ",nfeatures))}
}
iterLength = 1:nrow(iterations)
# take moving average to smooth out variation
moving_avg = movavg(iterations$error_before_removal, 100, "s")
save.image("SVM.RData")
load("SVM.RData")
# plot data to ensure we have the expected 'hockeystick' shape
# note that this will be truncated for the subsetted dataset provided for demoing!
hockeyData = data.frame(num = iterLength, error = moving_avg)
hockeyData_plot = hockeyData
hockeyData_plot$num = abs(iterLength - (max(iterLength)+1))
hockeyData_plot$error <- sqrt(hockeyData_plot$error)
library(ggprism)
g <- ggplot(data=hockeyData_plot, aes(x=num, y=error, group=1)) +
geom_line(linewidth=1.5) + theme_prism() +
scale_y_continuous(breaks=seq.int(.15,.55,.1),limits=c(.15,.55)) +
scale_x_reverse(breaks=c(12000,10000,8000,6000,4000,2000,0)) +
xlab("Genes") + ylab("RMSE") +
geom_vline(xintercept=100, linetype="dashed", color = "red") +
geom_hline(yintercept=0.2093285, linetype="dashed", color = "red")
g
ggsave("figS6.png",g,width=7,height=5,dpi=300)
# get minimum of this curve to find the point at which the error window is at its minimum
optimal_removal = which(moving_avg == min(moving_avg));
# list the features to be removed from the original set of genes
features_to_remove = iterations$feature[1:optimal_removal]
# new dataframe with less-useful features removed
counts_clean_subsample = subset(counts_adj,!(rownames(counts_adj) %in% features_to_remove))
# re-perform support vector classification using the new, optimally caste-separating set of features
svm.optimal = svm.train(counts_clean_subsample,
referencelevel = "responsive",
svm.data.train,
svm.data.test,
crossfold = 3,
vstCheck = F)
print(paste0("Number of genes included in optimised model: ", nrow(counts_clean_subsample)))
print(paste0("Root mean cross-validation error rate for optimised model: ", sqrt(svm.optimal$validation_error)))
write.csv(data.frame(GeneID=row.names(counts_clean_subsample)),
"SVM_GeneIDs.csv",row.names=F)
fit.full <- list()
fit.optimal <- list()
metadata = read.csv("metadata.csv")
for(i in 1:10){
treatment = metadata[sample(nrow(metadata), 6), "ID"]
metadata0 <- metadata
metadata0[metadata0$ID%in%treatment,"Role"] =
paste(metadata0[metadata0$ID%in%treatment,"Role"],"test",sep="_")
svm.data.boot = metadata0[,c("ID","Role")]
svm.data.train.boot = subset(svm.data.boot, Role %in% c("responsive","unresponsive"))
svm.data.test.boot = subset(svm.data.boot, !(Role %in% c("responsive","unresponsive")))
fit.full[[i]] <- svm.train(counts_adj,
svm.data.train.boot,
svm.data.test.boot,
crossfold = 3,
vstCheck = F,
referencelevel = "responsive")[["prediction"]]
fit.optimal[[i]] <- svm.train(counts_clean_subsample,
referencelevel = "responsive",
svm.data.train.boot,
svm.data.test.boot,
crossfold = 3,
vstCheck = F)[["prediction"]]
}
dat.full.fit <- data.frame(value=as.numeric(unlist(fit.full)),
ID=names(unlist(fit.full)))
dat.full.fit <- left_join(dat.full.fit,metadata,"ID")
dat.full.fit <- dat.full.fit[,1:3]
dat.full.fit$fit <- "full"
dat.optimal.fit <- data.frame(value=as.numeric(unlist(fit.optimal)),
ID=names(unlist(fit.optimal)))
dat.optimal.fit <- left_join(dat.optimal.fit,metadata,"ID")
dat.optimal.fit <- dat.optimal.fit[,1:3]
dat.optimal.fit$fit <- "optimal"
dat.fit <- rbind(dat.optimal.fit,dat.full.fit)
dat.fit$Role <- factor(dat.fit$Role,
levels=c("unresponsive","responsive"))
dat.fit$fit <- factor(dat.fit$fit,
levels=c("full","optimal"))
levels(dat.fit$fit) <- c("Full","Optimal")
g <- ggplot(dat.fit, aes(x=fit, y=value, color=Role,group=Role)) +
geom_point(position = position_jitterdodge(jitter.width = .05,
jitter.height = .005,
dodge.width=.75),size=3) +
scale_color_manual(values=alpha(c("blue", "red"), 0.2),
guide = guide_legend(override.aes = list(alpha=1))) +
ylab("Classifier Estimate") + xlab("Model") +
geom_point(stat="summary",size=3,
position=position_dodge(0.75),color="black") +
geom_errorbar(stat="summary",position=position_dodge(0.75),width=.25,
color="black") +
scale_y_continuous(breaks=c(0,.25,.5,.75,1),limits=c(0,1)) +
theme_prism()
g
ggsave("figS4.png",g,width=6,height=5,dpi=300)
sessionInfo()