RGLab / flowWorkspace

flowWorkspace
GNU Affero General Public License v3.0
44 stars 21 forks source link

Apply Existing GatingHierarchy/GatingSet Gates to New GatingSet > Error in get_cytoset_from_node(obj@pointer, y) : trying to get Indices for unGated node! #377

Closed Biomiha closed 2 years ago

Biomiha commented 2 years ago

Hi,

Apologies for reopening a closed issue. My issue is essentially exactly the same as #293, which seemed to be resolved, however I seem to be getting a strange error. I'd like to be able to apply existing gates to a new GatingSet. The reasons for wanting to do this are fairly obscure but by way of a reprex I've used the CytoExploreRdata that comes with the CytoExploreR package and have turned it into a FlowJo archive (available here) for development purposes.

library(tidyverse)
library(cytoverse) 
library(CytoExploreR)

source("https://raw.githubusercontent.com/Biomiha/useful_scripts/master/custom_ggplot_theme.R")

Parse workspace:

ws <- open_flowjo_xml(file = "~/Downloads/CytoExploreR_data/CytoExploreR_data.1.wsp")
ws_output <- capture.output(ws) # an extremely hacky way of capturing the relevant details of the workspace file including the file location

# Extract the parent directory: 
Parent_dir <- ws_output %>% 
  str_detect(pattern = "File location") %>% 
  ws_output[.] %>% 
  str_remove(pattern = "File location:  ") %>% 
  dirname()

# List fcs files in the folder. 
rawfiles <- list.files(Parent_dir, ".fcs$", full.names = TRUE)
# Read in relevant fcs files. Remove "Activation_33"
cs <- load_cytoset_from_fcs(rawfiles %>% grep(pattern = "Activation", value = TRUE) %>% grep(pattern = "Activation_33", invert = TRUE, value = TRUE) )

gs <- flowjo_to_gatingset(ws = ws, name = 2, cytoset = cs)
gs_get_pop_paths(gs)

[1] "root" "/Lymphocytes"
[3] "/Lymphocytes/Single Cells" "/Lymphocytes/Single Cells/Q1: CD4- , CD8+" [5] "/Lymphocytes/Single Cells/Q2: CD4+ , CD8+" "/Lymphocytes/Single Cells/Q3: CD4+ , CD8-" [7] "/Lymphocytes/Single Cells/Q4: CD4- , CD8-"

Plot

gs[1:4] %>% 
  ggcyto(aes(x = `CD4`, `CD8`), subset = "Lymphocytes") +
  geom_hex(bins = 128) +
  geom_gate("Q1: CD4- , CD8+") +
  geom_stats() +
  axis_x_inverse_trans() +
  axis_y_inverse_trans()

image

Now, if I clone both the GatingSet as well as the gates and try to apply the same gates to the cloned gatingset I get an error.

Clone gates:

get_all_gates <- function(gh){
  paths <- gh_get_pop_paths(gh)
  paths <- paths[paths != "root"]
  all_gates <- lapply(paths, function(pop_path){
    list("parent"=gh_pop_get_parent(gh, pop_path), "gate"=gh_pop_get_gate(gh, pop_path))
  })
  return(all_gates)
}

copied_gates <- get_all_gates(gs[[1]])

add_all_gates <- function(gs, gate_list){
  for(gate in gate_list){
    gs_pop_add(gs, gate$gate, parent=gate$parent)
  }
}

Clone gs:

cloned_gs <- gs_clone(x = gs)

gs_pop_remove(gs = cloned_gs, node = "Lymphocytes")
> gs_get_pop_paths(cloned_gs)

"root"

add_all_gates(gs = cloned_gs, gate_list = copied_gates)
gs_get_pop_paths(cloned_gs)

[1] "root" "/Lymphocytes"
[3] "/Lymphocytes/Single Cells" "/Lymphocytes/Single Cells/Q1: CD4- , CD8+" [5] "/Lymphocytes/Single Cells/Q2: CD4+ , CD8+" "/Lymphocytes/Single Cells/Q3: CD4+ , CD8-" [7] "/Lymphocytes/Single Cells/Q4: CD4- , CD8-"

Plot cloned:

cloned_gs[1:4] %>% 
  ggcyto(aes(x = `CD4`, `CD8`), subset = "Lymphocytes") +
  geom_hex(bins = 128) +
  geom_gate("Q1: CD4- , CD8+") +
  geom_stats() +
  axis_x_inverse_trans() +
  axis_y_inverse_trans()

Error in get_cytoset_from_node(obj@pointer, y) : 
  trying to get Indices for unGated node!

Not sure why I am not able to apply the same gates again to the cloned object? Obviously in my real world use case I would like to apply existing gates to a new gatingset but the issue is the same. Is this because cytoframes and cytosets exhibit different behaviours to flowframes and flowsets?

mikejiang commented 2 years ago

You need to recompute(cloned_gs) after gates are added

Biomiha commented 2 years ago

Thanks for the suggestion @mikejiang. It works with the reprex but when I apply to my actual dataset I get an error:

Error in cpp_gating(x@pointer, y, alwaysLoadData, verbose, leaf.bool) : 
  Reference node:  /Lymphocytes/Single Cells/Q4: CD4- , CD8- not found!
Biomiha commented 2 years ago

The error was actually due to the renaming of a parent gate of a boolean gate. Closing the issue. Thanks for the help @mikejiang.