/
cyto_plot_contour.R
204 lines (184 loc) · 6.28 KB
/
cyto_plot_contour.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
## CYTO_PLOT_CONTOUR -----------------------------------------------------------
#' Add contour lines to cyto_plot
#'
#' @param x either an object of class
#' \code{\link[flowCore:flowFrame-class]{flowFrame}}, or
#' \code{\link[flowCore:flowSet-class]{flowSet}} or a list of
#' \code{\link[flowCore:flowFrame-class]{flowFrame}} objects.
#' @param channels channels used to construct the existing cyto_plot.
#' @param contour_lines numeric indicating the number of levels to use for
#' contour lines, set to 15 by default.
#' @param contour_line_type type of line to use for contour lines, set to 1 by
#' default.
#' @param contour_line_width line width for contour lines, set to 1 by default.
#' @param contour_line_col colour to use for contour lines, set to "black" by
#' default.
#' @param contour_line_alpha numeric [0,1] to control transparency of contour
#' lines, set to 1 by default to remove transparency.
#' @param ... not in use.
#'
#' @importFrom MASS kde2d
#' @importFrom graphics contour par
#' @importFrom flowCore exprs
#' @importFrom grDevices adjustcolor
#' @importFrom methods is
#'
#' @examples
#' library(CytoExploreRData)
#'
#' # Load in Samples
#' fs <- Activation
#'
#' # Apply compensation
#' fs <- cyto_compensate(fs)
#'
#' # Transform fluorescent channels
#' trans <- cyto_transformer_logicle(fs)
#' fs <- cyto_transform(fs, trans)
#'
#' # Plot
#' cyto_plot(fs[[32]],
#' channels = c("Alexa Fluor 488-A", "Alexa Fluor 700-A"),
#' axes_trans = trans
#' )
#'
#' # Contour lines
#' cyto_plot_contour(fs[[32]],
#' channels = c("Alexa Fluor 488-A", "Alexa Fluor 700-A"),
#' contour_lines = 20
#' )
#'
#' @author Dillon Hammill, \email{Dillon.Hammill@anu.edu.au}
#'
#' @name cyto_plot_contour
NULL
#' @rdname cyto_plot_contour
#' @export
cyto_plot_contour <- function(x, ...){
UseMethod("cyto_plot_contour")
}
#' @rdname cyto_plot_contour
#' @export
cyto_plot_contour.flowFrame <- function(x,
channels,
contour_lines = 15,
contour_line_type = 1,
contour_line_width = 1,
contour_line_col = "black",
contour_line_alpha = 1,
...){
# CHECKS -------------------------------------------------------------------
# CHANNELS
channels <- cyto_channels_extract(x, channels)
# PREPARE DATA -------------------------------------------------------------
fr <- x
fr_exprs <- exprs(fr)[, channels]
# CONTOUR_LINES ------------------------------------------------------------
if (contour_lines != 0) {
# BYPASS INSUFFICIENT EVENTS
if (nrow(fr_exprs) > 2) {
# 2D KERNEL DENSITY
z <- kde2d(
x = fr_exprs[, 1],
y = fr_exprs[, 2],
n = 75,
lims = par("usr")
)
# PLOT CONTOUR LINES
graphics::contour(
z = z$z,
x = z$x,
y = z$y,
add = TRUE,
drawlabels = FALSE,
nlevels = contour_lines,
col = adjustcolor(contour_line_col, contour_line_alpha),
lwd = contour_line_width,
lty = contour_line_type
)
}
}
}
#' @rdname cyto_plot_contour
#' @export
cyto_plot_contour.flowSet <- function(x,
channels,
contour_lines = 15,
contour_line_type = 1,
contour_line_width = 1,
contour_line_col = "black",
contour_line_alpha = 1,
...){
# CHECKS ---------------------------------------------------------------------
# CHANNELS
channels <- cyto_channels_extract(x, channels)
# PREPARE DATA ---------------------------------------------------------------
# LIST OF FLOWFRAMES
fr_list <- lapply(seq_len(length(x)), function(z) {
x[[z]]
})
# CONTOUR_LINES --------------------------------------------------------------
invisible(mapply(
function(fr,
contour_lines,
contour_line_type,
contour_line_width,
contour_line_col,
contour_line_alpha) {
cyto_plot_contour(fr,
channels = channels,
contour_lines = contour_lines,
contour_line_type = contour_line_type,
contour_line_width = contour_line_width,
contour_line_col = contour_line_col,
contour_line_alpha = contour_line_alpha
)
}, fr_list,
contour_lines,
contour_line_type,
contour_line_width,
contour_line_col,
contour_line_alpha
))
}
#' @rdname cyto_plot_contour
#' @export
cyto_plot_contour.list <- function(x,
channels,
contour_lines = 15,
contour_line_type = 1,
contour_line_width = 1,
contour_line_col = "black",
contour_line_alpha = 1,
...){
# CHECKS ---------------------------------------------------------------------
# CLASS
if (!all(lapply(x, is, "flowFrame"))) {
stop("x should be a list of flowFrame objects.")
}
# CHANNELS
channels <- cyto_channels_extract(x[[1]], channels)
# CONTOUR_LINES --------------------------------------------------------------
invisible(mapply(
function(fr,
contour_lines,
contour_line_type,
contour_line_width,
contour_line_col,
contour_line_alpha) {
cyto_plot_contour(fr,
channels = channels,
contour_lines = contour_lines,
contour_line_type = contour_line_type,
contour_line_width = contour_line_width,
contour_line_col = contour_line_col,
contour_line_alpha = contour_line_alpha
)
}, x,
contour_lines,
contour_line_type,
contour_line_width,
contour_line_col,
contour_line_alpha
))
}