-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCassiopeia_LFQ.Rnw
executable file
·2791 lines (2034 loc) · 107 KB
/
Cassiopeia_LFQ.Rnw
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
\documentclass[a4paper, 12pt]{article}
\usepackage[a4paper,left=1.5cm,right=1.5cm,top=1cm,bottom=2cm]{geometry}
\usepackage{float}
\begin{document}
\title{LFQ data analysis}
\date{\today}
\maketitle
\tableofcontents
\newpage
<< set your analysis parameters , echo = FALSE, message = FALSE, warning = FALSE>>=
# main parameters
filename = "vignette_proteinGroups.txt"
groups = rep(c("ctrl+N","ctrl-N", "PD1+N","PD1-N"),each=3)
export_matrix = TRUE
export_amica = TRUE
remove_contaminants = TRUE
razor_plus_unique_peptides_filter = TRUE
min_number_razor_plus_unique_peptides = 2
mode_valid_values_filter = "in_at_least_one_group"
number_valid_values_filter = 3
renormalization_median = FALSE
renormalization_quantile = FALSE
renormalization_loess = TRUE
renormalization_to_proteins = NULL
renormalization_to_sample = NULL
mode_imputation = "normal"
downshift = 1.8
width = 0.3
pairwise_comp = list(c("ctrl-N","PD1-N"),c("ctrl+N","PD1+N"),c("PD1-N","PD1+N"))
batch = NULL
proteins_of_special_interest = c("RTCB","DDX1","C14orf166","FAM98B","PYROXD1","FAM96B")
perform_gsea = TRUE
organism = "hsapiens"
number_of_clusters = 5
export_clusters = TRUE
infer_optimal_number_of_clusters = TRUE
reorder_samples_for_k_means_clustering = FALSE
# less relevant parameters:
colors = NULL
seed = 123
list_number = 10
plot_number = 10
closer_look_sample_number = "all"
trend_limma = TRUE
number_of_GSEAplots = 1
@
<< load required packages, echo = FALSE, message = FALSE, warning = FALSE>>=
## read in packages
library(ggfortify)
library(limma)
library(ggplot2)
library(fpc)
library(RColorBrewer)
library(dendextend)
library(pals)
library(readr)
library(dplyr)
library(stringr)
library(gplots)
library(msigdbr)
library(clusterProfiler)
library(enrichplot)
@
\section{Chosen Parameters}
\vspace{0.5cm}
\noindent These are the parameters used for generating this report:
<< print chosen parameters >>=
print(filename)
print(groups)
print(export_matrix)
print(export_amica)
print(remove_contaminants)
print(razor_plus_unique_peptides_filter)
print(min_number_razor_plus_unique_peptides)
print(mode_valid_values_filter)
print(number_valid_values_filter)
print(renormalization_median)
print(renormalization_quantile)
print(renormalization_loess)
print(renormalization_to_proteins)
print(renormalization_to_sample)
print(mode_imputation)
print(downshift)
print(width)
print(pairwise_comp)
print(perform_gsea)
print(organism)
print(trend_limma)
print(batch)
print(proteins_of_special_interest)
print(number_of_clusters)
print(reorder_samples_for_k_means_clustering)
print(infer_optimal_number_of_clusters)
print(export_clusters)
@
\noindent Based on the parameter called groups, it was assumed that every experimental condition had the following number of replicates:
<< required calculations, echo = FALSE, message = FALSE, warning = FALSE>>=
## check number of replicates
number_of_replicates <- length(groups)/length(unique(groups))
if ((number_of_replicates) %in% 1:5){
print(number_of_replicates)
} else {
writeLines("warning: number of replicates per group either not uniform or > 5")
number_of_replicates <- 4
}
## convert batch-variable to a factor
if(!is.null(batch)){
factor_batch <- factor(batch)
} else{
factor_batch <- factor(rep(1, times=length(groups)))
}
## check colors
if (is.null(colors)){
ngroups <- length(table(groups))
palette <- rainbow(ngroups)
colors <- palette[factor(groups)]
}
## create custom color palette functions (for CONS as well as all proteins) barring (!) yellow
f_CONs <- colorRampPalette(c("white","#404040"))
f_proteins <- colorRampPalette(c("darkslateblue","blue","red","darkorchid","aquamarine","lightgreen","navy","turquoise","forestgreen","skyblue","plum","dodgerblue2","darkseagreen2", "cadetblue4", "chocolate", "bisque", "gold", "deeppink", "dodgerblue4", "indianred4", "indianred1", "lightsalmon1", "midnightblue", "mediumblue", "orange1", "mediumpurple3", "red3", "rosybrown2", "turquoise2", "wheat2", "slateblue2", "royalblue2", "purple2", "deeppink4", "firebrick", "coral", "tomato2", "#ff073a", "#06ffcb", "#00725a", "#610043", "#0024c3","#c30024", "#e94b3c", "#e94b3c", "#9becf3", "#9af3cd", "#cc9af2", "#d9a583", "#F0C013"))
## write heatmap function
heatmap_plot <- function(m, groups, legend_colors, sample_names, type="normal", dendrogram="column", labrow="", bool_rowv=TRUE, bool_colv = TRUE, plot_path=NULL){
# create groups
names(colors) <- levels(groups)
# replaces NAs with 0
m[is.na(m)] <- 0
colnames(m) <- sample_names
# should rows be reordered
if (bool_rowv){
rowv <- as.dendrogram(hclust(dist(m)))
} else {
rowv <- FALSE
}
# should columns be reordered
if (is.logical(bool_colv)){
if (bool_colv){
colv <- as.dendrogram(hclust(dist(t(m))))
} else {
colv <- FALSE
}
} else {
colv <- bool_colv
}
# specify colors
if(is.null(legend_colors)){
sidecolors <- rep("white", times=ncol(m))
} else{
sidecolors <- legend_colors[groups]
}
# create color palette
colors_heatmap <- rev(brewer.pal(11, "RdBu"))
colors_heatmap[6] <- "#fffec8"
heatmap_pal <- colorRampPalette(colors_heatmap)
rdbu_colors = heatmap_pal(20)[2:19]
heatmap_pal <- colorRampPalette(rdbu_colors)
# plot heatmap
par(mfrow=c(1,1))
par(xpd=TRUE)
if (type == "normal"){
heatmap.2(m,
Rowv = rowv,
Colv=colv,
margins=c(8,8), cexCol = 1,labRow=labrow, col=heatmap_pal(50), ColSideColors = sidecolors, symkey = F,
cex.lab=1.5, scale="none", trace="none", dendrogram=dendrogram,
key.xlab = "log2 Intensity")
}
if (type == "centered"){
min_m <- min(m, na.rm=TRUE)
max_m <- max(m, na.rm=TRUE)
heatmap.2(m,
Rowv = rowv,
Colv=colv,
labRow=labrow, margins=c(8,8), cexCol=1, ColSideColors = sidecolors, trace="none",col=heatmap_pal(50),
breaks = seq(from=-0.95,to=0.95, length.out=51),
symkey = F,
key.xlab = "log2 Intensity\n(centered at 0)",
dendrogram=dendrogram)
}
if (type == "standardized"){
min_m <- min(m, na.rm=TRUE)
max_m <- max(m, na.rm=TRUE)
heatmap.2(m,
Rowv = rowv,
Colv= colv,
labRow=labrow, margins=c(8,8), ColSideColors = sidecolors, trace="none",col=heatmap_pal(50), symkey = F,
breaks = seq(from=-2,to=2, length.out=51),
dendrogram=dendrogram)
}
# add legend
par(xpd=TRUE)
}
@
<< read in data and curate it, echo = FALSE, message = FALSE, warning = FALSE>>=
## read proteinGroups.txt file
df <- read.delim(file=filename,sep="\t", header=TRUE,stringsAsFactors=FALSE, check.names=FALSE)
## add column "Protein names" if missing in proteingroups.txt (extract info from FASTA-header)
if (!"Protein names" %in% names(df) || sum(df$`Protein names` == "") > 0 ){
protein_names <- character(nrow(df)) #initialize final vector
fasta_headers <- df$`Fasta headers`
fasta_headers_split <- strsplit(fasta_headers, split=";")
for (i in 1:length(fasta_headers_split)){
fasta_headers_split_i <- fasta_headers_split[[i]]
protein_names_split_i <- substring(fasta_headers_split_i ,first= regexpr(fasta_headers_split_i,pattern=" ") + 1, last=regexpr(fasta_headers_split_i,pattern="OS=") -2)
protein_names_i <- paste0(protein_names_split_i, collapse=";")
protein_names[i] <- protein_names_i
}
df$`Protein names` <- protein_names
}
## add column "Gene names" if missing in proteingroups.txt (extract info from FASTA-header)
if (!"Gene names" %in% names(df) || sum(df$`Gene names` == "") > 0 ){
gene_names <- character(nrow(df)) #initialize final vector
fasta_headers <- df$`Fasta headers`
fasta_headers <- ifelse(substring(fasta_headers, first=1, last=1)==";",
no=fasta_headers,
yes=substring(fasta_headers, first=regexpr(fasta_headers,pattern="[A-Za-z0-9]"), last=10000))
fasta_headers_split <- strsplit(fasta_headers, split=";")
for (i in 1:length(fasta_headers_split)){
# in case protein i had a valid entry already, take the original entry
if ("Gene names" %in% names(df) && !df$`Gene names`[i]== ""){
gene_names[i] <- df$`Gene names`[i]
next()
}
fasta_headers_split_i <- fasta_headers_split[[i]]
if (length(fasta_headers_split_i) > 0 && any(grepl(fasta_headers_split_i,pattern="GN="))){
gene_names_split_i <- substring(fasta_headers_split_i ,first= regexpr(fasta_headers_split_i,pattern="GN=") + 3, last=nchar(fasta_headers_split_i))
} else{
gene_names_split_i <- substring(fasta_headers_split_i ,first = 1, last=nchar(fasta_headers_split_i))
}
gene_names_split_i <- substring(gene_names_split_i, first=1, last=regexpr(gene_names_split_i, pattern=" |$")-1)
gene_names_split_i
if(length(gene_names_split_i)>1){
gene_names_i <- paste0(gene_names_split_i, collapse=";")
} else{
gene_names_i <- gene_names_split_i
}
gene_names[i] <- ifelse(length(gene_names_i)>0, yes=gene_names_i, no="")
}
df$`Gene names` <- gene_names
}
## replace NAs in column "potential contaminants" with ""
potential_con <- df$`Potential contaminant`
potential_con[is.na(potential_con)] <- ""
df$`Potential contaminant` <- potential_con
## create nice and short, non-empty names (if gene name is empty, take protein ID)
nice_names <- ifelse(df$`Gene names`=="", yes=df$`Majority protein IDs`,no = df$`Gene names`)
ind_split <- regexpr(nice_names, pattern=";")
for (j in 1:length(nice_names)){
if(ind_split[j]==-1) {
next
}
temp_entry <- nice_names[j]
temp_entry_new <- substring(temp_entry,first=1, last=ind_split[j] -1)
nice_names[j] <- temp_entry_new
}
df$`Nice names` <- nice_names
## attribute a unique color to each "nice name", saved as "col_all_proteins". Contaminants will be colored in different grey tones. Also add "other", assign yellow color
col_all_proteins <- sample(f_proteins(nrow(df)))
CON_bool <- df$`Potential contaminant` == "+"
col_all_proteins[CON_bool] <- f_CONs(sum(CON_bool))
names(col_all_proteins) <- df$`Nice names`
col_all_proteins <- c(col_all_proteins, setNames("yellow",nm="other"))
## create a copy of dataframe for matrix export later if needed
if (export_matrix | export_amica){
df_initial <- df
}
@
\vspace{0.5cm}
\vspace{0.5cm}
\vspace{0.5cm}
\section{Quality Control and Initial Filtering}
\vspace{0.5cm}
\subsection{Sample Names}
\noindent These are the samples that Cassiopeia will be analyzing (extracted from intensity column names):
<< extract samplenames from intensity columns, echo = FALSE, message = FALSE, warning = FALSE>>=
## find the columns that contain LFQ intensities
bool_LFQ <- grepl(colnames(df), pattern="LFQ")
## extract LFQ intensity columns as a seperate dataframe
names <- names(df[,bool_LFQ, drop=FALSE])
## define a shorter version of intensity column names denoting the sample identity
samplenames<- sub(names, pattern="LFQ.intensity.", replace = "")
samplenames
@
<<echo = FALSE, message = FALSE, warning = FALSE>>=
writeLines(paste("In total:", length(samplenames), "samples"))
@
\noindent Make sure that this sample order corresponds to your specified groups parameter:
<<>>=
print(groups)
@
\vspace{0.5cm}
\subsection{Distribution of Protein Scores}
\vspace{0.5cm}
\noindent The following plot shows the distribution of Protein Scores as density for both reverse and non-reverse hits:
\vspace{0.5cm}
<<density_reverse_nonreverse,echo = FALSE, fig.width =4.5, fig.height =4, warning = FALSE, fig.align="center">>=
## plot densities of the score-variable for reverse and non-reverse hits
ggplot(data=df) +
geom_density(aes(x=Score, fill=Reverse), alpha=0.4, size=0) +
scale_fill_manual(values=c("#E69F00","#999999")) +
theme_classic(base_size = 8) +
xlim(range(df$Score))
@
\subsection{Initial Filtering}
<<echo = FALSE, message = FALSE, warning = FALSE>>=
writeLines(paste("Before filtering, proteinGroups.txt has", dim(df)[1], "rows (protein groups)."))
@
\noindent The subsequent initial filtering includes filtering out reverse hits as well as protein groups that were only identified by (modification) site.
<<echo = FALSE, message = FALSE, warning = FALSE>>=
## filter out "reverse" hits
if ("Reverse" %in% names(df)){
df$`Reverse`[is.na(df$`Reverse`)] <- ""
reverse_bool <- df$Reverse=="+"
df <- df[!reverse_bool,]
}
## filter out "only identified by site" hits
if ("Only identified by site" %in% names(df)){
df$`Only identified by site`[is.na(df$`Only identified by site`)] <- ""
only_site_bool<- df$`Only identified by site`=="+"
df <- df[!only_site_bool,]
}
@
<<echo = FALSE, message = FALSE, warning = FALSE>>=
writeLines(paste("After initial filtering,", dim(df)[1], "rows (protein groups) remain."))
@
\subsection{Checking Normalization}
\vspace{0.5cm}
\subsubsection{Based on Boxplots}
<<prepare intensities for plotting, echo = FALSE, fig.width =7, fig.height =4, warning = FALSE>>=
## extract LFQ intensity columns as a seperate dataframe
df_LFQ <- df[,bool_LFQ, drop=FALSE]
## replace zeros with NAs
df_LFQ[df_LFQ==0] <- NA
## log-transform
df_LFQ <- log(df_LFQ,base=2)
## replace each sample name with a shorter, more convenient version
colnames(df_LFQ) <- samplenames
## find the columns that contain raw intensities
raw_bool <- grepl(colnames(df), pattern="Intensity ")
## extract raw intensity columns as a seperate dataframe
df_raw <- df[,raw_bool, drop=FALSE]
## replace zeros with NAs
df_raw[df_raw==0] <- NA
# log2-transform
df_raw <- log(df_raw,base=2)
# replace each sample name with a shorter, more convenient version
colnames(df_raw) <- samplenames
@
\noindent Plotting distributions of log2 raw intensities as well as log2 LFQ intensities for each sample:
<<raw_intensities_boxplots, echo = FALSE, fig.width =10, fig.height =5.5, warning = FALSE, fig.align="center">>=
## plot raw intensities
par(mar=c(8,4,4,2))
boxplot(df_raw,las=2,main="Raw Intensities",border=colors,xaxt="n",yaxt="n",ylab="log2 Raw Intensity", lwd=1.5)
axis(side=1, at= 1:length(samplenames),las=2, labels=samplenames, cex.axis = 0.75)
axis(side=2, at= 1:10*5)
@
<<LFQ_intensities_boxplots, echo = FALSE, fig.width =10, fig.height =5.5, warning = TRUE, fig.align="center">>=
## plot LFQ intensities
par(mar=c(8,4,4,2))
boxplot(df_LFQ,las=2,main="LFQ Intensities",border=colors,xaxt="n",yaxt="n",ylab="log2 LFQ Intensity", lwd=1.5)
axis(side=1, at= 1:length(samplenames),las=2, labels=samplenames, cex.axis = 0.75)
axis(side=2, at= 1:10*5)
@
\vspace{0.5cm}
\subsubsection{Based on Scatterplots}
\noindent If there are more than 5 samples, the following plot will randomly select 5 samples and plot their LFQ intensities as pairwise scatterplots:
<<scatterplot_check_normalization, echo = FALSE, fig.width =9, fig.height =6, warning = TRUE, fig.align="center">>=
## define helper function for pairs-plot:
my_line <- function(x,y,...){
points(x,y,pch=16, col=colors_scatter[prot_con][order(prot_con)], xlim=range(as.matrix(df_LFQ),na.rm=TRUE),ylim=range(as.matrix(df_LFQ),na.rm=TRUE))
abline(a = 0,b = 1, col="black", lty="dashed")
}
## paiwise scatterplots of LFQ intensities from different samples
if(ncol(df_LFQ)< 2){
print("not enough samples to create a scatterplot")
} else{
if (ncol(df_LFQ) == 2){
CON_bool <- df$`Potential contaminant`=="+"
names(df_LFQ)
plot(df_LFQ[,1], df_LFQ[,2], main="", yaxt="n",xaxt="n",yaxt="n", pch=16, col="grey", ylab="", xlab="", cex=1.2)
points(df_LFQ[CON_bool,1], df_LFQ[CON_bool,2],pch=16, col="#E69F00", cex=1.2)
abline(a=0,b=1, col="black", lty="dashed")
axis(side=1, cex.axis=0.8)
axis(side=2, cex.axis=0.8)
title(xlab=names(df_LFQ)[1], ylab=names(df_LFQ)[2], cex.lab=0.85)
legend("bottomright", bty="n", legend=c("non-CON", "CON"), col=c("grey", "#E69F00"),pch=16, inset=c(0.02,0.02))
} else{
if (ncol(df_LFQ) >= 3 & ncol(df_LFQ) <= 5){
colors_scatter <- c("#E69F00","grey")
names(colors_scatter) <- c("+","-")
prot_con <- df$`Potential contaminant`
prot_con <- ifelse(prot_con=="+",yes=prot_con, no="-")
pairs(df_LFQ[rev(order(prot_con)),], panel=my_line, cex.labels=0.9 ,oma=c(3,3,3,12))
par(xpd=TRUE)
legend("right", bty="n", legend=c("non-CON", "CON"), col=c("grey", "#E69F00"),pch=16,cex=0.8)
} else {
colors_scatter <- c("#E69F00","grey")
names(colors_scatter) <- c("+","-")
prot_con <- df$`Potential contaminant`
prot_con <- ifelse(prot_con=="+",yes=prot_con, no="-")
five_random_indices <- sample(1:length(names), size=5)
five_random_indices
df_LFQ_five <- df_LFQ[,five_random_indices]
pairs(df_LFQ_five[order(prot_con),], panel=my_line, oma=c(3,3,3,12), cex.labels=0.9)
par(xpd=TRUE)
legend("right", bty="n", legend=c("non-CON", "CON"), col=c("grey", "#E69F00"),pch=16, cex=0.8)
}
}
}
@
\vspace{0.5cm}
\subsection{Contamination and Top Proteins}
\vspace{0.5cm}
\noindent Plotting relative amount of contaminants per sample by iBAQ intensities:
<<CON_relative_amount, echo = FALSE, fig.width =7, fig.height =4, warning = FALSE>>=
## find rows containing contaminants
CON_bool <- df$`Potential contaminant`=="+"
## find the columns that contain iBAQ intensities and extract them (and not iBAQ peptides!)
iBAQ_bool <- grepl(colnames(df), pattern="iBAQ.") & !grepl(colnames(df), pattern="iBAQ peptides")
df_iBAQ <- df[,iBAQ_bool, drop=FALSE]
df_iBAQ[df_iBAQ==0] <- NA
## plot results
df_iBAQ_CON <- df_iBAQ[CON_bool,,drop=FALSE]
summed_iBAQ_CON <- colSums(df_iBAQ_CON,na.rm=TRUE)
summed_iBAQ <- colSums(df_iBAQ, na.rm=TRUE)
CON_ratios <- summed_iBAQ_CON/summed_iBAQ
par(mar=c(8,4,4,2))
barplot(CON_ratios,las=2,main="Relative Amount of Contaminants \n based on iBAQ Intensities",border=colors,ylim=c(0,1), names.arg=samplenames, cex.names=0.7, cex.main=0.8, yaxt="n")
axis(side=2, cex.axis=0.7, mgp=c(0,0.7,0), las=2, lwd.ticks=0.5)
@
\vspace{0.5cm}
\noindent Listing the top protein groups (rows) of the whole experiment based on total iBAQ Intensities over all samples, including contaminants:
<< list top proteins in terms of overall iBAQ Intensities, echo = FALSE, message = FALSE, warning = FALSE,fig.width =4, fig.height =3>>=
## calculate iBAQ percentages
summed_iBAQ <- rowSums(df_iBAQ,na.rm=TRUE)
df_temp <- cbind(df, summed_iBAQ)
df_temp$Percentage <- round(summed_iBAQ/sum(summed_iBAQ,na.rm=TRUE), digits=3)*100
## reorder rows of new dataframe according to total iBAQ (decreasing). Extract top iBAQ proteins
ind_order <- order(summed_iBAQ, decreasing = TRUE)
df_temp <- df_temp[ind_order,]
df_temp$Name<- df_temp$`Nice names`
df_topProteins <- head(df_temp[,c("summed_iBAQ","Percentage","Name")], n=list_number)
df_topProteins
@
\vspace{0.5cm}
\noindent Taking a closer look at the following samples (per default: all samples):
<<echo = FALSE, message = FALSE, warning = FALSE, fig.width =4, fig.height =1.75, >>=
if(closer_look_sample_number=="all"){
ind_closer_look <- (1:length(samplenames))[order(groups)]
} else{
ind_closer_look <- closer_look_sample_number
}
cat("\n")
print(samplenames[ind_closer_look])
@
\vspace{0.5cm}
\noindent The following barplots show relative iBAQ intensities for each sample seperately. In each plot, the top x protein groups per sample including contaminants are highlighted. Per default, the top 10 protein groups + all other protein groups (aggregated to a single category "other", displayed in yellow) are shown, arranged in decreasing order from bottom to top - with the exception of "other" proteins, which are always put at the very bottom.
\vspace{0.5cm}
\vspace{0.5cm}
<<top_iBAQ_proteins, echo = FALSE, message = FALSE, warning = FALSE, fig.width =10, fig.height =2, >>=
## set plot specifics
if (closer_look_sample_number == "all"){
par(mfrow=c(1,number_of_replicates))
} else{
par(mfrow=c(1,1))
}
par(mar=c(1,2.5,2,1))
## go over each sample and plot top proteins
for (i in ind_closer_look){
# calculate iBAQ fraction for each protein
df_iBAQ_i <- df_iBAQ[,i]
fraction_iBAQ_i<- df_iBAQ_i/(sum(df_iBAQ_i,na.rm=TRUE))
# reorder, and assign and replace non-top proteins with "other"
iBAQ_order_i <- order(fraction_iBAQ_i, decreasing=TRUE)
plot_fraction_iBAQ <- fraction_iBAQ_i[iBAQ_order_i]
plotNames <- df$`Nice names`[iBAQ_order_i]
plotNames[(plot_number+1):nrow(df)] <- "other"
# calculate plot values, and convert into matrix
plot_values <- tapply(plot_fraction_iBAQ, INDEX=plotNames, FUN=sum, na.rm=TRUE)
plot_rownames <- names(plot_values)
plot_values <- matrix(plot_values,ncol=1)
rownames(plot_values) <- plot_rownames
# set "other" proteins to the bottom per default
bool <- rownames(plot_values)=="other"
plot_values <- rbind(plot_values[bool,,drop=FALSE],plot_values[!bool,,drop=FALSE])
# set title and plot
title <- paste("sample", samplenames[i])
# generate list for legend
if(number_of_replicates == 5){
bty <- "n"
cex <- 0.5
x <- "right"
argslegend <- list(bty=bty,cex=cex,x=x, inset=c(-0.1,0))
} else {
if (number_of_replicates == 1){
bty <- "n"
cex <- 0.65
x <- "right"
argslegend <- list(bty=bty,cex=cex,x=x, inset=c(0.3,0))
} else {
if (number_of_replicates == 2 | number_of_replicates == 3){
bty <- "n"
cex <- 0.65
x <- "right"
argslegend <- list(bty=bty,cex=cex,x=x, inset=c(0.12,0))
} else{
bty <- "n"
cex <- 0.6
x <- "right"
argslegend <- list(bty=bty,cex=cex,x=x, inset=c(0,0))
}
}
}
# plot top proteins
barplot(plot_values, main=title, col=col_all_proteins[rownames(plot_values)], xlim = c(0, 4), legend.text=rownames(plot_values),args.legend=argslegend,cex.main=0.8, cex.axis=1)
}
@
\vspace{0.5cm}
\noindent The following barplots show the relative amount of the top x contaminants based on all Contaminants (therefore always scaling up to 1!), for each sample seperately.
\vspace{0.5cm}
<<top_contaminants, echo = FALSE, message = FALSE, warning = FALSE, fig.width =10, fig.height =2>>=
# List top contaminants for each sample
CON_bool <- df$`Potential contaminant` == "+"
## set plot specifics
if (closer_look_sample_number == "all"){
par(mfrow=c(1,number_of_replicates))
} else{
par(mfrow=c(1,1))
}
par(mar=c(1,2.5,2,1))
## go over each sample and plot top contaminants proteins if there are contaminants
if( any(CON_bool) ){
for (i in ind_closer_look){
# calculate iBAQ fraction for each protein
df_iBAQ_i <- df_iBAQ[CON_bool,i]
fraction_iBAQ_i<- df_iBAQ_i/(sum(df_iBAQ_i,na.rm=TRUE))
# reorder, and assign and replace non-top proteins with "other"
iBAQ_order_i <- order(fraction_iBAQ_i, decreasing=TRUE)
plot_fraction_iBAQ <- fraction_iBAQ_i[iBAQ_order_i]
plotNames <- df[CON_bool,]$`Nice names`[iBAQ_order_i]
plotNames[(plot_number+1):length(df_iBAQ_i)] <- "other"
# calculate plot values, and convert into matrix
plot_values <- tapply(plot_fraction_iBAQ, INDEX=plotNames, FUN=sum, na.rm=TRUE)
plot_rownames <- names(plot_values)
plot_values <- matrix(plot_values,ncol=1)
rownames(plot_values) <- plot_rownames
# set "other" proteins to the bottom per default
bool <- rownames(plot_values)=="other"
plot_values <- rbind(plot_values[bool,,drop=FALSE],plot_values[!bool,,drop=FALSE])
# set title and plot
title <- paste("sample", samplenames[i])
# generate list for legend
if(number_of_replicates == 5){
bty <- "n"
cex <- 0.5
x <- "right"
argslegend <- list(bty=bty,cex=cex,x=x, inset=c(-0.1,0))
} else {
if (number_of_replicates == 1){
bty <- "n"
cex <- 0.65
x <- "right"
argslegend <- list(bty=bty,cex=cex,x=x, inset=c(0.3,0))
} else {
if (number_of_replicates == 2 | number_of_replicates == 3){
bty <- "n"
cex <- 0.65
x <- "right"
argslegend <- list(bty=bty,cex=cex,x=x, inset=c(0.12,0))
} else{
bty <- "n"
cex <- 0.6
x <- "right"
argslegend <- list(bty=bty,cex=cex,x=x, inset=c(0,0))
}
}
}
# plot top proteins
barplot(plot_values, main=title, col=col_all_proteins[rownames(plot_values)], xlim = c(0, 4), legend.text=rownames(plot_values),args.legend=argslegend,cex.main=0.8, cex.axis=1)
}
}
@
\vspace{0.5cm}
\vspace{0.5cm}
\vspace{0.5cm}
\section{Advanced Filtering}
\vspace{0.5cm}
\subsection{Based on Contaminants}
\noindent This filtering step filters out rows (protein groups) considered as contaminants, as long as the respective parameter is set on TRUE (default setting). The current parameter chosen is:
<< contaminants filter settings >>=
print(remove_contaminants)
@
<<remove contaminants, echo = FALSE, message = FALSE, warning = FALSE>>=
writeLines(paste("Before this filtering step, there are ", dim(df)[1], "rows (protein groups)."))
if (remove_contaminants == TRUE & "Potential contaminant" %in% names(df)){
CON_bool <- df$`Potential contaminant`=="+"
df <- df[!CON_bool,]
}
writeLines(paste("After this filtering step,", dim(df)[1], "rows (protein groups) remain."))
@
\subsection{Based on Razor + Unique Peptides}
<< razor + unique filter settings >>=
print(razor_plus_unique_peptides_filter)
print(min_number_razor_plus_unique_peptides)
@
<<filter based on razor + unique peptides, echo = FALSE, message = FALSE, warning = FALSE>>=
if (razor_plus_unique_peptides_filter & "Razor + unique peptides" %in% names(df)) {
writeLines(paste("Before this filtering step, there are ", dim(df)[1], "rows (protein groups)."))
writeLines(paste("Removing rows (protein groups) with less than", min_number_razor_plus_unique_peptides, "razor + unique peptides."))
df <- df[df$`Razor + unique peptides` >= min_number_razor_plus_unique_peptides,]
writeLines(paste("After this filtering step,", dim(df)[1], "rows (protein groups) remain."))
}
@
\subsection{Based on Valid Values}
\noindent This final filtering step filters out rows (protein groups) based on minimum number of valid values in the LFQ intensity columns (in case a renormalization stragegy is employed, this filtering step is instead based on the minimum number of valid values in the raw intensity columns). The mode and the minimum number of valid values can be changed via their corresponding parameters. The parameters currently chosen are:
<< valid value filter settings >>=
print(mode_valid_values_filter)
print(number_valid_values_filter)
@
<< filter based on valid values, echo = FALSE, message = FALSE, warning = FALSE>>=
## number of protein groups before filtering
writeLines(paste("Before this filtering step, there are ", dim(df)[1], "rows (protein groups)."))
## get unique groups and its length
unique_groups <- unique(groups)
n_unique_groups <- length(unique_groups)
## create dataframe where each valid value is represented by a TRUE. In case a renormalization strategy is employed, the NA-information will be extracted from the raw intensities
if( renormalization_median | renormalization_quantile | renormalization_loess | !is.null(renormalization_to_proteins) | !is.null(renormalization_to_sample) ){
df_validvalues <- df[,raw_bool,drop=FALSE]
df_validvalues[df_validvalues==0] <- NA
df_validvalues <- !is.na(df_validvalues)
} else{
df_validvalues <- df[,bool_LFQ,drop=FALSE]
df_validvalues[df_validvalues== 0] <- NA
df_validvalues <- !is.na(df_validvalues)
}
###############################################################################
## filter when mode is "in_at_least_one_group"
if (mode_valid_values_filter == "in_at_least_one_group"){
# initiate helper matrix (one column for each group)
bool_matrix <- matrix(rep(TRUE,times=nrow(df)*n_unique_groups), ncol=n_unique_groups)
colnames(bool_matrix) <- unique_groups
# create logical vector indicating which rows to keep; then filter
for (i in unique_groups){
df_i <- df_validvalues[,groups==i,drop=FALSE]
rowsum_i <- apply(df_i,MARGIN = 1, FUN=sum)
rowsum_i_bool <- rowsum_i >= number_valid_values_filter
bool_matrix[,i] <- rowsum_i_bool
}
bool_keep <- apply(bool_matrix, MARGIN = 1, FUN=any)
df$`Valid Values Filter (removed)` <- ifelse(test=bool_keep, yes="", no="+")
df <- df[bool_keep,]
}
###############################################################################
# filter when mode is "in_each_group"
if (mode_valid_values_filter == "in_each_group"){
# initiate helper matrix (one column for each group)
bool_matrix <- matrix(rep(TRUE,times=nrow(df)*n_unique_groups), ncol=n_unique_groups)
colnames(bool_matrix) <- unique_groups
# create logical vector indicating which rows to keep; then filter
for (i in unique_groups){
df_i <- df_validvalues[,groups==i, drop=FALSE]
rowsum_i <- apply(df_i,MARGIN = 1, FUN=sum)
rowsum_i_bool <- rowsum_i >= number_valid_values_filter
bool_matrix[,i] <- rowsum_i_bool
}
bool_keep <- apply(bool_matrix, MARGIN = 1, FUN=all)
df$`Valid Values Filter (removed)` <- ifelse(test=bool_keep, yes="", no="+")
df <- df[bool_keep,]
}
###########################################################################
# filter when mode is "in_total"
if (mode_valid_values_filter == "in_total"){
# calculate logical vector indicating which rows to keep; then filter
rowsum_valid_values <- apply(df_validvalues, MARGIN=1, FUN=sum)
rowsum_bool <- rowsum_valid_values >= number_valid_values_filter
bool_keep <- rowsum_bool
df$`Valid Values Filter (removed)` <- ifelse(test=bool_keep, yes="", no="+")
df <- df[bool_keep,]
}
## number of protein groups after filtering
writeLines(paste("After this filtering step,", dim(df)[1], "rows (protein groups) remain."))
@
\noindent The rest of this report will focus exclusively on the proteins (rows) that are left after this final filtering step, i.e. every protein that has been discarded by now will not be included in the subsequent analysis.
<<log transform LFQ intensities, echo = FALSE, message = FALSE, warning = FALSE>>=
## extract feature data as a seperate data frame
df_feature <- df[,!bool_LFQ, drop=FALSE]
## extract LFQ intensity columns as a seperate dataframe
df_LFQ <- df[,bool_LFQ, drop=FALSE]
## replace zeros with NAs
df_LFQ[df_LFQ==0] <- NA
## log transform
df_LFQ <- log(df_LFQ,base=2)
## stitch together
df <- cbind(df_LFQ, df_feature)
@
\vspace{0.5cm}
\subsection{Renormalization after Advanced Filtering}
\noindent All the available renormalization methods use the raw intensities only. Choosing one will replace the MaxQuant LFQ intensities with normalized raw intensities (i.e. new LFQ intensities are created and used for the remainder of the analysis).
<< renormalization settings >>=
print(renormalization_median)
print(renormalization_quantile)
print(renormalization_loess)
print(renormalization_to_proteins)
print(renormalization_to_sample)
@
<<renormalization_LFQ_boxplots, echo = FALSE, fig.width =10, fig.height =5.5, warning = TRUE>>=