SGDDNB / ShinyCell

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

Gene co-expression threshold #16

Closed jvelghe closed 3 years ago

jvelghe commented 3 years ago

Hi John, I'm currently tweaking some items on the application, and was wondering if it is possible to adjust the colour threshold for the gene co-expression plots? We changed the threshold for some Seurat plots and were hoping to do something similar for this interface as well to make it more biologically representative.

Along with two Seurat co-expression examples, here is the work-in-progress link to the app for your reference: https://lynnlab.shinyapps.io/Human_Islet_Atlas/

INS_GCG_Endo_FeatBlend.pdf INS_SST_Endo_FeatBlend.pdf

If you could point me in the direction of where to modify the server code I would be very appreciative :)

Thank you!

jfouyang commented 3 years ago

Hi, that's a very nice customized version of ShinyCell app! :)

If you look into server.R, this chunk of code controls the colour palette for gene co-expression plot:

  # Generate coex color palette 
  cInp = strsplit(inpcol, "; ")[[1]] 
  if(cInp[1] == "Red (Gene1)"){ 
    c10 = c(255,0,0) 
  } else if(cInp[1] == "Orange (Gene1)"){ 
    c10 = c(255,140,0) 
  } else { 
    c10 = c(0,255,0) 
  } 
  if(cInp[2] == "Green (Gene2)"){ 
    c01 = c(0,255,0) 
  } else { 
    c01 = c(0,0,255) 
  } 
  c00 = c(217,217,217) ; c11 = c10 + c01 
  nGrid = 16; nPad = 2; nTot = nGrid + nPad * 2 
  gg = data.table(v1 = rep(0:nTot,nTot+1), v2 = sort(rep(0:nTot,nTot+1))) 
  gg$vv1 = gg$v1 - nPad ; gg[vv1 < 0]$vv1 = 0; gg[vv1 > nGrid]$vv1 = nGrid 
  gg$vv2 = gg$v2 - nPad ; gg[vv2 < 0]$vv2 = 0; gg[vv2 > nGrid]$vv2 = nGrid 
  gg$cR = bilinear(gg$vv1, gg$vv2, nGrid, c00[1], c10[1], c01[1], c11[1]) 
  gg$cG = bilinear(gg$vv1, gg$vv2, nGrid, c00[2], c10[2], c01[2], c11[2]) 
  gg$cB = bilinear(gg$vv1, gg$vv2, nGrid, c00[3], c10[3], c01[3], c11[3]) 
  gg$cMix = rgb(gg$cR, gg$cG, gg$cB, maxColorValue = 255) 
  gg = gg[, c("v1", "v2", "cMix")] 

To summarise briefly, First, these four variables define the extreme colours c00: colour for zero expression of both gene A and B, which is set to RGB of c(217,217,217) c10: colour for max expression of A, zero expression of B c01: colour for zero expression of A, max expression of B c11: colour for max expression of both gene A and B, which is set to c10 + c01

Second, we then define the number of bins to bin the colours, which is set to 16 bins (nGrid = 16) and we also added some "padding" on both the lower and higher end of the expression nPad = 2. So there will be a total of 20 bins (2+16+2) where the first 2 bins, as well as the third bin, are of the same colour. This basically adds the grey bits to the bottom left side of the colour palette.

Third and finally, the whole colour palette is generated using a bilinear interpolation function.

I think what you want is to make grey part of the bottom left side of the colour palette extend further. This would require you to modify nPad in some way. Maybe you can try the following code and change the nPadLow?:

  nGrid = 16; nPadLow = 2; nPadHigh = 2; nTot = nGrid + nPadLow + nPadHigh
  gg = data.table(v1 = rep(0:nTot,nTot+1), v2 = sort(rep(0:nTot,nTot+1))) 
  gg$vv1 = gg$v1 - nPadLow ; gg[vv1 < 0]$vv1 = 0; gg[vv1 > nGrid]$vv1 = nGrid 
  gg$vv2 = gg$v2 - nPadLow ; gg[vv2 < 0]$vv2 = 0; gg[vv2 > nGrid]$vv2 = nGrid 

And one last important thing! This chunk of code appears twice, once for the main plot, once for the legend so the code needs to be replaced twice.

Hope that my explanation is not too confusing!

jvelghe commented 3 years ago

Hi John,

Thanks!! There's maybe 100,000 cells as well (if you count the duplications that were generated for the application). We are super happy your package was able to handle it!

Do you think it's possible for me to (attempt) to add a toggle to be able to have the user control the colour threshold? I think your fix will work but we have noticed that depending on the gene, some are more visible at certain thresholds than others. I'm trying to figure out a way such that the user can control the colour threshold in some way like with the Seurat plot command. Your solution will work I believe, but I'm not the biologist so I'm trying to give as much autonomy to the specialists as I can :)

I'll start with what you have and go from there, I'm new to the Webapp UI/server double files so please bear with me!

Cheers, Jane

jfouyang commented 3 years ago

Hi Jane,

Glad that the app is running smoothly for your dataset. It should run quite well as we implemented all the plotting functions from scratch instead of relying on other single-cell packages and we also try to keep the memory consumption and access to a minimum for a smoother browsing experience. :)

A few comments:

jfouyang commented 3 years ago

Closing this issue due to inactivity.