library(flowCore)
library(flowWorkspace)
library(openCyto)
library(ggcyto)
library(flowViz)
library(flowStats)
library(scales)
library(dplyr)
library(stringr)
library(viridis)

Read & transform

fs <- read.flowSet(pattern = "1000.fcs")

Transform (log):

fs_trans <- fsApply(fs, function(x) {transform(x, estimateLogicle(x, c("FSC-H", "SSC-H", "FL1-H", "FL4-H", "FSC-A", "SSC-A")))})

Plot transformed non-gated

Plot FSC-A vs FSC-H:

ggcyto(fs_trans, aes(x = `FSC-H`, y = `FSC-A`)) +
  geom_hex(bins = 128) + 
  theme_bw() + theme(legend.position = "none", aspect.ratio = 1) + facet_wrap(~name, ncol = 4) 

Plot Syto9 vs FSC:

ggcyto(fs_trans, aes(x = `FSC-H`, y = `FL1-H`)) +
  geom_hex(bins = 128) + 
  labs(title = "Syto9 vs FSC", y = "Syto9", x = "FSC-H") + 
  theme_bw() + theme(legend.position = "none", aspect.ratio = 1) + facet_wrap(~name, ncol = 4) 

Plot SSC vs FSC:

ggcyto(fs_trans, aes(x = `FSC-H`, y = `SSC-H`)) +
  geom_hex(bins = 128) + 
  labs(title = "SSC vs FSC", x = "FSC-H", y = "SSC-H") + 
  theme_bw() + theme(legend.position = "none", aspect.ratio = 1) + facet_wrap(~name, ncol = 4) 

Plot Syto9 vs ebpC:

ggcyto(fs_trans, aes(x = `FL4-H`, y = `FL1-H`)) +
  geom_hex(bins = 128) + 
  labs(title = "Syto9 vs ebpC", x = "ebpC", y = "Syto9") + 
  theme_bw() + theme(legend.position = "none", aspect.ratio = 1) + facet_wrap(~name, ncol = 4) 

Gate

Create gate set:

gs <- GatingSet(fs_trans)

Syto9 gate: 98% quantile gate (left tail cutoff)

g_syto9 <- fsApply(fs_trans, function(x) {openCyto:::.quantileGate(x, channels = "FL1-H", probs = 0.02, filterId="Syto9")})
gs_pop_add(gs, g_syto9, parent = "root")
## [1] 2
gs_get_pop_paths(gs)
## [1] "root"   "/Syto9"
recompute(gs)
## done!

Plot 1D:

ggcyto(gs, aes(x = `FL1-H`)) +
  geom_density(fill = "green") +
  geom_gate("Syto9") + 
  labs(title = "Gating (Syto9)", x = "Syto9") + 
  theme_bw() + theme(legend.position = "none") + facet_wrap(~name, ncol = 4) 

Plot 2D:

ggcyto(gs, aes(x = `FL4-H`, y = `FL1-H`)) +
  geom_hex(bins = 128) +
  geom_gate("Syto9") + 
  labs(title = "Gating (Syto9)", x = "ebpC", y = "Syto9") + 
  theme_bw() + theme(legend.position = "none", aspect.ratio = 1) + facet_wrap(~name, ncol = 4) 

Singlet gate:

g_singlet <- fsApply(fs_trans, function(x) {flowStats::gate_singlet(x, area = "FSC-A", height = "FSC-H", filterId = "Singlet")})
gs_pop_add(gs, g_singlet, parent = "Syto9")
## [1] 3
gs_get_pop_paths(gs)
## [1] "root"           "/Syto9"         "/Syto9/Singlet"
recompute(gs)
## done!
ggcyto(gs, aes(x = `FSC-H`, y = `FSC-A`)) +
  geom_hex(bins = 128) + geom_gate("Singlet") + geom_stats(adjust = 0.3, y= 4, size = 5, fill = NA) +  
  labs(title = "Gating (Syto9+/Singlet)", x = "FSC-H (height)", y = "FSC-A (area)") + 
  theme_bw() + theme(legend.position = "none", aspect.ratio = 1) + facet_wrap(~name, ncol = 4) 

