-
Notifications
You must be signed in to change notification settings - Fork 0
/
Proteomics_premodials.Rmd
1432 lines (1146 loc) · 60.1 KB
/
Proteomics_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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: "Proteomics Premodials TF Christof Lenz"
author: "Clara Meijs"
date: "2023-10-06"
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)
library(ggpubr)
library(tibble)
```
## 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}
#load the dataset
TF_data = read.table(file = 'data/L_Tzeplaeff_TearFluidTable_NonNormalized_NonImputed.txt', sep = '\t', header = TRUE)
plasma_data = read.table(file = 'data/2023_37b_L_Tzeplaeff_PlasmaTable_NonNormalized_NonImputed.txt', sep = '\t', header = TRUE)
#select only Homo Sapiens genes (filter n=5)
dim(TF_data)
TF_data = TF_data[grep("Homo sapiens", TF_data$PG.Organisms),]
dim(TF_data)
dim(plasma_data)
plasma_data = plasma_data[grep("Homo sapiens", plasma_data$PG.Organisms),]
dim(plasma_data)
#take only gene name, UniProt accession number, and quantity columns
TF_data = TF_data[,c("PG.ProteinGroups", "PG.Genes", colnames(TF_data)[grep("PG.Quantity", colnames(TF_data))])]
dim(TF_data)
plasma_data = plasma_data[,c("PG.ProteinGroups", "PG.Genes", colnames(plasma_data)[grep("PG.Quantity", colnames(plasma_data))])]
dim(plasma_data)
length(unique(c(TF_data$PG.ProteinGroups, plasma_data$PG.ProteinGroups)))
#remove proteins without name
TF_data$PG.Genes[TF_data$PG.Genes==";KRT10;KRT10"] = "KRT10"
TF_data$PG.Genes[TF_data$PG.Genes==";KRT84"] = "KRT84"
TF_data = TF_data[TF_data$PG.Genes!="",]
plasma_data$PG.Genes[plasma_data$PG.Genes==";TGM3"] = "TGM3"
plasma_data$PG.Genes[plasma_data$PG.Genes==";ATP5MG;ATP5MG"] = "ATP5MG"
plasma_data$PG.Genes[plasma_data$PG.Genes==";NDUFA9"] = "NDUFA9"
plasma_data$PG.Genes[plasma_data$PG.Genes==";HBG1;HBG2"] = "HBG1"
plasma_data$PG.Genes[plasma_data$PG.Genes==";KRT10"] = "KRT10"
plasma_data = plasma_data[plasma_data$PG.Genes!="",]
#select rows with duplicate A and duplicate B and check if the duplicates have the same order
index_dup_A_TF = grep("_A_", colnames(TF_data))
index_dup_B_TF = grep("_B_", colnames(TF_data))
index_dup_A_plasma = grep("_A_", colnames(plasma_data))
index_dup_B_plasma = grep("_B_", colnames(plasma_data))
a = as.data.frame(cbind(colnames(TF_data[,index_dup_A_TF]), colnames(TF_data[,index_dup_B_TF])))
a = as.data.frame(cbind(colnames(plasma_data[,index_dup_A_plasma]), colnames(plasma_data[,index_dup_B_plasma])))
#Conclusion, for plasma they are not the same order
#shorten sample names
f = function(name){
a = str_split(name, pattern = "_")[[1]]
a = paste0(a[7], "_", a[8], "_", a[9])
return(a)
}
colnames(TF_data)[3:ncol(TF_data)] = as.vector(sapply(colnames(TF_data)[3:ncol(TF_data)], function(x) f(x)))
colnames(plasma_data)[3:ncol(plasma_data)] = as.vector(sapply(colnames(plasma_data)[3:ncol(plasma_data)], function(x) f(x)))
#make NaN into NA
TF_data[TF_data == "NaN"] = NA
for(i in 1:ncol(TF_data)){TF_data[is.nan(TF_data[,i]),i] = NA}
plasma_data[plasma_data == "NaN"] = NA
for(i in 1:ncol(plasma_data)){plasma_data[is.nan(plasma_data[,i]),i] = NA}
#take first gene name or first uniprot accession number
f = function(name){
a = str_split(name, pattern = ";")[[1]]
return(a[1])
}
TF_data[,"PG.ProteinGroups"] = as.vector(sapply(TF_data[,"PG.ProteinGroups"], function(x) f(x)))
TF_data[,"PG.Genes"] = as.vector(sapply(TF_data[,"PG.Genes"], function(x) f(x)))
plasma_data[,"PG.ProteinGroups"] = as.vector(sapply(plasma_data[,"PG.ProteinGroups"], function(x) f(x)))
plasma_data[,"PG.Genes"] = as.vector(sapply(plasma_data[,"PG.Genes"], function(x) f(x)))
TF_data = TF_data[TF_data$PG.Genes!="",]
plasma_data = plasma_data[plasma_data$PG.Genes!="",]
#make the gene names the rownames, after making them unique.
rownames(TF_data) = make.unique(TF_data[,"PG.Genes"])
rownames(plasma_data) = make.unique(plasma_data[,"PG.Genes"])
#make empty matrix for merged duplicates
plasma_merged = as.data.frame(matrix(data = NA, nrow = nrow(plasma_data), ncol = length(index_dup_A_plasma)+2))
TF_merged = as.data.frame(matrix(data = NA, nrow = nrow(TF_data), ncol = length(index_dup_A_TF)+2))
plasma_merged[,1:2] = plasma_data[,1:2]
TF_merged[,1:2] = TF_data[,1:2]
rownames(plasma_merged) = rownames(plasma_data)
rownames(TF_merged) = rownames(TF_data)
sample_IDs_TF = colnames(TF_data)[index_dup_A_TF]
sample_IDs_TF = gsub("A_", "", sample_IDs_TF)
sample_IDs_plasma = colnames(plasma_data)[index_dup_A_plasma]
sample_IDs_plasma = gsub("A_", "", sample_IDs_plasma)
colnames(TF_merged) = c("Uniprot", "Gene_Symbol", sample_IDs_TF)
colnames(plasma_merged) = c("Uniprot", "Gene_Symbol", sample_IDs_plasma)
TF_difference = TF_missing = TF_merged
plasma_difference = plasma_missing = plasma_merged
#perform merging of duplicates
for(i in 1:length(sample_IDs_plasma)){
for(j in 1:nrow(plasma_data)){
A = plasma_data[j,grep(sample_IDs_plasma[i], colnames(plasma_data))[1]]
B = plasma_data[j,grep(sample_IDs_plasma[i], colnames(plasma_data))[2]]
mean = mean(c(A,B), na.rm = T)
difference = abs(A - B)
missing = sum(is.na(c(A,B)))
plasma_merged[j, sample_IDs_plasma[i]] = mean
plasma_difference[j, sample_IDs_plasma[i]] = difference
plasma_missing[j, sample_IDs_plasma[i]] = missing
}}
for(i in 1:length(sample_IDs_TF)){
for(j in 1:nrow(TF_data)){
A = TF_data[j,grep(sample_IDs_TF[i], colnames(TF_data))[1]]
B = TF_data[j,grep(sample_IDs_TF[i], colnames(TF_data))[2]]
mean = mean(c(A,B), na.rm = T)
difference = abs(A - B)
missing = sum(is.na(c(A,B)))
TF_merged[j, sample_IDs_TF[i]] = mean
TF_difference[j, sample_IDs_TF[i]] = difference
TF_missing[j, sample_IDs_TF[i]] = missing
}}
TF_merged[TF_merged == "NaN"] = NA
plasma_merged[plasma_merged == "NaN"] = NA
#count missing
sum(is.na(TF_data))
sum(is.na(TF_merged))
sum(is.na(plasma_data))
sum(is.na(plasma_merged))
#calculate relative variance
TF_relative_diff = (TF_difference[,3:ncol(TF_difference)]/TF_merged[,3:ncol(TF_difference)])*100
plasma_relative_diff = (plasma_difference[,3:ncol(plasma_difference)]/plasma_merged[,3:ncol(plasma_difference)])*100
#make summarized experiments
#TF
TF_merged2 = TF_merged[,3:ncol(TF_merged)]
abundance.columns <- 1:ncol(TF_merged2) # get abundance column numbers
clin = data.frame(label = colnames(TF_merged2)[1:ncol(TF_merged2)], #very limited clinical variables
condition = rep("control", ncol(TF_merged2)) ,
replicate = 1:ncol(TF_merged2))
TF_merged2$name = rownames(TF_merged2)
TF_merged2$ID = TF_merged$Uniprot
experimental.design = clin
se_TF <- make_se(TF_merged2, abundance.columns, experimental.design)
#plasma
plasma_merged2 = plasma_merged[,3:ncol(plasma_merged)]
abundance.columns <- 1:ncol(plasma_merged2) # get abundance column numbers
clin = data.frame(label = colnames(plasma_merged2), #very limited clinical variables
condition = rep("control", ncol(plasma_merged2)) ,
replicate = 1:ncol(plasma_merged2))
plasma_merged2$name = rownames(plasma_merged2)
plasma_merged2$ID = plasma_merged$Uniprot
experimental.design = clin
se_plasma <- make_se(plasma_merged2, abundance.columns, experimental.design)
#save data
write.csv(plasma_data, "results/raw_data_plasma.csv", row.names=TRUE)
write.csv(TF_data, "results/raw_data_TF.csv", row.names=TRUE)
write.csv(plasma_merged, "results/raw_data_no_duplicates_plasma.csv", row.names=TRUE)
write.csv(TF_merged, "results/raw_data_no_duplicates_TF.csv", row.names=TRUE)
write.csv(plasma_difference, "results/difference_of_duplicates_plasma.csv", row.names=TRUE)
write.csv(TF_difference, "results/variance_of_duplicates_TF.csv", row.names=TRUE)
write.csv(plasma_relative_diff, "results/relative_difference_of_duplicates_plasma.csv", row.names=TRUE)
write.csv(TF_relative_diff, "results/relative_difference_of_duplicates_TF.csv", row.names=TRUE)
write.csv(plasma_missing, "results/missing_within_duplicates_plasma.csv", row.names=TRUE)
write.csv(TF_missing, "results/missing_within_duplicates_TF.csv", row.names=TRUE)
```
## Scatterplots duplicates
```{r scatterplots duplicates}
#create the duplicate matrices using the duplicates indices
plasma_A = plasma_data[,index_dup_A_plasma]
plasma_B = plasma_data[,index_dup_B_plasma]
TF_A = TF_data[,index_dup_A_TF]
TF_B = TF_data[,index_dup_B_TF]
#remove the "A_" and "B_" from the colnames
colnames(plasma_A) = gsub("A_", "", colnames(plasma_A))
colnames(plasma_B) = gsub("B_", "", colnames(plasma_B))
colnames(TF_A) = gsub("A_", "", colnames(TF_A))
colnames(TF_B) = gsub("B_", "", colnames(TF_B))
#make the order of the matrices identical
plasma_A = plasma_A[,sample_IDs_plasma]
plasma_B = plasma_B[,sample_IDs_plasma]
TF_A = TF_A[,sample_IDs_TF]
TF_B = TF_B[,sample_IDs_TF]
plasma_A = reshape::melt(as.matrix(plasma_A))
plasma_B = reshape::melt(as.matrix(plasma_B))
TF_A = reshape::melt(as.matrix(TF_A))
TF_B = reshape::melt(as.matrix(TF_B))
plasma_long = as.data.frame(cbind(plasma_A, plasma_B$value))
colnames(plasma_long)[3:4] = c("duplicate_A", "duplicate_B")
plasma_long = na.omit(plasma_long)
plasma_long$duplicate_A = log2(plasma_long$duplicate_A)
plasma_long$duplicate_B = log2(plasma_long$duplicate_B)
TF_long = as.data.frame(cbind(TF_A, TF_B$value))
colnames(TF_long)[3:4] = c("duplicate_A", "duplicate_B")
TF_long = na.omit(TF_long)
TF_long$duplicate_A = log2(TF_long$duplicate_A)
TF_long$duplicate_B = log2(TF_long$duplicate_B)
a = ggplot(plasma_long, aes(x=duplicate_A, y=duplicate_B)) +
geom_point( color="darksalmon", alpha = 0.5) +
geom_abline(intercept = 0, slope = 1) +
ggtitle("scatterplot duplicates plasma") +
theme_few()
b = ggplot(TF_long, aes(x=duplicate_A, y=duplicate_B)) +
geom_point( color="yellow4", alpha = 0.5) +
geom_abline(intercept = 0, slope = 1) +
ggtitle("scatterplot duplicates tear fluid") +
theme_few()
ggarrange(a, b, ncol = 2, nrow = 1)
ggsave("plots/scatterplots_duplicates.pdf", width = 11, height = 8/2, units = "in")
```
## Missing inspection
```{r missing inspection}
#filter plasma and TF data
se_plasma_filt <- filter_proteins(se_plasma, "fraction", min = 0.66)
se_TF_filt = filter_proteins(se_TF, "fraction", min = 0.66)
#make missing heatmap and frequency plots
se_list = list(se_plasma = se_plasma, se_TF = se_TF, se_plasma_filt = se_plasma_filt, se_TF_filt = se_TF_filt)
freq_plots = list()
missing_plots = list()
for(i in 1:length(se_list)){
name = names(se_list)[i]
missing_plots[[i]] = vis_miss(as.data.frame(assay(se_list[[i]])) ,
show_perc = TRUE, show_perc_col = TRUE, cluster = F) + ggtitle(name)
freq_plots[[i]] = plot_frequency(se_list[[i]]) + ggtitle(name)
names(freq_plots)[i] = names(missing_plots)[i] = name
}
missing_plots$raw_plasma = vis_miss(plasma_data[,3:ncol(plasma_data)],
show_perc = TRUE, show_perc_col = TRUE, cluster = F) + ggtitle("plasma raw with duplicates")
missing_plots$raw_TF = vis_miss(TF_data[,3:ncol(TF_data)],
show_perc = TRUE, show_perc_col = TRUE, cluster = F) + ggtitle("tear fluid raw with duplicates")
#plot all missing heatmaps
ggarrange(plotlist = missing_plots, ncol = 2, nrow = 3)
ggsave("plots/missing_heatmap_plots.jpg", width = 11, height = 8*2, units = "in")
#plot all frequency plots
ggarrange(plotlist = freq_plots, ncol = 2, nrow = 3)
ggsave("plots/missing_freq_plots.jpg", width = 11, height = 8*2, units = "in")
#dimensions of the data
dim(se_plasma)
dim(se_plasma_filt)
dim(se_TF)
dim(se_TF_filt)
#normalization
se_list$se_plasma_filt_norm = normalize_vsn(se_list[["se_plasma_filt"]])
se_list$se_TF_filt_norm = normalize_vsn(se_list[["se_TF_filt"]])
# Plot intensity distributions and cumulative fraction of proteins
# with and without missing values
detect_plots = list()
for(i in 1:length(se_list)){
name = names(se_list)[i]
detect_plots[[i]] = plot_detect(se_list[[i]])
names(detect_plots)[i] = name
}
ggarrange(plotlist = detect_plots, ncol = 2, nrow = 3, labels = names(detect_plots))
ggsave("plots/intensity_distribution_missing_vs_non-missing.pdf", width = 11, height = 8*2, units = "in")
# Impute missing data using random draws from a
# Gaussian distribution centered around a minimal value (for MNAR)
se_list$se_TF_MinProb_imp <- impute(se_list$se_TF_filt_norm, fun = "MinProb", q = 0.01)
se_list$se_plasma_MinProb_imp <- impute(se_list[["se_plasma_filt_norm"]], fun = "MinProb", q = 0.01)
# Impute missing data using random draws from a
# manually defined left-shifted Gaussian distribution (for MNAR)
se_list$se_TF_man_imp <- impute(se_list[["se_TF_filt_norm"]], fun = "man", shift = 1.8, scale = 0.3)
se_list$se_plasma_man_imp <- impute(se_list[["se_plasma_filt_norm"]], fun = "man", shift = 1.8, scale = 0.3)
# Impute missing data using the k-nearest neighbour approach (for MAR)
se_list$se_TF_knn_imp <- impute(se_list[["se_TF_filt_norm"]], fun = "knn", rowmax = 0.9)
se_list$se_plasma_knn_imp <- impute(se_list[["se_plasma_filt_norm"]], fun = "knn", rowmax = 0.9)
# Plot intensity distributions before and after imputation
imp_plots = list()
imp_plots$TF = plot_imputation(se_list$se_TF_filt_norm, se_list$se_TF_MinProb_imp, se_list$se_TF_man_imp, se_list$se_TF_knn_imp)
imp_plots$plasma = plot_imputation(se_list$se_plasma_filt_norm, se_list$se_plasma_MinProb_imp, se_list$se_plasma_man_imp, se_list$se_plasma_knn_imp)
ggarrange(plotlist = imp_plots, ncol = 2, nrow = 1, labels = names(imp_plots))
ggsave("plots/intensity_distribution_imputations.pdf", width = 11, height = 8, units = "in")
#save all data in results
for(i in 1:length(se_list)){
write.csv(as.data.frame(assay(se_list[[i]])), paste0("results/data_",names(se_list)[i],".csv"), row.names=TRUE)
}
```
## Make boxplots data
```{r make boxplots data}
#visualize every dataset, also raw
mean_expression_plot = function(data, title){
plot_patient = 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)
plot_protein = 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)
return(list(plot_patient = plot_patient, plot_protein = plot_protein))
}
patient_plots = protein_plots = list()
for(i in 1:length(se_list)){
r = mean_expression_plot(t(assay(se_list[[i]])), title = names(se_list)[i])
patient_plots[[i]] = r[["plot_patient"]]
protein_plots[[i]] = r[["plot_protein"]]
names(protein_plots)[i] = names(patient_plots)[i]= names(se_list)[i]
}
ggarrange(plotlist = patient_plots, nrow = 3, ncol = 4)
ggsave("plots/boxplots_each_patient.pdf", width = 11*4, height = 8*4, units = "in")
ggarrange(plotlist = protein_plots, nrow = 12, ncol = 1)
ggsave("plots/boxplots_each_protein.pdf", width = 11*2, height = 8*4, units = "in")
```
## Venn Diagram Proteins
```{r venn diagram proteins}
library(ggVennDiagram)
#compare within Lenz lab data
#UNIPROT
proteins_plasma = se_list[["se_plasma_filt_norm"]]@elementMetadata@listData[["ID"]]
proteins_TF = se_list[["se_TF_filt_norm"]]@elementMetadata@listData[["ID"]]
proteins = list(proteins_plasma = proteins_plasma,
proteins_TF = proteins_TF)
ggVennDiagram(proteins, set_color = c("darksalmon", "yellow4")) +
scale_fill_gradient(low = "white", high = "white") +
scale_color_manual(values = c("darksalmon", "yellow4")) +
ggtitle("Uniprot")
ggsave(file = "plots/venn_diagram_uniprot.pdf", width = 11/2, height = 8/2, units = "in")
#GENE_SYMBOL
proteins_plasma_gene_symbol = se_list[["se_plasma_filt_norm"]]@NAMES
proteins_TF_gene_symbol = se_list[["se_TF_filt_norm"]]@NAMES
proteins = list(proteins_plasma = proteins_plasma_gene_symbol,
proteins_TF = proteins_TF_gene_symbol)
ggVennDiagram(proteins, set_color = c("darksalmon", "yellow4")) +
scale_fill_gradient(low = "white", high = "white") +
scale_color_manual(values = c("darksalmon", "yellow4"))+
ggtitle("Gene Symbol")
ggsave(file = "plots/venn_diagram_gene.pdf", width = 11/2, height = 8/2, units = "in")
#venn diagram plasma from Wojciech
se_Wojciech = readRDS(file = "/Users/clara.meijs/Desktop/PhD/Proj_PremodiALS/Proteomics Wojciech Kuban/results/se_plasma_list.rds")
#GENE SYMBOL
Wojciech = se_Wojciech$plasma_norm@NAMES
ggVennDiagram(list(proteins_plasma_Lenz = proteins_plasma_gene_symbol, proteins_plasma_Wojciech = Wojciech), set_color = c("darksalmon", "orange4")) +
scale_fill_gradient(low = "white", high = "white") +
scale_color_manual(values = c("darksalmon", "orange4"))+
ggtitle("Gene Symbol")
ggsave(file = "plots/venn_diagram_vs_Wojciech_proteins_gene.pdf", width = 11/2, height = 8/2, units = "in")
#UNIPROT
Wojciech = se_Wojciech$plasma_norm@elementMetadata@listData[["ID"]]
ggVennDiagram(list(proteins_plasma_Lenz = proteins_plasma, proteins_plasma_Wojciech = Wojciech), set_color = c("darksalmon", "orange4")) +
scale_fill_gradient(low = "white", high = "white") +
scale_color_manual(values = c("darksalmon", "orange4"))+
ggtitle("Uniprot")
ggsave(file = "plots/venn_diagram_vs_Wojciech_proteins_uniprot.pdf", width = 11/2, height = 8/2, units = "in")
#venn diagram tear fluid proteins from Lena-sophie's project
TF_data_Lena_Sophie = read.table(file = '/Users/clara.meijs/Desktop/PhD/Proj_ALS_tear_fluid/Claras_code/Proteomics models/data/New_proteomics_data_expression.txt', sep = '\t', header = TRUE)
#select only Homo Sapiens genes (filter n=5)
TF_data_Lena_Sophie = TF_data_Lena_Sophie[grep("Homo sapiens", TF_data_Lena_Sophie$PG.Organisms),]
#take only gene name, UniProt accession number, and quantity columns
TF_data_Lena_Sophie = TF_data_Lena_Sophie[,c("PG.ProteinGroups", "PG.Genes", colnames(TF_data_Lena_Sophie)[grep("PG.Quantity", colnames(TF_data_Lena_Sophie))])]
#remove proteins without name
TF_data_Lena_Sophie = TF_data_Lena_Sophie[TF_data_Lena_Sophie$PG.Genes!="",]
#select rows with duplicate A and duplicate B and check if the duplicates have the same order
index_dup_A_TF_LS = grep("_A_", colnames(TF_data_Lena_Sophie))
index_dup_B_TF_LS = grep("_B_", colnames(TF_data_Lena_Sophie))
#Conclusion, for plasma they are not the same order
#shorten sample names
f = function(name){
a = str_split(name, pattern = "_")[[1]]
a = paste0(a[5], "_", a[6], "_", a[7])
return(a)
}
colnames(TF_data_Lena_Sophie)[3:ncol(TF_data_Lena_Sophie)] = as.vector(sapply(colnames(TF_data_Lena_Sophie)[3:ncol(TF_data_Lena_Sophie)], function(x) f(x)))
#make NaN into NA
TF_data_Lena_Sophie[TF_data_Lena_Sophie == "NaN"] = NA
TF_data_Lena_Sophie[TF_data_Lena_Sophie == "Filtered"] = NA
for(i in 1:ncol(TF_data_Lena_Sophie)){TF_data_Lena_Sophie[is.nan(TF_data_Lena_Sophie[,i]),i] = NA}
#take first gene name or first uniprot accession number
f = function(name){
a = str_split(name, pattern = ";")[[1]]
return(a[1])
}
TF_data_Lena_Sophie[,"PG.ProteinGroups"] = as.vector(sapply(TF_data_Lena_Sophie[,"PG.ProteinGroups"], function(x) f(x)))
TF_data_Lena_Sophie[,"PG.Genes"] = as.vector(sapply(TF_data_Lena_Sophie[,"PG.Genes"], function(x) f(x)))
TF_data_Lena_Sophie = TF_data_Lena_Sophie[TF_data_Lena_Sophie$PG.Genes!="",]
#make the gene names the rownames, after making them unique.
rownames(TF_data_Lena_Sophie) = make.unique(TF_data_Lena_Sophie[,"PG.Genes"])
#make empty matrix for merged duplicates
TF_merged_LS = as.data.frame(matrix(data = NA, nrow = nrow(TF_data_Lena_Sophie), ncol = length(index_dup_A_TF_LS)+2))
rownames(TF_merged_LS) = rownames(TF_data_Lena_Sophie)
sample_IDs_TF_LS = colnames(TF_data_Lena_Sophie)[index_dup_B_TF_LS]
sample_IDs_TF_LS = gsub("B_", "", sample_IDs_TF_LS)
colnames(TF_merged_LS) = c("Uniprot", "Gene_Symbol", sample_IDs_TF_LS)
TF_data_Lena_Sophie[, 3:ncol(TF_data_Lena_Sophie)] <- lapply(TF_data_Lena_Sophie[, 3:ncol(TF_data_Lena_Sophie)], function(x) gsub(",", ".", x))
TF_data_Lena_Sophie[, 3:ncol(TF_data_Lena_Sophie)] <- lapply(TF_data_Lena_Sophie[, 3:ncol(TF_data_Lena_Sophie)], as.numeric)
#perform merging of duplicates
for(i in 1:length(sample_IDs_TF_LS)){
for(j in 1:nrow(TF_data_Lena_Sophie)){
A = TF_data_Lena_Sophie[j,grep(sample_IDs_TF_LS[i], colnames(TF_data_Lena_Sophie))[1]]
B = TF_data_Lena_Sophie[j,grep(sample_IDs_TF_LS[i], colnames(TF_data_Lena_Sophie))[2]]
mean = mean(c(A,B), na.rm = T)
TF_merged_LS[j, sample_IDs_TF_LS[i]] = mean
}}
TF_merged_LS[TF_merged_LS == "NaN"] = NA
TF_merged_LS[,1:2] = TF_data_Lena_Sophie[,1:2]
#make summarized experiments
#TF
TF_merged_LS2 = TF_merged_LS[,3:ncol(TF_merged_LS)]
abundance.columns <- 1:ncol(TF_merged_LS2) # get abundance column numbers
clin = data.frame(label = colnames(TF_merged_LS2)[1:ncol(TF_merged_LS2)], #very limited clinical variables
condition = rep("control", ncol(TF_merged_LS2)) ,
replicate = 1:ncol(TF_merged_LS2))
TF_merged_LS2$name = rownames(TF_merged_LS2)
TF_merged_LS2$ID = TF_merged_LS$Uniprot
experimental.design = clin
se_TF_LS <- make_se(TF_merged_LS2, abundance.columns, experimental.design)
se_TF_LS_filt = filter_proteins(se_TF_LS, "fraction", min = 0.66)
#GENE SYMBOL
Lena_Sophie = se_TF_LS_filt@NAMES
ggVennDiagram(list(proteins_TF_Lenz = proteins_TF_gene_symbol, proteins_TF_Lena_Sophie = Lena_Sophie), set_color = c("yellow4", "mediumpurple2")) +
scale_fill_gradient(low = "white", high = "white") +
scale_color_manual(values = c("yellow4", "mediumpurple2"))+
ggtitle("Gene Symbol")
ggsave(file = "plots/venn_diagram_vs_Lena_Sophie_proteins_gene.pdf", width = 11/2, height = 8/2, units = "in")
#UNIPROT
Lena_Sophie = se_TF_LS_filt@elementMetadata@listData[["ID"]]
ggVennDiagram(list(proteins_TF_Lenz = proteins_TF, proteins_TF_Lena_Sophie = Lena_Sophie), set_color = c("yellow4", "mediumpurple2")) +
scale_fill_gradient(low = "white", high = "white") +
scale_color_manual(values = c("yellow4", "mediumpurple2"))+
ggtitle("Uniprot")
ggsave(file = "plots/venn_diagram_vs_Lena_Sophie_proteins_uniprot.pdf", width = 11/2, height = 8/2, units = "in")
venn_diagram_results = list()
venn_diagram_results$uniprot_only_plasma = proteins_plasma[!proteins_plasma %in% proteins_TF]
venn_diagram_results$uniprot_only_TF = proteins_TF[!proteins_TF %in% proteins_plasma]
venn_diagram_results$uniprot_both_plasma_TF = proteins_TF[proteins_TF %in% proteins_plasma]
venn_diagram_results$uniprot_only_TF_Lena_Sophie = Lena_Sophie[!Lena_Sophie %in% proteins_TF]
venn_diagram_results$uniprot_only_TF_Lenz = proteins_TF[!proteins_TF %in% Lena_Sophie]
venn_diagram_results$uniprot_both_TF_Lena_Sophie_Lenz = proteins_TF[proteins_TF %in% Lena_Sophie]
venn_diagram_results$uniprot_only_plasma_Lenz = proteins_plasma[!proteins_plasma %in% Wojciech]
venn_diagram_results$uniprot_only_plasma_Wojciech = Wojciech[!Wojciech %in% proteins_plasma]
venn_diagram_results$uniprot_both_plasma_Lenz_Wojciech = Wojciech[Wojciech %in% proteins_plasma]
saveRDS(venn_diagram_results, file = "results/Venn_diagram_results_in_list.rds")
```
## Density plot
```{r Visualization 1b: Density plot}
#figure raw
d = as.data.frame(assay(se_list[["se_plasma"]]))
d = reshape2::melt(d)
d$technique = rep("plasma", nrow(d))
d2 = as.data.frame(assay(se_list[["se_TF"]]))
d2 = reshape2::melt(d2)
d2$technique = rep("TF", nrow(d2))
d = as.data.frame(rbind(d, d2))
a = ggplot(d, aes(x=value, color=technique)) +
geom_density() +
theme_few() +
scale_colour_few() +
ggtitle("raw data") +
scale_color_manual(values = c("darksalmon", "yellow4"))
#figure filtered
d = as.data.frame(assay(se_list[["se_plasma_filt"]]))
d = reshape2::melt(d)
d$technique = rep("plasma", nrow(d))
d2 = as.data.frame(assay(se_list[["se_TF_filt"]]))
d2 = reshape2::melt(d2)
d2$technique = rep("TF", nrow(d2))
d = as.data.frame(rbind(d, d2))
b = ggplot(d, aes(x=value, color=technique)) +
geom_density() +
theme_few() +
scale_colour_few() +
ggtitle("filtered data") +
scale_color_manual(values = c("darksalmon", "yellow4"))
#figure normalized
d = as.data.frame(assay(se_list[["se_plasma_filt_norm"]]))
d = reshape2::melt(d)
d$technique = rep("plasma", nrow(d))
d2 = as.data.frame(assay(se_list[["se_TF_filt_norm"]]))
d2 = reshape2::melt(d2)
d2$technique = rep("TF", nrow(d2))
d = as.data.frame(rbind(d, d2))
c = ggplot(d, aes(x=value, color=technique)) +
geom_density() +
theme_few() +
scale_colour_few() +
ggtitle("filtered & normalized data") +
scale_color_manual(values = c("darksalmon", "yellow4"))
ggarrange(a,b,c, ncol = 3, nrow = 1)
ggsave(file = "plots/density.pdf", width = 11*1.5, height = 3, units = "in")
```
## Heatmap
```{r heatmap}
library(Polychrome)
library(tidyr)
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, main = "Heatmap", show_rownames = T,
#labels_col,
annotation_col, annotation_colors){
p = pheatmap::pheatmap(data,
name = "expression",
show_colnames = T,
show_rownames = show_rownames,
fontsize = 4,
fontsize_col = 4,
fontsize_row = 2,
annotation_col = NA,
annotation_colors = NA,
#annotation_row = annotation_row,
color = viridis::viridis(100, option="G", direction = -1,),
main = main,
border_color = NA,
cluster_cols = F,
cluster_rows = F,
#labels_col = labels_col,
na_col = "grey80")
return(p)
}
f = function(name){
a = str_split(name, pattern = "_")[[1]][2]
a = substr(a, 0, nchar(a)-1)
return(a)
}
# loop for all datasets and all methods
for(i in 1:length(se_list)){
title = names(se_list)[i]
print(title)
se = se_list[[i]]
labels = se$label
data = assay(se)
colnames(data) = se$label
# if(grep("plasma", title)){
# labels = as.vector(sapply(labels, function(x) f(x)))
# # Generate annotations for rows and columns
# annotation_col = data.frame(sample_type = as.factor(labels))
# rownames(annotation_col) = se$label
#
# mycolors = glasbey.colors(length(unique(annotation_col$sample_type)))
# mycolors[1] = "yellow4"
# names(mycolors) <- unique(annotation_col$sample_type)
# annotation_colors <- list(sample_type = list(mycolors)[[1]])
# }else{
# annotation_col = NA
# mycolors = NA
# }
#create heatmaps with all patients
#without grouping, all proteins
p = make_pheatmap(data = data,
main = paste0("Heatmap all proteins\n",title, "\n not clustered"),
show_rownames = F)
save_pheatmap_pdf(p, filename = paste0("plots/heatmap_",title,".pdf"))
# without grouping, 100 most variable proteins
d = data
d2 = head(order(rowVars(d),decreasing = T),100)
p = make_pheatmap(data = d[d2,],
main = paste0("Heatmap 100 most variable proteins\n",title, "\nnot clustered"))
save_pheatmap_pdf(p, filename = paste0("plots/heatmap_mostvar_",title,".pdf"))
}
#heatmap with relative variance
hist(pivot_longer(TF_relative_diff, cols = 1:ncol(TF_relative_diff))$value)
title = "relative_difference_duplicates"
TF_relative_diff[TF_relative_diff > 300] = 300
TF_relative_diff[TF_relative_diff == 0] = NA
plasma_relative_diff[plasma_relative_diff > 300] = 300
plasma_relative_diff[plasma_relative_diff == 0] = NA
#without grouping, all proteins
p = make_pheatmap(data = TF_relative_diff,
main = paste0("Heatmap all proteins TF\n",title, "\n not clustered"),
show_rownames = F)
save_pheatmap_pdf(p, filename = paste0("plots/heatmap_TF_",title,".pdf"))
p = make_pheatmap(data = plasma_relative_diff,
main = paste0("Heatmap all proteins plasma\n",title, "\n not clustered"),
show_rownames = F)
save_pheatmap_pdf(p, filename = paste0("plots/heatmap_plasma_",title,".pdf"))
title = "difference_duplicates"
TF_difference[TF_difference > 300] = 300
TF_difference[TF_difference == 0] = NA
plasma_difference[plasma_difference > 300] = 300
plasma_difference[plasma_difference == 0] = NA
#without grouping, all proteins
p = make_pheatmap(data = TF_difference[,3:ncol(TF_difference)],
main = paste0("Heatmap all proteins TF\n",title, "\n not clustered"),
show_rownames = F)
save_pheatmap_pdf(p, filename = paste0("plots/heatmap_TF_",title,".pdf"))
p = make_pheatmap(data = plasma_difference[,3:ncol(TF_difference)],
main = paste0("Heatmap all proteins plasma\n",title, "\n not clustered"),
show_rownames = F)
save_pheatmap_pdf(p, filename = paste0("plots/heatmap_plasma_",title,".pdf"))
#create heatmap of data before merging triplicates
title = "raw_before_merging_duplicates"
#remove ridiculously high values
plasma_data2 = log2(plasma_data[,3:ncol(plasma_data)])
TF_data2 = log2(TF_data[,3:ncol(TF_data)])
#without grouping, all proteins
p = make_pheatmap(data = TF_data2,
main = paste0("Heatmap all proteins TF\n",title, "\n not clustered"),
show_rownames = F)
save_pheatmap_pdf(p, filename = paste0("plots/heatmap_TF_",title,".pdf"))
p = make_pheatmap(data = plasma_data2,
main = paste0("Heatmap all proteins plasma\n",title, "\n not clustered"),
show_rownames = F)
save_pheatmap_pdf(p, filename = paste0("plots/heatmap_plasma_",title,".pdf"))
```
## UMAP
```{r UMAP}
# # set seed for reproducible results
set.seed(9)
group = c("darksalmon", "yellow4")
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)
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)), alpha = 0.5) +
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")
}
d1 = t(assay(se_list[["se_plasma_MinProb_imp"]]))
d2 = t(assay(se_list[["se_TF_MinProb_imp"]]))
proteins_in_both_fluids = colnames(d1)[colnames(d1) %in% colnames(d2)]
d1 = d1[,proteins_in_both_fluids]
d2 = d2[,proteins_in_both_fluids]
d = as.data.frame(rbind(d1,d2))
labels_group = c(rep("plasma", nrow(d1)), rep("TF", nrow(d2)))
title = "plasma_vs_TF"
#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)
```
## ORA with clusterprofiler package, and only Perseus results
```{r Visualization 5c: ORA with clusterprofiler package, and only Perseus results}
library(clusterProfiler)
library(enrichplot)
library(ggplot2)
library(msigdbr)
library(dichromat)
library(stringr)
redblue<-colorRampPalette(c("red","blue"))
clusterprofiler_ORA = function(data, universe, ont, title, alpha = 0.05){ #input: named vector with log fold change
# FUNCTIONS WITHIN FUNCTION
f_enrich = function(){
cutoff = enrichGO(gene = data,
universe = universe,
OrgDb = 'org.Hs.eg.db',
keyType = 'UNIPROT',
readable = T,
ont = ont,
pvalueCutoff = alpha,
qvalueCutoff = alpha)
return(cutoff)
}
f_enrichplot = function(result){
barplot = ggplot(data=result,
aes(x=reorder(Description, minlogFDR), y=minlogFDR, fill = "red")) +
geom_bar(stat="identity") +
coord_flip() +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
#axis.title.x=element_blank(),
axis.title.y=element_blank(),
panel.background = element_blank(),
text = element_text(size = 13, family="sans"),
axis.line = element_line(colour = "black")) +
labs(
title=paste0(title, "\nFDR cut-off of ", alpha),
y ="-log10(FDR)") +
geom_text(aes(label = result$GeneRatio), colour="white",
position = position_stack(vjust = 0.5)) +
scale_x_discrete(labels = function(x) str_wrap(x, width = 40)) +
ylim(0, max(c(-log10(alpha), max(result$minlogFDR)))) +
geom_hline(yintercept = -log10(alpha))
return(barplot)
}
#END OF FUNCTIONS WITHIN FUNCTION
#HERE COMES THE ANALYSIS
res_cutoff = f_enrich()
if(nrow(res_cutoff@result[res_cutoff@result$pvalue<=alpha,])>30){
res_cutoff = clusterProfiler::simplify(res_cutoff)
}
result = res_cutoff@result
result = result[result$p.adjust<=alpha,]
if(nrow(result)>30){result = result[1:30,]}
if(nrow(result)<1){result = "no significant results"}
#change description into factor and take -log of p-adjust
if(!is.character(result)){
result$Description <- as.factor(result$Description)
result$minlogFDR = -log10(result$p.adjust)
#plot figure individually
plot = f_enrichplot(result)
#cnetplot
cnetplot_labels = cnetplot(res_cutoff, node_label = 'all', showCategory = 1500) +
ggtitle(paste0("ORA gene ontology with \n",title))
cnetplot_nolabels = cnetplot(res_cutoff, node_label = 'none', showCategory = 1500) +
ggtitle(paste0("ORA gene ontology with \n",title))
#goplot
if(nrow(result)<2){
goplot = "only one results"
}else{
goplot = goplot(res_cutoff) +
ggtitle(paste0("ORA gene ontology with \n",title))
}
#emapplot
res_cutoff2 <- pairwise_termsim(res_cutoff)
emapplot = emapplot(res_cutoff2, cex.params = list(category_label = 0.75), showCategory = 30) +
ggtitle(paste0("ORA gene ontology with \n",title))
}else{
plot = "no_significant_result"
cnetplot_labels = cnetplot_nolabels = goplot = emapplot = plot
}
results = list(pathways = result,
barplot = plot,
cnetplot_labels = cnetplot_labels,
cnetplot_nolabels = cnetplot_nolabels,
goplot = goplot,
emapplot = emapplot)
return(results)
}
##### PERFORMING THE GSEA WITH THE FUNCTION ABOVE ON PERSEUS RESULTS
#the ontologies to test:
ontologies = c("BP", "CC", "MF")
res = list()
l = 1
alpha = 0.05
plasma_universe = unique(c(venn_diagram_results$uniprot_only_plasma_Lenz,
venn_diagram_results$uniprot_only_plasma_Wojciech,