-
Notifications
You must be signed in to change notification settings - Fork 41
/
survival.Rmd
544 lines (494 loc) · 26.6 KB
/
survival.Rmd
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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
---
title: "TCGA survival analysis"
output:
html_document:
toc: true
# toc_float: true
theme: united
# theme: cerulean
# number_sections: true
date: "`r Sys.Date()`"
author: "Mikhail Dozmorov"
bibliography: data.TCGA/TCGA.bib
csl: styles.ref/genomebiology.csl
editor_options:
chunk_output_type: console
---
```{r setup, echo=FALSE, message=FALSE, warning=FALSE}
# Set up the environment
library(knitr)
opts_chunk$set(cache.path='cache/', fig.path='img/', cache=F, tidy=T, fig.keep='high', echo=F, dpi=100, warnings=F, message=F, comment=NA, warning=F, results='as.is', fig.width = 10, fig.height = 6) #out.width=700,
library(pander)
panderOptions('table.split.table', Inf)
set.seed(1)
library(dplyr)
options(stringsAsFactors = FALSE)
```
# Methods
## Survival analysis of gene expression data from TCGA
Level 3 gene expression data summarized as RSEM values were obtained using the `TCGA2STAT` R package v 1.2, along with the corresponding clinical annotations. Data for each of the 34 cancers were obtained separately. The data was log2-transformed and analyzed using Kaplan-Meier curves and Cox proportional hazard model. Each gene of interest was analyzed for its effect on survival by separating patients into high/low expression subgroups. A modified approach from [@Mihaly:2013aa] was used to estimate the best gene expression cutoff that separates high/low expression subgroups with differential survival.
We took advantage of the availability of clinical annotations. To identify if the expression of a gene of interest affects survival in any specific clinical subgroup, subsets of patients annotated with specific clinical annotations were selected (e.g., “males” or “females” in the “gender” clinical annotation). Subgroups with < 40 patients were not considered.
## Differential expression analysis
Samples in the selected cancer cohort were sorted by expression of the selected genes. Differentially expressed genes were detected between samples in the upper 75 percentile of the expression gradient and samples in the lower 25 percentile using `limma` v 3.32.6 R package [@Ritchie:2015aa; @Smyth:2004aa]. P-values were corrected for multiple testing using the False Discovery Rate (FDR) method [@Benjamini:1995aa]. Genes differentially expressed at FDR < 0.01 were selected for further analysis.
# Libraries
```{r libraries}
# BiocManager::install("CNTools") # Needed for TCGA2STAT
# remotes::install_github("cran/TCGA2STAT")
library(TCGA2STAT)
library(ggplot2)
library(cowplot)
library(rmarkdown)
library(reshape2)
if (!require(survplot)) {
install.packages("misc/survplot_0.0.7.tar.gz", repos = NULL, type="source")
}
source("Supplemental_R_script_1.R")
```
# Helper functions
```{r functions}
# A function to load TCGA data, from remote repository, or a local R object
load_data <- function(disease = cancer, data.type = data.type, type = type, data_dir = data_dir, force_reload = FALSE) {
FILE = paste0(data_dir, "/mtx_", disease, "_", data.type, "_", type, ".rda") # R object with data
if (all(file.exists(FILE), !(force_reload))) {
# If the data has been previously saved, load it
load(file = FILE)
} else {
# If no saved data exists, get it from the remote source
mtx <- getTCGA(disease = disease, data.type = data.type, type = type, clinical = TRUE)
save(file = FILE, list = c("mtx")) # Save it
}
return(mtx)
}
# A function to get data overview
summarize_data <- function(mtx = mtx) {
print(paste0("Dimensions of expression matrix, genex X patients: ", paste(dim(mtx$dat), collapse = " ")))
print(paste0("Dimensions of clinical matrix, patients X parameters: ", paste(dim(mtx$clinical), collapse = " ")))
print(paste0("Dimensions of merged matrix, patients X parameters + genes: ", paste(dim(mtx$merged.dat), collapse = " ")))
print("Head of the merged matrix")
print(mtx$merged.dat[1:5, 1:10])
print("Head of the clinical matrix")
print(mtx$clinical[1:5, 1:7])
print("List of clinical values, and frequency of each variable: ")
clin_vars <- apply(mtx$clinical, 2, function(x) length(table(x[ !(is.na(x) & x != "" )]))) %>% as.data.frame()
# Filter clinical variables to have at least 2, but no more than 10 categories,
# And they are not dates
clin_vars <- clin_vars[ as.numeric(clin_vars$.) > 1 & as.numeric(clin_vars$.) < 10 & !grepl("years|days|date|vital|OS|RFS|TIME|sample_type", rownames(clin_vars), perl = TRUE) , , drop = FALSE]
print(kable(clin_vars))
return(rownames(clin_vars))
}
# A function to create expression matrix
make_expression_matrix <- function(mtx = mtx, disease = cancer, data.type = data.type, type = type, results_dir = results_dir) {
# transposed expression matrix (genes start at column 4)
mtx.expression <- mtx$merged.dat[, 4:ncol(mtx$merged.dat) ] %>% t
# Set column names as patient IDs
colnames(mtx.expression) <- mtx$merged.dat$bcr
# Set row names as probe IDs
rownames(mtx.expression) <- colnames(mtx$merged.dat)[ 4:ncol(mtx$merged.dat) ]
# Save gzipped matrix
fileName.gz <- gzfile(paste0(results_dir, "/mtx_", disease, "_", data.type, "_", type, "_1expression.txt.gz"), "w")
write.table(mtx.expression, fileName.gz, sep = ";", quote = FALSE)
close(fileName.gz)
}
```
# Settings
```{r settings}
# Path where the downloaded data is stored
data_dir = "/Users/mdozmorov/Documents/Data/GenomeRunner/TCGAsurvival/data" # Mikhail
# data_dir = "/Users/stevenmeas/TCGAsurvival/data" # Steve
# Cancer types: http://www.liuzlab.org/TCGA2STAT/CancerDataChecklist.pdf
# Data types: http://www.liuzlab.org/TCGA2STAT/DataPlatforms.pdf
# Expression types: http://www.liuzlab.org/TCGA2STAT/DataPlatforms.pdf
# Clinical values: http://www.liuzlab.org/TCGA2STAT/ClinicalVariables.pdf
# General settings
data.type = "RNASeq2"
type = ""
# data.type = "miRNASeq" # miRNASeq - "count" for raw read counts (default); "rpmmm" for normalized read counts
# type = "rpmmm"
# data.type = "Mutation"
# type = "somatic"
# type="all"
# data.type = "Methylation"
# type = "450K"
# clinical = TRUE
# Select cancer type
cancer = "BRCA" # Breast cancer
# cancer = "OV" # Ovarian cancer
# cancer = "LIHC" # Liver hepatocellular carcinoma
# cancer = "HNSC" # Head and Neck squamous cell carcinoma
# cancer = "SARC" # Sarcoma
# cancer = "PAAD" # Pancreatic cancer
# cancer = "LUAD" # Lung adenocarcinoma
# cancer = "LUSC" # Lung squamous cell carcinoma
# cancer = "GBMLGG" # Glioma
# Select genes of interest
selected_genes = "MYBL2"
# Remove previous results
system(paste0("rm -r ", selected_genes, ".", cancer, ".Analysis*"))
# Censor KM plots at certain duration
max_days <- 365 * 5 # Cutoff for maximum survival time, days x years
# If true, Analysis 3 (survival differences in subcategories) will be run for each cancer
subcategories_in_all_cancers <- TRUE
```
# Load data
```{r loadData}
mtx <- load_data(disease = cancer, data.type = data.type, type = type, data_dir = data_dir, force_reload = FALSE)
clinical_annotations <- summarize_data(mtx = mtx)
# Prepare expression data
expr <- mtx$merged.dat[ , 4:ncol(mtx$merged.dat)] %>% as.matrix
# Filter out low expressed genes
# Should be more than 90% of non-zero values
ff <- genefilter::pOverA(p = 0.9, A = 0, na.rm = TRUE)
expr <- expr[, apply(expr, 2, ff)]
expr <- data.frame(AffyID = mtx$merged.dat$bcr, expr, stringsAsFactors = FALSE)
# Prepare clinical data
clin <- mtx$merged.dat[, 1:3]
colnames(clin)[1] <- "AffyID"
```
### Analysis 1: Selected genes, selected cancers, no clinical annotations
```{r analysis1}
system("mkdir res") # Create results folder
# Censor KM plots at certain duration
index <- clin$OS <= max_days & !is.na(clin$OS)
clin <- clin[index, ]
expr <- expr[index, ]
# Run survival analysis for selected genes
kmplot(expr, clin, event_index=2, time_index=3, affyid = selected_genes, auto_cutoff="true", transform_to_log2 = TRUE, cancer_type = cancer, fileType = "png", use_survminer = TRUE)
dev.off()
# Rename the results folder
system(paste0("mv res ", selected_genes, ".", cancer, ".Analysis1"))
```
```{r analysisExploratory, eval = FALSE}
### Exploratory: All genes, selected cancers, no clinical annotations
# kmplot(expr, clin, event_index=2, time_index=3, affyid = "", auto_cutoff="true", transform_to_log2 = TRUE)
```
### Analysis 2: Selected genes, all (or selected) cancers, no clinical annotations
```{r analysis2}
# All cancers with RNASeq2 data
all_cancers = c("ACC", "BLCA", "BRCA", "CESC", "CHOL", "COAD", "COADREAD", "DLBC", "ESCA", "GBM", "GBMLGG", "HNSC", "KICH", "KIPAN", "KIRC", "KIRP", "LGG", "LIHC", "LUAD", "LUSC", "MESO", "OV", "PAAD", "PCPG", "PRAD", "READ", "SARC", "SKCM", "STAD", "STES", "TGCT", "THCA", "THYM", "UCEC", "UCS", "UVM") # "LAML",
data.type = "RNASeq2"; type = ""
system("mkdir res") # Create results folder
for (cancer_type in all_cancers) {
print(paste0("Processing cancer ", cancer_type))
mtx <- load_data(disease = cancer_type, data.type = data.type, type = type, data_dir = data_dir, force_reload = FALSE)
# Prepare expression data
expr <- mtx$merged.dat[ , 4:ncol(mtx$merged.dat)] %>% as.matrix
# Filter out low expressed genes
# Should be more than 90% of non-zero values
# ff <- genefilter::pOverA(p = 0.9, A = 0, na.rm = TRUE)
# expr <- expr[, apply(expr, 2, ff)]
expr <- data.frame(AffyID = mtx$merged.dat$bcr, expr, stringsAsFactors = FALSE)
# Prepare clinical data
clin <- mtx$merged.dat[, 1:3]
colnames(clin)[1] <- "AffyID"
# Censor KM plots at certain duration
index <- clin$OS <= max_days & !is.na(clin$OS)
clin <- clin[index, ]
expr <- expr[index, ]
# Run survival analysis for selected genes
kmplot(expr, clin, event_index=2, time_index=3, affyid = selected_genes, auto_cutoff="true", transform_to_log2 = TRUE, cancer_type = cancer_type, fileType = "png", use_survminer = TRUE)
}
### Plot the results of one gene across all cancers
# Read in analysis natrix
mtx <- read.table("res/global_stats.txt", sep = "\t", header = TRUE, stringsAsFactors = FALSE, fill = TRUE)
# Add -log10-transformed p-value
mtx <- mtx %>% mutate(log10.pval = -1 * log10(p.adjust(p.value, method = "BH")))
# Print into PNG
mtx_to_plot <- mtx %>% subset(Gene == selected_genes)
mtx_to_plot <- mtx_to_plot[order(mtx_to_plot$log10.pval), ]
mtx_to_plot$Cancer <- factor(mtx_to_plot$Cancer, levels = mtx_to_plot$Cancer)
ggplot(mtx_to_plot, aes(x = Cancer, y = log10.pval)) +
geom_bar(stat = "identity") +
theme(legend.position="none") +
labs(x="Cancer", y="-log10(p-value)") +
coord_flip() +
theme_cowplot()
ggsave(paste0("res/", selected_genes, "_all_TCGA_cancers.png"), width = 5, height = 6.5)
# Rename the results folder
system(paste0("mv res ", selected_genes, ".", cancer, ".Analysis2"))
```
### Analysis 3: Selected genes, all (or, selected) cancers, all unique categories
```{r analysis3}
system("mkdir res") # Create results folder
# If true, analyze survival differences in subcategories in all cancers
if (subcategories_in_all_cancers) {
cancer_type <- c("ACC", "BLCA", "BRCA", "CESC", "CHOL", "COAD", "COADREAD", "DLBC", "ESCA", "GBM", "GBMLGG", "HNSC", "KICH", "KIPAN", "KIRC", "KIRP", "LGG", "LIHC", "LUAD", "LUSC", "MESO", "OV", "PAAD", "PCPG", "PRAD", "READ", "SARC", "SKCM", "STAD", "STES", "TGCT", "THCA", "THYM", "UCEC", "UCS", "UVM") # "LAML",
} else {
cancer_type <- cancer # Selected in settings
}
data.type = "RNASeq2"; type = ""
for (cancer_type in cancer_type) {
print(paste0("Processing cancer ", cancer_type))
mtx <- load_data(disease = cancer_type, data.type = data.type, type = type, data_dir = data_dir, force_reload = FALSE)
if (cancer_type == "BRCA") {
# BRCA-specific - replace original annotations with XENA
mtx$clinical <- read.csv("data.TCGA/XENA_classification.csv", row.names = 1)
}
clinical_annotations <- summarize_data(mtx = mtx)
# Prepare expression data
expr <- mtx$merged.dat[ , 4:ncol(mtx$merged.dat)] %>% as.matrix
# Filter out low expressed genes
# Should be more than 90% of non-zero values
# ff <- genefilter::pOverA(p = 0.9, A = 0, na.rm = TRUE)
# expr <- expr[, apply(expr, 2, ff)]
expr <- data.frame(AffyID = mtx$merged.dat$bcr, expr, stringsAsFactors = FALSE)
# Prepare clinical data
clin <- mtx$merged.dat[, 1:3]
colnames(clin)[1] <- "AffyID"
# Censor KM plots at certain duration
index <- clin$OS <= max_days & !is.na(clin$OS)
clin <- clin[index, ]
expr <- expr[index, ]
# Full clinical information
# clin_full <- mtx$clinical
# Match to the order of small clinical annitation
# clin_full <- clin_full[rownames(clin_full) %in% clin$AffyID, ]
# clin_full <- clin_full[match(clin$AffyID, rownames(clin_full)), ]
clin_full <- mtx$clinical[rownames(mtx$clinical) %in% clin$AffyID, ]
clin_full <- clin_full[match(expr$AffyID, rownames(clin_full)), ]
all.equal(rownames(clin_full), expr$AffyID)
if (cancer_type == "OV") {
# OV - specific Prepare extra clinical annotation
clin_extra <- read.table("data.TCGA/TCGA_489_UE.k4.txt", sep = "\t", header = TRUE)
clin_extra$Sample.ID <- sapply(clin_extra$Sample.ID, function(x) strsplit(x, ".", fixed = TRUE)[[1]][1:3] %>% paste(., collapse = "-")) # Shorten sample IDs
# Subset to common samples
common_AffyID <- intersect(rownames(clin_full), clin_extra$Sample.ID)
expr <- expr[ expr$AffyID %in% common_AffyID, ]
clin <- clin[ clin$AffyID %in% common_AffyID, ]
clin_full <- clin_full[ rownames(clin_full) %in% common_AffyID, ]
clin_extra <- clin_extra[ clin_extra$Sample.ID %in% common_AffyID, ]
# Make clinical annotation the same order
clin <- clin[ match(expr$AffyID, clin$AffyID), ]
clin_extra <- clin_extra[ match(expr$AffyID, clin_extra$Sample.ID), ]
all.equal(expr$AffyID, clin$AffyID, rownames(clin_full), clin_extra$Sample.ID)
# Add extra clinical annotation
clin_full <- data.frame(clin_full, subtype = clin_extra$k4)
clinical_annotations <- c( clinical_annotations, "subtype")
}
# For each clinical annotation
for (annotation in clinical_annotations) {
# Get the number of patients per category in the current annotation
annotations <- table(clin_full[, annotation], useNA = "no")
# How many categories in the current annotation
num_of_annot_categories <- length(annotations)
# How many categories to select at one time
for (num_of_selected_categories in 1:num_of_annot_categories) {
# Select patients annotated with this categories
patients <- rownames(clin_full)[ clin_full[, annotation] %in% names(annotations)[ num_of_selected_categories] ]
# Get their index in the clin and expr matrixes
index_patients <- which(clin$AffyID %in% patients)
# If the number of patients annotated with the combination of categories is large enough, proceed
if (length(index_patients) > 40) {
print(paste("Processing annotation:", annotation,
", categories:", names(annotations)[ num_of_selected_categories],
", number of patients:", length(index_patients)))
# Get a subset of clinical information for these patients
clin_selected <- clin[ index_patients, ]
# Get a subset of expression information for these patients
expr_selected <- expr[ index_patients, ]
# For this subset of expression, filter out low expressed genes
index_genes <- apply(expr_selected %>% dplyr::select(-AffyID), 2, ff) # index of expression values to keep
expr_selected <- cbind(expr_selected$AffyID, dplyr::select(expr_selected, -AffyID)[, index_genes]) # patient IDs and expression values to keep
# Perform actual survival analysis
kmplot(expr_selected, clin_selected, event_index=2, time_index=3, affyid = selected_genes, auto_cutoff="true", transform_to_log2 = TRUE, cancer_type = paste(c(cancer_type, annotation, names(annotations)[ num_of_selected_categories] ), collapse = "-"), fileType = "png", use_survminer = TRUE)
}
}
}
}
# Rename the results folder
system(paste0("mv res ", selected_genes, ".", cancer, ".Analysis3"))
```
```{r analysis4, eval = FALSE}
### Analysis 4: Selected genes, selected cancers, all combinations of clinical annotations
system("mkdir res") # Create results folder
clin_full <- mtx$clinical # Full clinical information
# For each clinical annotation
for (annotation in clinical_annotations) {
# Get the number of patients per category in the current annotation
annotations <- table(clin_full[, annotation], useNA = "no")
# How many categories in the current annotation
num_of_annot_categories <- length(annotations)
# How many categories to select at one time
for (num_of_selected_categories in 1:num_of_annot_categories) {
# All combinations of categories, m categories selected at one time
combination_of_categories <- combn(x = names(annotations), m = num_of_selected_categories)
# For each combination of categories (column)
for (combination in 1:ncol(combination_of_categories)) {
# Select patients annotated with this combination of categories
patients <- rownames(clin_full)[ clin_full[, annotation] %in% combination_of_categories[, combination]]
# Get their index in the clin and expr matrixes
index_patients <- which(clin$AffyID %in% patients)
# If the number of patients annotated with the combination of categories is large enough, proceed
if (length(index_patients) > 40) {
print(paste("Processing annotation:", annotation,
", categories:", paste(combination_of_categories[, combination], collapse = ","),
", number of patients:", length(index_patients)))
# Get a subset of clinical information for these patients
clin_selected <- clin[ index_patients, ]
# Get a subset of expression information for these patients
expr_selected <- expr[ index_patients, ]
# For this subset of expression, filter out low expressed genes
ff <- genefilter::pOverA(p = 0.9, A = 0, na.rm = TRUE)
expr <- expr[, apply(expr, 2, ff)]
index_genes <- apply(expr_selected %>% dplyr::select(-AffyID), 2, ff) # index of expression values to keep
expr_selected <- cbind(expr_selected$AffyID, select(expr_selected, -AffyID)[, index_genes]) # patient IDs and expression values to keep
# Perform actual survival analysis
kmplot(expr_selected, clin_selected, event_index=2, time_index=3, affyid = selected_genes, auto_cutoff="true", transform_to_log2 = TRUE, cancer_type = paste(c(cancer, annotation, combination_of_categories[, combination]), collapse = "-"), fileType = "png", use_survminer = FALSE)
}
}
}
}
# Rename the results folder
system("mv res res.genes.Analysis4")
```
```{r}
if (cancer %in% c("BRCA","OV")){
continue_analysis <- TRUE
} else {
continue_analysis <- FALSE
}
opts_chunk$set(eval = continue_analysis, include = continue_analysis)
```
### Analysis 5: Clinical-centric analysis. Selected cancer, selected clinical subcategory, survival difference between all pairs of subcategories
```{r}
system("mkdir -p res")
# Select cancer and load expression data
# cancer <- "BRCA"
mtx <- load_data(disease = cancer, data.type = data.type, type = type, data_dir = data_dir, force_reload = FALSE)
clinical_annotations <- summarize_data(mtx = mtx)
# Prepare expression data
expr <- mtx$merged.dat[ , 4:ncol(mtx$merged.dat)] %>% as.matrix
# Filter out low expressed genes
# Should be more than 90% of non-zero values
ff <- genefilter::pOverA(p = 0.9, A = 0, na.rm = TRUE)
expr <- expr[, apply(expr, 2, ff)]
expr <- data.frame(AffyID = mtx$merged.dat$bcr, expr, stringsAsFactors = FALSE)
clinical_annotations_selected <- "pathologyMstage" # Default selected clinical annotation
# Prepare clinical data
if (cancer == "BRCA") {
# BRCA-specific - replace original annotations with XENA
mtx$clinical <- read.csv("data.TCGA/XENA_classification.csv", row.names = 1)
clin <- mtx$merged.dat[, 1:3]
colnames(clin)[1] <- "AffyID"
# Full clinical information
clin_full <- mtx$clinical
# Match to the order of small clinical annitation
clin_full <- clin_full[rownames(clin_full) %in% clin$AffyID, ]
clin_full <- clin_full[match(clin$AffyID, rownames(clin_full)), ]
all.equal(expr$AffyID, rownames(clin_full))
# Select clinical category and subcategories
clinical_annotations <- summarize_data(mtx = mtx)
clinical_annotations_selected <- "PAM50Call_RNAseq" # For PAM50 classifier
clin <- cbind(clin, clin_full[, clinical_annotations_selected, drop = FALSE])
} else {
clin <- mtx$merged.dat[, 1:3]
colnames(clin)[1] <- "AffyID"
# Full clinical information
clin_full <- mtx$clinical
# Match to the order of small clinical annitation
clin_full <- clin_full[rownames(clin_full) %in% clin$AffyID, ]
clin_full <- clin_full[match(clin$AffyID, rownames(clin_full)), ]
all.equal(expr$AffyID, rownames(clin_full))
}
if (cancer == "OV") {
# OV-specific Prepare extra clinical annotation
clin_extra <- read.table("data.TCGA/TCGA_489_UE.k4.txt", sep = "\t", header = TRUE)
clin_extra$Sample.ID <- sapply(clin_extra$Sample.ID, function(x) strsplit(x, ".", fixed = TRUE)[[1]][1:3] %>% paste(., collapse = "-")) # Shorten sample IDs
# Subset to common samples
common_AffyID <- intersect(rownames(clin_full), clin_extra$Sample.ID)
expr <- expr[ expr$AffyID %in% common_AffyID, ]
clin <- clin[ clin$AffyID %in% common_AffyID, ]
clin_full <- clin_full[ rownames(clin_full) %in% common_AffyID, ]
clin_extra <- clin_extra[ clin_extra$Sample.ID %in% common_AffyID, ]
# Make clinical annotation the same order
clin <- clin[ match(expr$AffyID, clin$AffyID), ]
clin_extra <- clin_extra[ match(expr$AffyID, clin_extra$Sample.ID), ]
all.equal(expr$AffyID, clin$AffyID, rownames(clin_full), clin_extra$Sample.ID)
# Add extra clinical annotation
clin_full <- data.frame(clin_full, subtype = clin_extra$k4)
# Select clinical category and subcategories
clinical_annotations_selected <- "subtype" # For OV classifier
clin <- cbind(clin, clin_full[, clinical_annotations_selected, drop = FALSE])
}
print(paste0("Number of patients in each subcategory, in the ", clinical_annotations_selected, " category"))
table(clin[, clinical_annotations_selected])
# All pairs of subcategories
group_combinations <- combn(unique(clin[!is.na(clin[, clinical_annotations_selected]), clinical_annotations_selected]), 2)
# Survival analysis for all group combinations
for (pair in 1:ncol(group_combinations)) {
# KM analysis on subcategories
kmplot.clin(clin = clin, event_index=2, time_index=3, clinical_annotations = clinical_annotations_selected, group1 = group_combinations[1, pair], group2 = group_combinations[2, pair], cancer_type = cancer, fileType = "png", use_survminer = TRUE)
}
# Plot expression boxplots for the selected gene in selected subcategories
# selected_genes = c("SDC1") # BRCA
# Prepare expression data
expr <- mtx$merged.dat[ , 4:ncol(mtx$merged.dat)] %>% as.matrix
# Filter out low expressed genes
# Should be more than 90% of non-zero values
# ff <- genefilter::pOverA(p = 0.9, A = 0, na.rm = TRUE)
# expr <- expr[, apply(expr, 2, ff)]
expr <- data.frame(AffyID = mtx$merged.dat$bcr, expr, stringsAsFactors = FALSE)
expr <- expr[ expr$AffyID %in% clin$AffyID, ]
expr <- expr[match(clin$AffyID, expr$AffyID), ]
# A matrix to plot
mtx_to_plot <- data.frame(Gene = log2(expr[, selected_genes] + 1), Clinical = clin[, clinical_annotations_selected])
mtx_to_plot <- mtx_to_plot[complete.cases(mtx_to_plot), ]
save(mtx_to_plot, file = "res/mtx_to_plot.rda")
# Plot and save
p <- ggplot(melt(mtx_to_plot, id.vars = "Clinical"), aes(x = Clinical, y = value, fill = Clinical)) +
geom_boxplot() +
ylab("log2 expression") + theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
plot(p)
ggsave(filename = paste0("res/", cancer, "_", selected_genes, "_", clinical_annotations_selected, ".png"), p, width = 5, height = 4, device = "png")
system(paste0("mv res ", selected_genes, ".", cancer, ".Analysis5"))
```
```{r eval = FALSE}
## Analysis 6: Dimensionality reduction of a gene signature across all cancers using NMF, PCA, or FA
# For each cancer, extracts gene expression of a signature, reduces its dimensionality,
# plots a heatmap sorted by the first component, biplots, saves eigenvectors in files
# named after cancer, signature, method. They are used in `correlations.Rmd`
system("rm -r res")
system("mkdir res") # Create results folder
library(NMF)
library(ggfortify)
library(pheatmap)
signature <- readLines("data.TCGA/EINAV_INTERFERON_SIGNATURE_IN_CANCER.txt") # Interferon signature
selected_genes <- "interferon_signature"
pdf(file = paste0("res/dimreduction_", selected_genes, ".pdf"))
# All cancers with RNASeq2 data
cancer_RNASeq2 = c("ACC", "BLCA", "BRCA", "CESC", "CHOL", "COAD", "COADREAD", "DLBC", "ESCA", "GBM", "HNSC", "KICH", "KIPAN", "KIRC", "KIRP", "LIHC", "LUAD", "LUSC", "MESO", "OV", "PAAD", "PCPG", "PRAD", "READ", "SARC", "SKCM", "STAD", "TGCT", "THCA", "THYM", "UCEC", "UCS") # "GBMLGG", "LGG",
data.type = "RNASeq2"; type = ""
# Process each cancer
for (cancer_type in cancer_RNASeq2) {
print(paste0("Processing cancer ", cancer_type))
mtx <- load_data(disease = cancer_type, data.type = data.type, type = type, data_dir = data_dir, force_reload = FALSE)
# Prepare subset of expression data for a given signature
mtx_selected <- t(log2(mtx$merged.dat[, signature]))
colnames(mtx_selected) <- mtx$merged.dat$bcr
# NMF
method <- "NMF"
mtx_nmf <- nmf(t(mtx_selected), 3) # with three components
pheatmap(mtx_selected[, order(mtx_nmf@fit@W[, 1])], cluster_cols = FALSE, treeheight_row = FALSE, treeheight_col = FALSE, scale = "row", main = paste0(cancer_type, "_", method))
mtx_reduced <- mtx_nmf@fit@W
save(mtx_reduced, file = paste0("res/", cancer_type, "_", selected_genes, "_", method, ".Rda"))
# PCA
# https://cran.r-project.org/web/packages/ggfortify/vignettes/plot_pca.html
method <- "PCA"
mtx_pca <- prcomp(t(mtx_selected), scale. = TRUE, center = TRUE)
pheatmap(mtx_selected[, order(mtx_pca$x[, 1])], cluster_cols = FALSE, treeheight_row = FALSE, treeheight_col = FALSE, scale = "row", main = paste0(cancer_type, "_", method))
p <- autoplot(prcomp(t(mtx_selected), scale. = TRUE, center = TRUE), loadings = TRUE, loadings.label = TRUE) + ggtitle(paste0(cancer_type, "_", method))
print(p)
mtx_reduced <- mtx_pca$x
save(mtx_reduced, file = paste0("res/", cancer_type, "_", selected_genes, "_", method, ".Rda"))
# Factor analysis
method <- "FA"
mtx_fa <- factanal(t(mtx_selected), factors = 3, scores = "regression")
pheatmap(mtx_selected[, order(mtx_fa$scores[, 1])], cluster_cols = FALSE, treeheight_row = FALSE, treeheight_col = FALSE, scale = "row", main = paste0(cancer_type, "_", method))
p <- autoplot(mtx_fa, loadings = TRUE, loadings.label = TRUE) + ggtitle(paste0(cancer_type, "_", method))
print(p)
mtx_reduced <- mtx_fa$scores
save(mtx_reduced, file = paste0("res/", cancer_type, "_", selected_genes, "_", method, ".Rda"))
}
dev.off()
system("mv res res.genes.Analysis6")
```
# References