-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfer_gene-free_relapse_signature.R
384 lines (298 loc) · 15.5 KB
/
infer_gene-free_relapse_signature.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
################
# Author : Thi Ngoc Ha Nguyen
# Date : 04/10/2020
# Email : [email protected]
################
rm(list=ls())
set.seed(12678)
################
# Load libraries
################
library(data.table)
################
# Load list of functions
################
source("useful_functions.R")
#######################################################################
## scripts
#######################################################################
# Discovery data using top 500 contigs
dir.discovery <- "Data_discovery/Relapse/"
topContig <- paste0(dir.discovery, "top500_contig_merge_norm.nb5.tsv")
sampleTCGA <- paste0(dir.discovery, "sample_conditions.tsv")
# Validation ICGC data
dir.validation <- "Data_validation/Relapse/"
sampleICGC <- paste0(dir.validation, "sample_cond_icgc.tsv")
kmerICGC <- paste0(dir.validation, "raw-counts.tsv.gz")
totalKmersICGC <- paste0(dir.validation, "sum_counts_icgc.tsv")
# Validation Stello data
sampleStelloo <- paste0(dir.validation, "sample_cond_stelloo.tsv")
kmerStelloo <- paste0(dir.validation, "raw-counts-Stello-Relapse-3-3.tsv.gz")
totalKmersStelloo <- paste0(dir.validation, "sum_counts_stelloo.tsv")
# Number of time for sampling
NUM_RUNS=100
# Directory of kmerFilter tool
dir.kmerFilter <- "/store/EQUIPES/SSFA/MEMBERS/thi-ngoc-ha.nguyen/Haoliang_Find_Kmer/kmer-filter/kmerFilter"
# Directory of BLAST database
dir.blastDB <- "/store/EQUIPES/SSFA/Index/Gencode/gencode.v34.transcripts.fa"
# Directory to store result
dir.store <- paste0("Result_infer_signature/Relapse/gene_free")
dir.create(file.path(dir.store), showWarnings = FALSE, recursive = TRUE)
############################################################################################
evaluate_contig <- function(ctgMappDis, dataSigDis, sigContigDis, sigContigDisPath, dataValidPath, samplesConditionValidPath, modeValid){
# Call the sript infer
cmdRun <- paste0("bash -c \"", dir.kmerFilter, " -n -k 31 -f ",
sigContigDisPath, " <(zcat ", dataValidPath ,") > ", dir.store,"/sig-contig-valid-", modeValid, ".tsv\"")
system(cmdRun)
#Total kmers in this signature
totalKmerContig <- fromContig2Kmers(sigDiscovery = sigContigDis)
# loading list of kmer found in ICGC dataset based on method
dataValidPath <- paste0(dir.store,"/sig-contig-valid-", modeValid, ".tsv")
if(modeValid == "icgc"){
countKmerValid <- normalizeContig(validCountPath = dataValidPath, libSizePath = totalKmersICGC )
}else{
countKmerValid <- normalizeContig(validCountPath = dataValidPath, libSizePath = totalKmersStelloo )
}
# Classify kmer belong to contig in ICGC dataset
kmerValid <- rownames(countKmerValid)
listClf <- list()
for(i in 1:length(totalKmerContig)){
tmp <- c()
for(kmer in kmerValid){
if(kmer%in%totalKmerContig[[i]])
tmp <- c(tmp, kmer)
}
listClf[[i]]<- tmp
}
names(listClf) <- sigContigDis[1:length(listClf)]
#Extract contigs present in validation dataset
listClf[sapply(listClf, is.null)] <- NULL
# Calculate the count for each contig - based on mean count
dataValidRepre <- data.frame(matrix(ncol = dim(countKmerValid)[2], nrow = 0))
for(i in 1:length(listClf)){
frame <- countKmerValid[c(listClf[[i]]),]
repreCtg <- apply(data.matrix(frame),2, FUN = median)
dataValidRepre <- rbind(dataValidRepre, repreCtg)
}
rownames(dataValidRepre) <- names(listClf)
colnames(dataValidRepre) <- names(countKmerValid)
strInfo <-paste("Contig signature include:", length(sigContigDis),"contigs that are created", length(unlist(totalKmerContig)),"kmer(s)\n",
"There are: ",length(kmerValid),"kmers found in validation and belong to",length(listClf),"contigs")
print(strInfo)
name.file <- paste0(dir.store, "/info-", modeValid, ".txt")
write.csv(strInfo, name.file)
probesID <- rownames(dataValidRepre)
dataSignatureValid <- dataValidRepre
########## processing of conditions ##############
#create sample condition for signature
samplesConditionValid<-as.data.frame(fread(samplesConditionValidPath,sep="\t", header= FALSE ,check.names=F))
names(samplesConditionValid)<-c("Sample","condition")
for (i in 1:dim(samplesConditionValid)[1]){
if(samplesConditionValid$condition[i] == "no"){
samplesConditionValid$condition[i] = "No"
}
if(samplesConditionValid$condition[i] == "yes"){
samplesConditionValid$condition[i] = "Yes"
}
}
#Transpose
dataSignatureValid <- as.data.frame(t(dataSignatureValid))
dataSignatureValid$Sample <- row.names(dataSignatureValid)
# Mapping count table and condition in validation set
dataValid<-merge(dataSignatureValid,samplesConditionValid,
by.x="Sample",
by.y="Sample",
all.x=TRUE,
all.y=FALSE)
dataValid$condition <- factor(dataValid$condition)
# dataframe of signature in ICGC dataset
dataSigValid <- dataValid[,c("condition", probesID)]
dataSave <- dataValid[,c("Sample", "condition", probesID)]
print(paste("List of contig from TCGA found in", modeValid, "validation:"))
print(probesID)
ctgValidName <- c()
# Mapping contig to genome
for ( i in 1: length(probesID)){
ctgValidName[i] = ctgMappDis[ctgMappDis$contig == probesID[i],]$UniqueName
print(ctgValidName[i])
}
names(dataSigValid) <- c("condition", ctgValidName)
names(dataSave) <- c("Sample","condition", ctgValidName)
########## signature performance ##############
#Combine data in TCGA and validation set
absentCtg <- setdiff(names(dataSigDis),names(dataSigValid))
print(paste(length(absentCtg), "contigs are not found in", modeValid, "valid set:"))
print(absentCtg)
if (length(absentCtg) != 0){
for (contig in absentCtg){
dataSigValid$contig <- 0
names(dataSigValid)[length(names(dataSigValid))] <- contig
}
}
dataSigValid <- dataSigValid[,names(dataSigDis)]
resSignROC <- takeDataReturnAUC(frameTrain = dataSigDis, frameTest = dataSigValid, absentContig = absentCtg, status = "relapse")
resSignPR <- takeDataReturnPR(frameTrain = dataSigDis, frameTest = dataSigValid, absentContig = absentCtg, status = "relapse")
return (list(resSignROC, dataSave, resSignPR))
}
################################
pipeline <- function(topProbesPath, samplesConditionDisPath, numruns){
# loading top genes/contigs in TCGA discovery dataset based on approach level
countTopProbe <- as.data.frame(fread(topProbesPath, sep="\t", header = TRUE))
countTopProbe <- data.frame(row.names = countTopProbe$feature, countTopProbe[,-c(1:4)], check.names=F)
########## processing of conditions ##############
samplesConditionDis<-as.data.frame(fread(samplesConditionDisPath,sep="\t", header= FALSE ,check.names=F))
names(samplesConditionDis)<-c("Sample","condition")
#Transpose
countTopProbe <- as.data.frame(t(countTopProbe))
#Change the double value in right format
for (i in 1:dim(countTopProbe)[2]){
countTopProbe[,i] <- as.double(as.character(countTopProbe[,i]))
}
countTopProbe$Sample <- row.names(countTopProbe)
# Mapping count table and condition in TCGA
dataTCGA<-merge(countTopProbe,samplesConditionDis,
by.x="Sample",
by.y="Sample",
all.x=TRUE,
all.y=FALSE)
dataTCGA$condition <- factor(dataTCGA$condition)
########## processing of feature selection ##############
# Finding the signature in TCGA discovery dataset
dataModel <-dataTCGA[,-which(names(dataTCGA) %in% c("condition","Sample"))]
dataModel$condition <-factor(dataTCGA$condition)
# Set threshold for Lasso logistic regression
th_lasso = 0.5
sigTCGA <- extractSignatureStb(dataModel, thres = th_lasso)
print("List of contig in the signature:")
print(sigTCGA)
# Mapping contig to genenome
# Create fa file
insertStr <- paste0(">ctg_",seq(1:length(sigTCGA)))
preFA <- as.vector(t(cbind(insertStr, matrix(sigTCGA, length(sigTCGA), byrow=T))))
name.file <- paste0(dir.store, "/sig-contig-tcga.fa")
write.table(preFA, file = name.file, quote = FALSE, row.names = FALSE, col.names = FALSE)
# Table assig ctg -> contig
assignCtg <- as.data.frame(cbind(paste0("ctg_",seq(1:length(sigTCGA))), sigTCGA))
names(assignCtg) <- c("Alias", "contig")
# Call blast command
cmdRun <- paste0("bash -c \"blastn -db ", dir.blastDB, " -query ",
dir.store, "/sig-contig-tcga.fa -evalue 1e-3 -max_target_seqs 1 -outfmt 6 -max_hsps 1 -word_size 10 ",
# '|awk -F"\t"', "'{print $1" , '"\t"', "$2}'", '|awk -F"|"', " '{print $1",' "\t" ', "$6}'",'|awk -F"\t"'," '{print $1",' "\t"', " $3}'",
" > ", dir.store,"/sig-contig-tcga-geneName.tsv\"")
system(cmdRun)
#Open file geneName
contigName <- as.data.frame(fread(input = paste0(dir.store,"/sig-contig-tcga-geneName.tsv"), sep = "\t", header = FALSE))
contigName <- contigName[,c(1,2)]
names(contigName) <- c("Alias", "Name")
for (i in 1:dim(contigName)[1]){
contigName$Name[[i]] = unlist(strsplit(contigName$Name[[i]],"\\|"))[6]
}
# Mapping
mapAlliasName <- merge(assignCtg, contigName,
by.x = "Alias",
by.y = "Alias",
all.x = TRUE,
all.y = FALSE)
uniqueGene <- c()
for (i in 1:dim(mapAlliasName)[1]){
if(is.na(mapAlliasName$Name[i])){
uniqueGene[i] = mapAlliasName$Alias[i]
}else{
if(mapAlliasName$Name[i] %in% uniqueGene){
uniqueGene[i] = paste0(mapAlliasName$Name[i], ".v",i)
}else{
uniqueGene[i] = mapAlliasName$Name[i]
}
}
}
mapAlliasName$UniqueName = uniqueGene
contigMapp <- mapAlliasName[, c("contig", "UniqueName")]
#Save to file
name.file <- paste0(dir.store, "/sig-contig-tcga.tsv")
write.table(mapAlliasName[, c("contig")], file = name.file, sep = "\t", quote = FALSE, row.names = FALSE, col.names = FALSE)
name.file <- paste0(dir.store, "/sig-contig-genemap-tcga.tsv")
write.table(contigMapp, file = name.file, sep = "\t", quote = FALSE, row.names = FALSE, col.names = TRUE)
# dataframe of signature in TCGA dataset
dataSigTCGA <- dataTCGA[,c("condition", sigTCGA)]
#Order the geneName base on the contig sequence
# Transpose contigMapp with colname is contig
contigName <- as.data.frame(t(contigMapp[,-c(1)]))
colnames(contigName) <- contigMapp$contig
contigName = as.data.frame(contigName[, sigTCGA])
ctgNameDis = unname(unlist(contigName[1,]))
print("List of contig mapping to genome: ")
print(ctgNameDis)
name.file <- paste0(dir.store, "/data-sig-contig-tcga.tsv")
dataSaveTCGA <- dataTCGA[,c("Sample","condition",sigTCGA)]
colnames(dataSaveTCGA) <- c("Sample", "condition", ctgNameDis)
write.table(dataSaveTCGA, file = name.file, sep = "\t", quote = FALSE, row.names = FALSE)
names(dataSigTCGA) <- c("condition", ctgNameDis)
########## finding signature in validation set ##############
#Valid in ICGC
resSigICGC <- evaluate_contig(ctgMappDis = contigMapp, dataSigDis = dataSigTCGA, sigContigDis = sigTCGA, sigContigDisPath = paste0(dir.store, "/sig-contig-tcga.tsv"),
dataValidPath = kmerICGC, samplesConditionValidPath = sampleICGC, modeValid = "icgc")
#Store data frame valid
name.file <- paste0(dir.store, "/data-sig-contig-icgc.tsv")
write.table(resSigICGC[[2]], file = name.file, sep = "\t", quote = FALSE, row.names = FALSE, col.names = TRUE)
finalTab <- rbind(resSigICGC[[1]][[1]], resSigICGC[[1]][[2]], resSigICGC[[1]][[3]], resSigICGC[[1]][[4]],
resSigICGC[[1]][[5]], resSigICGC[[1]][[6]], resSigICGC[[1]][[7]], resSigICGC[[1]][[8]],
resSigICGC[[1]][[9]], resSigICGC[[1]][[10]], resSigICGC[[1]][[11]], resSigICGC[[1]][[12]])
rownames(finalTab) <- c(
"Mean ROC_AUC cv (TCGA)", "SD ROC_AUC cv (TCGA)",
"Mean ROC_AUC down cv (TCGA)", "SD ROC_AUC down cv (TCGA)",
"Mean ROCAUC up cv (TCGA)", "SD ROC_AUC up cv (TCGA)",
"ROC_AUC sig contig in ICGC", "PrAUC sig contig in ICGC",
"ROC_AUC down sig contig in ICGC", "PrAUC down sig contig in ICGC",
"ROC_AUC up sig contig in ICGC", "PrAUC up sig contig in ICGC")
print(finalTab)
name.file <- paste0(dir.store, "/Roc_auc-sig-contig-icgc.tsv")
write.table(finalTab, file = name.file, quote = FALSE, row.name = TRUE, col.names = FALSE)
finalTab <- rbind(resSigICGC[[3]][[1]], resSigICGC[[3]][[2]], resSigICGC[[3]][[3]], resSigICGC[[3]][[4]],
resSigICGC[[3]][[5]], resSigICGC[[3]][[6]], resSigICGC[[3]][[7]], resSigICGC[[3]][[8]],
resSigICGC[[3]][[9]], resSigICGC[[3]][[10]], resSigICGC[[3]][[11]], resSigICGC[[3]][[12]])
rownames(finalTab) <- c(
"Mean PR_AUC cv (TCGA)", "SD PR_AUC cv (TCGA)",
"Mean PR_AUC down cv (TCGA)", "SD PR_AUC down cv (TCGA)",
"Mean PR_AUC up cv (TCGA)", "SD PR_AUC up cv (TCGA)",
"ROC_AUC sig contig in ICGC", "PrAUC sig contig in ICGC",
"ROC_AUC down sig contig in ICGC", "PrAUC down sig contig in ICGC",
"ROC_AUC up sig contig in ICGC", "PrAUC up sig contig in ICGC")
print(finalTab)
name.file <- paste0(dir.store, "/Pr_auc-sig-contig-icgc.tsv")
write.table(finalTab, file = name.file, quote = FALSE, row.name = TRUE, col.names = FALSE)
# Valid in Stelloo
resSigStelloo <- evaluate_contig(ctgMappDis = contigMapp, dataSigDis = dataSigTCGA,sigContigDis = sigTCGA, sigContigDisPath = paste0(dir.store, "/sig-contig-tcga.tsv"),
dataValidPath = kmerStelloo, samplesConditionValidPath = sampleStelloo, modeValid = "stello")
#Store data frame valid
name.file <- paste0(dir.store, "/data-sig-contig-stelloo.tsv")
write.table(resSigStelloo[[2]], file = name.file, sep = "\t", quote = FALSE, row.names = FALSE, col.names = TRUE)
finalTab <- rbind(resSigStelloo[[1]][[1]], resSigStelloo[[1]][[2]], resSigStelloo[[1]][[3]], resSigStelloo[[1]][[4]],
resSigStelloo[[1]][[5]], resSigStelloo[[1]][[6]], resSigStelloo[[1]][[7]], resSigStelloo[[1]][[8]],
resSigStelloo[[1]][[9]], resSigStelloo[[1]][[10]], resSigStelloo[[1]][[11]], resSigStelloo[[1]][[12]])
rownames(finalTab) <- c(
"Mean ROC_AUC cv (TCGA)", "SD ROC_AUC cv (TCGA)",
"Mean ROC_AUC down cv (TCGA)", "SD ROC_AUC down cv (TCGA)",
"Mean ROCAUC up cv (TCGA)", "SD ROC_AUC up cv (TCGA)",
"ROC_AUC sig contig in Stelloo", "PrAUC sig contig in Stelloo",
"ROC_AUC down sig contig in Stelloo", "PrAUC down sig contig in Stelloo",
"ROC_AUC up sig contig in Stelloo", "PrAUC up sig contig in Stelloo")
print(finalTab)
name.file <- paste0(dir.store, "/Roc_auc-sig-contig-stelloo.tsv")
write.table(finalTab, file = name.file, quote = FALSE, row.name = TRUE, col.names = FALSE)
finalTab <- rbind(resSigStelloo[[3]][[1]], resSigStelloo[[3]][[2]], resSigStelloo[[3]][[3]], resSigStelloo[[3]][[4]],
resSigStelloo[[3]][[5]], resSigStelloo[[3]][[6]], resSigStelloo[[3]][[7]], resSigStelloo[[3]][[8]],
resSigStelloo[[3]][[9]], resSigStelloo[[3]][[10]], resSigStelloo[[3]][[11]], resSigStelloo[[3]][[12]])
rownames(finalTab) <- c(
"Mean PR_AUC cv (TCGA)", "SD PR_AUC cv (TCGA)",
"Mean PR_AUC down cv (TCGA)", "SD PR_AUC down cv (TCGA)",
"Mean PR_AUC up cv (TCGA)", "SD PR_AUC up cv (TCGA)",
"ROC_AUC sig contig in Stelloo", "PrAUC sig contig in Stelloo",
"ROC_AUC down sig contig in Stelloo", "PrAUC down sig contig in Stelloo",
"ROC_AUC up sig contig in Stelloo", "PrAUC up sig contig in Stelloo")
print(finalTab)
name.file <- paste0(dir.store, "/Pr_auc-sig-contig-stelloo.tsv")
write.table(finalTab, file = name.file, quote = FALSE, row.name = TRUE, col.names = FALSE)
}
#################################
#Run the script to infer
resContig <- pipeline(topProbesPath = topContig, samplesConditionDisPath = sampleTCGA, numruns = NUM_RUNS)