/
qqly.R
214 lines (199 loc) · 7.26 KB
/
qqly.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
#' Creates a plotly Q-Q plot
#'
#' Creates an interactive Q-Q plot with multiple annotation options
#'
#' @param x Can be an object of class \code{qqr} produced by the
#' \code{\link{qqr}} function or a \code{data.frame} which must contain at
#' least the following column: \itemize{ \item{a p-value, must be numeric} }
#' @param col A \code{character} indicating the color of the points. Can
#' be \href{https://www.rapidtables.com/web/color/RGB_Color.html}{Hex Codes} as
#' well.
#' @param size A \code{numeric} specifying the size of the points. Default
#' is 1
#' @param type An integer between 0 and 25 specifying the point shape.
#' Default is 20 (filled circle). Deprecated.
#' @param abline_col A \code{character} indicating the color of the 45 degree
#' diagonal line. Can be
#' \href{https://www.rapidtables.com/web/color/RGB_Color.html}{Hex Codes} as
#' well. Default is \code{"red"}.
#' @param abline_size A \code{numeric} indicating the size of the 45 degree
#' diagonal line. Default is 0.5.
#' @param abline_type Sets the line type of the
#' 45 degree line. Set to a dash type character among "solid", "dot",
#' "dash", "longdash", "dashdot", or "longdashdot", or a dash length list in
#' px (eg "5px","10px","2px"). Can also be a positive numeric value (e.g 5,
#' 10, 2). Default is "dash". See
#' \href{https://plotly.com/r/reference/#layout-shapes}{plotly help page on
#' layouts} for complete list and more details
#' @param highlight A character vector of SNPs in your dataset to highlight.
#' These SNPs should all be in your dataset. Default is \code{NULL} which
#' means that nothing is highlighted.
#' @param highlight_color Color used to highlight points. Only used if
#' \code{highlight} argument has been specified
#' @param xlab X-axis label. Default is \code{"Expected -log10(p)"}
#' @param ylab Y-axis label. Default is \code{"Observed -log10(p)"}
#' @param title Title of the plot. Default is \code{"Q-Q Plot"}
#' @param ... other parameters passed to \code{\link{qqr}}
#' @return An interactive Q-Q plot.
#' @aliases qqly.default qqly.qqr
#' @seealso \code{\link{qqr}}, \code{\link{HapMap}},
#' \code{\link{significantSNP}}
#' @export
#' @importFrom magrittr '%>%'
#' @examples
#' \dontrun{
#' library(manhattanly)
#' qqly(HapMap)
#'
#' # highlight SNPs of interest
#' # 'signigicantSNP' is a character vector of SNPs included in this package
#' qqly(HapMap, snp = "SNP", highlight = significantSNP)
#' }
#'
qqly <- function(x,
...,
col = "#252525",
size = 5,
type = 20,
abline_col = "red",
abline_size = 1,
abline_type = "solid",
highlight = NULL,
highlight_color = "#00FF00",
xlab = "Expected -log10(p)",
ylab = "Observed -log10(p)",
title = "Q-Q Plot") {
UseMethod("qqly")
}
#' @export
qqly.default <- function(x,
...,
col = "#252525",
size = 5,
type = 20,
abline_col = "red",
abline_size = 1,
abline_type = "solid",
highlight = NULL,
highlight_color = "#00FF00",
xlab = "Expected -log10(p)",
ylab = "Observed -log10(p)",
title = "Q-Q Plot") {
qq <- qqr(x, ...)
qqly.qqr(qq,
col = col,
size = size,
type = type,
abline_col = abline_col,
abline_size = abline_size,
abline_type = abline_type,
highlight = highlight,
highlight_color = highlight_color,
xlab = xlab,
ylab = ylab,
title = title
)
}
#' @export
qqly.qqr <- function(x,
...,
col = "#252525",
size = 5,
type = 20,
abline_col = "red",
abline_size = 1,
abline_type = "solid",
highlight = NULL,
highlight_color = "#00FF00",
xlab = "Expected -log10(p)",
ylab = "Observed -log10(p)",
title = "Q-Q Plot") {
d <- x$data
pName <- x$pName
snpName <- x$snpName
geneName <- x$geneName
annotation1Name <- x$annotation1Name
annotation2Name <- x$annotation2Name
observed_vector <- d[["OBSERVED"]]
expected_vector <- d[["EXPECTED"]]
# used for identity line
for (i in 1:length(expected_vector)) {
if (i == 1 || max(expected_vector[[i]]) > max_x) {
max_x <- max(expected_vector[[i]])
}
if (i == 1 || max(observed_vector[[i]]) > max_y) {
max_y <- max(observed_vector[[i]])
}
if (i == 1 || min(expected_vector[[i]]) < min_x) {
min_x <- min(expected_vector[[i]])
}
if (i == 1 || min(observed_vector[[i]]) < min_y) {
min_y <- min(observed_vector[[i]])
}
}
if (!is.null(highlight) & is.na(snpName)) stop("You're trying to highlight snps, but havent provided a snp column")
my_xlab <- list(
title = xlab
)
my_ylab <- list(
title = ylab
)
fig <- plotly::plot_ly(d,
x = ~EXPECTED, y = ~OBSERVED,
type = "scatter", mode = "markers",
hoverinfo = "text",
marker = list(
color = col[1],
size = size
),
text = ~ paste0(
if (!is.na(snpName)) paste0(snpName, ": ", d[[snpName]], "<br>"),
if (!is.na(geneName)) paste0(geneName, ": ", d[[geneName]], "<br>"),
if (!is.na(annotation1Name)) paste0(annotation1Name, ": ", d[[annotation1Name]], "<br>"),
if (!is.na(annotation2Name)) paste0(annotation2Name, ": ", d[[annotation2Name]], "<br>"),
paste0("Observed", ": ", OBSERVED, "<br>"),
paste0("Expected", ": ", EXPECTED)
)
) %>%
plotly::layout(xaxis = my_xlab, yaxis = my_ylab, title = list(text = title)) %>%
plotly::layout(
shapes = list(
type = "line",
x0 = min_x,
x1 = max_x,
y0 = p_abline(min_x),
y1 = p_abline(max_x),
line = list(color = abline_col, dash = abline_type, width = abline_size)
)
)
# EXPECTED=OBSERVED=NULL
# Highlight snps from a character vector
if (!is.na(snpName)) {
if (!is.null(highlight)) {
if (any(!(highlight %in% d[[snpName]]))) warning("You're trying to highlight SNPs that don't exist in your results.")
d.highlight <- d[which(d[[snpName]] %in% highlight), ]
fig %<>% plotly::add_trace(
x = ~EXPECTED,
y = ~OBSERVED,
data = d.highlight,
inherit = FALSE,
type = "scatter",
mode = "markers",
marker = list(
color = highlight_color,
size = size
),
showlegend = FALSE,
text = ~ paste0(
if (!is.na(snpName)) paste0(snpName, ": ", d.highlight[[snpName]], "<br>"),
if (!is.na(geneName)) paste0(geneName, ": ", d.highlight[[geneName]], "<br>"),
if (!is.na(annotation1Name)) paste0(annotation1Name, ": ", d.highlight[[annotation1Name]], "<br>"),
if (!is.na(annotation2Name)) paste0(annotation2Name, ": ", d.highlight[[annotation2Name]], "<br>"),
paste0("Observed", ": ", OBSERVED, "<br>"),
paste0("Expected", ": ", EXPECTED)
)
)
}
}
fig
}