Skip to content

Commit 97827bc

Browse files
committed
add visualize.target() (fixed #10)
1 parent ce24b4c commit 97827bc

7 files changed

Lines changed: 242 additions & 1 deletion

File tree

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export(visualize.ma)
3434
export(visualize.rank)
3535
export(visualize.score)
3636
export(visualize.scree)
37+
export(visualize.target)
3738
export(visualize.test)
3839
export(visualize.upset)
3940
export(visualize.venn)
@@ -80,6 +81,7 @@ importFrom(stats,na.omit)
8081
importFrom(stats,p.adjust)
8182
importFrom(stats,pnorm)
8283
importFrom(stats,prcomp)
84+
importFrom(stats,qnorm)
8385
importFrom(stats,sd)
8486
importFrom(stats,setNames)
8587
importFrom(stats,t.test)

R/globals.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ utils::globalVariables(c(
4141
"Type", # <visualize.rank>
4242
"Rank", # <visualize.rank>
4343
"Label", # <visualize.rank>
44+
"R.Condition", # <visualize.target>
45+
"R.Replicate", # <visualize.target>
46+
"Protein", # <visualize.target>
47+
"Abundance", # <visualize.target>
48+
"Condition", # <visualize.target>
49+
"error", # <visualize.target>
50+
"Replicate", # <visualize.target>
4451
"Variable", # <visualize.test>
4552
"name", # <visualize.test>
4653
"value", # <visualize.test>

R/visualization.R

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

R/zzz.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
#' @importFrom RColorBrewer brewer.pal
99
#' @importFrom Rdpack reprompt
1010
#' @importFrom stats as.formula cor density integrate median model.matrix na.omit p.adjust
11-
#' @importFrom stats pnorm prcomp sd setNames t.test wilcox.test var
11+
#' @importFrom stats pnorm prcomp sd setNames t.test qnorm wilcox.test var
1212
#' @importFrom utils combn read.csv write.csv
1313
NULL

_pkgdown.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ reference:
7575
- visualize.rank
7676
- visualize.score
7777
- visualize.scree
78+
- visualize.target
7879
- visualize.test
7980
- visualize.upset
8081
- visualize.venn

man/visualize.target.Rd

Lines changed: 52 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vignettes/visualization.qmd

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,23 @@ visualize.rank(dataImput, listName = "POLK_HUMAN",
240240
```
241241

242242

243+
## Target protein abundance plot
244+
245+
246+
The `visualize.target()` function provides a convenient way to explore
247+
the abundance of specific proteins across experimental conditions.
248+
It is particularly useful for quickly examining target protein behavior,
249+
complementing global visualizations like rank abundance plots.
250+
251+
252+
```{r}
253+
visualize.target(dataImput, type = "violin", facet = TRUE,
254+
listName = "POLK_HUMAN", regexName = c("ZN840"))
255+
visualize.target(dataImput, type = "bar", facet = FALSE,
256+
listName = "POLK_HUMAN")
257+
```
258+
259+
243260
## Histogram of fold changes and p-values for test
244261

245262

0 commit comments

Comments
 (0)