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 package. The gates are conducted in two-dimensional space comprised of two markers.
Examples of a single condition with two groups:
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:
\[\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 for an example using publicly available flow cytometry data from the flowWorkspaceData package available via Bioconductor. Here, we generate data with two conditions, four markers, and two additional features.
We start with the necessary packages and seed for the vignette.
<- c("gateR", "graphics", "stats", "tibble", "utils")
loadedPackages invisible(lapply(loadedPackages, library, character.only = TRUE))
set.seed(1234) # for reproducibility
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
).
<- function(centre, ncell, scalar) {
rand_mvn <- centre[1]
x0 <- centre[2]
y0 <- rep(x0, ncell)
x1 <- rep(y0, ncell)
y1 <- x1 + stats::rnorm(ncell, 0, scalar)
x2 <- y1 + stats::rnorm(ncell, 0, scalar)
y2 <- cbind(x2, y2)
x }
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
).
# Initial parameters
<- 100000 # number of observations per group per condition
ncell <- c(0.55, 0.55)
c1_cas_center <- c(0.40, 0.40)
c1_con_center # V1 and V2 at Condition 1
<- rand_mvn(centre = c1_cas_center, ncell = ncell, scalar = 0.05)
c1_cas <- rand_mvn(centre = c1_con_center, ncell = ncell, scalar = 0.15)
c1_con ::par(pty = "s")
graphics::plot(c1_con,
graphicscol = "blue",
xlim = c(0, 1),
ylim = c(0, 1),
main = "Gate 1, Condition 1",
xlab = "V1",
ylab = "V2")
::points(c1_cas, col = "orangered4") graphics
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
).
# Initial parameters
<- c(0.45, 0.45)
c2_cas_center <- c(0.40, 0.40)
c2_con_center # V1 and V2 at Condition 2
<- rand_mvn(centre = c2_cas_center, ncell = ncell, scalar = 0.05)
c2_cas <- rand_mvn(centre = c2_con_center, ncell = ncell, scalar = 0.10)
c2_con ::par(pty = "s")
graphics::plot(c2_con,
graphicscol = "cornflowerblue",
xlim = c(0, 1),
ylim = c(0, 1),
main = "Gate 1, Condition 2",
xlab = "V1",
ylab = "V2")
::points(c2_cas, col = "orangered1") graphics
# compile data
<- tibble::tibble("id" = seq(1, ncell * 2 * 2, 1),
df_full "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
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
).
# Initial parameters
<- c(0.55, 0.55)
c1_cas_center <- c(0.50, 0.50)
c1_con_center # V3 and V4 at Condition 1
<- rand_mvn(centre = c1_cas_center, ncell = ncell, scalar = 0.05)
c1_cas <- rand_mvn(centre = c1_con_center, ncell = ncell, scalar = 0.10)
c1_con ::par(pty = "s")
graphics::plot(c1_con,
graphicscol = "blue",
xlim = c(0, 1),
ylim = c(0, 1),
main = "Gate 2, Condition 1",
xlab = "V3",
ylab = "V4")
::points(c1_cas, col = "orangered4") graphics
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
).
# Initial parameters
<- c(0.65, 0.65)
c2_cas_center <- c(0.50, 0.50)
c2_con_center # V3 and V4 at Condition 2
<- rand_mvn(centre = c2_cas_center, ncell = ncell, scalar = 0.05)
c2_cas <- rand_mvn(centre = c2_con_center, ncell = ncell, scalar = 0.10)
c2_con ::par(pty = "s")
graphics::plot(c2_con,
graphicscol = "cornflowerblue",
xlim = c(0, 1),
ylim = c(0, 1),
main = "Gate 2, Condition 2",
xlab = "V3",
ylab = "V4")
::points(c2_cas, col = "orangered1") graphics
Compile the toy data into a data frame
$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])
df_full
rm(c2_cas, c1_cas, c2_con, c1_con) # conserve memory
Generate random values for two example cytokines and append to the data frame.
# Two Cytokines
<- stats::rchisq(ncell * 4, df = 5) # Random Chi-square distribution
Z1 <- stats::rnorm(ncell * 4, 0, 1) # Random Gaussian distribution
Z2 # Append to data.frame
$Z1 <- Z1
df_full$Z2 <- Z2
df_fullrm(Z1, Z2) # conserve memory
# Visualize histograms by the two group conditions
::par(mfrow = c(2, 2), pty = "s")
graphics::plot(stats::density(df_full$Z1[df_full$group == "case"
graphics& df_full$condition == "1"]),
main = "Cytokine 1 of Cases at Condition 1")
::plot(stats::density(df_full$Z1[df_full$group == "case"
graphics& df_full$condition == "2"]),
main = "Cytokine 1 of Cases at Condition 2")
::plot(stats::density(df_full$Z1[df_full$group == "control"
graphics& df_full$condition == "1"]),
main = "Cytokine 1 of Controls at Condition 1")
::plot(stats::density(df_full$Z1[df_full$group == "control"
graphics& df_full$condition == "2"]),
main = "Cytokine 1 of Controls at Condition 2")
::plot(stats::density(df_full$Z2[df_full$group == "case"
graphics& df_full$condition == "1"]),
main = "Cytokine 2 of Cases at Condition 1")
::plot(stats::density(df_full$Z2[df_full$group == "case"
graphics& df_full$condition == "2"]),
main = "Cytokine 2 of Cases at Condition 2")
::plot(stats::density(df_full$Z2[df_full$group == "control"
graphics& df_full$condition == "1"]),
main = "Cytokine 2 of Controls at Condition 1")
::plot(stats::density(df_full$Z2[df_full$group == "control"
graphics& df_full$condition == "2"]),
main = "Cytokine 2 of Controls at Condition 2")
The toy data frame has nine columns (id, groups, markers, and cytokines).
::head(df_full) utils
## # A tibble: 6 × 9
## id group condition V1 V2 V3 V4 Z1 Z2
## <dbl> <fct> <fct> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1 case 2 0.491 0.402 0.677 0.586 4.35 -0.488
## 2 2 case 2 0.407 0.493 0.714 0.698 8.61 0.279
## 3 3 case 2 0.508 0.409 0.547 0.644 6.79 -0.786
## 4 4 case 2 0.423 0.480 0.657 0.656 1.04 -0.552
## 5 5 case 2 0.367 0.420 0.635 0.637 4.10 0.239
## 6 6 case 2 0.499 0.405 0.547 0.656 6.99 0.0472
# Initial parameters
<- 0.05
alpha <- c("V1", "V2", "V3", "V4")
vars <- "correlated Bonferroni"
p_correct set.seed(1234) # for reproducibility
<- as.data.frame(df_full)
df_full
# Gates 1 and 2
<- Sys.time() # record start time
start_time <- gateR::gating(dat = df_full,
out_gate 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
<- Sys.time() # record end time
end_time <- end_time - start_time # calculate duration of gating() example total_time
The gating process took about 14.9 seconds on a machine with the features listed at the end of the vignette (4 variables, 2 gates, 2 cytokines, 400,000 observations). The corrected significance level in the first gate was . The histograms for the two cytokines are the same as above.
# Plot of Cytokine 1
::par(mfrow = c(1, 2), pty = "s")
graphics::plot(stats::density(out_gate$obs$Z1[out_gate$obs$group == "case"
graphics& out_gate$obs$condition == "2"]),
col = "red", main = "Cytokine 1 of cases\npost-gating",
xlim = c(-5, 30),
ylim = c(0, 0.2))
::plot(stats::density(out_gate$obs$Z1[out_gate$obs$group == "control"
graphics& 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
::par(mfrow = c(1, 2), pty = "s")
graphics::plot(stats::density(out_gate$obs$Z2[out_gate$obs$group == "case"
graphics& out_gate$obs$condition == "2"]),
col = "red",
main = "Cytokine 2 of cases\npost-gating",
xlim = c(-5, 5),
ylim = c(0, 0.5))
::plot(stats::density(out_gate$obs$Z2[out_gate$obs$group == "control"
graphics& 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 400,000 (cases & controls and Condition 1 & Condition 2) to 73,316 observations (cases & controls and Condition 1 & Condition 2).
# Plot of Cytokine 1
::par(mfrow = c(1, 2), pty = "s")
graphics::plot(stats::density(df_full$Z1[df_full$group == "case"
graphics& df_full$condition == "2"]),
col = "black",
lty = 1,
main = "Cytokine 1 of cases\npre-gating",
xlim = c(-5, 30),
ylim = c(0, 0.2))
::plot(stats::density(out_gate$obs$Z1[out_gate$obs$group == "case"
graphics& 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
::par(mfrow = c(1, 2), pty = "s")
graphics::plot(stats::density(df_full$Z2[df_full$group == "case"
graphics& df_full$condition == "2"]),
col = "black",
lty = 1,
main = "Cytokine 2 of cases\npre-gating",
xlim = c(-5, 5),
ylim = c(0, 0.5))
::plot(stats::density(out_gate$obs$Z2[out_gate$obs$group == "case"
graphics& 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))
# Data subset, only c1
<- df_full[df_full$condition == 1, ] # For only condition condition = 1
df_sub
# Initial parameters
<- 0.05
alpha <- c("V1", "V2", "V3", "V4")
vars <- "correlated Bonferroni"
p_correct set.seed(1234) # for reproducibility
# Gates 1 and 2
<- Sys.time() # record start time
start_time <- gateR::gating(dat = df_sub,
out_gate vars = vars,
plot_gate = TRUE,
n_condition = 1,
alpha = alpha,
p_correct = p_correct,
c1n = "case") # level "case" as the numerator of first condition
<- Sys.time() # record end time
end_time <- end_time - start_time # calculate duration of gating() example total_time
The gating process took about 18 seconds on a machine with the features listed at the end of the vignette (4 variables, 2 gates, 2 cytokines, 200,000 observations). The corrected significance level in the first gate was . The histograms for the two cytokines are the same as above.
# Plot of Cytokine 1
::par(mfrow = c(1, 2), pty = "s")
graphics::plot(stats::density(out_gate$obs$Z1[out_gate$obs$group == "case"]),
graphicscol = "red",
main = "Cytokine 1 of cases\npost-gating",
xlim = c(-5, 30),
ylim = c(0, 0.2))
::plot(stats::density(out_gate$obs$Z1[out_gate$obs$group == "control"]),
graphicscol = "blue",
main = "Cytokine 1 of controls\npost-gating",
xlim = c(-5, 30),
ylim = c(0, 0.2))
# Plot of Cytokine 2
::par(mfrow = c(1, 2), pty = "s")
graphics::plot(stats::density(out_gate$obs$Z2[out_gate$obs$group == "case"]),
graphicscol = "red",
main = "Cytokine 2 of cases\npost-gating",
xlim = c(-5, 5),
ylim = c(0, 0.5))
::plot(stats::density(out_gate$obs$Z2[out_gate$obs$group == "control"]),
graphicscol = "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 200,000 (cases & controls) to 86,167 observations (cases & controls).
# Plot of Cytokine 1
::par(mfrow = c(1, 2), pty = "s")
graphics::plot(stats::density(df_full$Z1[df_full$group == "case"]),
graphicscol = "black",
lty = 1,
main = "Cytokine 1 of cases\npre-gating",
xlim = c(-5, 30),
ylim = c(0, 0.2))
::plot(stats::density(out_gate$obs$Z1[out_gate$obs$group == "case"]),
graphicscol = "black",
lty = 1,
main = "Cytokine 1 of cases\npost-gating",
xlim = c(-5, 30),
ylim = c(0, 0.2))
# Plot of Cytokine 2
::par(mfrow = c(1, 2), pty = "s")
graphics::plot(stats::density(df_full$Z2[df_full$group == "case"]),
graphicscol = "black",
lty = 1,
main = "Cytokine 2 of cases\npre-gating",
xlim = c(-5, 5),
ylim = c(0, 0.5))
::plot(stats::density(out_gate$obs$Z2[out_gate$obs$group == "case"]),
graphicscol = "black",
lty = 1,
main = "Cytokine 2 of cases\npost-gating",
xlim = c(-5, 5),
ylim = c(0, 0.5))
sessionInfo()
## R version 4.2.1 (2022-06-23)
## Platform: x86_64-apple-darwin17.0 (64-bit)
## Running under: macOS Catalina 10.15.7
##
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRlapack.dylib
##
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] tibble_3.1.8 gateR_0.1.11
##
## loaded via a namespace (and not attached):
## [1] viridis_0.6.2 sass_0.4.2 maps_3.4.0
## [4] jsonlite_1.8.0 viridisLite_0.4.1 splines_4.2.1
## [7] foreach_1.5.2 sparr_2.2-16 dotCall64_1.0-1
## [10] bslib_0.4.0 SpatialPack_0.4 highr_0.9
## [13] sp_1.5-0 spatstat.geom_2.4-0 yaml_2.3.5
## [16] pillar_1.8.1 lattice_0.20-45 glue_1.6.2
## [19] digest_0.6.29 polyclip_1.10-0 colorspace_2.0-3
## [22] htmltools_0.5.3 Matrix_1.4-1 spatstat.sparse_2.1-1
## [25] pkgconfig_2.0.3 raster_3.5-21 misc3d_0.9-1
## [28] purrr_0.3.4 spatstat.core_2.4-4 scales_1.2.1
## [31] tensor_1.5 terra_1.5-21 spatstat.utils_2.3-1
## [34] mgcv_1.8-40 generics_0.1.3 ggplot2_3.3.6
## [37] spatstat.random_2.2-0 cachem_1.0.6 cli_3.3.0
## [40] magrittr_2.0.3 deldir_1.0-6 evaluate_0.15
## [43] fansi_1.0.3 doParallel_1.0.17 nlme_3.1-158
## [46] tools_4.2.1 lifecycle_1.0.1 stringr_1.4.0
## [49] munsell_0.5.0 compiler_4.2.1 jquerylib_0.1.4
## [52] rlang_1.0.4 grid_4.2.1 iterators_1.0.14
## [55] rstudioapi_0.13 goftest_1.2-3 spam_2.9-1
## [58] tcltk_4.2.1 rmarkdown_2.14 spatstat.linnet_2.3-2
## [61] gtable_0.3.0 codetools_0.2-18 abind_1.4-5
## [64] R6_2.5.1 gridExtra_2.3 knitr_1.39
## [67] dplyr_1.0.9 fastmap_1.1.0 utf8_1.2.2
## [70] fastmatrix_0.4-124 stringi_1.7.8 spatstat.data_2.2-0
## [73] parallel_4.2.1 spatstat_2.3-4 Rcpp_1.0.9
## [76] fields_14.1 vctrs_0.4.1 rpart_4.1.16
## [79] tidyselect_1.1.2 xfun_0.31