DillonHammill / CytoExploreR

Interactive Cytometry Data Analysis
60 stars 13 forks source link

Fail to save gated data #188

Open avdvloet opened 1 year ago

avdvloet commented 1 year ago

I have a problem with gating a subset within a previous gated subset, also obtained via gating. Below is the code I already have. For all samples, I want to first obtain the nuclei, denoted each time by the name nuclei_{n}. Within the nuclei of each sample, I now want to obtain the 'filtered nuclei', denoted by the alias f1_nuclei_{i}. Everything seems to be running, and I can pull the second gate nicely inside on what appear to be my nuclei, within my roots (in other words, the gating hierarchy works and the second gate is effectively taken from the nuclei and not from the roots). However, does not store the data correctly in the gatingTemplate. Do you have an answer to this? thanks in advance!

My code:

library(CytoExploreR)
library(shiny)
library(glue)
library(ggplot2)
library(stringr)

## 0.1 data collection 
################################################################################
trial_n <- 19
data.dir <- glue('~/Nextcloud/PhD/Flow Cytometry/script dev/FCM data')
#data.dir <- '~/Nextcloud/PhD/Flow Cytometry/Duckweed check/FCM data'
setwd(data.dir)

x <- cyto_setup(path=data.dir, emptyValue=FALSE, gatingTemplate = 'nuclei-gatingTemplate.csv')
cs <- cyto_transform(x, channels=c('YL2-A', 'YL1-A', 'SSC-A'), type='log')
sample_names <- sampleNames(cs)
# rename sample_names
for (i in 1:length(sample_names)){
  x <- strsplit(sample_names[i], '_')[[1]][3]
  sample_names[i] <- str_sub(x, end=-5)
}

## 1. manual gating of the nuclei on all the samples
################################################################################

# gate for nuclei
for(n in 1:length(cs)){
  cyto_gate_draw(cs[n],
                 parent = 'root',
                 channels = c('YL2-A', 'SSC-A'),
                 alias = glue('nuclei_{n}'),
                 type = 'rectangle',
                 contour_lines = 15)
}

# extra filtering step within nuclei data
for(i in 1:length(cs)){
  print(i)
  gs <- cyto_extract(
    cs[i],
    parent = glue('nuclei_{i}'),
    channels = c('YL2-A', 'YL1-A'),
    copy = TRUE)
  cyto_gate_draw(gs,
                 parent = 'root',
                 channels = c('YL2-A', 'YL1-A'),
                 alias = glue('f1_nuclei_{i}'),
                 type = 'polygon',
                 contour_lines = 15)
}
DillonHammill commented 1 year ago

@avdvloet in order to save the gates to the gatingTemplate correctly, you should avoid subsetting the samples in cyto_gate_draw(). Also, the GatingSet is designed to stored the same set of gates for all samples (i.e. the same gating strategy), the locations of these gates may differ per sample but all samples must contain the same set of gates.

cyto_gate_draw() supports gating samples separately through the group_by argument - in your case setting group_by = "name" will allow you set a different gate for each sample. Alternatively, group_by can merge data into groups for gating based on experiment variables stored in cyto_details().

P.S. cyto_extract() will return a cytoset object not a GatingSet object. Passing a cytoset to cyto_gate_draw() will allow you to draw gates (and return them) but they cannot be attached to cytoset objects.