g_morph <- fsApply(fs_trans, function(x) {flowStats::lymphGate(x, channels = c("FSC-H", "SSC-H"), filterId = "Morph")})
gs_pop_add(gs, g_morph, parent = "Singlet")
## [1] 4
gs_get_pop_paths(gs)
## [1] "root"                 "/Syto9"               "/Syto9/Singlet"      
## [4] "/Syto9/Singlet/Morph"
recompute(gs)
## done!
ggcyto(gs, aes(x = `FSC-H`, y =`SSC-H`)) +
  geom_hex(bins = 128) + geom_gate("Morph") +
  labs(title = "Gating (Syto9+/Singlet/Morph)", x = "FSC-H", y = "SSC-H") + geom_stats(x = 3.5, y= 4, size = 5, fill = NA) +  
  theme_bw() + theme(legend.position = "none", aspect.ratio = 1) + facet_wrap(~name, ncol = 4) 

Pili gate: minimum density on FL4

g_ebpC <- fsApply(fs_trans, function(x) {openCyto:::.mindensity(x, channels = "FL4-H", filterId="ebpC")})
gs_pop_add(gs, g_ebpC, parent = "Morph")
## [1] 5
gs_get_pop_paths(gs)
## [1] "root"                      "/Syto9"                   
## [3] "/Syto9/Singlet"            "/Syto9/Singlet/Morph"     
## [5] "/Syto9/Singlet/Morph/ebpC"
recompute(gs)
## done!

Plot 1D:

ggcyto(gs, aes(x = `FL4-H`)) +
  geom_density(fill = "red") +
  geom_gate("ebpC") + 
  labs(title = "Gating (Syto9+/Singlet/ebpC)", x = "ebpC") + 
  theme_bw() + theme(legend.position = "none") + facet_wrap(~name, ncol = 4) + geom_stats(digits = 2, size = 5, y = 2)

Plot 2d:

ggcyto(gs, aes(x = `FL4-H`, y = `FL1-H`)) +
  geom_hex(bins = 128) +
  geom_gate("ebpC") + geom_stats(fill = NA, adjust = 0.7, digits = 2, size = 5) + 
  labs(title = "Gating (Syto9+/Singlet/ebpC)", x = "ebpC", y = "Syto9") + 
  theme_bw() + theme(legend.position = "none", aspect.ratio = 1) + facet_wrap(~name, ncol = 4) 

Statistics:

df <- gs_pop_get_stats(gs,"ebpC", type = "percent") %>%
  mutate(day = str_extract(sample, pattern = "(?<=-D).+?(?=-1)")) %>%
  mutate(type = case_when(str_detect(sample, "TSB") ~ "planktonic (TSB)",
                          str_detect(sample, "HS") ~ "planktonic (TSB + serum 40%)",
                          str_detect(sample, "BF") ~ "biofilm",
                          str_detect(sample, "MC") ~ "macrocolony"
                          )) %>%
  rename(file = sample, sample = type) %>%
  mutate(day = factor(day)) %>%
  select(sample, day, percent, file) %>%
  arrange(sample)
df
##                          sample day    percent                file
## 1:                      biofilm   1 0.20918293  A03 BF-D1-1000.fcs
## 2:                      biofilm   3 0.21791941  B03 BF-D3-1000.fcs
## 3:                  macrocolony   1 0.05307825  A04 MC-D1-1000.fcs
## 4:                  macrocolony   3 0.02926616  B04 MC-D3-1000.fcs
## 5: planktonic (TSB + serum 40%)   1 0.16065227  A02 HS-D1-1000.fcs
## 6: planktonic (TSB + serum 40%)   3 0.30529412  B02 HS-D3-1000.fcs
## 7:             planktonic (TSB)   1 0.09840997 A01 TSB-D1-1000.fcs
## 8:             planktonic (TSB)   3 0.10546472 B01 TSB-D3-1000.fcs

Plot:

ggplot(df, aes(x = sample, y = percent, fill = day)) + 
  geom_col(position = "dodge", color = "black") + 
  scale_fill_brewer(palette = "Accent") +
  scale_y_continuous(labels = scales::percent) + 
  labs(title = "OG1RF piliation", y = "ebpC positive cells", x = "", fill = "Day") + 
  coord_flip() + 
  theme_bw()