First we load the required libreries and import functions from functionsCyto.R:

library(tidyverse)
library(flowWorkspace)
library(Biobase)
library(flowGate)
source("functionsCyto.R")

We import the .LMD or .fcs files to a FlowSet object that we convert into a GatingSet (in the case of LMD files, they include both the format FCS2 and FCS3, which is accessed as dataset=2):

fs<-read.ncdfFlowSet(files=list.files("BcellPhenotype-Files/",".LMD", full.names = T), readonly = F, dataset=2)
gs <- GatingSet(fs)
gs
## A GatingSet with 2 samples

Next, we will compensate with the compensation matrix of the adquisition, which is embed into the file. We can import another compensation matrix:

comp<-spillover(fs[[1]])$`$SPILLOVER`
colnames(comp)<-colnames(gs)[5:14]
rownames(comp)<-colnames(gs)[5:14]
comp
##        FL1-A FL2-A FL3-A FL4-A FL5-A FL6-A FL7-A FL8-A FL9-A FL10-A
## FL1-A  1.000 0.699 0.118 0.000 0.000 0.000 0.000 0.000 0.000  0.265
## FL2-A  0.002 1.000 0.542 0.000 0.000 0.000 0.000 0.000 0.000  0.000
## FL3-A  0.000 0.036 1.000 0.949 0.000 0.000 0.000 0.000 0.000  0.000
## FL4-A  0.000 0.000 0.003 1.000 0.182 0.168 0.161 0.000 0.000  0.000
## FL5-A  0.000 0.000 0.000 0.002 1.000 0.000 0.006 0.000 0.000  0.000
## FL6-A  0.000 0.000 0.000 0.000 0.000 1.000 0.293 0.000 0.000  0.000
## FL7-A  0.000 0.000 0.000 0.000 0.000 0.027 1.000 0.166 0.000  0.000
## FL8-A  0.000 0.000 0.000 0.000 0.000 0.000 0.090 1.000 0.000  0.000
## FL9-A  0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 1.000  0.136
## FL10-A 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.055  1.000
gs<-compensate(gs, comp)

To transform the axis in an interactive and visual way, I have created the following function:

trans_params<-transform_gs(gs)

trans_params$`FL3-A`
## $channel
## [1] "FL3-A"
## 
## $scale
## [1] "biexp"
## 
## $maxvalue
## [1] 250000
## 
## $pos
## [1] 5
## 
## $widthBasis
## [1] -400
## 
## $max
## [1] 5011.872
## 
## $min
## [1] 956

We can save the transformation params in a file and latter we can import and apply directly (including in other experiments):

saveRDS(trans_params, "BcellPhenotype-trans_params.rds")
trans_params<-readRDS("BcellPhenotype-trans_params.rds")
trans_apply(gs, trans_params = trans_params)

As it wasn’t done during the acquisition, we will define the marker name for the channels of interest:

markers<-colnames(gs)
markers[c(7,13)]<-c("CD19","L&D")
names(markers)<-colnames(gs)
markernames(gs)<-markers
markernames(gs)
##     FS-H     FS-A     FS-W     SS-A    FL1-A    FL2-A    FL3-A    FL4-A 
##   "FS-H"   "FS-A"   "FS-W"   "SS-A"  "FL1-A"  "FL2-A"   "CD19"  "FL4-A" 
##    FL5-A    FL6-A    FL7-A    FL8-A    FL9-A   FL10-A 
##  "FL5-A"  "FL6-A"  "FL7-A"  "FL8-A"    "L&D" "FL10-A"

Finally, we will clean a bit the sample names:

sampleNames(gs)
## [1] "FC20339 2020.09.14 LD CD19PECF594 aCDE19 002.LMD"
## [2] "FC20339 2020.09.14 LD CD19PECF594 Unst 001.LMD"
sampleNames(gs)<-gsub("\\s[0-9]*.LMD","",sampleNames(gs))
sampleNames(gs)<-gsub(".*\\s","",sampleNames(gs))
pData(gs)$name<-rownames(pData(gs))
sampleNames(gs)
## [1] "aCDE19" "Unst"

And we are ready to gate! We will be using the gs_interactive_gate function from flowGate package.

