-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy path7-extract_plier_pathways.R
488 lines (395 loc) · 14.6 KB
/
7-extract_plier_pathways.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
# Steven Foltz Nov 2021
# The purpose of this analysis is to use PLIER to identify expression pathways
# in our data, and quantify the rate of return for significant PLIER pathways
# for data coming from different normalization methods and titration levels.
#
# USAGE: Rscript 7-extract_plier_pathways.R --cancer_type --seed --ncores
option_list <- list(
optparse::make_option("--cancer_type",
default = NA_character_,
help = "Cancer type"
),
optparse::make_option("--seed",
default = 8934,
help = "Random seed"
),
optparse::make_option("--ncores",
default = NA_integer_,
help = "Set the number of cores to use"
),
optparse::make_option("--permute",
action = "store_true",
default = FALSE,
help = "Permute the pathway-gene relationship matrix")
)
opt <- optparse::parse_args(optparse::OptionParser(option_list = option_list))
source(here::here("util/option_functions.R"))
check_options(opt)
# load libraries
suppressMessages(source(here::here("load_packages.R")))
source(here::here("util", "normalization_functions.R"))
source(here::here("util", "color_blind_friendly_palette.R"))
# set options
cancer_type <- opt$cancer_type
file_identifier <- str_c(cancer_type, "subtype", sep = "_") # assuming subtype
ncores <- min(parallel::detectCores() - 1,
opt$ncores,
na.rm = TRUE)
permute <- opt$permute
# set seed
initial.seed <- opt$seed
set.seed(initial.seed)
message(paste("\nPLIER initial seed set to:", initial.seed))
# define directories
data.dir <- here::here("data")
norm.data.dir <- here::here("normalized_data")
res.dir <- here::here("results")
plot.dir <- here::here("plots")
plot.data.dir <- file.path(plot.dir, "data")
# define input files
norm.train.files <- file.path(
norm.data.dir,
list.files(norm.data.dir,
pattern = paste0(
file_identifier,
"_array_seq_train_titrate_normalized_list_"
)
)
)
# define output files
PLIER_objects_filename <- file.path(
res.dir,
str_c(file_identifier, "_PLIER_objects_list",
ifelse(permute, # distinguish between permuted/not permuted output files
".permuted.rds",
".rds"))
)
pathways_filename <- file.path(
plot.data.dir,
str_c(file_identifier, "_PLIER_pathways",
ifelse(permute, # distinguish between permuted/not permuted output files
".permuted.tsv",
".tsv"))
)
plot_data_filename <- file.path(
plot.data.dir,
str_c(file_identifier, "_PLIER_jaccard",
ifelse(permute, # distinguish between permuted/not permuted output files
".permuted.tsv",
".tsv"))
)
#### set up PLIER data ---------------------------------------------------------
data(bloodCellMarkersIRISDMAP)
data(canonicalPathways)
data(oncogenicPathways)
data(svmMarkers)
all.paths <- PLIER::combinePaths(
bloodCellMarkersIRISDMAP,
canonicalPathways,
oncogenicPathways,
svmMarkers
)
PLIER_pathways <- colnames(all.paths)
#### Function for converting column to row names -------------------------------
convert_row_names <- function(expr, cancer_type) {
# If the cancer type is GBM, convert ENSG to gene symbols
# then convert the gene column to rownames
#
# Inputs: gene expression matrix with genes in first column, and cancer type
# Returns: modified gene expression matrix with gene names as row names
if ("gene" %in% colnames(expr)) { # check that gene column exists
if (cancer_type == "GBM") {
expr <- expr %>%
mutate(gene = ensembldb::select(EnsDb.Hsapiens.v86::EnsDb.Hsapiens.v86,
keys = as.character(gene),
keytype = "GENEID",
columns = "SYMBOL"
)$SYMBOL)
}
column_to_rownames(expr,
var = "gene"
)
} else { # return NULL when no gene column exists (Seurat)
return(NULL)
}
}
#### Failure of PLIER to converge may manifest in different error messages...
check_plier_failure_to_converge <- function(plier_result) {
# if the plier result contained an error message
if ("message" %in% names(plier_result)) {
# and if that error massage refers to convergence failure directly or indirectly
if (str_detect(plier_result$message, "system is computationally singular") |
str_detect(plier_result$message, "subscript out of bounds")) {
NA # return NA
} else { # PLIER failed for another reason and we need to know about that
stop("PLIER run failed for reason other than system is computationally singular")
}
} else {
plier_result # return the plier result as is
}
}
#### Functions to get jaccard values from a list of PLIER results ---------------
return_plier_jaccard_silver <- function(test_PLIER, array_silver, seq_silver) {
# Given a set of PLIER results (which is a list), compare significant pathways
# to two silver sets of pathways defined by array and RNA-seq data only
# Jaccard similarity is defined as O(intersect)/O(union). If the input is not
# a properly completed PLIER result, then return all NAs.
#
# Inputs: PLIER result, pathway set 1, pathway set 2
# Returns: data frame with two rows (array, seq) with stats for each overlap
if ("summary" %in% names(test_PLIER)) {
test_pathways <- test_PLIER[["summary"]] %>%
filter(FDR < 0.05) %>%
pull(pathway) %>%
unique()
array_jaccard <- length(intersect(array_silver, test_pathways)) / length(union(array_silver, test_pathways))
seq_jaccard <- length(intersect(seq_silver, test_pathways)) / length(union(seq_silver, test_pathways))
data.frame(
silver = c("array", "seq"),
n_silver = c(
length(array_silver),
length(seq_silver)
),
n_test = length(test_pathways),
n_intersect = c(
length(intersect(array_silver, test_pathways)),
length(intersect(seq_silver, test_pathways))
),
n_union = c(
length(union(array_silver, test_pathways)),
length(union(seq_silver, test_pathways))
),
n_common_genes = nrow(test_PLIER[["Z"]]),
k = ncol(test_PLIER[["Z"]]),
jaccard = c(
array_jaccard,
seq_jaccard
)
)
} else {
data.frame(
silver = NA,
n_silver = NA,
n_test = NA,
n_intersect = NA,
n_union = NA,
n_common_genes = NA,
k = NA,
jaccard = NA
)
}
}
return_plier_jaccard_global <- function(test_PLIER, global_pathways) {
# Given a set of PLIER results (which is a list), compare significant pathways
# to a global set of pathways defined by the existing PLIER pathways
# Jaccard similarity is defined as O(intersect)/O(union). If the input is not
# a properly completed PLIER result, then return all NAs.
#
# Inputs: PLIER result, global pathways
# Returns: data frame with one row with stats for each overlap
if ("summary" %in% names(test_PLIER)) {
test_pathways <- test_PLIER[["summary"]] %>%
filter(FDR < 0.05) %>%
pull(pathway) %>%
unique()
global_jaccard <- length(intersect(global_pathways, test_pathways)) / length(global_pathways)
data.frame(
n_global = length(global_pathways),
n_test = length(test_pathways),
n_intersect = length(intersect(global_pathways, test_pathways)),
n_common_genes = nrow(test_PLIER[["Z"]]),
k = ncol(test_PLIER[["Z"]]),
jaccard = global_jaccard
)
} else {
data.frame(
n_global = NA,
n_test = NA,
n_intersect = NA,
n_common_genes = NA,
k = NA,
jaccard = NA
)
}
}
#### loop over data for each seed and get PLIER results ------------------------
plier_objects_list <- list()
pathways_list <- list()
jaccard_list <- list()
for (seed_index in 1:length(norm.train.files)) {
message(str_c("PLIER with data seed", seed_index,
"out of", length(norm.train.files), "...",
sep = " "
))
#### read in data ------------------------------------------------------------
norm.train.list <- read_rds(norm.train.files[seed_index])
# convert gene names column to row names
# if GBM, also convert from GENEID to SYMBOL
norm.train.list <- purrr::modify_depth(
norm.train.list, 2,
function(x) {
convert_row_names(
expr = x,
cancer_type = cancer_type
)
}
)
#### main --------------------------------------------------------------------
# parallel backend
cl <- parallel::makeCluster(ncores)
doParallel::registerDoParallel(cl)
# create an output list
plier_results_list <- list()
# at different titration levels (0-100% RNA-seq) and normalization methods
# generate the PLIER results for array alone, seq alone, and array + seq combo
perc_seq <- as.character(seq(0, 100, 50))
norm_methods_if_0_100 <- c("log")
norm_methods_else <- c("log", "npn", "qn", "qn (cn)", "qn-z", "tdm", "z",
"array_only", "seq_only")
# set random seeds to use inside %dopar% loop for each %seq and norm method
use_seeds_inside_dopar <- list()
for (ps in perc_seq) {
if (ps %in% c("0", "100")) {
for (nm in norm_methods_if_0_100) {
use_seeds_inside_dopar[[ps]][[nm]] <- sample(1:1000, size = 1)
}
} else {
for (nm in norm_methods_else) {
use_seeds_inside_dopar[[ps]][[nm]] <- sample(1:1000, size = 1)
}
}
}
plier_results_list <- foreach(
ps = perc_seq,
.packages = c("PLIER", "doParallel")
) %do% {
message(str_c(" PLIER at ", ps, "% RNA-seq"))
if (ps %in% c("0", "100")) { # no need to add array_only or seq_only
norm_methods <- norm_methods_if_0_100
} else {
norm_methods <- norm_methods_else
# get array and seq sample columns
array_only_columns_tf <- names(norm.train.list[["0"]][["log"]]) %in%
names(norm.train.list[[ps]][["raw.array"]])
seq_only_columns_tf <- !array_only_columns_tf
# add array only and seq only data to each % RNA-seq
norm.train.list[[ps]][["array_only"]] <- norm.train.list[["0"]][["log"]][,array_only_columns_tf]
norm.train.list[[ps]][["seq_only"]] <- norm.train.list[["100"]][["log"]][,seq_only_columns_tf]
}
foreach(
nm = norm_methods,
.packages = c("PLIER", "doParallel"),
.errorhandling = "pass" # let pass on inside loop
) %dopar% {
# set seed again since we are inside %dopar%
set.seed(use_seeds_inside_dopar[[ps]][[nm]])
# check that the norm method exists at the %RNA-seq and it is not null
if (nm %in% names(norm.train.list[[ps]])) {
# remove any rows with all the same value
all.same.indx <- which(apply(
norm.train.list[[ps]][[nm]], 1,
check_all_same
))
if (length(all.same.indx) > 0) {
norm.train.list[[ps]][[nm]] <- norm.train.list[[ps]][[nm]][-all.same.indx, ]
}
# Permute every time and never re-use the same permuted matrix
if (permute) {
# permutes all 0-1 values within column (pathway)
# keeps row names the same
all.paths.row.names <- row.names(all.paths)
all.paths <- apply(all.paths, 2, sample)
row.names(all.paths) <- all.paths.row.names
}
# get common genes
common.genes <- PLIER::commonRows(
all.paths,
norm.train.list[[ps]][[nm]]
)
# minimum k for PLIER = 2*num.pc
set.k <- 2 * PLIER::num.pc(PLIER::rowNorm(norm.train.list[[ps]][[nm]][common.genes, ]))
# PLIER main function
PLIER::PLIER(as.matrix(norm.train.list[[ps]][[nm]][common.genes, ]),
all.paths[common.genes, ],
k = set.k,
scale = TRUE # PLIER z-scores input values by row
)
} else {
NA # NA for no data at this ps nm combination (0% and 100% TDM)
}
}
}
# stop parallel backend
parallel::stopCluster(cl)
# renames list levels
names(plier_results_list) <- perc_seq
for (i in perc_seq) {
if (i %in% c("0", "100")) {
names(plier_results_list[[i]]) <- norm_methods_if_0_100
} else {
names(plier_results_list[[i]]) <- norm_methods_else
}
}
# Check for failure to converge, and set to NA
plier_results_list <- purrr::modify_depth(plier_results_list, 2,
check_plier_failure_to_converge
)
plier_objects_list[[seed_index]] <- plier_results_list
# plier_results_list is structured:
# Level 0 (percentage RNA-seq)
# Level 1 (normalization method)
# Level 2 (PLIER object)
pathways_list[[seed_index]] <- purrr::modify_depth(
plier_results_list, 2,
function(x) x[["summary"]]) %>%
reshape2::melt(id.var = c("pathway", "LV index")) %>%
tidyr::pivot_wider(names_from = "variable",
values_from = "value") %>%
dplyr::rename("LV_index" = "LV index",
"nmeth" = "L2",
"pseq" = "L1",
"pvalue" = "p-value")
# Return pathway comparison for appropriate level of PLIER results list
jaccard_list[[seed_index]] <- purrr::modify_depth(
plier_results_list, 2,
function(x) return_plier_jaccard_global(x, PLIER_pathways)
)
}
# save list of PLIER objects
readr::write_rds(plier_objects_list,
file = PLIER_objects_filename)
if (length(pathways_list) > 0) {
# melt pathways list into one data frame
pathways_df <- reshape2::melt(pathways_list,
id.vars = c("pathway",
"LV_index",
"nmeth",
"pseq",
"AUC",
"pvalue",
"FDR")) %>%
dplyr::rename("seed_index" = "L1")
readr::write_tsv(pathways_df,
pathways_filename
)
}
if (length(jaccard_list) > 0) {
# melt jaccard list elements into one data frame
jaccard_df <- reshape2::melt(
data = jaccard_list,
id.vars = c(
"n_global", "n_test", "n_intersect",
"n_common_genes", "k"
),
value.name = "jaccard"
) %>%
rename(
"nmeth" = "L3", # normalization method
"pseq" = "L2", # percentage RNA-seq
"seed_index" = "L1"
) %>%
mutate(genes_pathways_permuted = permute)
readr::write_tsv(jaccard_df,
plot_data_filename
)
}