-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvignette.Rmd
402 lines (351 loc) · 19.2 KB
/
vignette.Rmd
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
---
title: "gateR: Flow/Mass Cytometry Gating via Spatial Kernel Density Estimation"
author: 'Ian D. Buller, Ph.D., M.A. (Github: @idblr)'
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{gateR: Flow/Mass Cytometry Gating via Spatial Kernel Density Estimation}
%\VignetteEngine{R.rsp::asis}
%\VignetteEncoding{UTF-8}
---
```{r setup, include = FALSE}
knitr::opts_chunk$set(echo = TRUE, warning = FALSE, message = FALSE, cache = FALSE, fig.width = 7, fig.height = 7, fig.show = "hold")
```
The gateR package is a suite of R functions to identify significant spatial clustering of mass and flow cytometry data used in immunological investigations. The gateR package can be used for a panel of all surface markers or a mixture of surface markers and functional readouts. The gateR package performs a gating technique that estimates statistically significant marker combination values within which one immunologically distinctive group (i.e., disease case) is more associated than another group (i.e., healthy control), successively, using various combinations (i.e., "gates") of markers to examine features of cells that may be different between groups. For a two-group comparison, the gateR package uses the spatial relative risk function estimated using the [sparr](https://CRAN.R-project.org/package=sparr) package. The gates are conducted in two-dimensional space comprised of two markers.
Examples of a single condition with two groups:
1. Disease case vs. Healthy control
2. Time 2 vs. Time 1 (baseline)
For a two-group comparison of two conditions, we estimate two relative risk surfaces for one condition and then a ratio of the relative risks. For example:
1. Estimate a relative risk surface for:
1. Condition 2B vs. Condition 2A
2. Condition 1B vs. Condition 1A
2. Estimate the relative risk surface for the ratio:
$$\frac{(\frac{Condition2B}{Condition2A})}{(\frac{Condition1B}{Condition1A})}$$
Within areas where the relative risk exceeds an asymptotic normal assumption, the gateR package has the functionality to examine the features of these cells.
This vignette implements the gateR package using a randomly generated data set. Please see the README.md file within the [gateR GitHub repository](https://github.com/lance-waller-lab/gateR) for an example using publicly available flow cytometry data from the [flowWorkspaceData](https://bioconductor.org/packages/release/data/experiment/html/flowWorkspaceData.html) package available via [Bioconductor](https://bioconductor.org/). Here, we generate data with two conditions, four markers, and two additional features.
We start with the necessary packages and seed for the vignette.
```{r packages}
loadedPackages <- c("gateR", "graphics", "stats", "tibble", "utils")
invisible(lapply(loadedPackages, library, character.only = TRUE))
set.seed(1234) # for reproducibility
```
### Generate random toy data
A unique function randomly generates multivariate normal (MVN) data around a central point. Parameters include the centroid coordinates (`centre`), the number of observations to generate (`ncell`), and the standard deviation of the normal distribution (`scalar`).
```{r rand_mvn_function}
rand_mvn <- function(centre, ncell, scalar) {
x0 <- centre[1]
y0 <- centre[2]
x1 <- rep(x0, ncell)
y1 <- rep(y0, ncell)
x2 <- x1 + stats::rnorm(ncell, 0, scalar)
y2 <- y1 + stats::rnorm(ncell, 0, scalar)
x <- cbind(x2, y2)
}
```
#### Gate 1: Marker 1 and Marker 2
At Condition 1, we generate 100,000 cases and 100,000 controls (`ncell = 100000`) randomly MVN with a case centroid at (`0.55, 0.55`) and a control centroid at (`0.40, 0.40`) within a unit square window `(0, 1)`, and cases have a more focal cluster (`scalar = 0.05`) than controls (`scalar = 0.15`).
```{r gate1_condition1}
# Initial parameters
ncell <- 100000 # number of observations per group per condition
c1_cas_center <- c(0.55, 0.55)
c1_con_center <- c(0.40, 0.40)
# V1 and V2 at Condition 1
c1_cas <- rand_mvn(centre = c1_cas_center, ncell = ncell, scalar = 0.05)
c1_con <- rand_mvn(centre = c1_con_center, ncell = ncell, scalar = 0.15)
graphics::par(pty = "s")
graphics::plot(c1_con,
col = "blue",
xlim = c(0, 1),
ylim = c(0, 1),
main = "Gate 1, Condition 1",
xlab = "V1",
ylab = "V2")
graphics::points(c1_cas, col = "orangered4")
```
At Condition 2, we generate 100,000 cases and 100,000 controls (`ncell = 100000`) randomly MVN with a case centroid at (`0.45, 0.45`) and a control centroid at (`0.40, 0.40`) within a unit square window `(0, 1)`, and cases have a more focal cluster (`scalar = 0.05`) than controls (`scalar = 0.10`).
```{r gate1_condition2}
# Initial parameters
c2_cas_center <- c(0.45, 0.45)
c2_con_center <- c(0.40, 0.40)
# V1 and V2 at Condition 2
c2_cas <- rand_mvn(centre = c2_cas_center, ncell = ncell, scalar = 0.05)
c2_con <- rand_mvn(centre = c2_con_center, ncell = ncell, scalar = 0.10)
graphics::par(pty = "s")
graphics::plot(c2_con,
col = "cornflowerblue",
xlim = c(0, 1),
ylim = c(0, 1),
main = "Gate 1, Condition 2",
xlab = "V1",
ylab = "V2")
graphics::points(c2_cas, col = "orangered1")
```
```{r compile_data}
# compile data
df_full <- tibble::tibble("id" = seq(1, ncell * 2 * 2, 1),
"group" = factor(c(rep("case", ncell * 2),
rep("control", ncell * 2))),
"condition" = factor(c(rep("2", ncell), rep("1", ncell),
rep("2", ncell), rep("1", ncell))),
"V1" = c(c2_cas[ , 1], c1_cas[ , 1], c2_con[ , 1], c1_con[ , 1]),
"V2" = c(c2_cas[ , 2], c1_cas[ , 2], c2_con[ , 2], c1_con[ , 2]))
rm(c2_cas, c1_cas, c2_con, c1_con) # conserve memory
```
#### Gate 2: Marker 3 and Marker 4
At Condition 1, we generate 100,000 cases and 100,000 controls (`ncell = 100000`) randomly MVN with a case centroid at (`0.55, 0.55`) and a control centroid at (`0.50, 0.50`) within a unit square window `(0, 05)`, but both have the same amount of spread (`scalar = 0.10`).
```{r gate2_condition1}
# Initial parameters
c1_cas_center <- c(0.55, 0.55)
c1_con_center <- c(0.50, 0.50)
# V3 and V4 at Condition 1
c1_cas <- rand_mvn(centre = c1_cas_center, ncell = ncell, scalar = 0.05)
c1_con <- rand_mvn(centre = c1_con_center, ncell = ncell, scalar = 0.10)
graphics::par(pty = "s")
graphics::plot(c1_con,
col = "blue",
xlim = c(0, 1),
ylim = c(0, 1),
main = "Gate 2, Condition 1",
xlab = "V3",
ylab = "V4")
graphics::points(c1_cas, col = "orangered4")
```
At Condition 2, we generate 100,000 cases and 100,000 controls (`ncell = 100000`) randomly with a case centroid at (`0.65, 0.65`) and control a centroid at (`0.50, 0.50`) within a unit square window `(0, 1)`, and cases have a more focal cluster (`scalar = 0.05`) than controls (`scalar = 0.10`).
```{r gate2_condition2}
# Initial parameters
c2_cas_center <- c(0.65, 0.65)
c2_con_center <- c(0.50, 0.50)
# V3 and V4 at Condition 2
c2_cas <- rand_mvn(centre = c2_cas_center, ncell = ncell, scalar = 0.05)
c2_con <- rand_mvn(centre = c2_con_center, ncell = ncell, scalar = 0.10)
graphics::par(pty = "s")
graphics::plot(c2_con,
col = "cornflowerblue",
xlim = c(0, 1),
ylim = c(0, 1),
main = "Gate 2, Condition 2",
xlab = "V3",
ylab = "V4")
graphics::points(c2_cas, col = "orangered1")
```
Compile the toy data into a data frame
```{r append_data}
df_full$V3 <- c(c2_cas[ , 1], c1_cas[ , 1], c2_con[ , 1], c1_con[ , 1])
df_full$V4 <- c(c2_cas[ , 2], c1_cas[ , 2], c2_con[ , 2], c1_con[ , 2])
rm(c2_cas, c1_cas, c2_con, c1_con) # conserve memory
```
Generate random values for two example cytokines and append to the data frame.
```{r cytokines}
# Two Cytokines
Z1 <- stats::rchisq(ncell * 4, df = 5) # Random Chi-square distribution
Z2 <- stats::rnorm(ncell * 4, 0, 1) # Random Gaussian distribution
# Append to data.frame
df_full$Z1 <- Z1
df_full$Z2 <- Z2
rm(Z1, Z2) # conserve memory
# Visualize histograms by the two group conditions
graphics::par(mfrow = c(2, 2), pty = "s")
graphics::plot(stats::density(df_full$Z1[df_full$group == "case"
& df_full$condition == "1"]),
main = "Cytokine 1 of Cases at Condition 1")
graphics::plot(stats::density(df_full$Z1[df_full$group == "case"
& df_full$condition == "2"]),
main = "Cytokine 1 of Cases at Condition 2")
graphics::plot(stats::density(df_full$Z1[df_full$group == "control"
& df_full$condition == "1"]),
main = "Cytokine 1 of Controls at Condition 1")
graphics::plot(stats::density(df_full$Z1[df_full$group == "control"
& df_full$condition == "2"]),
main = "Cytokine 1 of Controls at Condition 2")
graphics::plot(stats::density(df_full$Z2[df_full$group == "case"
& df_full$condition == "1"]),
main = "Cytokine 2 of Cases at Condition 1")
graphics::plot(stats::density(df_full$Z2[df_full$group == "case"
& df_full$condition == "2"]),
main = "Cytokine 2 of Cases at Condition 2")
graphics::plot(stats::density(df_full$Z2[df_full$group == "control"
& df_full$condition == "1"]),
main = "Cytokine 2 of Controls at Condition 1")
graphics::plot(stats::density(df_full$Z2[df_full$group == "control"
& df_full$condition == "2"]),
main = "Cytokine 2 of Controls at Condition 2")
```
The toy data frame has nine columns (id, groups, markers, and cytokines).
```{r full_data}
utils::head(df_full)
```
### For two conditions
```{r 2C}
# Initial parameters
alpha <- 0.05
vars <- c("V1", "V2", "V3", "V4")
p_correct <- "correlated Bonferroni"
set.seed(1234) # for reproducibility
df_full <- as.data.frame(df_full)
# Gates 1 and 2
start_time <- Sys.time() # record start time
out_gate <- gateR::gating(dat = df_full,
vars = vars,
n_condition = 2,
plot_gate = TRUE,
alpha = alpha,
p_correct = p_correct,
c1n = "case", # level "case" as the numerator of first condition
c2n = "2") # level "2" as the numerator of second condition
end_time <- Sys.time() # record end time
total_time <- end_time - start_time # calculate duration of gating() example
```
The gating process took about `r round(total_time, digits = 1)` seconds on a machine with the features listed at the end of the vignette (4 variables, 2 gates, 2 cytokines, `r format(nrow(df_full), big.mark= ",")` observations). The corrected significance level in the first gate was `r formatC(out_gate$lrr[[1]]$alpha, format = "e", digits = 2)`. The histograms for the two cytokines are the same as above.
```{r 2C_cytokinesA}
# Plot of Cytokine 1
graphics::par(mfrow = c(1, 2), pty = "s")
graphics::plot(stats::density(out_gate$obs$Z1[out_gate$obs$group == "case"
& out_gate$obs$condition == "2"]),
col = "red", main = "Cytokine 1 of cases\npost-gating",
xlim = c(-5, 30),
ylim = c(0, 0.2))
graphics::plot(stats::density(out_gate$obs$Z1[out_gate$obs$group == "control"
& out_gate$obs$condition == "2"]),
col = "blue",
main = "Cytokine 1 of controls\npost-gating",
xlim = c(-5, 30),
ylim = c(0, 0.2))
# Plot of Cytokine 2
graphics::par(mfrow = c(1, 2), pty = "s")
graphics::plot(stats::density(out_gate$obs$Z2[out_gate$obs$group == "case"
& out_gate$obs$condition == "2"]),
col = "red",
main = "Cytokine 2 of cases\npost-gating",
xlim = c(-5, 5),
ylim = c(0, 0.5))
graphics::plot(stats::density(out_gate$obs$Z2[out_gate$obs$group == "control"
& out_gate$obs$condition == "2"]),
col = "blue",
main = "Cytokine 2 of controls\npost-gating",
xlim = c(-5, 5),
ylim = c(0, 0.5))
```
Compare histograms before and after gating. Gating reduced the overall sample size of observations from `r format(nrow(df_full), big.mark= ",")` (cases & controls and Condition 1 & Condition 2) to `r format(nrow(out_gate$obs), big.mark = ",")` observations (cases & controls and Condition 1 & Condition 2).
```{r 2C_cytokinesB}
# Plot of Cytokine 1
graphics::par(mfrow = c(1, 2), pty = "s")
graphics::plot(stats::density(df_full$Z1[df_full$group == "case"
& df_full$condition == "2"]),
col = "black",
lty = 1,
main = "Cytokine 1 of cases\npre-gating",
xlim = c(-5, 30),
ylim = c(0, 0.2))
graphics::plot(stats::density(out_gate$obs$Z1[out_gate$obs$group == "case"
& out_gate$obs$condition == "2"]),
col = "black",
lty = 1,
main = "Cytokine 1 of cases\npost-gating",
xlim = c(-5, 30),
ylim = c(0, 0.2))
# Plot of Cytokine 2
graphics::par(mfrow = c(1, 2), pty = "s")
graphics::plot(stats::density(df_full$Z2[df_full$group == "case"
& df_full$condition == "2"]),
col = "black",
lty = 1,
main = "Cytokine 2 of cases\npre-gating",
xlim = c(-5, 5),
ylim = c(0, 0.5))
graphics::plot(stats::density(out_gate$obs$Z2[out_gate$obs$group == "case"
& out_gate$obs$condition == "2"]),
col = "black",
lty = 1,
main = "Cytokine 2 of cases\npost-gating",
xlim = c(-5, 5),
ylim = c(0, 0.5))
```
### For a one condition (using only Condition 1)
```{r 1C}
# Data subset, only c1
df_sub <- df_full[df_full$condition == 1, ] # For only condition condition = 1
# Initial parameters
alpha <- 0.05
vars <- c("V1", "V2", "V3", "V4")
p_correct <- "correlated Bonferroni"
set.seed(1234) # for reproducibility
# Gates 1 and 2
start_time <- Sys.time() # record start time
out_gate <- gateR::gating(dat = df_sub,
vars = vars,
plot_gate = TRUE,
n_condition = 1,
alpha = alpha,
p_correct = p_correct,
c1n = "case") # level "case" as the numerator of first condition
end_time <- Sys.time() # record end time
total_time <- end_time - start_time # calculate duration of gating() example
```
The gating process took about `r round(total_time, digits = 1)` seconds on a machine with the features listed at the end of the vignette (4 variables, 2 gates, 2 cytokines, `r format(nrow(df_sub), big.mark= ",")` observations). The corrected significance level in the first gate was `r formatC(out_gate$lrr[[1]]$alpha, format = "e", digits = 2)`. The histograms for the two cytokines are the same as above.
```{r 1C_cytokinesA}
# Plot of Cytokine 1
graphics::par(mfrow = c(1, 2), pty = "s")
graphics::plot(stats::density(out_gate$obs$Z1[out_gate$obs$group == "case"]),
col = "red",
main = "Cytokine 1 of cases\npost-gating",
xlim = c(-5, 30),
ylim = c(0, 0.2))
graphics::plot(stats::density(out_gate$obs$Z1[out_gate$obs$group == "control"]),
col = "blue",
main = "Cytokine 1 of controls\npost-gating",
xlim = c(-5, 30),
ylim = c(0, 0.2))
# Plot of Cytokine 2
graphics::par(mfrow = c(1, 2), pty = "s")
graphics::plot(stats::density(out_gate$obs$Z2[out_gate$obs$group == "case"]),
col = "red",
main = "Cytokine 2 of cases\npost-gating",
xlim = c(-5, 5),
ylim = c(0, 0.5))
graphics::plot(stats::density(out_gate$obs$Z2[out_gate$obs$group == "control"]),
col = "blue",
main = "Cytokine 2 of controls\npost-gating",
xlim = c(-5, 5),
ylim = c(0, 0.5))
```
Compare histograms before and after gating. Gating reduced the overall sample size of observations from `r format(nrow(df_sub), big.mark= ",")` (cases & controls) to `r format(nrow(out_gate$obs), big.mark = ",")` observations (cases & controls).
```{r 1C_cytokinesB}
# Plot of Cytokine 1
graphics::par(mfrow = c(1, 2), pty = "s")
graphics::plot(stats::density(df_full$Z1[df_full$group == "case"]),
col = "black",
lty = 1,
main = "Cytokine 1 of cases\npre-gating",
xlim = c(-5, 30),
ylim = c(0, 0.2))
graphics::plot(stats::density(out_gate$obs$Z1[out_gate$obs$group == "case"]),
col = "black",
lty = 1,
main = "Cytokine 1 of cases\npost-gating",
xlim = c(-5, 30),
ylim = c(0, 0.2))
# Plot of Cytokine 2
graphics::par(mfrow = c(1, 2), pty = "s")
graphics::plot(stats::density(df_full$Z2[df_full$group == "case"]),
col = "black",
lty = 1,
main = "Cytokine 2 of cases\npre-gating",
xlim = c(-5, 5),
ylim = c(0, 0.5))
graphics::plot(stats::density(out_gate$obs$Z2[out_gate$obs$group == "case"]),
col = "black",
lty = 1,
main = "Cytokine 2 of cases\npost-gating",
xlim = c(-5, 5),
ylim = c(0, 0.5))
```
### Current limitations
1. Extracts observations at *all* significant clusters (either case or controls), and there is currently no functionality to select cells within a specific (set of) cluster(s) for the next gate.
2. Only two dimensions (i.e., markers) per gate because the spatial relative risk function is a two-dimensional spatial statistic.
3. Only two-group comparisons (e.g., case vs. control) per gate because the spatial relative risk function is a ratio by nature.
4. Only comparisons of one condition or two conditions are possible.
5. Large computational expense (i.e., run-time) to calculate the correlated Bonferroni correction.
6. A large sample size of observations (i.e., cells) may overload the gateR process. We are evaluating this potential limitation and developing a possible solution (e.g., randomly subsetting the data to estimate the clusters at each gate).
```{r system}
sessionInfo()
```