ImmuneDynamics / Spectre

A computational toolkit in R for the integration, exploration, and analysis of high-dimensional single-cell cytometry and imaging data.
https://immunedynamics.github.io/spectre/
MIT License
56 stars 21 forks source link

How to use a different cofactor for every channel in Spectre workflow? #147

Closed denvercal1234GitHub closed 1 year ago

denvercal1234GitHub commented 1 year ago

Hi there,

Thanks for the package and detailed tutorials.

I used flowVS to decide cofactor for each channel and exported the transformed data as FCS files. I attempted doing so as shown in https://github.com/HdBraanker/Spectral_Flow_Workflow/issues/3.

Question 1. Does the flowVS::transFlowVS() function perform the same transformation as the Spectre::do.asinh()?

Question 2. How may I use a different cofactor for each channel in the Spectre::doasinh() ?

Thanks again.

Related Discussion #95

ghar1821 commented 1 year ago

Hi @denvercal1234GitHub

  1. I've never used transFlowVS before, but reading its vignette, the mathematics behind both functions look be the same, i.e. asinh(x/cofactor) where x is the expression of a marker of a cell.
  2. By running do.asinh several times, each specifying which marker and co-factor you want to transform. Something like the following where I want co-factor 5 for NK11, CD3, and CD45, but co-factor 11 for the rest. Note, the append.cf is not essential. I only specified it as true so it is easier to later refer to what co-factor I used for what markers.
library(Spectre)

x <- demo.start

# Assign some cell id so we can uniquely identify the cells for merging data.table later
x$CellID <- paste0("Cell_", seq_len(nrow(x)))

# Say cofactor 5 for NK11, CD3, CD45, and 11 for the rest
marker_cofactor_5 <- c("NK11", "CD3", "CD45")
marker_cofactor_11 <- c("Ly6G", "CD11b", "B220", "CD8a", "Ly6C", "CD4")

# Do the transformation
x_asinh_cofactor_5 <- do.asinh(x, use.cols=marker_cofactor_5, cofactor=5, append.cf=TRUE)
x_asinh_cofactor_11<- do.asinh(x, use.cols=marker_cofactor_11, cofactor=11, append.cf=TRUE)

# Recreate the data.table so we have raw data and the transformed data

# Subset the transformed data so it contains just the transformed columns and cell ID
x_asinh_cofactor_5 <- x_asinh_cofactor_5[, c("CellID", paste0(marker_cofactor_5, "_asinh_cf5")), with=FALSE]
x_asinh_cofactor_11 <- x_asinh_cofactor_11[, c("CellID", paste0(marker_cofactor_11, "_asinh_cf11")), with=FALSE]

# Combine them with the original data so we have the transformed and raw data together in one data.table
x_asinh <- merge.data.table(x, 
                            merge.data.table(
                                x_asinh_cofactor_11, 
                                x_asinh_cofactor_5, by='CellID'),
                            by='CellID')

head(x_asinh)
tomashhurst commented 1 year ago

Hi @denvercal1234GitHub ,

Re: Question 2:

You could also just run multiple lines. For example, if you wanted to use 500 with CD3 and CD4, and 1000 with CD11b and CD45...

cell.dat <- do.asinh(cell.dat, use.cols = c('CD3', 'CD4'), cofactor = 500)
cell.dat <- do.asinh(cell.dat, use.cols = c('CD11b', 'CD45'), cofactor = 1000)

If you then look at cell.dat, the last four columns would be CD3_asinh, CD4_asinh, CD11b_asinh, and CD45_asinh.