Analyzing high-throughput FACS assays with R

FACS
Author

Songqi Duan

Published

August 31, 2024

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.1     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(flowCore)
library(ggcyto)
Loading required package: ncdfFlow
Loading required package: BH
Loading required package: flowWorkspace
As part of improvements to flowWorkspace, some behavior of
GatingSet objects has changed. For details, please read the section
titled "The cytoframe and cytoset classes" in the package vignette:

  vignette("flowWorkspace-Introduction", "flowWorkspace")
fs <- read.flowSet(files = "data/mPSCA_CAR_mCD8_CAR.fcs")
gs <- GatingSet(fs)
singlets <- polygonGate(
  filterId = "Singlets",
  "FSC-A" = c(0e4,15e4,20e4,2e4),
  "FSC-H" = c(0e4,10e4,15e4,3e4)
)
gs[[1]] |> 
  ggcyto(aes(x = `FSC-A`, y = `FSC-H`), subset = "root") + 
  geom_hex(bins = 200) +
  # stat_density_2d(aes(fill = after_stat(level)), geom = "polygon")+
  geom_gate(singlets) +
  ggcyto_par_set(limits = "instrument") +
  theme(aspect.ratio = 1)
Coordinate system already present. Adding new coordinate system, which will
replace the existing one.

add(gs = gs, gate = singlets) # add gate to GatingSet
Warning in add.default(gs = gs, gate = singlets): 'add.default' is deprecated.
Use 'gs_pop_add' instead.
See help("Deprecated")
[1] 2
recompute(x = gs) # recompute GatingSet
done!
gs[[1]] |> 
  ggcyto(aes(x = `FSC-A`, y = `SSC-A`), subset = "Singlets") + geom_hex(bins = 200) +
  ggcyto_par_set(limits = "instrument") +
  theme(aspect.ratio = 1)
Coordinate system already present. Adding new coordinate system, which will
replace the existing one.

gs[[1]] |>
  ggcyto(aes(x = `FSC-A`, y = `FSC-H`), subset = "root") + geom_hex(bins = 100) +
  geom_gate("Singlets") +
  geom_stats(adjust = 0.8) + ggcyto_par_set(limits = "instrument") +
  theme(aspect.ratio = 1)
Coordinate system already present. Adding new coordinate system, which will
replace the existing one.

g_live <- polygonGate(
  filterId = "Live",
  "FSC-A" = c(3e4, 5e4,16e4, 10e4),
  "SSC-A" = c(2e4, 8e4,8e4, 2e4)
) # define gate
ggcyto(gs[[1]], aes(x = `FSC-A`, y = `SSC-A`), subset = "Singlets") + geom_hex(bins = 200) +
  geom_gate(g_live) + ggcyto_par_set(limits = "instrument") +
  theme(aspect.ratio = 1)
Coordinate system already present. Adding new coordinate system, which will
replace the existing one.

add(gs,g_live,parent="Singlets") # add gate to GatingSet
Warning in add.default(gs, g_live, parent = "Singlets"): 'add.default' is deprecated.
Use 'gs_pop_add' instead.
See help("Deprecated")
[1] 3
recompute(gs) # recompute GatingSet
done!
gs[[1]] |> 
  ggcyto(aes(x = `FSC-A`, y = `SSC-A`), subset = "Singlets") + geom_hex(bins = 200) +
  geom_gate("Live") +
  geom_stats(adjust = 0.8) + 
  ggcyto_par_set(limits = "instrument") +
  catplot::theme_cat(aspect_ratio = 1) +
  scale_y_logicle()
Coordinate system already present. Adding new coordinate system, which will
replace the existing one.

g.gfp <- rectangleGate(filterId = "GFP positive", "PE-A" = c(1000, Inf)) # set gate
gs[[1]] |> 
  ggcyto(aes(x = `PE-A`), subset = "Live") + 
  geom_density(fill = "forestgreen") + 
  # geom_gate(g.gfp) + 
  ggcyto_par_set(limits = "instrument") + 
  scale_x_flowJo_biexp() # check gate
Coordinate system already present. Adding new coordinate system, which will
replace the existing one.
Warning in scale_x_flowJo_biexp(): 'scale_x_flowJo_biexp' is deprecated.
Use 'scale_x_flowjo_biexp' instead.
See help("Deprecated")

add(gs,g.gfp,parent="Live") # add gate to GatingSet
Warning in add.default(gs, g.gfp, parent = "Live"): 'add.default' is deprecated.
Use 'gs_pop_add' instead.
See help("Deprecated")
[1] 4
recompute(gs) # recalculate Gatingset
done!
gs[[1]] |> 
  ggcyto(aes(x=`PE-A`),subset="Live",)+geom_density(fill="forestgreen")+geom_gate("GFP positive")+
  geom_stats(adjust = 0.1,y=0.002,digits = 1)+
  ggcyto_par_set(limits = "instrument")+scale_x_flowJo_biexp()
Coordinate system already present. Adding new coordinate system, which will
replace the existing one.
Warning in scale_x_flowJo_biexp(): 'scale_x_flowJo_biexp' is deprecated.
Use 'scale_x_flowjo_biexp' instead.
See help("Deprecated")
Warning: Removed 1 row containing missing values or values outside the scale range
(`geom_segment()`).