DillonHammill / CytoExploreR

Interactive Cytometry Data Analysis
60 stars 13 forks source link

Extracting event numbers that are within a gate #185

Closed wnkc closed 1 year ago

wnkc commented 1 year ago

I am familiarizing myself with the CytoExploreR package and would like to know how I can extract the event numbers of the events that are found within a gate. I have been trying to use cyto_extract but this yields a cytoframe dataframe I cannot seem to glean the data out of . Is there a quick way of reaching this goal? Thank you.

This is my code (basically copy pasted from the vignette); basically I am trying to obtain the event numbers that are found within the Single cells gate (NIL).

# Load required packages
library(CytoExploreR)
library(CytoExploreRData)

# set working directory
directory = "C:\\Users\\wnauwync\\Desktop\\Doctoraat\\Papers\\double_emulsion_methodology\\data\\20230613_1534_DEvortex4pL3\\demo"
setwd(directory)

# Compensation FCS Files
cyto_save(Compensation, 
          save_as = "Compensation-Samples")

# Activation FCS Files
cyto_save(Activation,
          save_as = "Activation-Samples")

# Setup compensation controls
gs <- cyto_setup("Compensation-Samples",
                 gatingTemplate = "Compensation-gatingTemplate.csv")

# Transform fluorescent channels - default logicle transformations
gs <- cyto_transform(gs)

# Gate Cells
cyto_gate_draw(gs,
               parent = "root",
               alias = "Cells",
               channels = c("FSC-A", "SSC-A"))

# Gate Single Cells
cyto_gate_draw(gs,
               parent = "Cells",
               alias = "Single Cells",
               channels = c("FSC-A", "FSC-H"))

# Visualise uncompensated data
cyto_plot_compensation(gs,
                       parent = "Single Cells")

# Visualise compensated data
cyto_plot_compensation(gs,
                       parent = "Single Cells",
                       spillover = "Spillover-Matrix.csv",
                       compensate = TRUE)

# Load and annotate samples
gs <- cyto_setup("Activation-Samples",
                 gatingTemplate = "Activation-gatingTemplate.csv")

# Apply compensation
gs <- cyto_compensate(gs,
                      spillover = "Spillover-Matrix.csv")

# Transform fluorescent channels - default logicle transformations
gs <- cyto_transform(gs)

# Gate Cells
cyto_gate_draw(gs,
               parent = "root",
               alias = "Cells",
               channels = c("FSC-A","SSC-A"))

# Gate Single Cells
cyto_gate_draw(gs,
               parent = "Cells",
               alias = "Single Cells",
               channels = c("FSC-A","FSC-H"))

# Extract unstained control 
NIL <- cyto_extract(gs, "Single Cells")[[33]]
DillonHammill commented 1 year ago

Take a look at cyto_stats_compute().

wnkc commented 1 year ago

Hi @DillonHammill, thanks for the prompt response. My last message was not clearly phrased. I am trying to extract the ID's of the events that are within a gate (event number), not the number of events. I have an imaging flow cytometric dataset and am attempting to correlate the ID of each event with the photo ID. Is there a way of doing this in CytoExploreR?

DillonHammill commented 1 year ago

See flowWorkspace's gh_pop_get_indices().

wnkc commented 1 year ago

Thanks, this worked!

wnkc commented 1 year ago

I am now dealing with a reverse problem: I have an array of events I want to give a different color in cyto_plot. Is there a way of doing this? I cannot seem to find anything in the R documentation of cyto_plot that does this.

DillonHammill commented 1 year ago

@wnkc just pass the names of the nodes through the overlay argument in cyto_plot().

wnkc commented 1 year ago

The problem is, my population is not obtained through gating, it is a vector of events IDs that I confirmed through Image analysis to have the right morphology. I now want to see where these specific events lie in the FCM plots. Imagine the FCM data of a single experiment is a 2D dataframe,, where rows are the event ID and columns are the flow cytometric parameters that have been measured during the experiment. I want to subset this dataframe based on event IDs that I have in a different vector. Is there a quick way of doing this in CytoExploreR?

DillonHammill commented 1 year ago

Import the data into a GatingSet and then convert event IDs into a filter that can be added to the GatingSet as a set of gates. If all your data is imported in a single FCS file:

# gs is the GatingSet with one sample
# ids is the column of ids from your data.frame
# convert ids to factor then a filter
ids <- as(factor(ids), "filterResult"))
# add the ids as a gate to GatingSet
gs_pop_add(gs, gate = ids, parent = "root")
# check gates have been added
cyto_nodes(gs)

If you have multiple samples in your GatingSet, you need to convert your ids into a list named with each sample in the GatingSet before giving it to gs_pop_add().

Finally, you can now pass the names of the populations to overlay in cyto_plot() for visualisation.

wnkc commented 1 year ago

Hi @DillonHammill, I got it to work by making a logical vector indicating the indices that are "positive" events in my dataset as true. This, for each sample. And then indeed like you said adding it all in a list named with each sample. However it seems like cyto_plot_explore did not know how to handle this when adding it as an overlay or overlay_list argument (after adding it to the gatingset through gs_pop_add) so I plotted with a for loop exploring the parameters of interest.