library(flowWorkspace)
library(openCyto)

data(GvHD)
fs <- ncdfFlowSet(GvHD)#h5 based flowSet is the recommended data structure for openCyto
gs <- GatingSet(GvHD)
#select subset for demo
gs <- subset(gs, Patient %in% c(5, 6) & Visit %in% c(1,2,3))
#append the stim col for demo purpose
pData(gs)[["stim"]] <- c("neg", "antigen", "antigen", "neg", "antigen", "antigen")
pData(gs)
##       Patient Visit Days Grade  name    stim
## s5a01       5     1   -6     3 s5a01     neg
## s5a02       5     2    0     3 s5a02 antigen
## s5a03       5     3    6     3 s5a03 antigen
## s6a01       6     1   -8     3 s6a01     neg
## s6a02       6     2    0     3 s6a02 antigen
## s6a03       6     3    5     3 s6a03 antigen

convetional gating independently for each sample

add_pop(gs, alias = "A", pop = "+", parent = "root", dims = "FSC-H", gating_method = "mindensity")
library(ggcyto)
autoplot(gs, "A" , y = "SSC-H") + facet_grid(Patient ~ stim)

negative control based gating

# preprocessing function does actual gating on the neg sample
# fs contains the samples that belong to the same group
.ppnegGate <- function(fs, gs, gm, channels, groupBy, isCollapse, ...) {
  #select the target sample
  sn <- rownames(subset(pData(fs), stim == "neg"))
  g <- openCyto::gate_mindensity(fs[[sn]], channels, ...) 
  #replicate that gate across samples for this group and return them as pp results
  sapply(sampleNames(fs), function(i)g)
}
registerPlugins(fun=.ppnegGate, methodName='ppnegGate', type = "preprocessing")
## [1] TRUE
# the customized gating function simply receive the gate from preprocessing through pp_res argument
# and return it as it is
.negGate <- function(fr, pp_res, channels, ...){ 
  g <- pp_res
  
  return(g)
}
registerPlugins(fun=.negGate,methodName='negGate')
## [1] TRUE
add_pop(gs, alias = "B", pop = "+", parent = "root", dims = "FSC-H"
        , gating_method = "negGate"
        , groupBy = "Patient" #this will split data into groups and pass each individual group to preprocessing
        , preprocessing_method = "ppnegGate"
        )

autoplot(gs, "B" , y = "SSC-H") + facet_grid(Patient ~ stim)