@@ -520,6 +520,168 @@ visualize.rank <- function(dataSet, listName = c(), regexName = c(), by = NULL,
520520 return (plot )
521521}
522522
523+ # #------------------------------------------------------------------------------
524+ # '
525+ # ' Target protein abundance plot
526+ # '
527+ # ' @description
528+ # ' Generate bar, box, or violin plots for selected protein(s) of interest.
529+ # '
530+ # ' @param dataSet A data frame containing the data signals.
531+ # '
532+ # ' @param type A character string specifying the plot type.
533+ # ' Allowable options include:
534+ # ' \itemize{
535+ # ' \item "bar": Bar chart.
536+ # ' \item "boxplot": Boxplot.
537+ # ' \item "violin": Violin plot.
538+ # ' }
539+ # '
540+ # ' @param facet A logical value (default = TRUE) specifying whether
541+ # ' to facet the plot by protein.
542+ # '
543+ # ' @param listName A character vector specifying
544+ # ' proteins for exact matching to highlight.
545+ # '
546+ # ' @param regexName A character vector specifying
547+ # ' proteins for regular expression pattern matching to highlight.
548+ # '
549+ # ' @param by A character string (default = "PG.ProteinName" for Spectronaut,
550+ # ' default = "AccessionNumber" for Scaffold) specifying
551+ # ' the information to which \code{listName} and/or \code{regexName} filter
552+ # ' is applied. Allowable options include:
553+ # ' \itemize{
554+ # ' \item For Spectronaut: "PG.Genes", "PG.ProteinAccession",
555+ # ' "PG.ProteinDescriptions", and "PG.ProteinName".
556+ # ' \item For Scaffold: "ProteinDescriptions", "AccessionNumber", and
557+ # ' "AlternateID".
558+ # ' }
559+ # '
560+ # ' @return
561+ # ' An object of class \code{ggplot}.
562+ # '
563+ # ' @autoglobal
564+ # '
565+ # ' @export
566+
567+ visualize.target <- function (dataSet , type = " bar" , facet = TRUE ,
568+ listName = c(), regexName = c(), by = NULL ) {
569+
570+ information <- read.csv(" preprocess_protein_information.csv" , check.names = FALSE )
571+ scaffoldCheck <- " Visible?" %in% colnames(information )
572+ IDcol <- if (scaffoldCheck ) " AccessionNumber" else " PG.ProteinName"
573+ by <- if (is.null(by )) IDcol else by
574+
575+ # # only list filter if listName is present
576+ if (length(listName ) != 0 ) {
577+ listIndex <- which(information [[by ]] %in% listName )
578+ } else {
579+ listIndex <- NULL
580+ }
581+
582+ # # only regex filter if regexName is present
583+ if (length(regexName ) != 0 ) {
584+ regexIndex <- grep(paste(regexName , collapse = " |" ), information [[by ]])
585+ } else {
586+ regexIndex <- NULL
587+ }
588+
589+ # # combine protein names from list and regex names
590+ unionIndex <- sort(union(listIndex , regexIndex ))
591+ unionName <- information [unionIndex , IDcol ]
592+
593+ if (length(unionName ) == 0 ) {
594+ stop(" No matching proteins found to plot!" )
595+ }
596+
597+ plotData <- dataSet %> %
598+ rename(Condition = R.Condition , Replicate = R.Replicate ) %> %
599+ pivot_longer(- c(" Condition" , " Replicate" ),
600+ names_to = " Protein" , values_to = " Abundance" ) %> %
601+ filter(Protein %in% unionName , ! is.na(Abundance ))
602+
603+ if (type == " bar" ) {
604+ smrData <- plotData %> %
605+ group_by(Protein , Condition ) %> %
606+ summarise(mean = mean(Abundance , na.rm = TRUE ),
607+ sd = sd(Abundance , na.rm = TRUE ),
608+ n = n(), .groups = " drop" ) %> %
609+ mutate(error = qnorm(0.975 ) * sd / sqrt(n ))
610+ if (facet ) {
611+ ggplot(smrData , aes(x = Condition , y = mean )) +
612+ geom_col(width = 0.7 , color = " black" , fill = " gray" ) +
613+ geom_errorbar(aes(ymin = mean - error , ymax = mean + error ),
614+ width = 0.3 ) +
615+ geom_point(data = plotData , aes(x = Condition , y = Abundance , color = Replicate ),
616+ position = position_dodge(width = 0.3 ), size = 1 ) +
617+ facet_wrap(~ Protein ) +
618+ labs(y = " Abundance" ) +
619+ theme_bw() +
620+ theme(legend.position = " bottom" )
621+ } else {
622+ ggplot(smrData , aes(x = Protein , y = mean , fill = Condition )) +
623+ geom_col(width = 0.7 , position = position_dodge(width = 0.8 )) +
624+ geom_errorbar(aes(ymin = mean - error , ymax = mean + error ),
625+ position = position_dodge(width = 0.8 ),
626+ width = 0.3 ) +
627+ geom_point(data = plotData , aes(x = Protein , y = Abundance ,
628+ shape = Replicate ,
629+ group = interaction(Protein , Condition )),
630+ position = position_jitterdodge(dodge.width = 0.8 ,
631+ jitter.width = 0.3 ),
632+ size = 1 ) +
633+ guides(fill = guide_legend(override.aes = list (shape = NA )), shape = " none" ) +
634+ labs(x = NULL , y = " Abundance" ) +
635+ theme_bw() +
636+ theme(legend.position = " bottom" )
637+ }
638+ } else if (type == " boxplot" ) {
639+ if (facet ) {
640+ ggplot(plotData , aes(x = Condition , y = Abundance )) +
641+ geom_boxplot(width = 0.7 ) +
642+ geom_point(aes(color = Replicate ),
643+ position = position_dodge(width = 0.3 ), size = 1 ) +
644+ facet_wrap(~ Protein ) +
645+ theme_bw() +
646+ theme(legend.position = " bottom" )
647+ } else {
648+ ggplot(plotData , aes(x = Protein , y = Abundance , fill = Condition ,
649+ group = interaction(Protein , Condition ))) +
650+ geom_boxplot(position = position_dodge(width = 0.8 )) +
651+ geom_point(aes(shape = Replicate ),
652+ position = position_jitterdodge(dodge.width = 0.8 ,
653+ jitter.width = 0.3 ),
654+ size = 1 ) +
655+ guides(fill = guide_legend(override.aes = list (shape = NA )), shape = " none" ) +
656+ labs(x = NULL ) +
657+ theme_bw() +
658+ theme(legend.position = " bottom" )
659+ }
660+ } else if (type == " violin" ) {
661+ if (facet ) {
662+ ggplot(plotData , aes(x = Condition , y = Abundance )) +
663+ geom_violin(width = 0.7 ) +
664+ geom_point(aes(color = Replicate ),
665+ position = position_dodge(width = 0.1 ), size = 1 ) +
666+ facet_wrap(~ Protein ) +
667+ theme_bw() +
668+ theme(legend.position = " bottom" )
669+ } else {
670+ ggplot(plotData , aes(x = Protein , y = Abundance , fill = Condition ,
671+ group = interaction(Protein , Condition ))) +
672+ geom_violin(position = position_dodge(width = 0.8 )) +
673+ geom_point(aes(shape = Replicate ),
674+ position = position_jitterdodge(dodge.width = 0.8 ,
675+ jitter.width = 0.1 ),
676+ size = 1 ) +
677+ guides(fill = guide_legend(override.aes = list (shape = NA )), shape = " none" ) +
678+ labs(x = NULL ) +
679+ theme_bw() +
680+ theme(legend.position = " bottom" )
681+ }
682+ }
683+ }
684+
523685# #------------------------------------------------------------------------------
524686# '
525687# ' Histograms of fold changes and p-values from test results
0 commit comments