-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmethods.R
1527 lines (1457 loc) · 69.1 KB
/
methods.R
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
# Due to: http://stackoverflow.com/questions/24501245/data-table-throws-object-not-found-error
.datatable.aware=TRUE
#' Subset traces or fractions in traces object
#' @description Subset a taces object by a specified column in trace_annotation and/or fraction ids.
#' @param traces Object of class traces.
#' @param trace_subset_ids Character vector specifying the trace identifiers
#' for subsetting traces, e.g. peptide or protein ids.
#' @param trace_subset_type Character string specifying the column name
#' for applying the trace_subset_ids filter, defaults to "id".
#' @param fraction_ids Numeric vector specifying the fraction identifiers
#' for subsetting traces. The resulting traces object will not have
#' fraction ids starting from 1, co-elution feature finding might thus
#' be compromised. Please don't use this option for general
#' pre-processing purposes.
#' @return Object of class traces.
#' @examples
#' # Load some example data:
#' inputPeptideTraces <- examplePeptideTraces
#' inputProteinTraces <- exampleProteinTraces
#' subsetPeptides <- c("AIIDEFEQK","AIQLSGAEQLEALK","AKEALIAASETLK")
#' subsetProtein <- "Q15021"
#' subsetFractions <- c(5:20)
#' # Run subsetting and inspect resulting traces object:
#' peptideSubsettedPeptideTraces <- subset(inputPeptideTraces,trace_subset_ids=subsetPeptides,fraction_ids=subsetFractions)
#' summary(peptideSubsettedPeptideTraces)
#'
#' proteinSubsettedPeptideTraces <- subset(inputPeptideTraces,trace_subset_ids=subsetProtein,trace_subset_type="protein_id")
#' summary(proteinSubsettedPeptideTraces)
#'
#' proteinSubsettedProteinTraces <- subset(inputProteinTraces,trace_subset_ids=subsetProtein)
#' summary(proteinSubsettedProteinTraces)
#' @export
#'
subset.traces <- function(traces,trace_subset_ids=NULL,trace_subset_type="id",fraction_ids=NULL){
.tracesTest(traces)
if (!is.null(trace_subset_ids)) {
if (trace_subset_type %in% names(traces$trace_annotation)) {
traces$trace_annotation <- subset(traces$trace_annotation, get(trace_subset_type) %in% trace_subset_ids)
trace_ids <- traces$trace_annotation$id
traces$traces <- subset(traces$traces,id %in% trace_ids)
if(!is.null(traces[["genomic_coord"]])){
traces$genomic_coord <- traces$genomic_coord[trace_ids]
}
if (nrow(traces$traces) == 0) {
message("Caution! Subsetting returns empty traces object.")
}
} else {
stop(paste0(trace_subset_type, "is not a valid trace_subset_type."))
}
}
if (!is.null(fraction_ids)){
if(class(fraction_ids) == "numeric"){
fraction_ids <- as.character(fraction_ids)
}
fraction_ids <- intersect(names(traces$traces), fraction_ids)
traces$traces <- subset(traces$traces,select=c(fraction_ids,"id"))
traces$fraction_annotation <- subset(traces$fraction_annotation ,id %in% fraction_ids)
if (nrow(traces$traces) == 0) {
stop(paste0("fraction_ids (",fraction_ids,") do not match the available fractions in the traces object."))
}
}
traces
}
#' Subset traces or fractions in tracesList object
#' @description Subset a taces object by a specified column in trace_annotation and/or fraction ids.
#' @param traces Object of class traces.
#' @param trace_subset_ids Character vector specifying the trace identifiers
#' for subsetting traces, e.g. peptide or protein ids.
#' @param trace_subset_type Character string specifying the column name
#' for applying the trace_subset_ids filter, defaults to "id".
#' @param fraction_ids Numeric vector specifying the fraction identifiers
#' for subsetting traces. The resulting traces object will not have
#' fraction ids starting from 1, co-elution feature finding might thus
#' be compromised. Please don't use this option for general
#' pre-processing purposes.
#' @return Object of class traces.
#' @export
#'
subset.tracesList <- function(tracesList,
trace_subset_ids=NULL,
trace_subset_type="id",
fraction_ids=NULL){
.tracesListTest(tracesList)
res <- lapply(tracesList, subset.traces,
trace_subset_ids=trace_subset_ids,
trace_subset_type=trace_subset_type,
fraction_ids=fraction_ids)
class(res) <- "tracesList"
.tracesListTest(res)
return(res)
}
#' Get intensity matrix from traces object
#' @description Get a matrix of intensity values from a traces object.
#' @param traces Object of class traces.
#' @return Numeric matrix with intensity values.
#' @examples
#' intensityMatrix <- getIntensityMatrix(examplePeptideTraces)
#' head(intensityMatrix)
#' @export
getIntensityMatrix <- function(traces){
.tracesTest(traces)
tr <- copy(traces$traces)
ids <- tr$id
intensity.mat <- data.matrix(tr[,id := NULL])
rownames(intensity.mat) <- ids
intensity.mat
}
#' Convert traces from wide format to long format
#' @description Convert the data.table traces from a trace object from wide format to long format.
#' @param traces.dt A data.table with an id column \code{id} and
#' columns of continuously numbered fractions.
#' @return A data.table with columns
#' \itemize{
#' \item \code{id}
#' \item \code{fraction}
#' \item \code{intensity}
#' }
#' @examples
#' tracesWide <- examplePeptideTraces$traces
#' tracesLong <- toLongFormat(tracesWide)
#' tracesLong
#' @export
toLongFormat <- function(traces.dt) {
traces.dt.long <-
melt(traces.dt, id.var='id', variable.name='fraction',
value.name='intensity', variable.factor=FALSE)
traces.dt.long[, fraction := as.numeric(fraction)]
setkey(traces.dt.long,id)
data.table(traces.dt.long)
traces.dt.long
}
#' Annotate traces with molecular weight calibration.
#' @description Annotate fractions in traces object with calibrated molecular weight.
#' This only applies to specific fractionation strategies e.g. SEC)
#' @param traces Object of class traces.
#' @param calibration Calibration functions generated by calibrateMW().
#' @return Object of class traces with moleclar weight annotation of fractions.
#' @examples
#' # Load relevant data:
#' inputTraces <- examplePeptideTraces
#' calibrationTable <- exampleCalibrationTable
#' # Perform molecular weight calibration:
#' calibration = calibrateMW(calibrationTable)
#' # Perform molecular weight annotation:
#' mwTraces <- annotateMolecularWeight(inputTraces, calibration)
#' @export
annotateMolecularWeight <- function(traces, calibration){
UseMethod("annotateMolecularWeight", traces)
}
#' @describeIn annotateMolecularWeight Annotate single traces object
#' @export
annotateMolecularWeight.traces <- function(traces, calibration){
.tracesTest(traces)
traces$fraction_annotation[,molecular_weight := round(calibration$FractionToMW(traces$fraction_annotation$id),digits=3)]
traces
}
#' @describeIn annotateMolecularWeight Annotate tracesList object
#' @export
annotateMolecularWeight.tracesList <- function(traces, calibration){
.tracesListTest(traces)
res <- lapply(traces, annotateMolecularWeight.traces, calibration = calibration)
class(res) <- "tracesList"
.tracesListTest(res)
return(res)
}
#' Plot traces
#' @description Plot all chromatograms in a traces object. Most generic plotting function.
#' @param traces Object of class traces.
#' @param log Logical, whether the intensities should be plotted in log scale. Default is \code{FALSE}.
#' @param legend Logical, whether a legend of the traces should be plotted. Should be set to \code{FALSE}
#' if many chromatograms are plotted. Default is \code{TRUE}.
#' @param PDF Logical, whether to plot to PDF. PDF file is saved in working directory. Default is \code{FALSE}.
#' @param name Character string with name of the plot, only used if \code{PDF=TRUE}.
#' PDF file is saved under name.pdf. Default is "Traces".
#' @param colorMap named character vector containing valid color specifications for plotting.
#' The names of the vector must correspond to the ids of the peptides to be plotted.
#' @param monomer_MW Logical if monomer MWs should be indicated
#' @examples
#' # Protein traces
#' proteinTraces=exampleProteinTraces
#' plot(proteinTraces)
#' # Annotate traces with molecular weight to include molecular weight information in plot.
#' calibrationTable <- exampleCalibrationTable
#' # Perform molecular weight calibration:
#' calibration = calibrateMW(calibrationTable)
#' # Perform molecular weight annotation:
#' mwProteinTraces <- annotateMolecularWeight(proteinTraces, calibration)
#' plot(mwProteinTraces)
#' # Plot all peptides of a specific protein across a defined chromatographic region
#' peptideTraces <- examplePeptideTraces
#' subsetPeptideTraces <- subset(peptideTraces,trace_subset_ids="Q9UHV9",trace_subset_type="protein_id",fraction_ids=c(30:70))
#' plot(subsetPeptideTraces,legend=FALSE)
#' @export
plot.traces <- function(traces,
log=FALSE,
legend = TRUE,
PDF=FALSE,
name="Traces",
plot = TRUE,
colour_by = "id",
highlight=NULL,
highlight_col=NULL,
colorMap=NULL,
monomer_MW=TRUE) {
.tracesTest(traces)
traces.long <- toLongFormat(traces$traces)
traces.long <- merge(traces.long,traces$fraction_annotation,by.x="fraction",by.y="id")
if(!is.null(highlight)){
traces.long$outlier <- gsub("\\(.*?\\)","",traces.long$id) %in% gsub("\\(.*?\\)","",highlight)
if(!any(traces.long$outlier)) highlight <- NULL
}
if(colour_by!="id") {
if(!colour_by %in% names(traces$trace_annotation)){
stop("colour_by is not availbale in trace_annotation.")
}
isoform_annotation <- subset(traces$trace_annotation,select=c("id",colour_by))
traces.long <- merge(traces.long,isoform_annotation, by.x="id",by.y="id")
traces.long[,line:=paste0(get(colour_by),id)]
}
## Create a reproducible coloring for the peptides plotted
if(!is.null(colorMap)){
if(!all(unique(traces.long$id) %in% names(colorMap))){
stop("Invalid colorMap specified. Not all traces to be plotted are contained in the colorMap")
}
}else{
cbPalette <- c("#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7","#999999")
ids <- sort(unique(traces.long[[colour_by]]))
if (length(ids) <= length(cbPalette)) {
colorMap <- cbPalette[1:length(unique(traces.long[[colour_by]]))]
names(colorMap) <- ids
} else {
colorMap <- createGGplotColMap(unique(traces.long$id))
}
}
if(colour_by == "id") {
p <- ggplot(traces.long) +
geom_line(aes_string(x='fraction', y='intensity', colour='id', group='id'))
} else {
p <- ggplot(traces.long) +
geom_line(aes_string(x='fraction', y='intensity', colour=colour_by, group='line'))
}
p <- p + xlab('fraction') +
ylab('intensity') +
theme_bw() +
theme(panel.grid.major = element_blank(),panel.grid.minor = element_blank()) +
theme(plot.margin = unit(c(1,.5,.5,.5),"cm")) +
ggtitle(name) +
scale_color_manual(values=colorMap)
#theme(plot.title = element_text(vjust=19,size=10))
if (log) {
p <- p + scale_y_log10('log(intensity)')
}
if (!legend) {
p <- p + theme(legend.position="none")
} else {
if (length(unique(traces.long$id)) > 25) {
p <- p + theme(legend.position="none")
} else {
p <- p + theme(legend.position="bottom", legend.text=element_text(size = 5), legend.title = element_blank())
}
}
if(!is.null(highlight)){
legend_peps <- unique(traces.long[outlier == TRUE, id])
if(is.null(highlight_col)){
p <- p +
geom_line(data = traces.long[outlier == TRUE],
aes_string(x='fraction', y='intensity', color='id'), lwd=2) +
scale_color_manual(values=colorMap, breaks = legend_peps)
## scale_color_discrete(breaks = legend_peps)
}else{
## legend_map <- unique(ggplot_build(p)$data[[1]]$colour)
## names(legend_map) <- unique(p$data$id)
## legend_map[legend_peps] <- highlight_col
## legend_vals <- rep(highlight_col, ceiling(length(legend_peps)/ length(highlight_col)))[1:length(legend_peps)]
p <- p +
geom_line(data = traces.long[outlier == TRUE],
aes_string(x='fraction', y='intensity', lty = 'id'),
color = highlight_col, lwd=2)
# scale_color_discrete(guide = F)
## scale_color_manual(values = legend_map, limits = legend_peps)
# guides(lty = FALSE)
# scale_color_manual(limits = legend_peps, values = rep(highlight_col, length(legend_peps))) +
# geom_line(aes_string(x='fraction', y='intensity', color='id'))
}
}
if ("molecular_weight" %in% names(traces$fraction_annotation)) {
fraction_ann <- traces$fraction_annotation
tr <- lm(log(fraction_ann$molecular_weight) ~ fraction_ann$id)
intercept <- as.numeric(tr$coefficients[1])
slope <- as.numeric(tr$coefficients[2])
mwtransform <- function(x){exp(slope*x + intercept)}
MWtoFraction <- function(x){round((log(x)-intercept)/(slope), digits = 0)}
mw <- round(fraction_ann$molecular_weight, digits = 0)
breaks_MW <- mw[seq(1,length(mw), length.out = length(seq(min(traces$fraction_annotation$id),
max(traces$fraction_annotation$id),10)))]
p <- p + scale_x_continuous(name="fraction",
breaks=seq(min(traces$fraction_annotation$id),
max(traces$fraction_annotation$id),10),
labels=seq(min(traces$fraction_annotation$id),
max(traces$fraction_annotation$id),10),
sec.axis = dup_axis(trans = ~.,
breaks=seq(min(traces$fraction_annotation$id),
max(traces$fraction_annotation$id),10),
labels = breaks_MW,
name = "MW (kDa)"))
if (monomer_MW==TRUE){
if ("protein_mw" %in% names(traces$trace_annotation)) {
subunitMW.dt <- data.table(id=traces$trace_annotation$id,mw=traces$trace_annotation$protein_mw)
subunitMW.dt$fraction <- MWtoFraction(subunitMW.dt$mw)
subunitMW.dt[,boundary:=MWtoFraction(2*mw)]
if (length(unique(subunitMW.dt$mw)) > 1) {
p <- p + geom_point(data = subunitMW.dt, mapping = aes(x = fraction, y = Inf, colour=id),shape=18,size=5,alpha=.5)
} else {
p <- p + geom_vline(data = unique(subunitMW.dt), aes(xintercept = fraction), colour="red", linetype="dashed", size=.5)
p <- p + geom_vline(data = unique(subunitMW.dt), aes(xintercept = boundary), colour="red", linetype="dashed", size=.5, alpha=0.5)
}
} else {
message("No molecular weight annotation of the traces. Cannot plot monomer molecular weight.")
}
}
} else {
p <- p + scale_x_continuous(name="fraction",
breaks=seq(min(traces$fraction_annotation$id),
max(traces$fraction_annotation$id),10),
labels=seq(min(traces$fraction_annotation$id),
max(traces$fraction_annotation$id),10))
}
if(PDF){
pdf(paste0(name,".pdf"))
}
if(plot){
plot(p)
}else{
return(ggplot_gtable(ggplot_build(p)))
}
if(PDF){
dev.off()
}
}
#' Plot traces
#' @description Plot all chromatograms in a traces object. Most generic plotting function.
#' @param traces Object of class traces.
#' @param log Logical, whether the intensities should be plotted in log scale. Default is \code{FALSE}.
#' @param legend Logical, whether a legend of the traces should be plotted.
#' Should be set to \code{FALSE}
#' if many chromatograms are plotted. Default is \code{TRUE}.
#' @param PDF Logical, whether to plot to PDF. PDF file is saved in working directory.
#' Default is \code{FALSE}.
#' @param name Character string with name of the plot, only used if \code{PDF=TRUE}.
#' PDF file is saved under name.pdf. Default is "Traces".
#' @param plot Logical, wether to print or return the plot object
#' @param isoformAnnotation Logical, wether to colour traces by their isoform annotation
#' @param colour_by Character string specifying by which column to colour by. Default is id.
#' @param highlight Character vector, ids of the traces to highlight (can be multiple).
#' Default is \code{NULL}.
#' @param highlight_col Character string, A color to highlight traces in. Must be accepted by ggplot2.
#' @param colorMap named character vector containing valid color specifications for plotting.
#' The names of the vector must correspond to the ids of the peptides to be plotted.
#' @param monomer_MW Logical if monomer MWs should be indicated
#' @export
plot.tracesList <- function(traces,
design_matrix = NULL,
collapse_conditions = FALSE,
aggregateReplicates=FALSE,
log=FALSE,
legend = TRUE,
PDF=FALSE,
name="Traces",
plot = TRUE,
isoformAnnotation = FALSE,
colour_by = "id",
highlight=NULL,
highlight_col=NULL,
colorMap=NULL,
monomer_MW=TRUE) {
.tracesListTest(traces)
if(!is.null(design_matrix)){
if(!all(design_matrix$Sample_name %in% names(traces))){
stop("Invalid design matrix")
}
}else{
design_matrix <- data.table(Sample_name = names(traces),
Condition = "",
Replicate = 1:length(traces))
}
tracesList <- lapply(names(traces), function(tr){
res <- toLongFormat(traces[[tr]]$traces)
res$Condition <- design_matrix[Sample_name == tr, Condition]
res$Replicate <- design_matrix[Sample_name == tr, Replicate]
res
})
traces_long <- do.call("rbind", tracesList)
if(colour_by!="id") {
if(!colour_by %in% names(traces[[1]]$trace_annotation)){
stop("colour_by is not availbale in trace_annotation.")
}
isoform_annotation <- lapply(names(traces), function(tr){subset(traces[[tr]]$trace_annotation,select=c("id",colour_by))})
isoform_annotation <- unique(do.call("rbind", isoform_annotation))
traces_long <- merge(traces_long,isoform_annotation, by.x="id",by.y="id")
traces_long[,line:=paste0(get(colour_by),id)]
}
## Create a common fraction annotation
traces_frac <- unique(do.call("rbind", lapply(traces, "[[", "fraction_annotation")))
traces_frac <- unique(subset(traces_frac, select = names(traces_frac) %in% c("id","molecular_weight")))
traces_long <- merge(traces_long,traces_frac,by.x="fraction",by.y="id")
if(!is.null(highlight)){
traces_long$outlier <- gsub("\\(.*?\\)","",traces_long$id) %in% gsub("\\(.*?\\)","",highlight)
if(!any(traces_long$outlier)) highlight <- NULL
}
geom.text.size = 3
theme.size = (14/5) * geom.text.size
## Create a reproducible coloring for the peptides plotted
if(!is.null(colorMap)){
if(!all(unique(traces_long[[colour_by]]) %in% names(colorMap))){
stop("Invalid colorMap specified. Not all traces to be plotted are contained in the colorMap")
}
}else{
cbPalette <- c("#56B4E9","#E69F00", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7","#999999")
ids <- sort(unique(traces_long[[colour_by]]))
if (length(ids) <= length(cbPalette)) {
colorMap <- cbPalette[1:length(unique(traces_long[[colour_by]]))]
names(colorMap) <- ids
} else {
colorMap <- createGGplotColMap(ids)
}
}
if (aggregateReplicates){
traces_long[,meanIntensity := mean(intensity), by=c("Condition","fraction","id")]
traces_long[,sdIntensity := sd(intensity), by=c("Condition","fraction","id")]
traces_long <- unique(subset(traces_long,select=c("id","fraction","Condition","molecular_weight","meanIntensity","sdIntensity")))
traces_long[,Replicate := "average"]
setnames(traces_long, "meanIntensity", "intensity")
}
p <- ggplot(traces_long) +
xlab('fraction') +
ylab('intensity') +
theme_bw() +
theme(panel.grid.major = element_blank(),panel.grid.minor = element_blank()) +
theme(plot.margin = unit(c(1,.5,.5,.5),"cm")) +
ggtitle(name) +
scale_color_manual(values=colorMap) #+
#theme(plot.title = element_text(vjust=19,size=10))
if(collapse_conditions){
if(colour_by == "id") {
p <- p + facet_grid(~ Replicate) +
geom_line(aes_string(x='fraction', y='intensity', color='id', lty = 'Condition'))
if (aggregateReplicates){
p <- p + geom_errorbar(aes(x=fraction,ymin=ifelse(intensity-sdIntensity < 0, 0, intensity-sdIntensity), ymax=intensity+sdIntensity, color=id, lty = Condition), width=0.2, size=0.3, position=position_dodge(0.05))
}
} else {
message("Collapsing of conditions is not jet compatible with colouring by your selected id type.
Plot conditions separately instead.")
p <- p + facet_grid(Condition ~ Replicate) +
geom_line(aes_string(x='fraction', y='intensity', color=colour_by, group='line'))
if (aggregateReplicates){
p <- p + geom_errorbar(aes(x=fraction,ymin=ifelse(intensity-sdIntensity < 0, 0, intensity-sdIntensity), ymax=intensity+sdIntensity, color=colour_by), width=0.2, size=0.3, position=position_dodge(0.05))
}
}
}else{
if (length(unique(traces_long$Replicate)) > 1) {
if(colour_by == "id") {
p <- p + facet_grid(Condition ~ Replicate) +
geom_line(aes_string(x='fraction', y='intensity', color='id'))
if (aggregateReplicates){
p <- p + geom_errorbar(aes(x=fraction,ymin=ifelse(intensity-sdIntensity < 0, 0, intensity-sdIntensity), ymax=intensity+sdIntensity, color=id), width=0.2, size=0.3, position=position_dodge(0.05))
}
} else {
p <- p + facet_grid(Condition ~ Replicate) +
geom_line(aes_string(x='fraction', y='intensity', color=colour_by, group='line'))
}
if (aggregateReplicates){
p <- p + geom_errorbar(aes(x=fraction,ymin=ifelse(intensity-sdIntensity < 0, 0, intensity-sdIntensity), ymax=intensity+sdIntensity, color=colour_by), width=0.2, size=0.3, position=position_dodge(0.05))
}
} else {
if(colour_by == "id") {
p <- p + facet_grid(Condition ~ .) +
geom_line(aes_string(x='fraction', y='intensity', color='id'))
if (aggregateReplicates){
p <- p + geom_errorbar(aes(x=fraction,ymin=ifelse(intensity-sdIntensity < 0, 0, intensity-sdIntensity), ymax=intensity+sdIntensity, color=id), width=0.2, size=0.3, position=position_dodge(0.05))
}
} else {
p <- p + facet_grid(Condition ~ .) +
geom_line(aes_string(x='fraction', y='intensity', color=colour_by, group='line'))
if (aggregateReplicates){
p <- p + geom_errorbar(aes(x=fraction,ymin=ifelse(intensity-sdIntensity < 0, 0, intensity-sdIntensity), ymax=intensity+sdIntensity, color=colour_by), width=0.2, size=0.3, position=position_dodge(0.05))
}
}
}
}
if(!is.null(highlight)){
legend_peps <- unique(traces_long[outlier == TRUE, id])
if(is.null(highlight_col)){
if(collapse_conditions){
p <- p +
geom_line(data = traces_long[outlier == TRUE], aes_string(x='fraction', y='intensity', color='id', lty = 'Condition'), lwd=2) +
scale_color_manual(values = colorMap, breaks = legend_peps)
}else{
p <- p +
geom_line(data = traces_long[outlier == TRUE], aes_string(x='fraction', y='intensity', color='id'), lwd=2) +
scale_color_manual(values = colorMap, breaks = legend_peps)
}
}else{
## legend_map <- unique(ggplot_build(p)$data[[1]]$colour)
## names(legend_map) <- unique(p$data$id)
## legend_map[legend_peps] <- highlight_col
## legend_vals <- rep(highlight_col, ceiling(length(legend_peps)/ length(highlight_col)))[1:length(legend_peps)]
if(collapse_conditions){
p <- p +
geom_line(data = traces_long[outlier == TRUE],
aes(x=fraction, y=intensity, lty = Condition, group = interaction(Condition, id), color = id),
lwd=2) +
# scale_color_discrete(guide = F)
scale_color_manual(values = colorMap, breaks = legend_peps)
# guides(lty = FALSE)
# scale_color_manual(limits = legend_peps, values = rep(highlight_col, length(legend_peps))) +
# geom_line(aes_string(x='fraction', y='intensity', color='id'))
}else{
p <- p +
geom_line(data = traces_long[outlier == TRUE], aes_string(x='fraction', y='intensity', color = 'id'),
lwd=2) +
# scale_color_discrete(guide = F)
scale_color_manual(values = colorMap, breaks = legend_peps)
## scale_color_manual(values = legend_map, limits = legend_peps)
# guides(lty = FALSE)
# scale_color_manual(limits = legend_peps, values = rep(highlight_col, length(legend_peps))) +
# geom_line(aes_string(x='fraction', y='intensity', color='id'))
}
}
}
if ("molecular_weight" %in% names(traces_frac)) {
fraction_ann <- traces_frac
tr <- lm(log(fraction_ann$molecular_weight) ~ fraction_ann$id)
intercept <- as.numeric(tr$coefficients[1])
slope <- as.numeric(tr$coefficients[2])
mwtransform <- function(x){exp(slope*x + intercept)}
MWtoFraction <- function(x){round((log(x)-intercept)/(slope), digits = 0)}
mw <- round(fraction_ann$molecular_weight, digits = 0)
breaks_MW <- mw[seq(1,length(mw), length.out = length(seq(min(traces_frac$id),
max(traces_frac$id),10)))]
p <- p + scale_x_continuous(name="fraction",
breaks=seq(min(traces_frac$id),
max(traces_frac$id),10),
labels=seq(min(traces_frac$id),
max(traces_frac$id),10),
sec.axis = dup_axis(trans = ~.,
breaks=seq(min(traces_frac$id),
max(traces_frac$id),10),
labels = breaks_MW,
name = "MW (kDa)"))
if (monomer_MW==TRUE){
if ("protein_mw" %in% names(traces[[1]]$trace_annotation)) {
ann_tab <- lapply(traces, function(t){subset(t$trace_annotation, select=c(colour_by,"protein_mw"))})
ann_tab <- unique(do.call(rbind,ann_tab))
subunitMW.dt <- data.table(id=ann_tab[[colour_by]],mw=ann_tab$protein_mw)
subunitMW.dt$fraction <- MWtoFraction(subunitMW.dt$mw)
subunitMW.dt[,boundary:=MWtoFraction(2*mw)]
if (length(unique(subunitMW.dt$mw)) > 1) {
p <- p + geom_point(data = subunitMW.dt, mapping = aes(x = fraction, y = Inf, colour=id), shape=18,size=5,alpha=.5)
} else {
p <- p + geom_vline(data = unique(subunitMW.dt), aes(xintercept = fraction), colour="red", linetype="dashed", size=.5)
p <- p + geom_vline(data = unique(subunitMW.dt), aes(xintercept = boundary), colour="red", linetype="dashed", size=.5, alpha=0.5)
}
} else {
message("No molecular weight annotation of the traces. Cannot plot monomer molecular weight.")
}
}
} else {
p <- p + scale_x_continuous(name="fraction",
breaks=seq(min(traces$fraction_annotation$id),
max(traces$fraction_annotation$id),10),
labels=seq(min(traces$fraction_annotation$id),
max(traces$fraction_annotation$id),10))
}
if (log) {
p <- p + scale_y_log10('log(intensity)')
}
p <- p + theme(axis.text = element_text(size = theme.size, colour="black"))
p <- p + theme(legend.position="bottom") +
theme(legend.text=element_text(size=theme.size)) +
theme(legend.title=element_blank()) +
guides(col = guide_legend(ncol = 4))
if (!legend) {
p <- p + theme(legend.position="none")
}
if (length(ids) > 40) {
p <- p + theme(legend.position="none")
}
if(PDF){
pdf(paste0(name,".pdf"),width=5,height=4)
}
if(plot){
plot(p)
}else{
return(p)
}
if(PDF){
dev.off()
}
}
#' Summarize a traces object
#' @description Summarize a traces object to get an overview.
#' @param traces Object of class traces.
#' @return A summary list with following entries:
#' \itemize{
#' \item \code{metrics} Summary of traces counts including decoy statistics.
#' \item \code{type} Type of traces: peptides or proteins.
#' \item \code{annotations} Names of trace annotations.
#' \item \code{fraction_count} Number of fractions in traces object.
#' \item \code{SibPepCorr} Summary of sibling peptide correlation.
#' Only reported if sibling peptide correlations were previously calculated.
#' }
#' @examples
#' summary(examplePeptideTracesFiltered)
#' @export
summary.traces <- function(traces) {
.tracesTest(traces)
no_traces <- nrow(traces$trace_annotation)
no_decoys <- length(grep("DECOY", traces$trace_annotation$protein_id))
no_targets <- no_traces - no_decoys
pct_decoys <- signif(no_decoys/no_traces * 100, 2)
res <- c(no_traces, no_targets, no_decoys, pct_decoys)
names(res) <- c("No. of Traces", "No. of Targets", "No. of Decoys", "% Decoys")
if(traces$trace_type == "peptide"){
no_ptraces <- length(unique(traces$trace_annotation$protein_id))
no_pdecoys <- length(unique(grep("DECOY", traces$trace_annotation$protein_id)))
no_ptargets <- no_ptraces - no_pdecoys
pct_pdecoys <- signif(no_pdecoys/no_ptraces * 100, 2)
pres <- c(no_ptraces, no_ptargets, no_pdecoys, pct_pdecoys)
names(pres) <- c("No. of Proteins", "No. of Targets", "No. of Decoys", "% Decoys")
res <- list(Peptides = res, Proteins = pres)
}
annotation_info <- names(traces$trace_annotation)
fraction_info = length(traces$fraction_annotation$id)
type=traces$trace_type
summary=list(metrics=res,type=type,annotations=annotation_info,fraction_count=fraction_info)
if("SibPepCorr" %in% names(traces$trace_annotation)) {
SibPepCorr_summary <- summary(traces$trace_annotation$SibPepCorr)
summary$SibPepCorr_summary <- SibPepCorr_summary
}
if("RepPepCorr" %in% names(traces$trace_annotation)) {
RepPepCorr_summary <- summary(traces$trace_annotation$RepPepCorr)
summary$RepPepCorr_summary <- RepPepCorr_summary
}
summary
}
#' Summarize a tracesList object
#' @description Summarize a tracesList object to get an overview.
#' @param traces Object of class tracesList
#' @return A summary list with following entries:
#' \itemize{
#' \item \code{metrics} Summary of traces counts including decoy statistics.
#' \item \code{type} Type of traces: peptides or proteins.
#' \item \code{annotations} Names of trace annotations.
#' \item \code{fraction_count} Number of fractions in traces object.
#' \item \code{SibPepCorr} Summary of sibling peptide correlation.
#' Only reported if sibling peptide correlations were previously calculated.
#' }
#' @export
summary.tracesList <- function(traces) {
.tracesListTest(traces)
res <- lapply(names(traces),function(tr){
trace <- traces[[tr]]
cat(paste0("###########################\n## ",
tr, "\n",
"###########################\n\n"))
print(summary.traces(trace))
})
}
#' Summarize a tracesList object
#' @description Summarize a tracesList object to get an overview.
#' @param traces Object of class tracesList
#' @return A summary list with following entries:
#' \itemize{
#' \item \code{metrics} Summary of traces counts including decoy statistics.
#' \item \code{type} Type of traces: peptides or proteins.
#' \item \code{annotations} Names of trace annotations.
#' \item \code{fraction_count} Number of fractions in traces object.
#' \item \code{SibPepCorr} Summary of sibling peptide correlation.
#' Only reported if sibling peptide correlations were previously calculated.
#' }
#' @export
print.tracesList <- function(traces){
summary.tracesList(traces)
}
#' Summarize a traces object
#' @description Summarize a traces object to get an overview.
#' @param traces Object of class traces.
#' @return A summary list with following entries:
#' \itemize{
#' \item \code{metrics} Summary of traces counts including decoy statistics.
#' \item \code{type} Type of traces: peptides or proteins.
#' \item \code{annotations} Names of trace annotations.
#' \item \code{fraction_count} Number of fractions in traces object.
#' \item \code{SibPepCorr} Summary of sibling peptide correlation.
#' Only reported if sibling peptide correlations were previously calculated.
#' }
#' @examples
#' summary(examplePeptideTracesFiltered)
#' @export
print.traces <- function(traces){
summary.traces(traces)
}
#' Test if an object is of class traces.
#' @param traces Object of class traces.
#' @param type Character string specifying whether a specific type of traces is required.
#' @param additionalItems Character string specifying additional entries that are required in the list.
#' The two options are "peptide" or "protein". Default is \code{NULL},
#' meaning that no specific type is required.
.tracesTest <- function(traces,type=NULL, additionalItems=NULL){
if (! class(traces)=="traces") {
stop("Object is not of class traces.")
}
if (! all(c("traces","trace_type","trace_annotation","fraction_annotation") %in% names(traces))) {
stop("Traces object doesn't contain all necessary items: traces, trace_type, trace_annotation, and fraction_annotation.")
}
if (!is.null(type)) {
if (type != traces$trace_type) {
stop("Traces object is of wrong type. Please check your input traces.")
}
}
if (! identical(traces$traces$id,traces$trace_annotation$id)) {
stop("IDs in traces and trace_annotation are not identical.")
}
if (! identical(names(traces$traces),c(traces$fraction_annotation$id,"id"))) {
stop("Fractions in traces and fraction_annotation are not identical.")
}
if(!is.null(additionalItems)){
contained <- (additionalItems %in% names(traces))
if(!all(contained)){
stop(paste0("Required entries not found: ", additionalItems[!contained]))
}
}
}
#' Test if an object is of class tracesList.
#' @param traces Object of class tracesList.
#' @param type Character string specifying whether a specific type of traces is required.
#' @param additionalItems Character string specifying additional entries that are required in the list.
#' The two options are "peptide" or "protein". Default is \code{NULL},
#' meaning that no specific type is required.
.tracesListTest <- function(tracesList, type=NULL, additionalItems=NULL){
if (! class(tracesList)=="tracesList") {
stop("Object is not of class tracesList")
}
if(is.null(names(tracesList))) stop("TracesList must consist of named traces objects. No names detected.")
res <- lapply(tracesList, function(traces){
if (! all(c("traces","trace_type","trace_annotation","fraction_annotation") %in% names(traces))) {
stop("At least one traces object doesn't contain all necessary items: traces, trace_type, trace_annotation, and fraction_annotation.")
}
if (!is.null(type)) {
if (type != traces$trace_type) {
stop("At least one traces object is of wrong type. Please check your input traces.")
}
}
if (! identical(traces$traces$id,traces$trace_annotation$id)) {
stop("In at least one traces object: IDs in traces and trace_annotation are not identical.")
}
if (! identical(names(traces$traces),c(traces$fraction_annotation$id,"id"))) {
stop("In at least one traces object: Fractions in traces and fraction_annotation are not identical.")
}
if(!is.null(additionalItems)){
contained <- (additionalItems %in% names(traces))
if(!all(contained)){
stop(paste0("Required entries not found: ", additionalItems[!contained]))
}
}
})
}
#' Update trace and fraction annotation
#' @description Add information to trace and fraction annotation
#' @param traces Object of class traces.
#' @return Object of class traces.
#' @export
#'
updateTraces <- function(traces) {
UseMethod("updateTraces", traces)
}
#' @describeIn updateTraces Update trace and fraction annotation
#' @export
updateTraces.traces <- function(traces) {
# call all functions that compute summary statistics that could have changed due to the manipulation
# of the traces object (note that these functions should be relatively fast)
traces <- annotateFractions(traces)
.tracesTest(traces)
return(traces)
}
#' @describeIn updateTraces Update trace and fraction annotation
#' @export
updateTraces.tracesList <- function(traces) {
traces <- annotateFractions(traces)
.tracesListTest(traces)
return(traces)
}
# Manually added to ensure return of plot object for conversion to plotly
plotVolcano = function (testResults,
highlight = NULL,
FC_cutoff = 2,
pBHadj_cutoff = 0.01,
name = "volcanoPlot",
PDF = FALSE,
plot = FALSE,
level = c("feature", "global"))
{
level <- match.arg(level)
if (level == "feature") {
if (PDF) {
pdf(paste0(name, ".pdf"), height = 4, width = 4)
}
if ("medianLog2FC" %in% names(testResults)) {
p <- ggplot(testResults, aes(x = medianLog2FC, y = -log10(pBHadj), label = paste(feature_id, Entry_name, Gene_names)))
}
else {
p <- ggplot(testResults, aes(x = log2FC, y = -log10(pBHadj), label = paste(feature_id, Entry_name, Gene_names)))
}
p <- p + geom_point(size = 1, alpha = 0.3) + theme_bw() +
geom_hline(yintercept = -log10(pBHadj_cutoff), colour = "red",
linetype = "dashed") + geom_vline(xintercept = -log2(FC_cutoff),
colour = "red", linetype = "dashed") +
geom_vline(xintercept = log2(FC_cutoff), colour = "red",
linetype = "dashed")
if (!is.null(highlight)) {
if ("feature_id" %in% names(testResults)) {
sub <- subset(testResults, feature_id %in% highlight)
sub_significant <- sub[pBHadj < pBHadj_cutoff][abs(medianLog2FC) >
log2(FC_cutoff)]
sub_nonSignificant <- sub[(pBHadj >= pBHadj_cutoff) |
(abs(medianLog2FC) <= log2(FC_cutoff))]
col <- "feature_id"
}
else if ("complex_id" %in% names(testResults)) {
sub <- subset(testResults, complex_id %in% highlight)
sub_significant <- sub[pBHadj < pBHadj_cutoff][abs(medianLog2FC) >
log2(FC_cutoff)]
sub_nonSignificant <- sub[(pBHadj >= pBHadj_cutoff) |
(abs(medianLog2FC) <= log2(FC_cutoff))]
col <- "complex_id"
}
else if (highlight %in% testResults$protein_id) {
sub <- subset(testResults, protein_id %in% highlight)
sub_significant <- sub[pBHadj < pBHadj_cutoff][abs(medianLog2FC) >
log2(FC_cutoff)]
sub_nonSignificant <- sub[(pBHadj >= pBHadj_cutoff) |
(abs(medianLog2FC) <= log2(FC_cutoff))]
col <- "protein_id"
}
else if (highlight %in% testResults$proteoform_id) {
sub <- subset(testResults, proteoform_id %in%
highlight)
sub_significant <- sub[pBHadj < pBHadj_cutoff][abs(medianLog2FC) >
log2(FC_cutoff)]
sub_nonSignificant <- sub[(pBHadj >= pBHadj_cutoff) |
(abs(medianLog2FC) <= log2(FC_cutoff))]
col <- "proteoform_id"
}
else {
stop("The testResults do not have the proper format. Input should be the result from testDifferentialExpression.")
}
if ("medianLog2FC" %in% names(testResults)) {
p <- p + geom_point(data = sub_nonSignificant,
aes(x = medianLog2FC, y = -log10(pBHadj)),
colour = "darkgrey", fill = "darkgrey",
size = 2, shape = 23) #+ geom_text_repel(data = sub_nonSignificant,
# aes(label = get(col)), size = 3, vjust = 0,
# hjust = -0.1, colour = "darkgrey")
p <- p + geom_point(data = sub_significant, aes(x = medianLog2FC,
y = -log10(pBHadj)), colour = "red",
fill = "red", size = 3, shape = 23) #+
#geom_text_repel(data = sub_significant, aes(label = get(col)),
#size = 4, vjust = 0, hjust = -0.1, colour = "red")
}
else {
p <- p + geom_point(data = sub_nonSignificant,
aes(x = log2FC, y = -log10(pBHadj)), colour = "darkgrey",
fill = "darkgrey", size = 2, shape = 23) +
p <- p + geom_point(data = sub_significant, aes(x = log2FC,
y = -log10(pBHadj)), colour = "red",
fill = "red", size = 3, shape = 23)
}
}
}
else if (level == "global") {
if (PDF) {
pdf(paste0(name, "_", level, ".pdf"),
height = 4, width = 4)
}
if ("medianLog2FC" %in% names(testResults)) {
p <- ggplot(testResults, aes(x = global_medianLog2FC,
y = -log10(global_pBHadj)))
}
else {
p <- ggplot(testResults, aes(x = global_log2FC, y = -log10(global_pBHadj)))
}
p <- p + geom_point(size = 1, alpha = 0.3) + theme_bw() +
geom_hline(yintercept = -log10(pBHadj_cutoff), colour = "red",
linetype = "dashed") + geom_vline(xintercept = -log2(FC_cutoff),
colour = "red", linetype = "dashed") +
geom_vline(xintercept = log2(FC_cutoff), colour = "red",
linetype = "dashed")
if (!is.null(highlight)) {
if ("feature_id" %in% names(testResults)) {
sub <- subset(testResults, feature_id %in% highlight)
sub_significant <- sub[global_pBHadj < pBHadj_cutoff][abs(global_medianLog2FC) >
log2(FC_cutoff)]
sub_nonSignificant <- sub[(global_pBHadj >= pBHadj_cutoff) |
(abs(global_medianLog2FC) <= log2(FC_cutoff))]
col <- "feature_id"
}
else if ("complex_id" %in% names(testResults)) {
sub <- subset(testResults, complex_id %in% highlight)
sub_significant <- sub[global_pBHadj < pBHadj_cutoff][abs(global_medianLog2FC) >
log2(FC_cutoff)]
sub_nonSignificant <- sub[(global_pBHadj >= pBHadj_cutoff) |
(abs(global_medianLog2FC) <= log2(FC_cutoff))]
col <- "complex_id"
}
else if (highlight %in% testResults$protein_id) {
sub <- subset(testResults, protein_id %in% highlight)
sub_significant <- sub[global_pBHadj < pBHadj_cutoff][abs(global_medianLog2FC) >
log2(FC_cutoff)]
sub_nonSignificant <- sub[(global_pBHadj >= pBHadj_cutoff) |
(abs(global_medianLog2FC) <= log2(FC_cutoff))]
col <- "protein_id"
}
else if (highlight %in% testResults$proteoform_id) {
sub <- subset(testResults, proteoform_id %in%
highlight)
sub_significant <- sub[global_pBHadj < pBHadj_cutoff][abs(global_medianLog2FC) >
log2(FC_cutoff)]
sub_nonSignificant <- sub[(global_pBHadj >= pBHadj_cutoff) |
(abs(global_medianLog2FC) <= log2(FC_cutoff))]
col <- "proteoform_id"
}
else {
stop("The testResults do not have the proper format. Input should be the result from testDifferentialExpression.")
}
if ("global_medianLog2FC" %in% names(testResults)) {
p <- p + geom_point(data = sub_nonSignificant,
aes(x = global_medianLog2FC, y = -log10(global_pBHadj)),
colour = "darkgrey", fill = "darkgrey",
size = 2, shape = 23) #+ geom_text_repel(data = sub_nonSignificant,
# aes(label = get(col)), size = 3, vjust = 0,
# hjust = -0.1, colour = "darkgrey")
p <- p + geom_point(data = sub_significant, aes(x = global_medianLog2FC,
y = -log10(global_pBHadj)), colour = "red",
fill = "red", size = 3, shape = 23) #+
# geom_text_repel(data = sub_significant, aes(label = get(col)),
# size = 4, vjust = 0, hjust = -0.1, colour = "red")
}