-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path12_features.Rmd
352 lines (271 loc) · 8.83 KB
/
12_features.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
---
title: "Feature plots"
author: "Kat Moore"
date: "`r Sys.Date()`"
output:
html_document:
toc: yes
toc_float: yes
toc_depth: 5
df_print: paged
highlight: kate
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(edgeR)
library(here)
library(ComplexHeatmap)
library(tidyverse)
library(ggthemes)
library(ggsci)
theme_set(theme_bw())
```
Plot expression of top ranked elastic net and PSO-SVM features as box plots or as heatmaps.
## Load data
Expression & metadata for both original dataset and blind validation:
```{r}
dgeAll <- readRDS(file = here("Rds/07b_dgeAll.Rds"))
```
Create subsets for original data and blind val:
```{r}
dgeOriginal <- dgeAll[,dgeAll$samples$Dataset == "Original"]
dgeOriginal$samples <- droplevels(dgeOriginal$samples)
dgeBlindVal <- dgeAll[,dgeAll$samples$Dataset == "blindVal"]
dgeBlindVal$samples <- droplevels(dgeBlindVal$samples)
```
Elastic net model, from which we extract the features
```{r}
model_altlambda <- readRDS(here("Rds/04_model_altlambda.Rds"))
feature_extraction <- function(fit, dict = dgeAll$genes){
coef(fit$finalModel, fit$bestTune$lambda) %>%
as.matrix() %>% as.data.frame() %>%
slice(-1) %>% #Remove intercept
rownames_to_column("feature") %>%
rename(coef = "1") %>%
filter(coef !=0) %>%
left_join(., dict, by = c("feature" = "ensembl_gene_id")) %>%
rename("ensembl_gene_id"=feature) %>%
arrange(desc(abs(coef))) %>%
rowid_to_column("rank")
}
enet_feat <- feature_extraction(model_altlambda$fit)
head(enet_feat)
```
Total number of elastic net features in the final version:
```{r}
enet_feat %>% nrow()
```
PSO-SVM model & features:
```{r}
pso_output <- readRDS(here("Rds/02_thromboPSO.Rds"))
best.selection.pso <- paste(pso_output$lib.size,
pso_output$fdr,
pso_output$correlatedTranscripts,
pso_output$rankedTranscripts, sep="-")
particle_path <- file.path(here("pso-enhanced-thromboSeq1/outputPSO",
paste0(best.selection.pso,".RData")))
load(particle_path) #Becomes dgeTraining
dgeParticle <- dgeTraining #Rename to avoid namespace confusion
rm(dgeTraining)
#Features from PSO-SVM do not have coefficients,
#but they should come out of Thromboseq in a ranked order.
psosvm.feat <- enframe(dgeParticle$biomarker.transcripts, "rank", "ensembl_gene_id") %>%
left_join(dgeParticle$genes, by = "ensembl_gene_id")
head(psosvm.feat)
```
Number of PSO-SVM features:
```{r}
psosvm.feat %>% nrow()
```
## Overview
```{r}
enet_feat %>% head(30)
```
```{r}
psosvm.feat %>% head(30)
```
## Boxplots
### Elastic net features
```{r}
ggfeaturePlot <- function(dge, ens, df){
stopifnot(length(ens) == 1)
dge$samples$sample <- colnames(dge)
#Normalize
counts <- cpm(calcNormFactors(dge), log = T, normalized.lib.sizes = T)
#Select relevant gene, convert to tidy format
counts <- as.data.frame(counts) %>%
rownames_to_column("ensembl_gene_id") %>%
filter(ensembl_gene_id == ens) %>%
select(-ensembl_gene_id) %>%
gather(key = "sample", value = "exp")
#Add metadata
counts <- left_join(counts, dge$samples, by = "sample")
gndf <- df %>% filter(ensembl_gene_id == ens)
gn <- ifelse(gndf$hgnc_symbol == "", ensembl_gene_id,
paste(gndf$hgnc_symbol, gndf$ensembl_gene_id, sep=":"))
title <- paste0(gn, ", Rank: ", gndf$rank)
#Add coef if plotting enet
if("coef" %in% colnames(gndf)){
title <- paste0(title, ", Coef: ", signif(gndf$coef, 3))
}
counts %>%
ggplot(aes(x = group, y = exp)) +
geom_boxplot(outlier.shape = NA) +
geom_jitter(aes(color = hosp), alpha = 0.7, height = 0, width = 0.25) +
ggsci::scale_color_igv() +
ggtitle(title) +
facet_wrap(~Dataset)
}
#ggfeaturePlot(dgeAll, ens = "ENSG00000113269", enet_feat)
lapply(enet_feat$ensembl_gene_id[1:10],
function(x) ggfeaturePlot(dgeAll, ens = x, enet_feat))
```
### PSO-SVM features
```{r}
lapply(psosvm.feat$ensembl_gene_id[1:10],
function(x) ggfeaturePlot(dgeAll, ens = x, psosvm.feat))
```
## Heatmaps
Plot elastic net features as a heatmap.
### EN features in original data
```{r}
#Heatmap plotting
feature_heatmap = function(dge,
title="Heatmap", debug = F,
top_vars = c("group","hosp"),
features,
legend_title = NULL,
row_scale = F,
row_size = 8, col_size = 8,
show_col_names = F, show_row_names = T, ...){
#Define colors
hmap_colors = list(
group = c(healthyControl="lightgray", breastCancer="black"),
hosp = ggsci::pal_lancet()(length(unique(dge$samples$hosp))),
Dataset = ggsci::pal_jco()(length(unique(dge$samples$Dataset)))
)
names(hmap_colors$hosp) = unique(dge$samples$hosp)
names(hmap_colors$Dataset) = unique(dge$samples$Dataset)
top_colors = list(
#Dataset = hmap_colors$Dataset,
group = hmap_colors$group,
hosp = hmap_colors$hosp
)
#bottom_colors = list(group = hmap_colors$group)
#Subset to only those genes used as features
dge <- dge[rownames(dge) %in% features, ]
#Normalized count matrix
mat = edgeR::cpm(edgeR::calcNormFactors(dge),
log = T, normalized.lib.sizes = T)
#Row scale settings
if (row_scale==T){
mat = t(scale(t(mat)))
}
#Change legend according to whether input is scaled
if (row_scale==T){
hlp = list(title="rowscaled logcpm")
} else {
hlp = list(title="logcpm counts")
}
#Replace ensembl IDs with gene names, or keep ensembl ID if gene names are absent
genes <- dge$genes
genes <- genes %>%
dplyr::mutate(gene_name = as.character(hgnc_symbol)) %>%
dplyr::mutate(
label = ifelse(gene_name == "" | is.na(gene_name),
ensembl_gene_id, gene_name)
)
#return(genes)
rownames(mat) <- genes$label
sampledata <- as.data.frame(dge$samples)
#Heatmap annotation
ann_top = sampledata[,top_vars, drop=F]
#Top column annotation
colTop <- ComplexHeatmap::HeatmapAnnotation(
df=ann_top, which="col",
col = top_colors
#annotation_legend_param = list(list(title = legend_title))
)
#return(colTop)
#Bottom column annotation
# ann_bottom = sampledata[,bottom_vars, drop=F]
# colBottom <- ComplexHeatmap::HeatmapAnnotation(
# df=ann_bottom, which="col", col = bottom_colors
# )
if(debug){print(dim(mat))}
stopifnot(nrow(mat) == nrow(features))
#Draw the heatmap
ComplexHeatmap::Heatmap(mat,
top_annotation = colTop,
#bottom_annotation = colBottom,
#left_annotation = rowAnno,
heatmap_legend_param = hlp,
show_row_names = show_row_names,
show_column_names = show_col_names,
cluster_rows = T,
row_names_gp = gpar(fontsize = row_size),
column_names_gp = gpar(fontsize = col_size),
column_title = title,
...)
}
suppressMessages(
feature_heatmap(dgeOriginal,
features = enet_feat$ensembl_gene_id,
show_row_names = F,
title = paste0("Elastic net features (", nrow(enet_feat), ") in original data"),
row_scale = T)
)
```
### EN features in blind validation data
```{r}
suppressMessages(
feature_heatmap(dgeBlindVal,
features = enet_feat$ensembl_gene_id,
show_row_names = F,
title = paste0("Elastic net features (", nrow(enet_feat), ") in blind validation data"),
row_scale = T)
)
```
### Enet features in all data
```{r}
suppressMessages(
feature_heatmap(dgeAll,
features = enet_feat$ensembl_gene_id,
show_row_names = F,
title = paste0("Elastic net features (", nrow(enet_feat), ") in all data"),
row_scale = T)
)
```
### PSO-SVM features in original data
```{r}
suppressMessages(
feature_heatmap(dgeOriginal,
features = psosvm.feat$ensembl_gene_id,
show_row_names = F,
title = paste0("PSO-SVM features (", nrow(psosvm.feat), ") in original data"),
row_scale = T)
)
```
### PSO-SVM features in blind data
```{r}
suppressMessages(
feature_heatmap(dgeBlindVal,
features = psosvm.feat$ensembl_gene_id,
show_row_names = F,
title = paste0("PSO-SVM features (", nrow(psosvm.feat), ") in blind data"),
row_scale = T)
)
```
### PSO-SVM features in all data
```{r}
suppressMessages(
feature_heatmap(dgeAll,
features = psosvm.feat$ensembl_gene_id,
show_row_names = F,
title = paste0("PSO-SVM features (", nrow(psosvm.feat), ") in original data"),
row_scale = T)
)
```
```{r}
sessionInfo()
```