DillonHammill / CytoExploreR

Interactive Cytometry Data Analysis
60 stars 13 forks source link

Gate samples on log scale but plot them on linear scale #172

Closed avdvloet closed 1 year ago

avdvloet commented 1 year ago

I want to gate certain populations of cells on log scale, but I want to plot these subpopulations in histograms on linear scale. The problem is that if I log transform the appropriate channels, there is no way to revert them back to linear scale for plotting purposes. I am also unable to copy or create new channels within my GatingSet where I would save copies of the channels on another scale and use them for different purposes. Below my code;

Loading of the data and creating of the gatingset cs, where the channels 'YL2-A' and 'SSC-A' are log transformed for gating purposes:

## 0.1 data collection 
################################################################################
data.dir <- '~/Nextcloud/PhD/Flow Cytometry/Trial 5/FCM data'
setwd(data.dir)
sample_names <- list.files(data.dir)

x <- cyto_setup(path=data.dir, emptyValue=FALSE,
                gatingTemplate = 'nuclei-gatingTemplate.csv')
cs <- cyto_transform(x, channels=c('YL2-A', 'SSC-A'), type='log')

Next, I manually gate all samples in this gating set on log scale;

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

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)
}

Finally, I want to plot the subpopulations (nuclei) on LINEAR scale instead of log scale. This is the code I have so far, but of course this is still on log scale.

## 3. histogram plots of the nuclei
################################################################################
setwd(glue('{data.dir}/plots/hist'))

# separate plots + save
for (i in 1:length(cs)){
  cyto_plot_save(glue('histogram_{i}.png'), height = 7, width = 7, units = 'in', 
                 res = 300)
  print(strsplit(sample_names[i], '_')[[1]][3])
  cyto_plot(cs[i], parent = glue('nuclei_{i}'), channels = 'YL2-A', 
            title = strsplit(sample_names[i], '_')[[1]][3], xlim=c(1500, 50000))
  cyto_plot_complete()
}

Could you please provide some help? Thanks in advance!

DillonHammill commented 1 year ago

@avdvloet, it is rather unusual to require both linear and transformed data for visualisation purposes - I would carefully consider whether you need both.

Anyways, the best way to do this is to pull out the linear data using cyto_extract() and then pass that to cyto_plot():

cs <- cyto_extract(
  gs,
  parent = "Live Cells",
  channels = c("PE-A"),
  copy = TRUE,  # required so original data remains transformed
  inverse.transform = TRUE
)
cyto_plot(
  cs,
  channels = "PE-A"
)
avdvloet commented 1 year ago

@DillonHammill this worked, thanks!

I am comparing haploid with diploid with tetraploid nuclei and I want to see whether the fluorescence signal gets doubled along the ploidy gradient. That is why I wanted to visualize on a linear scale, to really illustrate the linear relationship between N, 2N and 4N.

avdvloet commented 1 year ago

@DillonHammill additional question, is it possible, to plot onto a histogram a vertical bar indicating the mean value? How could one extract this mean value and plot it accordingly?

DillonHammill commented 1 year ago

Just use label_stat = "median" in cyto_plot():

cyto_plot(
   cs, 
   channels = "PE-A", 
   label = TRUE,
   label_stat = "median"
)

The "median" option is more robust for cytometry data but "mean" and "count" are also supported.

avdvloet commented 1 year ago

thanks!