-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLipidomics_Andrej_Kovac_premodials.Rmd
554 lines (417 loc) · 19.9 KB
/
Lipidomics_Andrej_Kovac_premodials.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
545
546
547
548
549
550
551
552
---
title: "Lipidomics Premodials"
author: "Clara Meijs"
date: "2024-02-26"
output:
html_document:
df_print: paged
keep_md: yes
toc: true
toc_float: true
toc_collapsed: true
toc_depth: 5
theme: lumen
---
## Libraries
```{r libraries}
rm(list=ls())
library(pheatmap)
library(ggplot2)
# library(matrixStats)
# library(wesanderson)
# library(clusterProfiler)
# library(enrichplot)
# library(msigdbr)
library(dichromat)
# library(stringr)
library(dplyr)
library(ggrepel)
library(reshape2)
library(umap)
library(ggthemes)
library(cowplot)
#library(MetaboAnalystR)
library(vsn)
library(DEP)
library(readr)
library(naniar)
library(SummarizedExperiment)
library(data.table)
library(readxl)
```
## Set working directories
```{r set-working-directories, message=FALSE, class.source = 'fold-hide'}
# if you are using Rstudio run the following command, otherwise, set the working directory to the folder where this script is in
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
# create directory for results
dir.create(file.path(getwd(),'results'), showWarnings = FALSE)
# create directory for plots
dir.create(file.path(getwd(),'plots'), showWarnings = FALSE)
```
## Load data
```{r load data}
lipidomics = read.csv("data/2023-12-13 Plasma QCtest.csv")
#remove QC rows and blank rows
QC_rows = grepl("QC", lipidomics$Sample.Name)
blank_rows = lipidomics$Sample.Name == "blank"
lipidomics = lipidomics[!(QC_rows | blank_rows),]
#make first column the rownames and remove the sample name column
rownames(lipidomics) = lipidomics[,1]
lipidomics = lipidomics[,!colnames(lipidomics) == "Sample.Name"]
#transpose matrix
lipidomics = as.data.frame(t(lipidomics))
#make all columns numeric
for(i in 1:ncol(lipidomics)){lipidomics[,i] = as.numeric(lipidomics[,i])}
#make a list for all dataframes
lipidomics_data = list(raw_data = lipidomics)
#deal with the duplicates
samples = unlist(strsplit(colnames(lipidomics), "[.]"))
samples = samples[grep(pattern = "pl", samples)]
samples = unique(samples)
lipidomics_data$merged = as.data.frame(matrix(NA, nrow = nrow(lipidomics_data$raw_data), ncol = length(samples)))
colnames(lipidomics_data$merged) = samples
rownames(lipidomics_data$merged) = rownames(lipidomics_data$data)
lipidomics_data$difference = lipidomics_data$variance = lipidomics_data$missing = lipidomics_data$merged
for(i in 1:length(samples)){
p = grep(pattern = samples[i], colnames(lipidomics_data$raw_data))
lipidomics_data$merged[,i] = apply(X = lipidomics_data$raw_data[,p], function(x) mean(x, na.rm = TRUE), MARGIN = 1)
lipidomics_data$variance[,i] = apply(X = lipidomics_data$raw_data[,p], function(x) var(x, na.rm = TRUE), MARGIN = 1)
lipidomics_data$missing[,i] = apply(X = lipidomics_data$raw_data[,p], function(x) 3 - sum(is.na(x)), MARGIN = 1)
lipidomics_data$difference[,i] = apply(X = lipidomics_data$raw_data[,p], function(x) max(x, na.rm = TRUE) - min(x, na.rm = TRUE), MARGIN = 1)
}
#create a relative variance and relative difference dataset
lipidomics_data$relative_variance = lipidomics_data$variance/lipidomics_data$merged
lipidomics_data$relative_difference = lipidomics_data$difference/lipidomics_data$merged
#make all the rownames the same
for(i in 2:length(lipidomics_data)){
rownames(lipidomics_data[[i]]) = rownames(lipidomics_data$raw_data)
}
#make summarized experiments
abundance.columns <- 1:ncol(lipidomics_data$merged) # get abundance column numbers
clin = data.frame(label = colnames(lipidomics_data$merged), #very limited clinical variables
condition = rep("pl", 8),
replicate = 1:ncol(lipidomics_data$merged))
lipidomics_data$merged$name = lipidomics_data$merged$ID = rownames(lipidomics_data$merged)
experimental.design = clin
lipidomics_data$se <- make_se(lipidomics_data$merged, abundance.columns, experimental.design)
#save data
library(writexl)
write_xlsx(lipidomics_data[!names(lipidomics_data) =="se"], path = "results/lipidomics_data.xlsx")
```
## Missing inspection
```{r missing inspection}
#heatmap missing before merging replicates
vis_miss(lipidomics,show_perc = TRUE, show_perc_col = TRUE, cluster = F)
ggsave("plots/missing_vis_miss_heatmap_raw.png", width = 11, height = 8, units = "in")
#heatmap missing with merged replicates
vis_miss(lipidomics_data$merged, show_perc = TRUE, show_perc_col = TRUE, cluster = F)
ggsave("plots/missing_vis_miss_heatmap_merged.png", width = 11, height = 8, units = "in")
# Filter for proteins that are quantified in at least 2/3 of the samples.
lipidomics_data$se_filtered <- filter_proteins(lipidomics_data$se, "fraction", min = 0.66)
#heatmap missing with filtered se
vis_miss(as.data.frame(assay(lipidomics_data$se_filtered)),show_perc = TRUE, show_perc_col = TRUE, cluster = F)
ggsave("plots/missing_vis_miss_heatmap_filtered.png", width = 11, height = 8, units = "in")
plot_frequency(lipidomics_data$se)
ggsave("plots/frequency_met_identification_raw.pdf", width = 11, height = 8, units = "in")
plot_frequency(lipidomics_data$se_filtered)
ggsave("plots/frequency_met_identification_filtrered.pdf", width = 11, height = 8, units = "in")
#dimensions of the data
dim(lipidomics_data$se)
dim(lipidomics_data$se_filtered)
# % missing per patient:
round(apply(X = as.data.frame(assay(lipidomics_data$se)), function(x) sum(is.na(x)), MARGIN = 2) / nrow(as.data.frame(assay(lipidomics_data$se))) * 100 , 1)
round(apply(X = as.data.frame(assay(lipidomics_data$se_filtered)), function(x) sum(is.na(x)), MARGIN = 2) / nrow(as.data.frame(assay(lipidomics_data$se_filtered))) * 100 , 1)
#normalization
lipidomics_data$se_filt_norm <- normalize_vsn(lipidomics_data$se_filtered)
meanSdPlot(lipidomics_data$se_filt_norm)
write.csv(as.data.frame(assay(lipidomics_data$se_filtered)), "results/data_filtered.csv", row.names=TRUE)
write.csv(as.data.frame(assay(lipidomics_data$se_filt_norm)), "results/data_filt_norm.csv", row.names=TRUE)
```
## Density plot variance
```{r density plot variance}
sample_cat = rep("pl", 8)
lipidomics_data$variance_melt = reshape2::melt(t(100*lipidomics_data$relative_variance))
lipidomics_data$variance_melt$sample_cat = as.factor(rep(sample_cat, nrow(lipidomics_data$relative_variance)))
lipidomics_data$variance_melt = na.omit(lipidomics_data$variance_melt)
lipidomics_data$variance_melt$value[lipidomics_data$variance_melt$value > 300] = 300
ggplot(lipidomics_data$variance_melt, aes(x=value)) +
geom_density()
ggsave("plots/density_plot_variances_unstratified_no_correction.pdf", width = 11, height = 8, units = "in")
ggplot(lipidomics_data$variance_melt, aes(x=value, y = reorder(as.factor(Var2),value))) +
geom_boxplot() + coord_flip() +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
ggsave("plots/boxplot_variances_unstratified_no_correction.pdf", width = 11*2, height = 4, units = "in")
```
##Density plot difference
```{r density plot difference}
sample_cat = rep("pl", 8)
lipidomics_data$difference_melt = reshape2::melt(t(100*lipidomics_data$relative_difference))
lipidomics_data$difference_melt$sample_cat = as.factor(rep(sample_cat, nrow(lipidomics_data$relative_difference)))
lipidomics_data$difference_melt = na.omit(lipidomics_data$difference_melt)
lipidomics_data$difference_melt$value[lipidomics_data$difference_melt$value > 300] = 300
ggplot(lipidomics_data$difference_melt, aes(x=value)) +
geom_density()
ggsave("plots/density_plot_differences_unstratified_no_correction.pdf", width = 11, height = 8, units = "in")
ggplot(lipidomics_data$difference_melt, aes(x=value, y = reorder(as.factor(Var2),value))) +
geom_boxplot() + coord_flip() +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
ggsave("plots/boxplot_differences_unstratified_no_correction.pdf", width = 11*2, height = 4, units = "in")
```
## Make boxplots data
```{r make boxplots data}
#visualize every dataset, also raw
mean_expression_plot = function(data, file_sample, file_mass, title){
ggplot(data = reshape2::melt(data), aes(x=Var1, y=value)) +
geom_boxplot(color="darkseagreen4", fill="darkseagreen3") +
theme_set(theme_minimal()) +
theme_few() +
scale_colour_few() +
theme(legend.position = "none") +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) +
theme(axis.text=element_text(size=6)) +
ggtitle(title)
ggsave(file_sample, width = 11, height = 8, units = "in")
ggplot(data = reshape2::melt(data), aes(x=reorder(as.factor(Var2),value), y=value)) +
geom_boxplot(color="darkseagreen4", fill="darkseagreen3") +
theme_set(theme_minimal()) +
theme_few() +
scale_colour_few() +
theme(legend.position = "none") +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) +
theme(axis.text=element_text(size=6))+
ggtitle(title)
ggsave(file_mass, width = 11*2, height = 8, units = "in")
}
mean_expression_plot(data = t(assay(lipidomics_data$se)),
file_sample = "plots/boxplots_expression_each_sample.pdf",
file_mass = "plots/boxplots_expression_each_mass.pdf",
title = "Plasma lipidomics")
```
## Heatmap
```{r heatmap}
library(Polychrome)
set.seed(9)
#functions for saving the heatmaps as figures
save_pheatmap_pdf <- function(x, filename, width=11/2, height=8/2) {
stopifnot(!missing(x))
stopifnot(!missing(filename))
pdf(filename, width=width, height=height)
grid::grid.newpage()
grid::grid.draw(x$gtable)
dev.off()
}
make_pheatmap <- function(data, cluster_cols = T, main = "Heatmap", clustering_method = "ward.D", show_rownames = T,
labels_col = clin$label){
p = pheatmap::pheatmap(data, name = "expression", cutree_cols = 1,
show_colnames = T,
show_rownames = show_rownames,
fontsize = 4,
fontsize_col = 4,
fontsize_row = 2,
#annotation_col = annotation,
#annotation_colors = annotation_colours,
#annotation_row = annotation_row,
color = viridis::viridis(100, option="G", direction = -1,),
main = main,
border_color=NA,
cluster_cols = cluster_cols,
cluster_rows = F,
labels_col = labels_col,
#clustering_method = clustering_method,
na_col = "grey80")
return(p)
}
title = "lipidomics"
#create heatmaps with all patients
#without grouping, all proteins
p = make_pheatmap(data = assay(lipidomics_data$se),
cluster_cols = F,
main = paste0("Heatmap all lipids\n",title, "\n not clustered"),
show_rownames = F,
labels_col = lipidomics_data$se@colData$label)
save_pheatmap_pdf(p, filename = paste0("plots/heatmap_",title,".pdf"))
# without grouping, 100 most variable proteins
d = assay(lipidomics_data$se)
d2 = head(order(rowVars(d),decreasing = T),100)
p = make_pheatmap(data = d[d2,],
cluster_cols = F,
main = paste0("Heatmap 100 most variable lipids\n",title, "\nnot clustered"),
labels_col = lipidomics_data$se@colData$label)
save_pheatmap_pdf(p, filename = paste0("plots/heatmap_mostvar_",title,".pdf"))
#heatmap with relative variance
title = "relative_variance"
lipidomics_data$relative_variance = lipidomics_data$relative_variance * 100
lipidomics_data$relative_variance[lipidomics_data$relative_variance > 300] = 300
#without grouping, all proteins
p = make_pheatmap(data = lipidomics_data$relative_variance,
cluster_cols = F,
main = paste0("Heatmap all lipids\n",title, "\n not clustered"),
show_rownames = F,
labels_col = lipidomics_data$se@colData$label)
save_pheatmap_pdf(p, filename = paste0("plots/heatmap_",title,".pdf"))
#heatmap with relative difference
title = "relative_difference"
lipidomics_data$relative_difference = lipidomics_data$relative_difference * 100
lipidomics_data$relative_difference[lipidomics_data$relative_difference > 300] = 300
#without grouping, all proteins
p = make_pheatmap(data = lipidomics_data$relative_difference,
cluster_cols = F,
main = paste0("Heatmap all lipids\n",title, "\n not clustered"),
show_rownames = F,
labels_col = lipidomics_data$se@colData$label)
save_pheatmap_pdf(p, filename = paste0("plots/heatmap_",title,".pdf"))
#create heatmap of data before merging triplicates
title = "raw_before_merging_triplicates"
#remove ridiculous high value
data3 = lipidomics
q = quantile(data3, .9, na.rm = T)
data3[data3 > q] = q
#without grouping, all proteins
p = make_pheatmap(data = data3,
cluster_cols = F,
main = paste0("Heatmap all lipids\n",title, "\n not clustered"),
show_rownames = F,
labels_col = colnames(data3))
save_pheatmap_pdf(p, filename = paste0("plots/heatmap_",title,".pdf"))
```
## UMAP
```{r UMAP}
# set seed for reproducible results
set.seed(9)
group = "mediumpurple1"
UMAP_density_plot = function(data,
ggtitle = "UMAP with disease status labels",
legend_name = "Disease status",
labels = clin$Condition,
file_location = "plots/UMAP_condition.pdf",
file_location_labels = "plots/UMAP_condition_labels.pdf",
colour_set = c("seagreen4", "slateblue1", "salmon")){
# run umap function
umap_out = umap::umap(data, n_neighbors = 7)
umap_plot = as.data.frame(umap_out$layout)
#add condition labels
umap_plot$group = labels
# plot umap
p1 = ggplot(umap_plot) + geom_point(aes(x=V1, y=V2, color = as.factor(group))) +
ggtitle(ggtitle) +
theme_few() +
scale_colour_few() +
scale_color_manual(name = legend_name,
labels = levels(as.factor(umap_plot$group)),
values = colour_set)
xdens <-
axis_canvas(p1, axis = "x") +
geom_density(data = umap_plot, aes(x = V1, fill = group, colour = group), alpha = 0.3) +
scale_fill_manual( values = colour_set) +
scale_colour_manual( values = colour_set)
ydens <-
axis_canvas(p1, axis = "y", coord_flip = TRUE) +
geom_density(data = umap_plot, aes(x = V2, fill = group, colour = group), alpha = 0.3) +
coord_flip() +
scale_fill_manual(values = colour_set) +
scale_colour_manual( values = colour_set)
p1 %>%
insert_xaxis_grob(xdens, grid::unit(1, "in"), position = "top") %>%
insert_yaxis_grob(ydens, grid::unit(1, "in"), position = "right") %>%
ggdraw()
p1
# save umap
ggsave(file_location, width = 11/2, height = 8/2, units = "in")
p1 + geom_text(label = rownames(umap_plot), x = umap_plot$V1, y = umap_plot$V2,
hjust = 0, nudge_x = 1, size = 1.5, colour = "grey")
# save umap with labels
ggsave(file_location_labels, width = 11/2, height = 8/2, units = "in")
}
d = as.data.frame(t(assay(lipidomics_data$se_filt_norm)))
labels_group = as.factor(rep("plasma", 8))
title = "filt_norm"
#perform plots with function
UMAP_density_plot(data = d,
ggtitle = paste0("UMAP with fluid labels\n", title),
legend_name = "Fluid labels",
labels = labels_group,
file_location = paste0("plots/UMAP_fluid_group_",title,".pdf"),
file_location_labels = paste0("plots/UMAP_fluid_group_labels_",title,".pdf"),
colour_set = group)
```
## Scatterplots variance
```{r scatterplots variance}
combinations = list(c(1,2), c(1,3), c(2,3))
combinations_text = c("1_and_2", "1_and_3", "2_and_3")
plotlist = plotlist_relative =list()
k = 1
for(i in 1:length(samples)){
for(j in 1:length(combinations)){
print(paste0("i is ",i))
print(paste0("j is ",j))
title = paste0(samples[i], "_", combinations_text[j])
patient = grep(samples[i], colnames(lipidomics))
a = lipidomics[,patient]
a = a[,combinations[[j]]]
a = na.omit(a)
a = log2(a)
a$diff = abs(a[,1] - a[,2])
a$mean = apply(X = a[,1:2], MARGIN = 1, function(x) mean(x, na.rm = TRUE))
a$relative_diff = a$diff/a$mean
colnames(a) = c("t_1", "t_2", "diff", "mean", "relative_diff")
x = ggplot(a, aes(x=t_1, y=t_2)) +
geom_point(aes(color=diff), alpha = 0.5) +
scale_colour_gradient(low = "lightpink", high="seagreen") +
ggtitle(title)
#xlim(-8, 8) +
#ylim(-8, 8)
plotlist[[k]] = x
x = ggplot(a, aes(x=t_1, y=t_2)) +
geom_point(aes(color=relative_diff), alpha = 0.5) +
scale_colour_gradient(low = "lightpink", high="seagreen", limits = c(0, 1)) +
ggtitle(title)
#xlim(-8, 8) +
#ylim(-8, 8)
plotlist_relative[[k]] = x
k = k+1
}
}
library(ggpubr)
allplots <- ggarrange(plotlist=plotlist,
labels = 1:length(plotlist),
ncol = 3, nrow = (length(plotlist)/3))
ggsave("plots/scatterplot_differences_plasma.pdf", width = 11*2, height = 8*3, units = "in")
allplots_relative <- ggarrange(plotlist=plotlist_relative,
labels = 1:length(plotlist_relative),
ncol = 3, nrow = (length(plotlist_relative)/3))
ggsave("plots/scatterplot_relative_differences_plasma.pdf", width = 11*2, height = 8*3, units = "in")
```
## Overlap metabolomics and lipidomics
```{r overlap metabolomics and lipidomics}
metabolomics = read.csv(file = "/Users/clara.meijs/Desktop/PhD/Proj_PremodiALS/Metabolomics Andrej Kovac/results/data_filt_norm_imp_MinProb.csv", row.names = 1)
metabolites = rownames(metabolomics)
lipids = lipidomics_data$se_filt_norm@NAMES
symbols = c(" ", ":", "-", "_", "[.]", "/")
for(symbol in symbols){
metabolites = gsub(pattern = symbol, replacement = "", x = metabolites)
lipids = gsub(pattern = symbol, replacement = "", x = lipids)
}
sum(lipids %in% metabolites)
# install.packages("ggVennDiagram")
library(ggVennDiagram)
lipids_and_metabolites = list(lipids = lipids,
metabolites = metabolites)
# 2D Venn diagram
ggVennDiagram(lipids_and_metabolites, set_color = c("darksalmon", "yellow4")) +
scale_fill_gradient(low = "white", high = "grey50") +
scale_color_manual(values = c("darksalmon", "yellow4"))
ggsave(file = "plots/venn_diagram.pdf", width = 11/2, height = 8/2, units = "in")
lipids_and_metabolites_overlap = list(
only_lipids = lipidomics_data$se_filt_norm@NAMES[!lipids %in% metabolites],
overlap = lipidomics_data$se_filt_norm@NAMES[lipids %in% metabolites],
only_metabolites = rownames(metabolomics)[!metabolites %in% lipids]
)
for(i in 1:length(lipids_and_metabolites_overlap)){
lipids_and_metabolites_overlap[[i]] = as.data.frame(lipids_and_metabolites_overlap[[i]])
}
write_xlsx(lipids_and_metabolites_overlap, path = "results/lipids_and_metabolites_overlap.xlsx")
```
## Sessioninfo
```{r}
sessionInfo()
```