-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseful_functions.R
513 lines (396 loc) · 14.4 KB
/
useful_functions.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
################
# Author : Thi Ngoc Ha Nguyen
# Date : 04/10/2020
# Email : [email protected]
################
rm(list=ls())
set.seed(12678)
################
# Load libraries
################
library(glmnet)
library(caret)
library(precrec)
library(parallel)
library(EnsDb.Hsapiens.v79)
library(MLmetrics)
library(edgeR)
################
# Load list of functions
################
################
# Functions for probe selection
################
################
## A) Function extracts signature
################
extractSignatureStb <- function(frame, thres){
# Extract signature
#
# Args:
# frame : Data frame containes selected probes
# thres :
# Returns :
# The signature of the stable selected probes
X <- data.matrix(frame[,which(!names(frame)%in% c("condition"))])
Y <- frame$condition
n <- nrow(X)
p <- ncol(X)
Gene <- colnames(X)
# Perform k-fold cross validation for glmnet to find optimal value of lambda
cvGlm <- cv.glmnet(x = X, y = Y, family = "binomial", alpha = 1, type.measure = "mse")
cores <- detectCores()
cl <- makeCluster(cores-2)
NUM_RUNS <- 2e3
stabsel <- function(i){
cat("+")
b_sort <- sort(sample(1:n,round(3*n/4)))
out <- glmnet(X[b_sort,],Y[b_sort], family = "binomial",
lambda=cvGlm$lambda.1se, alpha = 1, standardize = FALSE)
return(tabulate(which(out$beta[,ncol(out$beta)]!=0),p))
}
clusterEvalQ(cl, expr = c(library(glmnet)))
clusterExport(cl,c('stabsel','frame', 'NUM_RUNS','n','glmnet','cvGlm','X','Y','p'),envir=environment())
res.cum <- Reduce("+", parLapply(cl, 1:NUM_RUNS, stabsel))
stopCluster(cl)
prob.sel <- res.cum/NUM_RUNS
plot(sort(prob.sel))
gene.stabsel <- Gene[prob.sel >= thres]
return (gene.stabsel)
}
################
## B) Function creates list kmers from a contig
################
contig2Kmer <- function(contig, N_KMER){
# Create list of kmers
#
# Args:
# contig : String stores a contig
# N_KMER : Length of generated kmer
# Returns :
# List of k-mers with N_KMER length that derived from contig
listKmer <-c()
for(i in seq(1, nchar(contig)-N_KMER+1)){
listKmer = c(listKmer, substr(contig, i, i+N_KMER-1))
}
return(listKmer)
}
################
## C) Function creates reverse complement sequence
################
convertToReverComplement<-function(seq){
# Reverse complement sequence
#
# Args:
# seq : String stores a sequence
# Returns :
# A reverse complement sequence
bases=c("A","C","G","T")
uSeq<-unlist(strsplit(toupper(seq),NULL))
complete <- lapply(uSeq,function(bbb){
if(bbb=="A") compString<-"T"
if(bbb=="C") compString<-"G"
if(bbb=="G") compString<-"C"
if(bbb=="T") compString<-"A"
if(!bbb %in% bases) compString<-"N"
return(compString)
})
return(paste(rev(unlist(complete)),collapse=""))
}
################
## D) Function to create list -kmers from list of contigs
################
fromContig2Kmers <- function(sigDiscovery, kmer_len=31, lib_type ="unstranded"){
# Create list k-mers
#
# Args:
# sigDiscovery : List of probes
# Returns :
# List of probes and list of k-mers that are derived from each probe
vectorKmer = c()
listKmer = list()
for(i in 1:length(sigDiscovery)){
vectorKmer <- c(vectorKmer,contig2Kmer(sigDiscovery[i], kmer_len))
listKmer[[i]] = contig2Kmer(sigDiscovery[i], kmer_len)
}
# check revers completement
vectorCono = c()
for(kmer in vectorKmer){
rever <-convertToReverComplement(kmer)
if(lib_type == "unstranded"){
# Just keep cononical kmer
if(kmer<rever){
vectorCono <- c(vectorCono, kmer)
}else{
vectorCono <- c(vectorCono, rever)
}
}else{
vectorCono <- c(vectorCono, kmer, rever)
}
}
listCono = list()
for (i in 1:length(listKmer)){
tmp = c()
for (kmer in listKmer[[i]]){
rever <-convertToReverComplement(kmer)
if(lib_type =="unstranded"){
if(kmer<rever){
tmp = c(tmp,kmer)
}else{
tmp = c(tmp,rever)
}
}else{
tmp <- c(tmp,kmer,rever)
}
}
listCono[[i]]<-tmp
}
names(listCono)<-sigDiscovery
return(listCono)
}
###############
# E) Function calculater auc of signature in validation set
###############
takeDataReturnAUC<- function(frameTrain, frameTest, absentContig, status){
# Calculate auc of signature
#
# Args:
# frameTrain: Data frame containes selected probes in training set
# frameTest : Data frame containes selected probes in test set
# absentContig : List of absence contig in test set
#
# Results:
# AUC ROC curve in test set
# List coefficents for each probe in the signature
data.train <- as.data.frame(frameTrain)
data.test <- as.data.frame(frameTest)
# Set up control function for training
ctrl <- trainControl(method = "repeatedcv",
number = 10,
repeats = 20,
classProbs = TRUE,
summaryFunction = twoClassSummary,
savePredictions = TRUE)
# Use glm method in caret package
set.seed(5627)
glmFit <- train(condition ~ ., data = data.train,
method = "glm",
metric = "ROC",
family = "binomial",
preProc = c("center", "scale"),
trControl = ctrl)
# Use the same seed to ensure same cross-validation splits
ctrl$seeds <- glmFit$control$seeds
ctrl$sampling <- "down"
down_fit <- train(condition ~ ., data = data.train,
method = "glm",
metric = "ROC",
family = "binomial",
preProc = c("center", "scale"),
trControl = ctrl)
ctrl$sampling <- "up"
up_fit <- train(condition ~ ., data = data.train,
method = "glm",
metric = "ROC",
family = "binomial",
preProc = c("center", "scale"),
trControl = ctrl)
#Roc AUC for TCGA tranning dataset
aucMeanOrg <- round(mean(glmFit$resample$ROC), 3)
aucSdOrg<- round(sd(glmFit$resample$ROC), 3)
aucMeanDown <- round(mean(down_fit$resample$ROC), 3)
aucSdDown<- round(sd(down_fit$resample$ROC), 3)
aucMeanUp <- round(mean(up_fit$resample$ROC), 3)
aucSdUp<- round(sd(up_fit$resample$ROC), 3)
# Set coefficent of absent contig =0
if(length(absentContig)!=0){
for (i in 1:length(absentContig)){
if(grepl("-", absentContig[i], fixed = TRUE)){
absentContig[i] = paste0("`\\`", absentContig[i], "\\``")
}
}
for (contig in absentContig){
glmFit$finalModel$coefficients[contig] = 0
down_fit$finalModel$coefficients[contig] = 0
up_fit$finalModel$coefficients[contig] = 0
}
}
# Predition for test data
if(status == "risk"){
pred = predict(glmFit, data.test[,-1, drop = FALSE], type = "prob")[, "LR"]
pred_down = predict(down_fit, data.test[,-1, drop = FALSE], type = "prob")[, "LR"]
pred_up = predict(up_fit, data.test[,-1, drop = FALSE], type = "prob")[, "LR"]
}
if(status == "relapse"){
pred = predict(glmFit, data.test[,-1, drop = FALSE], type = "prob")[, "No"]
pred_down = predict(down_fit, data.test[,-1, drop = FALSE], type = "prob")[, "No"]
pred_up = predict(up_fit, data.test[,-1, drop = FALSE], type = "prob")[, "No"]
}
# Calculate auc for ROC curve
resAUC <- auc(evalmod(scores = pred, labels = data.test$condition))
resAUC_down <- auc(evalmod(scores = pred_down, labels = data.test$condition))
resAUC_up <- auc(evalmod(scores = pred_up, labels = data.test$condition))
rocAUC <- round(resAUC$aucs[1], 3)
prAUC <- round(resAUC$aucs[2], 3)
rocAUC_down <- round(resAUC_down$aucs[1], 3)
prAUC_down <- round(resAUC_down$aucs[2], 3)
rocAUC_up <- round(resAUC_up$aucs[1], 3)
prAUC_up <- round(resAUC_up$aucs[2], 3)
if(rocAUC<0.5){ # Reverse roc value in case it lower than 0.5
rocAUC = 1 - rocAUC
}
if(rocAUC_down<0.5){ # Reverse roc value in case it lower than 0.5
rocAUC_down = 1 - rocAUC_down
}
if(rocAUC_up<0.5){ # Reverse roc value in case it lower than 0.5
rocAUC_up = 1 - rocAUC_up
}
# Save predicted scores
scores <- pred
# Save observed labels
label <- data.test$condition
return(list(aucMeanOrg, aucSdOrg, aucMeanDown, aucSdDown, aucMeanUp, aucSdUp,
rocAUC, prAUC ,rocAUC_down, prAUC_down, rocAUC_up, prAUC_up))
}
################
## G) Function to take gene name from gene ensembl
################
fromGeneIDtakeGenName <- function(geneEnsembl){
# Infer gene symbol from gene ensembl
#
# Args:
# geneEnsembel: String stores gene ensembl
# Results:
# Gene symbol
geneAnno <- gsub("\\..*","",geneEnsembl)
geneAnno <- ensembldb::select(EnsDb.Hsapiens.v79, keys= geneAnno, keytype = "GENEID", columns = c("SYMBOL","GENEID"))
return (geneAnno)
}
################
## G) Function to take gene name from gene ensembl
################
takeDataReturnPR<- function(frameTrain, frameTest, absentContig, status){
# Calculate auc of signature
#
# Args:
# frameTrain: Data frame containes selected probes in training set
# frameTest : Data frame containes selected probes in test set
# absentContig : List of absence contig in test set
#
# Results:
# AUC ROC curve in test set
# List coefficents for each probe in the signature
data.train <- as.data.frame(frameTrain)
data.test <- as.data.frame(frameTest)
# Set up control function for training
ctrl <- trainControl(method = "repeatedcv",
number = 10,
repeats = 20,
classProbs = TRUE,
summaryFunction = prSummary,
savePredictions = TRUE)
# Use glm method in caret package
set.seed(5678)
glmFit <- train(condition ~ ., data = data.train,
method = "glm",
metric = "AUC",
family = "binomial",
preProc = c("center", "scale"),
trControl = ctrl)
# Use the same seed to ensure same cross-validation splits
ctrl$seeds <- glmFit$control$seeds
ctrl$sampling <- "down"
down_fit <- train(condition ~ ., data = data.train,
method = "glm",
metric = "AUC",
family = "binomial",
preProc = c("center", "scale"),
trControl = ctrl)
ctrl$sampling <- "up"
up_fit <- train(condition ~ ., data = data.train,
method = "glm",
metric = "AUC",
family = "binomial",
preProc = c("center", "scale"),
trControl = ctrl)
#Roc AUC for TCGA tranning dataset
aucMeanOrg <- round(mean(glmFit$resample$AUC), 3)
aucSdOrg<- round(sd(glmFit$resample$AUC), 3)
aucMeanDown <- round(mean(down_fit$resample$AUC), 3)
aucSdDown<- round(sd(down_fit$resample$AUC), 3)
aucMeanUp <- round(mean(up_fit$resample$AUC), 3)
aucSdUp<- round(sd(up_fit$resample$AUC), 3)
# Set coefficent of absent contig =0
if(length(absentContig)!=0){
for (i in 1:length(absentContig)){
if(grepl("-", absentContig[i], fixed = TRUE)){
absentContig[i] = paste0("`\\`", absentContig[i], "\\``")
}
}
for (contig in absentContig){
glmFit$finalModel$coefficients[contig] = 0
down_fit$finalModel$coefficients[contig] = 0
up_fit$finalModel$coefficients[contig] = 0
}
}
# Predition for test data
if(status == "risk"){
pred = predict(glmFit, data.test[,-1, drop = FALSE], type = "prob")[, "LR"]
pred_down = predict(down_fit, data.test[,-1, drop = FALSE], type = "prob")[, "LR"]
pred_up = predict(up_fit, data.test[,-1, drop = FALSE], type = "prob")[, "LR"]
}
if(status == "relapse"){
pred = predict(glmFit, data.test[,-1, drop = FALSE], type = "prob")[, "No"]
pred_down = predict(down_fit, data.test[,-1, drop = FALSE], type = "prob")[, "No"]
pred_up = predict(up_fit, data.test[,-1, drop = FALSE], type = "prob")[, "No"]
}
# Calculate auc for ROC curve
resAUC <- auc(evalmod(scores = pred, labels = data.test$condition))
resAUC_down <- auc(evalmod(scores = pred_down, labels = data.test$condition))
resAUC_up <- auc(evalmod(scores = pred_up, labels = data.test$condition))
rocAUC <- round(resAUC$aucs[1], 3)
prAUC <- round(resAUC$aucs[2], 3)
rocAUC_down <- round(resAUC_down$aucs[1], 3)
prAUC_down <- round(resAUC_down$aucs[2], 3)
rocAUC_up <- round(resAUC_up$aucs[1], 3)
prAUC_up <- round(resAUC_up$aucs[2], 3)
if(rocAUC<0.5){ # Reverse roc value in case it lower than 0.5
rocAUC = 1 - rocAUC
}
if(rocAUC_down<0.5){ # Reverse roc value in case it lower than 0.5
rocAUC_down = 1 - rocAUC_down
}
if(rocAUC_up<0.5){ # Reverse roc value in case it lower than 0.5
rocAUC_up = 1 - rocAUC_up
}
# Save predicted scores
scores <- pred
# Save observed labels
label <- data.test$condition
return(list(aucMeanOrg, aucSdOrg, aucMeanDown, aucSdDown, aucMeanUp, aucSdUp,
rocAUC, prAUC ,rocAUC_down, prAUC_down, rocAUC_up, prAUC_up))
}
################
normalizeContig <-function(validCountPath, libSizePath){
# CBP normalization for kmer are found in validation set
#
# Args:
# validCountPath: Path to store validation data
# libSizePath: Path to store total of kmers for each sample in validation set
# Results:
# Dataframe store normalized kmer counts are found in validation set
# Dataframe stores kmers count that are found in validation set
countKmerValid <- as.data.frame(fread(validCountPath, sep="\t", header = TRUE))
rownames(countKmerValid) <- countKmerValid$tag
countKmerValid <- countKmerValid[,-1]
# Dataframe, each line is a sample and its corresponding total of kmers
inforKmerValid <- as.data.frame(fread(libSizePath, sep="\t", header = FALSE))
names(inforKmerValid) <- c("Sample", "Total_kmers")
rownames(inforKmerValid) <- inforKmerValid$Sample
#Sort libSize base on sample name
inforKmerValid <- inforKmerValid[colnames(countKmerValid),]
libSizeValid <- inforKmerValid$Total_kmers
# compute logCPB
logCpbValid <- log(countKmerValid/expandAsMatrix(libSizeValid/1e+09, dim(countKmerValid))+1)
return (logCpbValid)
}