gs_gate_interactive(gs,
                    filterId = "Leukocytes",
                    dims = list("FS-A", "SS-A"))

gs_gate_interactive(gs,
                    subset = "Leukocytes",
                    filterId = "CD19 L&D",
                    dims = list("CD19", "L&D"))

We can save the created gates into a file to import latter on (which may be also used to apply the gating strategy into a new experiment):

gates<-gates_save(gs, file = "BcellPhenotype-gates.rds")

gates<-readRDS("BcellPhenotype-gates.rds")
gs<-gates_apply(gs, gates)
plot(gs)

We can rapidly explore the results using the autoplot function:

autoplot(gs, "Leukocytes", bins=128, nrow=1)

autoplot(gs[["aCDE19"]], bins=128, nrow=1)

We can further personalize the plot with similar sintaxis as ggplot with the ggcyto package:

g<-ggcyto(gs, subset = "Leukocytes", bins=128, aes(CD19,`L&D`))+
  facet_grid(.~factor(name, levels=c("Unst","aCDE19")))+
  geom_hex(bins=128)+
  geom_gate()+
  geom_stats()+
  scale_fill_gradient(low="black", high="violet")
g

Finally, we can export the stats and plot them:

stats<-gs_pop_get_stats(gs, nodes=gs_get_pop_paths(gs, path = "auto")[3:6], type="perc")
stats
##    sample       pop     percent
##    <char>    <char>       <num>
## 1: aCDE19 CD19-L&D+ 0.013749535
## 2: aCDE19 CD19+L&D+ 0.101820884
## 3: aCDE19 CD19+L&D- 0.880936455
## 4: aCDE19 CD19-L&D- 0.003493125
## 5:   Unst CD19-L&D+ 0.069043075
## 6:   Unst CD19+L&D+ 0.119411123
## 7:   Unst CD19+L&D- 0.373977644
## 8:   Unst CD19-L&D- 0.437568157
g2<-ggplot(stats, aes(factor(sample, levels=c("Unst","aCDE19")), percent, fill=pop))+
  geom_bar(stat="identity", color="black")+xlab("Samples")

ggpubr::ggarrange(as.ggplot(g), g2, ncol=1)

Code:

library(tidyverse)
library(flowWorkspace)
library(Biobase)
library(flowGate)
source("functionsCyto.R")

fs<-read.ncdfFlowSet(files=list.files("BcellPhenotype-Files/",".LMD", full.names = T), readonly = F, dataset=2)
gs <- GatingSet(fs)

comp<-spillover(fs[[1]])$`$SPILLOVER`
colnames(comp)<-colnames(gs)[5:14]
rownames(comp)<-colnames(gs)[5:14]
gs<-compensate(gs, comp)

trans_params<-transform_gs(gs)

markers<-colnames(gs)
markers[c(7,13)]<-c("CD19","L&D")
names(markers)<-colnames(gs)
markernames(gs)<-markers

sampleNames(gs)<-gsub("\\s[0-9]*.LMD","",sampleNames(gs))
sampleNames(gs)<-gsub(".*\\s","",sampleNames(gs))
pData(gs)$name<-rownames(pData(gs))

gs_gate_interactive(gs,
                    filterId = "Leukocytes",
                    dims = list("FS-A", "SS-A"))

gs_gate_interactive(gs,
                    subset = "Leukocytes",
                    filterId = "CD19 L&D",
                    dims = list("CD19", "L&D"))
                    
g<-ggcyto(gs, subset = "Leukocytes", bins=128, aes(CD19,`L&D`))+
  facet_grid(.~factor(name, levels=c("Unst","aCDE19")))+
  geom_hex(bins=128)+
  geom_gate()+
  geom_stats()+
  scale_fill_gradient(low="black", high="violet")
  
stats<-gs_pop_get_stats(gs, nodes=gs_get_pop_paths(gs, path = "auto")[3:6], type="perc")
stats

g2<-ggplot(stats, aes(factor(sample, levels=c("Unst","aCDE19")), percent, fill=pop))+
  geom_bar(stat="identity", color="black")+xlab("Samples")

ggpubr::ggarrange(as.ggplot(g), g2, ncol=1)