RGLab / openCyto

A package that provides data analysis pipeline for flow cytometry.
GNU Affero General Public License v3.0
75 stars 29 forks source link

Returning gateobj saved to gatingTemplate gating_args #180

Closed DillonHammill closed 5 years ago

DillonHammill commented 6 years ago

Hi @mikejiang,

The ability to save gates in the gating_args of the gatingTemplate using the add_pop API is working well as you showed in this Rpubs workflow: http://rpubs.com/wjiang2/395270

gateobj <- DrawGate(fr, channels = c("FSC-H", "SSC-H"), gate_type = "ellipse")

row <- add_pop(gs, alias = "L"
              , dims = "FSC-H,SSC-H"
              , parent = "cd15+"
              , gating_method = "gate_manual"
              , gating_args = list(gate = gateobj)
              )

write.csv(row, "gatingTemplate.csv")

gt <- read.csv("gatingTemplate.csv", header = TRUE)

# How to extract gateobj from gt?

I was just wondering whether if you were given a gatingTemplate with a gate saved in this manner is there an easy way to extract/return the original gateobj as an R object?

Thanks, Dillon

mikejiang commented 6 years ago

It is a little hacky but works

gt <- gatingTemplate(csvfile)
> #list gating tree stored in gt
> getNodes(gt, only.names = T)
    root   /cd15+ /cd15+/L 
  "root"  "cd15+"      "L" 
> #extract the gating method from a specific pair of parent and child node
> gm <- getGate(gt, "/cd15+", "/cd15+/L")
> class(gm)
[1] "gtMethod"
attr(,"package")
[1] "openCyto"
> #extract the 'gate'  you just supplied through 'gating_args' in 'add_pop' call
> thiscall <- parameters(gm)$gate #now it is deparsed as an expression
> eval(thiscall)#convert it to the gate objects you need
A list of 1 filters applied to a flowFrame.
DillonHammill commented 6 years ago

Thanks Mike, just what I needed!

DillonHammill commented 6 years ago

Hi @mikejiang,

Just a follow question about passing gate objects as gating_args. In the case where you may want to gate samples separately would it be possible to use groupBy and collapseDataForGating arguments to achieve this?

See example below: If we have a flowSet containing two samples which we want to gate separately for a particular population. We get the gates and pass them to gatingTemplate gating_args as below:

gate1 <- DrawGate(fs[[1]], channels = c("FSC-A", "SSC-A")) # gate1 is a filters object
gate2 <- DrawGate(fs[[2]], channels = c("FSC-A", "SSC-A")) # gate2 is a filters object
gates <- filters(list(gate1[[1]], gate2[[1]]))

gs <- GatingSet(fs)

row <- add_pop(gs, alias = "L"
              , dims = "FSC-A,SSC-A"
              , parent = "cd15+"
              , gating_method = "gate_manual"
              , gating_args = list(gate = gates)
              )

# Here is the definition of gate_manual which passes on the gates
gate_manual <- function(fr, pp_res, channels, gate){
  return(gate)
}
registerPlugins(gate_manual, "gate_manual")

This will throw an error as only a single gate is expected. Does not seem as if using groupBy and CollapseDataForGating arguments can help in this case. Would I be able to this with a preprocessing function and if so what would it need to do?

mikejiang commented 5 years ago

filters is the container to hold multiple gates for the same single sample. filtersList should be the right container for holding gates for multiple samples.

DillonHammill commented 5 years ago

Thanks Mike, I have figured it out!