Skip to content

Commit 8711fcc

Browse files
committed
visualizations: update visualize.*() for Scaffold compatibility; use PG.ProteinName for Spectronaut and AlternateID for Scaffold (fixes #2).
1 parent fe595a9 commit 8711fcc

1 file changed

Lines changed: 111 additions & 66 deletions

File tree

R/visualizations.R

Lines changed: 111 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -142,14 +142,19 @@ visualize.heatmap <- function(dataSet, pkg = "pheatmap",
142142

143143
visualize.ma <- function(dataSet, M.thres = 1) {
144144

145+
information <- read.csv("preprocess_protein_information.csv", check.names = FALSE)
146+
scaffoldCheck <- any(colnames(information) == "Visible?")
147+
IDcol <- ifelse(scaffoldCheck, "AccessionNumber", "PG.ProteinName")
148+
labelCol <- ifelse(scaffoldCheck, "AlternateID", "PG.ProteinName")
149+
145150
if (is.data.frame(dataSet)) {
146-
plotData <- data.frame(t(dataSet[c("A","M"),])) %>%
147-
rownames_to_column("Variable")
151+
plotData <- as.data.frame(t(dataSet[c("A","M"),])) %>%
152+
rownames_to_column(IDcol)
148153
} else {
149154
plotData <- lapply(dataSet, function(df) {
150155
t(df[c("A","M"),]) %>%
151156
as.data.frame() %>%
152-
rownames_to_column("Variable")
157+
rownames_to_column(IDcol)
153158
}) %>%
154159
bind_rows(.id = "Comparison")
155160
}
@@ -160,9 +165,10 @@ visualize.ma <- function(dataSet, M.thres = 1) {
160165
M < -M.thres & M > -Inf ~ "Down",
161166
M >= -M.thres & M <= M.thres ~ "No",
162167
TRUE ~ "Unknown")) %>% ## optional catch-all for other cases
163-
mutate(label = ifelse(Regulation != "No", gsub("_.*", "", Variable), NA))
168+
left_join(information, by = IDcol) %>%
169+
mutate(Label = ifelse(Regulation != "No", .data[[labelCol]], NA)) # gsub("_.*", "", .data[[labelCol]])
164170

165-
ggplot(plotData, aes(x = A, y = M, color = Regulation, label = label)) +
171+
ggplot(plotData, aes(x = A, y = M, color = Regulation, label = Label)) +
166172
geom_hline(yintercept = c(-M.thres, M.thres), linetype = "dashed") +
167173
geom_point() +
168174
geom_text_repel(show.legend = FALSE) +
@@ -189,46 +195,72 @@ visualize.ma <- function(dataSet, M.thres = 1) {
189195
#' @param regexName A character vector specifying proteins for regular expression pattern
190196
#' matching to highlight.
191197
#'
198+
#' @param by A character string (default = "PG.ProteinName" for Spectronaut, default =
199+
#' "AccessionNumber" for Scaffold) specifying the information to which \code{listName}
200+
#' and/or \code{regexName} filter is applied. Allowable options include:
201+
#' \itemize{
202+
#' \item For Spectronaut: "PG.Genes", "PG.ProteinAccession", "PG.ProteinDescriptions", and
203+
#' "PG.ProteinName".
204+
#' \item For Scaffold: "ProteinDescriptions", "AccessionNumber", and "AlternateID".
205+
#' }
206+
#'
192207
#' @param facet A character string (default = c("Replicate", "Condition")) specifying
193208
#' grouping variables for faceting. Allowed values are "Condition", "Replicate",
194209
#' c("Condition", "Replicate"), c("Replicate", "Condition"), or "none" for no grouping.
195210
#'
196211
#' @param color A string (default = red") specifying the color used to highlight proteins.
197212
#'
198-
#' @import ggrepel
199-
#'
200213
#' @return
201214
#' An object of class \code{ggplot}.
202215
#'
203216
#' @autoglobal
204217
#'
205218
#' @export
206219

207-
visualize.rank <- function(dataSet, listName = NULL, regexName = NULL,
220+
visualize.rank <- function(dataSet, listName = NULL, regexName = NULL, by = NULL,
208221
facet = c("Condition", "Replicate"), color = "red") {
209222

210-
plotData <- dataSet %>%
211-
rename(Condition = R.Condition, Replicate = R.Replicate) %>%
212-
pivot_longer(cols = -c("Condition", "Replicate"),
213-
names_to = "Name", values_to = "Abundance")
223+
information <- read.csv("preprocess_protein_information.csv", check.names = FALSE)
224+
scaffoldCheck <- any(colnames(information) == "Visible?")
225+
IDcol <- ifelse(scaffoldCheck, "AccessionNumber", "PG.ProteinName")
226+
labelCol <- ifelse(scaffoldCheck, "AlternateID", "PG.ProteinName")
214227

215-
## match regexName if provided
216-
if (!is.null(regexName)) {
217-
regexName <- unique(grep(paste(regexName, collapse = "|"), plotData$Name, value = TRUE))
228+
if (is.null(by)) {
229+
by <- IDcol
218230
}
219231

220-
## combine protein names from list and regex names
221-
unionName <- union(listName, regexName)
232+
## only list filter if listName is present
233+
if (length(listName) != 0) {
234+
listIndex <- which(information[[by]] %in% listName)
235+
} else {
236+
listIndex <- NULL
237+
}
222238

223-
highlight_label <- if (length(unionName) == 1) unionName else "Highlight"
239+
## only regex filter if regexName is present
240+
if (length(regexName) != 0) {
241+
regexIndex <- grep(paste(regexName, collapse = "|"), information[[by]])
242+
} else {
243+
regexIndex <- NULL
244+
}
224245

225-
plotData <- plotData %>%
226-
mutate(Proteins = ifelse(Name %in% unionName, "Highlight", "Other"),
246+
## combine protein names from list and regex names
247+
unionIndex <- sort(union(listIndex, regexIndex))
248+
unionName <- information[unionIndex, IDcol]
249+
250+
plotData <- dataSet %>%
251+
rename(Condition = R.Condition, Replicate = R.Replicate) %>%
252+
pivot_longer(-c("Condition", "Replicate"), names_to = IDcol, values_to = "Abundance") %>%
253+
left_join(information, by = IDcol) %>%
254+
mutate(Type = ifelse(.data[[IDcol]] %in% unionName, "Highlight", "Other"),
227255
Label = case_when(
228-
Proteins == "Highlight" & identical(facet, "Condition") ~ paste(Name, Replicate, sep = "_"),
229-
Proteins == "Highlight" & identical(facet, "Replicate") ~ paste(Name, Condition, sep = "_"),
230-
Proteins == "Highlight" & length(facet) == 2 ~ Name,
231-
Proteins == "Other" ~ NA))
256+
Type == "Highlight" & identical(facet, "Condition") ~ paste(.data[[labelCol]], Replicate, sep = "_"),
257+
Type == "Highlight" & identical(facet, "Replicate") ~ paste(.data[[labelCol]], Condition, sep = "_"),
258+
Type == "Highlight" & length(facet) == 2 ~ .data[[labelCol]],
259+
Type == "Other" ~ NA))
260+
261+
if (!any(plotData$Type == "Highlight")) {
262+
stop("No matching proteins found in the input dataset to highlight!")
263+
}
232264

233265
if (all(facet %in% c("Condition", "Replicate"))) {
234266
plotData <- plotData %>%
@@ -242,7 +274,9 @@ visualize.rank <- function(dataSet, listName = NULL, regexName = NULL,
242274
mutate(Rank = row_number())
243275
}
244276

245-
plot <- ggplot(plotData, aes(x = Rank, y = Abundance, shape = Proteins, color = Proteins)) +
277+
highlight_label <- if (length(unionName) == 1) unionName else "Highlight"
278+
279+
plot <- ggplot(plotData, aes(x = Rank, y = Abundance, shape = Type, color = Type)) +
246280
geom_point() +
247281
scale_color_manual(values = c("Highlight" = color, "Other" = "black"),
248282
labels = c("Highlight" = highlight_label, "Other" = "Other")) +
@@ -289,7 +323,8 @@ visualize.rank <- function(dataSet, listName = NULL, regexName = NULL,
289323
visualize.test <- function(dataSet) {
290324

291325
if (is.data.frame(dataSet)) {
292-
plotData <- data.frame(t(dataSet[c("difference","p-value"),])) %>%
326+
plotData <- t(dataSet[c("difference","p-value"),]) %>%
327+
as.data.frame(check.names = FALSE) %>%
293328
rownames_to_column("Variable") %>%
294329
pivot_longer(-Variable) %>%
295330
group_by(name) %>%
@@ -298,11 +333,11 @@ visualize.test <- function(dataSet) {
298333
} else {
299334
plotData <- lapply(dataSet[names(dataSet) != "total"], function(df) {
300335
t(df[c("difference","p-value"),]) %>%
301-
as.data.frame() %>%
336+
as.data.frame(check.names = FALSE) %>%
302337
rownames_to_column("Variable")
303338
}) %>%
304339
bind_rows(.id = "Comparison") %>%
305-
rename(p.value = "p-value") %>%
340+
# rename(p.value = "p-value") %>%
306341
pivot_longer(-c("Variable", "Comparison")) %>%
307342
group_by(name, Comparison) %>%
308343
## binwidth information for reference
@@ -358,13 +393,9 @@ visualize.upset <- function(dataSet) {
358393
#' @param fill_color A text (default = c("blue", "yellow", "green", "red")) specifying the
359394
#' colors to fill in circles.
360395
#'
361-
#' @param saveVenn A boolean (default = TRUE) specifying whether to save the data, with
396+
#' @param saveRes A boolean (default = TRUE) specifying whether to save the data, with
362397
#' logical columns representing sets, to current working directory.
363398
#'
364-
#' @param proteinInformation The name of the .csv file containing protein information data
365-
#' (including the path to the file, if needed). This file is automatically generated by
366-
#' the function \code{\link[msDiaLogue]{preprocessing}}.
367-
#'
368399
#' @import ggvenn
369400
#'
370401
#' @return
@@ -374,25 +405,27 @@ visualize.upset <- function(dataSet) {
374405

375406
visualize.venn <- function(dataSet, show_percentage = TRUE,
376407
fill_color = c("blue", "yellow", "green", "red"),
377-
saveVenn = TRUE,
378-
proteinInformation = "preprocess_protein_information.csv") {
408+
saveRes = TRUE) {
379409

380410
if (length(dataSet) > 4) {
381411
message("More than 4 sets in a Venn diagram
382412
may result in crowded visualization and information overload.")
383413
}
384414

385-
if (saveVenn) {
386-
df <- tibble(Protein = unique(unlist(dataSet)))
415+
if (saveRes) {
416+
417+
information <- read.csv("preprocess_protein_information.csv", check.names = FALSE)
418+
scaffoldCheck <- any(colnames(information) == "Visible?")
419+
IDcol <- ifelse(scaffoldCheck, "AccessionNumber", "PG.ProteinName")
420+
421+
df <- tibble(!!IDcol := unique(unlist(dataSet)))
387422
for (name in names(dataSet)) {
388-
df[, name] <- df$Protein %in% dataSet[[name]]
423+
df[,name] <- df[[IDcol]] %in% dataSet[[name]]
389424
}
390-
if (!is.null(proteinInformation)) {
391-
proteinInformation <- read.csv(proteinInformation)
392-
keyid <- ifelse(startsWith(colnames(proteinInformation)[1], "PG."), "PG.ProteinNames", "AccessionNumber")
393-
df <- merge(proteinInformation, df, by.x = keyid, by.y = "Protein", sort = TRUE)
394-
}
395-
write.csv(df, file = "Venn_information.csv", row.names = FALSE)
425+
426+
df <- left_join(df, information, by = IDcol)
427+
428+
write.csv(df, file = "venn_information.csv", row.names = FALSE)
396429
}
397430

398431
ggvenn::ggvenn(dataSet, show_percentage = show_percentage, fill_color = fill_color)
@@ -428,30 +461,35 @@ visualize.venn <- function(dataSet, show_percentage = TRUE,
428461

429462
visualize.volcano <- function(dataSet, P.thres = 0.05, F.thres = 1) {
430463

464+
information <- read.csv("preprocess_protein_information.csv", check.names = FALSE)
465+
scaffoldCheck <- any(colnames(information) == "Visible?")
466+
IDcol <- ifelse(scaffoldCheck, "AccessionNumber", "PG.ProteinName")
467+
labelCol <- ifelse(scaffoldCheck, "AlternateID", "PG.ProteinName")
468+
431469
if (is.data.frame(dataSet)) {
432-
plotData <- data.frame(t(dataSet[c("difference","p-value"),])) %>%
433-
rownames_to_column("Variable")
470+
plotData <- t(dataSet[c("difference","p-value"),]) %>%
471+
as.data.frame() %>%
472+
rownames_to_column(IDcol)
434473
} else {
435-
plotData <- lapply(dataSet[names(dataSet) != "total"], function(df) {
474+
plotData <- lapply(dataSet, function(df) {
436475
t(df[c("difference","p-value"),]) %>%
437476
as.data.frame() %>%
438-
rownames_to_column("Variable")
477+
rownames_to_column(IDcol)
439478
}) %>%
440-
bind_rows(.id = "Comparison") %>%
441-
rename(p.value = "p-value")
479+
bind_rows(.id = "Comparison")
442480
}
443481

444482
plotData <- plotData %>%
445483
mutate(Significant = case_when(
446-
p.value < P.thres & difference > F.thres ~ "Up",
447-
p.value < P.thres & difference < -F.thres ~ "Down",
448-
p.value < P.thres & difference >= -F.thres & difference <= F.thres ~ "Inconclusive",
449-
p.value >= P.thres ~ "No",
484+
`p-value` < P.thres & difference > F.thres ~ "Up",
485+
`p-value` < P.thres & difference < -F.thres ~ "Down",
486+
`p-value` < P.thres & difference >= -F.thres & difference <= F.thres ~ "Inconclusive",
487+
`p-value` >= P.thres ~ "No",
450488
TRUE ~ "Unknown")) %>% ## optional catch-all for other cases
451-
mutate(label = ifelse(! Significant %in% c("No", "Inconclusive"),
452-
gsub("_.*", "", Variable), NA))
489+
left_join(information, by = IDcol) %>%
490+
mutate(Label = ifelse(! Significant %in% c("No", "Inconclusive"), .data[[labelCol]], NA)) # gsub("_.*", "", .data[[labelCol]])
453491

454-
ggplot(plotData, aes(x = difference, y = -log10(p.value), col = Significant, label = label)) +
492+
ggplot(plotData, aes(x = difference, y = -log10(`p-value`), col = Significant, label = Label)) +
455493
geom_vline(xintercept = c(-F.thres, F.thres), linetype = "dashed") +
456494
geom_hline(yintercept = -log10(P.thres), linetype = "dashed") +
457495
geom_point() +
@@ -773,14 +811,21 @@ visualize.biplot <- function(dataSet, ellipse = TRUE, ellipse.level = 0.95, labe
773811

774812
visualize.vip <- function(dataSet, comp = 1, num = 10, thres = 1, rel.widths) {
775813

776-
vips <- as.data.frame(dataSet$vips) %>%
814+
information <- read.csv("preprocess_protein_information.csv", check.names = FALSE)
815+
scaffoldCheck <- any(colnames(information) == "Visible?")
816+
IDcol <- ifelse(scaffoldCheck, "AccessionNumber", "PG.ProteinName")
817+
labelCol <- ifelse(scaffoldCheck, "AlternateID", "PG.ProteinName")
818+
819+
vips <- as.data.frame(dataSet[["vips"]]) %>%
777820
select(Score = paste("Comp", comp)) %>%
778821
slice_max(order_by = Score, n = num, with_ties = FALSE) %>%
779822
arrange(Score) %>%
780-
rownames_to_column("Variable") %>%
781-
mutate(Variable = factor(Variable, levels = Variable))
823+
rownames_to_column(IDcol) %>%
824+
left_join(information, by = IDcol) %>%
825+
mutate(!!IDcol := factor(.data[[IDcol]], levels = .data[[IDcol]]),
826+
!!labelCol := factor(.data[[labelCol]], levels = .data[[labelCol]]))
782827

783-
plot1 <- ggplot(vips, aes(x = Score, y = Variable)) +
828+
plot1 <- ggplot(vips, aes(x = Score, y = .data[[labelCol]])) +
784829
geom_point() +
785830
geom_vline(xintercept = thres, linetype = "dashed", color = "black") +
786831
labs(x = "VIP scores", y = NULL) +
@@ -789,16 +834,16 @@ visualize.vip <- function(dataSet, comp = 1, num = 10, thres = 1, rel.widths) {
789834
panel.grid.minor.x = element_blank(),
790835
panel.grid.major.y = element_line(linetype = "dashed"))
791836

792-
xs <- dataSet$model[["xs"]][,levels(vips$Variable), drop = FALSE]
837+
xs <- dataSet$model[["xs"]][,levels(vips[[IDcol]]), drop = FALSE]
793838
y <- dataSet$model[["y"]]
794839
group <- factor(colnames(y)[max.col(y, ties.method = "first")], levels = colnames(y))
795840
abundances <- do.call(cbind, by(xs, group, function(x) {apply(x, 2, mean, trim = 0.1)})) %>%
796841
as.data.frame() %>%
797-
rownames_to_column("Variable") %>%
798-
mutate(Variable = factor(Variable, levels = levels(vips$Variable))) %>%
799-
pivot_longer(-Variable, names_to = "Group", values_to = "Abundance")
842+
rownames_to_column(IDcol) %>%
843+
mutate(!!IDcol := factor(.data[[IDcol]], levels = .data[[IDcol]])) %>%
844+
pivot_longer(-!!IDcol, names_to = "Group", values_to = "Abundance")
800845

801-
plot2 <- ggplot(abundances, aes(x = Group, y = Variable, fill = Abundance)) +
846+
plot2 <- ggplot(abundances, aes(x = Group, y = .data[[IDcol]], fill = Abundance)) +
802847
# geom_tile(width = 0.5, height = 0.5, color = "white") +
803848
geom_tile(color = "white", lwd = 7) +
804849
scale_x_discrete(position = "bottom") +

0 commit comments

Comments
 (0)