Skip to content

Commit 4ca1fe7

Browse files
committed
visualize.rank(): add facet-based aggregation (avg over replicate/condition); update highlighting
1 parent 1acf32d commit 4ca1fe7

3 files changed

Lines changed: 37 additions & 48 deletions

File tree

R/globals.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ utils::globalVariables(c(
4343
"R.Condition", # <visualize.rank>
4444
"R.Replicate", # <visualize.rank>
4545
"Abundance", # <visualize.rank>
46-
"Rank", # <visualize.rank>
4746
"Type", # <visualize.rank>
47+
"Rank", # <visualize.rank>
4848
"Label", # <visualize.rank>
4949
"Variable", # <visualize.test>
5050
"name", # <visualize.test>

R/visualization.R

Lines changed: 32 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -425,11 +425,10 @@ visualize.ma <- function(dataSet, M.thres = 1) {
425425
#' @param facet A character string (default = c("Replicate", "Condition"))
426426
#' specifying grouping variables for faceting. Allowed values are:
427427
#' \itemize{
428-
#' \item "Condition"
429-
#' \item "Replicate"
430-
#' \item c("Condition", "Replicate")
431-
#' \item c("Replicate", "Condition")
432-
#' \item "none" for no faceting
428+
#' \item "Condition": Abundance values are averaged across replicates.
429+
#' \item "Replicate": Abundance values are averaged across conditions.
430+
#' \item c("Condition", "Replicate"): No averaging is performed.
431+
#' \item c("Replicate", "Condition"): No averaging is performed.
433432
#' }
434433
#'
435434
#' @param color A character string (default = red") specifying
@@ -449,13 +448,10 @@ visualize.rank <- function(dataSet, listName = NULL, regexName = NULL, by = NULL
449448
...) {
450449

451450
information <- read.csv("preprocess_protein_information.csv", check.names = FALSE)
452-
scaffoldCheck <- any(colnames(information) == "Visible?")
453-
IDcol <- ifelse(scaffoldCheck, "AccessionNumber", "PG.ProteinName")
454-
labelCol <- ifelse(scaffoldCheck, "AlternateID", "PG.ProteinName")
455-
456-
if (is.null(by)) {
457-
by <- IDcol
458-
}
451+
scaffoldCheck <- "Visible?" %in% colnames(information)
452+
IDcol <- if (scaffoldCheck) "AccessionNumber" else "PG.ProteinName"
453+
labelCol <- if (scaffoldCheck) "AlternateID" else "PG.ProteinName"
454+
by <- if (is.null(by)) IDcol else by
459455

460456
## only list filter if listName is present
461457
if (length(listName) != 0) {
@@ -475,44 +471,36 @@ visualize.rank <- function(dataSet, listName = NULL, regexName = NULL, by = NULL
475471
unionIndex <- sort(union(listIndex, regexIndex))
476472
unionName <- information[unionIndex, IDcol]
477473

474+
if (length(unionName) == 0) {
475+
message("No matching proteins found to highlight!")
476+
}
477+
478478
plotData <- dataSet %>%
479479
rename(Condition = R.Condition, Replicate = R.Replicate) %>%
480-
pivot_longer(-c("Condition", "Replicate"), names_to = IDcol, values_to = "Abundance") %>%
481-
left_join(information, by = IDcol) %>%
482-
mutate(Type = ifelse(.data[[IDcol]] %in% unionName, "Highlight", "Other"),
483-
Label = case_when(
484-
Type == "Highlight" & identical(facet, "Condition") ~ paste(.data[[labelCol]], Replicate, sep = "_"),
485-
Type == "Highlight" & identical(facet, "Replicate") ~ paste(.data[[labelCol]], Condition, sep = "_"),
486-
Type == "Highlight" & length(facet) == 2 ~ .data[[labelCol]],
487-
Type == "Other" ~ NA))
488-
489-
if (!any(plotData$Type == "Highlight")) {
490-
stop("No matching proteins found in the input dataset to highlight!")
491-
}
480+
pivot_longer(-c("Condition", "Replicate"), names_to = IDcol, values_to = "Abundance")
492481

493-
if (all(facet %in% c("Condition", "Replicate"))) {
494-
plotData <- plotData %>%
495-
group_by(across(all_of(facet))) %>%
496-
arrange(desc(Abundance), .by_group = TRUE) %>%
497-
mutate(Rank = row_number()) %>%
498-
ungroup()
499-
} else {
482+
if (!setequal(facet, c("Condition", "Replicate"))) {
500483
plotData <- plotData %>%
501-
arrange(desc(Abundance)) %>%
502-
mutate(Rank = row_number())
484+
group_by(across(all_of(c(facet, IDcol)))) %>%
485+
summarise(Abundance = mean(Abundance, na.rm = TRUE), .groups = "drop")
503486
}
504487

505-
highlight_label <- if (length(unionName) == 1) unionName else "Highlight"
488+
plotData <- plotData %>%
489+
left_join(information, by = IDcol) %>%
490+
mutate(Type = if_else(.data[[IDcol]] %in% unionName, "Highlight", "Other"),
491+
Label = if_else(Type == "Highlight", .data[[labelCol]], NA_character_)) %>%
492+
group_by(across(all_of(facet))) %>%
493+
arrange(desc(Abundance), .by_group = TRUE) %>%
494+
mutate(Rank = row_number()) %>%
495+
ungroup()
506496

507497
plot <- ggplot(plotData, aes(x = Rank, y = Abundance, shape = Type, color = Type)) +
508498
geom_point() +
509-
scale_color_manual(values = c("Highlight" = color, "Other" = "gray"),
510-
labels = c("Highlight" = highlight_label, "Other" = "Other")) +
511-
scale_shape_manual(values = c("Highlight" = 17, "Other" = 16),
512-
labels = c("Highlight" = highlight_label, "Other" = "Other")) +
499+
scale_color_manual(values = c("Highlight" = color, "Other" = "gray")) +
500+
scale_shape_manual(values = c("Highlight" = 17, "Other" = 16)) +
513501
labs(x = "Rank", y = "Abundance") +
514502
theme_bw() +
515-
theme(legend.position = "bottom",
503+
theme(legend.position = "none",
516504
panel.grid.major = element_blank(),
517505
panel.grid.minor = element_blank())
518506

@@ -522,9 +510,11 @@ visualize.rank <- function(dataSet, listName = NULL, regexName = NULL, by = NULL
522510
plot <- plot + facet_grid(as.formula(paste(facet[1], "~", facet[2])))
523511
}
524512

525-
if (length(unionName) > 1) {
526-
plot <- plot + geom_text_repel(data = plotData, aes(label = Label),
527-
size = 2.5, show.legend = FALSE, ...)
513+
if (length(unionName) != 0) {
514+
plot <- plot +
515+
geom_text_repel(data = subset(plotData, Type == "Highlight"),
516+
aes(label = Label), size = 2.5,
517+
show.legend = FALSE, ...)
528518
}
529519

530520
return(plot)

man/visualize.rank.Rd

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

0 commit comments

Comments
 (0)