-
Notifications
You must be signed in to change notification settings - Fork 3
/
plotVolcanoHTML.R
236 lines (207 loc) · 7.3 KB
/
plotVolcanoHTML.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
#' Generate HTML-based Volcano Plot
#'
#' @family volcano
#' @inheritParams boxplotBeeswarm
#' @inheritParams plotVolcano
#' @param data A data frame containing log2-transformed fold-changes and
#' corresponding p-values. An optional third column
#' containing, e.g. "Target" Names, can be passed if specified by
#' the `labels =` parameter.
#' @param ... Additional arguments passed to [plotly::plotly()].
#' @author Leigh Alexander, Stu Field
#' @seealso [plotly::plotly()]
#' @examples
#' # Dummy up a fake table with minimal variables
#' adat <- SomaDataIO::example_data
#' seqs <- SomaDataIO::getAnalytes(adat)
#' target_map <- SomaDataIO::getTargetNames(SomaDataIO::getAnalyteInfo(adat))
#' df <- withr::with_seed(101, {
#' fc1 <- sort(runif(500, -2.5, 0)) # Z-scores as dummy fold-changes
#' fc2 <- sort(runif(500, 0, 2.5)) # Z-scores as dummy fold-changes
#' p1 <- pnorm(fc1) # p-values for neg. scores
#' p2 <- pnorm(fc2, lower.tail = FALSE) # p-values for pos. scores
#' p <- jitter(c(p1, p2), amount = 0.1) # add noise
#' p[p < 0] <- runif(sum(p < 0), 1e-05, 1e-02) # floor p > 0 after jitter
#' seq_vec <- sample(seqs, length(p)) # random seqIds
#' data.frame(
#' AptName = seq_vec,
#' t_stat = runif(50, 10, 20),
#' log2_fc = c(fc1, fc2),
#' p_value = p,
#' target = unlist(target_map)[seq_vec] # map random target names
#' )
#' })
#'
#' # S3 `data.frame` method
#' # No TargetNames -> `NA`
#' plotVolcanoHTML(df, log2_fc, p_value, cutoff = 0.1, fc.cutoff = 0.5)
#'
#' # Add TargetNames via `labels=`
#' plotVolcanoHTML(df, log2_fc, p_value, cutoff = 0.1, fc.cutoff = 0.5, labels = target)
#' @importFrom stats setNames
#' @importFrom rlang enquo !!
#' @importFrom dplyr pull case_when ungroup select left_join
#' @export
plotVolcanoHTML <- function(data, FC, p.value, cutoff, fc.cutoff, main, x.lab, ...) {
UseMethod("plotVolcanoHTML")
}
#' S3 plotVolcanoHTML default method
#' @noRd
#' @export
plotVolcanoHTML.default <- function(data, ...) {
stop(
"Couldn't find a S3 method for this class object: ",
value(class(data)), call. = FALSE
)
}
#' @describeIn plotVolcanoHTML
#' Plot method for objects of class `data.frame`.
#' @importFrom SomaDataIO rn2col col2rn
#' @export
plotVolcanoHTML.data.frame <- function(data, FC, p.value,
cutoff = 0.05 / nrow(data),
fc.cutoff = 1,
main = NULL,
x.lab = NULL,
labels, ...) {
if ( !"AptName" %in% names(data) ) {
data <- rn2col(data, "AptName")
}
p_vec <- pull(data, !!enquo(p.value))
FC <- pull(data, !!enquo(FC))
if ( !missing(labels) ) {
labels <- data |> pull(!!enquo(labels))
} else {
labels <- NA_character_
}
text_labels <- paste(
"AptName:", data$AptName,
"<br>TargetName:", labels,
"<br>Fold Change:", format(2^FC, digits = 2), # linear space
"<br>p-value:", format(p_vec, digits = 2),
"<br>"
)
if ( is.null(main) ) {
main <- "Volcano Plot"
}
if ( is.null(x.lab) ) {
x.lab <- "<i>log<sub>2</sub></i> Fold-Change"
}
p_vec <- -log10(p_vec)
if ( all(FC >= 0) ) {
warning(
"It appears you are not passing log2-transformed ",
"fold-change values. Please check.", call. = FALSE
)
}
color.by <- case_when(
(p_vec >= -log10(cutoff)) & (abs(FC) >= fc.cutoff) ~ "Significant & Fold Change",
p_vec >= -log10(cutoff) ~ "Significant",
abs(FC) >= fc.cutoff ~ "Fold Change",
TRUE ~ "Non-Significant"
)
# matched to `plotVolcano()` palette
cols <- .volcano_cols()
plotly::plot_ly() |>
plotly::add_trace(
x = FC, y = p_vec, # primary scatter plot of ps and fold changes
color = color.by,
colors = cols,
mode = "markers", type = "scatter",
marker = list(opacity = 0.7, size = 9),
text = text_labels, ...) |>
plotly::layout(
title = main,
xaxis = list(title = x.lab, showgrid = FALSE),
yaxis = list(title = "-<i>log<sub>10</sub></i> p-value",
showgrid = FALSE),
margin = list(b = 60, t = 60), ...)
}
#' @describeIn plotVolcanoHTML
#' Plot method for objects of class `stat_table` (SomaLogic internal).
#' @inheritParams SomaDataIO::getTargetNames
#' @importFrom SomaDataIO rn2col
#' @export
plotVolcanoHTML.stat_table <- function(data, FC, p.value,
cutoff = 0.05 / nrow(data$stat.table),
fc.cutoff = 1,
main = NULL,
x.lab = NULL,
tbl, ...) {
calc_object <- data
stat_table <- rn2col(calc_object$stat.table, "AptName")
levels <- calc_object$counts
ref_group <- names(levels)[1L]
test <- calc_object$test
x_pred <- if ( is.null(calc_object$response) ) {
calc_object$x.predictor
} else {
calc_object$response
}
tbl <- dplyr::select(ungroup(tbl), AptName, TargetFullName)
stat_table <- left_join(stat_table, tbl, by = "AptName")
p_vec <- pull(stat_table, !!enquo(p.value))
p_vec <- -log10(p_vec)
FC <- pull(stat_table, !!enquo(FC))
text_labels <- paste(
"AptName:", stat_table$AptName,
"<br>TargetName:", stat_table$TargetFullName,
"<br>Fold Change:", format(2^FC, digits = 2L), # linear space
"<br>p-value:", format(p_vec, digits = 2L),
"<br>"
)
color.by <- case_when(
(p_vec >= -log10(cutoff)) & (abs(FC) >= fc.cutoff) ~ "Significant & Fold Change",
p_vec >= -log10(cutoff) ~ "Significant",
abs(FC) >= fc.cutoff ~ "Fold Change",
TRUE ~ "Non-Significant"
)
# matched to `plotVolcano` palette
cols <- .volcano_cols()
if ( is.null(main) ) {
if ( grepl("Student t-test|Wilcoxon|Logistic|Kolmog", test) ) {
main <- sprintf(
"%s (n = %s) vs. %s (n = %s)",
names(levels)[1L], levels[[1L]], names(levels)[2L], levels[[2L]]
)
} else if ( grepl("Linear Regression|Spearman", test) ) {
main <- sprintf("%s\n%s", test, x_pred)
} else if ( grepl("Cox|AFT", test) ) {
main <- sprintf(
"Event: %s | Time: %s", calc_object$status, calc_object$time
)
}
}
if ( is.null(x.lab) ) {
x.lab <- paste("<i>log</i><sub>2</sub> Fold Change<br>Reference Group:",
ref_group)
} else {
paste(x.lab, "<br>Reference Group:", ref_group)
}
if ( all(FC >= 0) ) {
warning(
"It appears you are not passing log2-transformed ",
"fold-change values. Please check `data` argument.",
call. = FALSE
)
}
plotly::plot_ly() |>
plotly::add_trace(
x = FC, y = p_vec,
color = color.by,
colors = cols,
mode = "markers", type = "scatter",
marker = list(opacity = 0.7, size = 9),
text = text_labels, ...) |>
plotly::layout(
title = main,
xaxis = list(title = x.lab, showgrid = FALSE),
yaxis = list(title = "-<i>log</i><sub>10</sub> p-value", showgrid = FALSE),
margin = list(b = 60, t = 60), ...)
}
.volcano_cols <- function() {
c("Non-Significant" = soma_colors$lightgrey,
"Significant" = soma_colors$purple,
"Fold Change" = soma_colors$lightgreen,
"Significant & Fold Change" = soma_colors$yellow)
}