SGDDNB / ShinyCell

Shiny Interactive Web Apps for Single-Cell Data
GNU General Public License v3.0
148 stars 57 forks source link

Scale gene expression #41

Closed Dorithockman closed 2 years ago

Dorithockman commented 2 years ago

When using the "scale gene expression" option on the bubble plots tab, how is the data being scaled exactly?

jfouyang commented 2 years ago

The bubble plots are handled by the scBubbHeat in server.R.

The code where the gene expression is aggregated and then scaled is reproduced below.

Briefly, ggData is a data.table with columns grpBy, geneName and val, representing the label where cells are grouped (i.e. cluster), the gene name and the gene expression value respectively.

The gene expression value is first exponentiated and then aggregated by averaging across each group and gene. The aggregated value is then log-transformed again.

The scaling is then performed gene-wise using the scale function.

  # Aggregate 
  ggData$val = expm1(ggData$val) 
  ggData = ggData[, .(val = mean(val), prop = sum(val>0) / length(sampleID)), 
                  by = c("geneName", "grpBy")] 
  ggData$val = log1p(ggData$val) 

  # Scale if required 
  colRange = range(ggData$val) 
  if(inpScl){ 
    ggData[, val:= scale(val), keyby = "geneName"] 
    colRange = c(-max(abs(range(ggData$val))), max(abs(range(ggData$val)))) 
  } 
jfouyang commented 2 years ago

Closing issue due to inactivity