Download this file

413 lines (292 with data), 15.2 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
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
# ******************************************************************************
# FUNCTION: GSVA run, obtain eFDR | 12/02/2013 | Petri Pölönen
# ******************************************************************************
library(GSVA)
library(parallel)
library(snow)
# ******************************************************
# Running notes: create file, named anything*EXPNAME*anything*ENTITYDF.txt
# This file is needed to run in multiple folders
# contains at least NAME, ENTNAME columns
#*******************************************************
#******************** Experiment, output processing, modify ***********************************
# This should mach your entity data frame partially
# EXPNAME="AgilentG4502A_07"
# EXPNAME="IlluminaHiSeq_RNASeqV2_geneExp"
# EXPNAME="illuminahiseq_rnaseqv2"
EXPNAME="data9544"
# ENT2 contains names for entities, create this file, contains NAME, ENTNAME columns, which are needed
ENT2 <- read.delim(list.files(,paste(EXPNAME,"*.*ENTITYDF.txt", sep=""), ignore.case=T)[1], header=T, stringsAsFactors=F)
# limit to certain samples
LIMITTO=F
# make fm suitable features
FM_PROCESSING=F
PANCAN=F # all in ENTITYDF are run
CORES=3 # parallel for PANCAN
TYPE="data9544" # if PANCAN is F, specify type, this can be any NAME in ENTITYDF
#******************* GSVA parameters *******************************
METHOD="gsva" # ssgsea, plage, zscore
RNASEQ=F
PARALLEL_SZ=10 # parallel run, important for several geneset testing
FDR=T # estimate significance, get table of p-values, this takes long time
# if FDR TRUE
SIMULATION="_GSVA_permutation"
NUMSIM=100
PERMUTATE_SUBJECT=F
PERMUTATE_GENE=T
PERMUTATE_GENE_LEAN=F # compute for different number of genes
BOOTSTRAP=F
PVAL=T
#******************* Pathways to run ******************************
# which pathway to run
SINGLEPATHW="Combined_pathway_drug_signatures_2017.gmt"
SINGLEPATHW="CLL_genesets"
GENESETS="~/genesets/"
GENESETS="/research/groups/sysgen/PROJECTS/HEMAP/HEMAP/dat2figs/revision_2018/"
#******************************* After this, you should not need to change anything **************
mainDir=getwd()
FUNPATHW=function(PATHW, matrix, NAME, entname){
#**************************** load genesets *******************************************
PATHW=gsub(".gmt", "", SINGLEPATHW)
if(LIMITTO){
FILE="AML_GSE13159,GSE13204_BHSNE_mean-shift.txt"
A=read.delim(FILE, sep="\t", header=T, stringsAsFactors=F)
FILE=gsub(".txt", "", FILE)
LIMIT=A$ID
matrix=matrix[,colnames(matrix)%in%LIMIT]
}else{
FILE="all_samples"
}
file=paste(GENESETS, PATHW, ".gmt", sep="")
if(file.exists(paste0(GENESETS, PATHW, "_listA_tempfile.Rdata"))){
load(paste0(GENESETS, PATHW, "_listA_tempfile.Rdata"))
print("previously computed geneset list loaded")
print("if you want to re-calculate, rm file:")
print(paste0(GENESETS, PATHW, "_listA_tempfile.Rdata"))
}else{
# Geneset list
Onc.pathways=read.delim(file, stringsAsFactors = FALSE, header=F, col.names = paste("V",1:max(count.fields(file, sep = '\t'), na.rm = T)), fill = TRUE)
# Make list
listA=mclapply(1:length(Onc.pathways[,1]), function(i){A=as.character(Onc.pathways[i,3:length(Onc.pathways),])
B=A[!A==""&!A=="NA"]}, mc.cores=PARALLEL_SZ)
names(listA) <- Onc.pathways[,1]
save(listA, file=paste0(GENESETS, PATHW, "_listA_tempfile.Rdata"))
names(listA) <- Onc.pathways[,1]
}
if(FM_PROCESSING){
colnames(matrix)=gsub("\\.", "-", substr(colnames(matrix), 1, 15))
names(listA) <- paste("N:SAMP:", gsub(":", "", names(listA)), "_", "GSVA", ":::::", sep="")
}
if(length(listA)==1){
listA=append(listA, listA)
}
# remove duplicates just in case....
listA=listA[!duplicated(names(listA))]
A=unlist(mclapply(listA, function(pw)sum(rownames(matrix)%in%pw), mc.cores=3))
listA=listA[!A<5&!A>500]
print(paste0("filtering out ", sum(A<5), " genesets, size too small"))
print(paste0("filtering out ", sum(A>500), " genesets, size too large"))
print(paste(length(listA), "genesets after filtering"))
#************************************** GSVA run ******************************************************
if(file.exists(paste(NAME, "_", FILE, "_", PATHW, "_GSVA.Rdata", sep=""))){
# load matrix
load(paste(NAME, "_", FILE, "_", PATHW, "_GSVA.Rdata", sep=""))
listA=listA[names(listA)%in%rownames(gsva_es)]
print("previously calculated GSVA matrix loaded")
print("if you want to re-calculate, rm file:")
print(paste(NAME, "_", FILE, "_", PATHW, "_GSVA.Rdata", sep=""))
}else{
#****************** Gene Set Variation Analysis function --> matrix with enrichment scores for genesets in each sample *****************
if(BOOTSTRAP){
if(RNASEQ){
gsva_es <- gsva(as.matrix(matrix), method=METHOD, listA, mx.diff=F, tau=0.25, verbose=T, rnaseq=TRUE, min.sz=5, max.sz=500, no.bootstraps=NUMSIM, parallel.sz=PARALLEL_SZ)
}else{
gsva_es <- gsva(as.matrix(matrix), method=METHOD, listA, mx.diff=F, tau=0.25, verbose=T, rnaseq=F, min.sz=5, max.sz=500, no.bootstraps=NUMSIM, parallel.sz=PARALLEL_SZ)
}
# write permutations
write.table(gsva_es$bootstrap$p.vals.sign, file=paste(NAME,"_", PATHW, "_bootstrap_pvals_GSVA.txt", sep=""), sep="\t", col.names=T, row.names=T, quote=FALSE)
save(gsva_es, file=paste(NAME, "_", PATHW, "_bootstrap_object_GSVA.Rdata", sep=""))
gsva_es=gsva_es$es.obs
}else{
if(RNASEQ){
gsva_es <- gsva(as.matrix(matrix), listA, mx.diff=F, method=METHOD, tau=0.25, verbose=T, rnaseq=TRUE, parallel.sz=PARALLEL_SZ)$es.obs
}else{
gsva_es <- gsva(as.matrix(matrix), listA, mx.diff=F, method=METHOD, tau=0.25, verbose=T, rnaseq=F, parallel.sz=PARALLEL_SZ)$es.obs
}
listA=listA[names(listA)%in%rownames(gsva_es)]
}
# make fm ready feature table
if(FM_PROCESSING){
write.table(t(c("N:SAMP", as.character(colnames(gsva_es)))), file=paste(NAME, "_", FILE, "_", PATHW, "_GSVA.tsv", sep=""), sep="\t", col.names=F, row.names=F, quote=FALSE, append=F)
write.table(gsva_es, file=paste(NAME, "_", FILE, "_", PATHW, "_GSVA.tsv", sep=""), sep="\t", col.names=F, row.names=T, quote=FALSE, append=T)
write.table(gsva_es, file=paste(NAME, "_", FILE, "_", PATHW, "_GSVA.txt", sep=""), sep="\t", col.names=T, row.names=T, quote=FALSE)
}else{
write.table(gsva_es, file=paste(NAME, "_", FILE, "_", PATHW, "_GSVA.txt", sep=""), sep="\t", col.names=T, row.names=T, quote=FALSE)
}
save(gsva_es, file=paste(NAME, "_", FILE, "_", PATHW, "_GSVA.Rdata", sep=""))
print("GSVA done: ")
print(NAME)
}
#**************************************************** eFDR ********************************************************
if(FDR){
# eFDR my way
if(PERMUTATE_SUBJECT){
PERMTYPE="sampleperm"
# Sample permutation
for(i in 1:length(listA)){
FUN_SIM=function(id, i){
bootstrap.percent=0.632
n.samples=dim(matrix)[2]
bootstrap.nsamples <- floor(bootstrap.percent * n.samples)
ind=sample(n.samples, bootstrap.nsamples, replace=T)
sim_es <- gsva(matrix[,ind], listA[i], mx.diff=T, verbose=F, parallel.sz=PARALLEL_SZ)$es.obs
rownames(sim_es)=paste("per", id, sep="")
if(any((i/(NUMSIM/10))==(1:10))){
print(paste("permutation ", i, "/", NUMSIM," done", sep=""))
}
flush.console()
return(sim_es)
}
simulations=mclapply(1:NUMSIM, FUN_SIM, mc.cores=CORES, i)
simulations_comb=do.call("rbind", simulations)
write.table(simulations_comb, file=paste(NAME, "_", PATHW, "_", names(listA)[i], "_", SIMULATION, "_sampleperm.txt", sep=""), sep="\t", col.names=T, row.names=T, quote=FALSE)
print(paste("permutation done for ", names(listA)[i], sep=""))
}
}
if(PERMUTATE_GENE){
PERMTYPE="geneperm"
print("permuting gene labels:")
print(PERMTYPE)
# calculate for this sequence permutations, SIM_SEQUENCE2 is automaticly finding necessary permutation steps
SIM_SEQUENCE=c(5:500)
# Make list
list_genesets=mclapply(SIM_SEQUENCE, function(geneset_l){perm_genesets=lapply(1:NUMSIM, function(i, ...){random.list=rownames(matrix[sample(length(matrix[,1]), geneset_l), ])})
names(perm_genesets)=paste("per", 1:NUMSIM, "_geneset_length_", geneset_l, sep="")
return(perm_genesets)}, mc.cores=PARALLEL_SZ)
list_genesets=unlist(list_genesets, recursive = F)
if(RNASEQ){
sim_es <- gsva(as.matrix(matrix), method=METHOD, list_genesets, mx.diff=F, tau=0.25, verbose=T, rnaseq=T, parallel.sz=PARALLEL_SZ)$es.obs
}else{
sim_es <- gsva(as.matrix(matrix), method=METHOD, list_genesets, mx.diff=F, tau=0.25, verbose=T, parallel.sz=PARALLEL_SZ)$es.obs
}
save(sim_es, file=paste(NAME, "_", FILE, "_", PATHW, "_GSVA_genepermutations_lean_all.Rdata", sep=""))
sim_df=lapply(SIM_SEQUENCE, function(geneset_l){find=paste("_geneset_length_", geneset_l, "$", sep="")
sim_es[grep(find, rownames(sim_es)),]})
save(sim_df, file=paste(NAME, "_", FILE, "_", PATHW, "_GSVA_genepermutations_lean.Rdata", sep=""))
}
if(PERMUTATE_GENE_LEAN){
PERMTYPE="geneperm_lean"
print("permuting gene labels:")
print(PERMTYPE)
# calculate for this sequence permutations, SIM_SEQUENCE2 is automaticly finding necessary permutation steps
SIM_SEQUENCE1=c(5:20, 25, 30, 40, 50, 75, 100, 200, 300, 400, 500)
geneset_length=unlist(lapply(listA, length))
# round to interval
iround <- function(x, interval){
interval[ifelse(x < min(interval), 1, findInterval(x, interval))]
}
SIM_SEQUENCE2=unique(iround(geneset_length, sort(SIM_SEQUENCE1)))
if(length(SIM_SEQUENCE2)<length(SIM_SEQUENCE1)){
SIM_SEQUENCE=SIM_SEQUENCE2
}else{
SIM_SEQUENCE=SIM_SEQUENCE1
}
# Make list
list_genesets=mclapply(SIM_SEQUENCE, function(geneset_l){perm_genesets=lapply(1:NUMSIM, function(i, ...){random.list=rownames(matrix[sample(length(matrix[,1]), geneset_l), ])})
names(perm_genesets)=paste("per", 1:NUMSIM, "_geneset_length_", geneset_l, sep="")
return(perm_genesets)}, mc.cores=PARALLEL_SZ)
list_genesets=unlist(list_genesets, recursive = F)
if(RNASEQ){
sim_es <- gsva(as.matrix(matrix), method=METHOD, list_genesets, mx.diff=F, tau=0.25, verbose=T, rnaseq=T, parallel.sz=PARALLEL_SZ)$es.obs
}else{
sim_es <- gsva(as.matrix(matrix), method=METHOD, list_genesets, mx.diff=F, tau=0.25, verbose=T, parallel.sz=PARALLEL_SZ)$es.obs
}
save(sim_es, file=paste(NAME, "_", FILE, "_", PATHW, "_GSVA_genepermutations_lean.Rdata", sep=""))
sim_df=lapply(SIM_SEQUENCE, function(geneset_l){find=paste("_geneset_length_", geneset_l, "$", sep="")
sim_es[grep(find, rownames(sim_es)),]})
save(sim_df, file=paste(NAME, "_", FILE, "_", PATHW, "_GSVA_genepermutations_lean.Rdata", sep=""))
}
if(PVAL){
print("computing eFDR for observed vs permuted score")
# find intersect
find=intersect(names(listA), rownames(gsva_es))
gsva_es=gsva_es[rownames(gsva_es)%in%find,]
listA=listA[names(listA)%in%find]
geneset_length=unlist(lapply(listA, length))
# round to interval
iround <- function(x, interval){
interval[ifelse(x < min(interval), 1, findInterval(x, interval))]
}
rounded_geneset=iround(geneset_length, sort(SIM_SEQUENCE))
# go through each geneset scores, find corresponding geneset length and compute eFDR
FUN_SIM=function(i){
set_length=rounded_geneset[i] # geneset length
permutation_df=sim_df[[which(SIM_SEQUENCE%in%set_length)]] # which permuation is accessed
observed_scores=as.numeric(gsva_es[i,]) # observed scores
#*********** go through observed scores and compute FDR *****************
m=unlist(lapply(1:length(observed_scores), function(j){
observed_score=observed_scores[j]
test=observed_score>0
if(test){
length(which(permutation_df[,j]>observed_score))/length(permutation_df[,1])
}else{
length(which(permutation_df[,j]<observed_score))/length(permutation_df[,1])
}
}))
#************************************************************************
return(signif(m,2))
}
m=mclapply(1:length(rounded_geneset), FUN_SIM, mc.cores=PARALLEL_SZ)
m_comb=as.matrix(do.call("rbind", m))
colnames(m_comb)=colnames(gsva_es)
rownames(m_comb)=rownames(gsva_es)
write.table(m_comb, file=paste(NAME, "_", PATHW, "_GSVA_", PERMTYPE, "_eFDR.txt", sep=""), sep="\t", col.names=T, row.names=T, quote=FALSE)
save(m_comb, file=paste(NAME, "_", PATHW, "_GSVA_", PERMTYPE, "_eFDR.Rdata", sep=""))
print("computing eFDR for observed vs permuted score: Done")
#**************************** make cutoff file *****************************
# round to interval
pw_length=data.frame(pw_newname=names(listA), geneset_length, rounded_geneset, stringsAsFactors=F)
sim_df2=do.call(rbind, sim_df)
sim_df2=sim_df2[!duplicated(rownames(sim_df2)),]
geneset_l=unique(gsub("*.*_geneset_length_", "", rownames(sim_df2)))
vals_gsva=do.call(rbind, mclapply(geneset_l, function(geneset_l){
df=sim_df2[gsub("*.*_geneset_length_", "", rownames(sim_df2))%in%geneset_l,]
B=sort(as.numeric(df), decreasing=T)
l=length(B)
c(geneset_l, B[0.05*l], B[l-0.05*l],B[0.01*l], B[l-0.01*l], B[0.001*l], B[l-0.001*l])
}, mc.cores=CORES))
vals_gsva=vals_gsva[order(as.numeric(vals_gsva[,1])),]
cutoffs=do.call(rbind, lapply(pw_length$rounded_geneset, function(v){vals_gsva[vals_gsva[,1]%in%v,]}))
df_all=cbind(pw_length, cutoffs[,2:7])
colnames(df_all)=c("name", "geneset_length", "geneset_length_rounded", "p_0.05_pos", "p_0.05_neg", "p_0.01_pos", "p_0.01_neg", "p_0.001_pos", "p_0.001_neg")
df_all=df_all[df_all[,1]%in%rownames(gsva_es),]
df_all=df_all[df_all[,2]>=5,]
write.table(df_all, paste0(NAME, "_", PATHW, "_pw_FDR_cutoffs_GSVA.txt"), sep="\t", quote=F, col.names=T, row.names=F)
}
}
}
# run per type
FUN=function(i){
entname=ENT2$ENTNAME[i]
NAME=ENT2$NAME[i]
# Set wd and create directory
subDir <- NAME
dir.create(file.path(mainDir, subDir), recursive=T, showWarnings = FALSE)
if(grepl("RData|Rdata", entname)){
matrix <- get(load(entname))
}else{
matrix=data.matrix(read.delim(entname, header=T, row.names=1, stringsAsFactors=F))
}
print("Data matrix loaded:")
print(NAME)
# outputs to GSVA folder
outDir <- paste(NAME, "/GSVA/", sep="")
dir.create(file.path(mainDir, outDir), showWarnings = FALSE)
setwd(file.path(mainDir, outDir))
log=FUNPATHW(SINGLEPATHW, matrix, NAME, entname)
}
if(PANCAN){
log=mclapply(1:length(ENT2$NAME), FUN, mc.cores=CORES)
}else{
log=lapply(which(ENT2$NAME%in%TYPE), FUN)
}