samuel-marsh / scCustomize

R package with collection of functions created and/or curated to aid in the visualization and analysis of single-cell data using R.
https://samuel-marsh.github.io/scCustomize/
GNU General Public License v3.0
216 stars 24 forks source link

Use of DiscretePalette_scCustomize() with DimPlotPlus? #173

Closed charlotteharken closed 7 months ago

charlotteharken commented 7 months ago

Is it, or will it be possible to use the scCustomize palettes with DimPlotPlus? I really wanted to combine the "stepped" palette from scCustomize and the black bordered circles from DimPlotPlus, but I can´t get it to work?

Would be so useful to many I believe :-)

Best regards,

Charlotte

samuel-marsh commented 7 months ago

Hi Charlotte,

So DiscretePalette_scCustomize() should be able to work with any plotting function as it just returns vectors of colors or hex codes. Can you explain better what is not working and full copy of the code you are running?

Thanks! Sam

charlotteharken commented 7 months ago

seeing the palette works: PalettePlot(pal = DiscretePalette_scCustomize(num_colors = 24, palette = "stepped"))

Saving as variable to global environment works: stepped_pal <- DiscretePalette_scCustomize(num_colors = 24, palette = "stepped")

Trying then to change colors to "stepped" does not work - it just returns standard DimPlot colors: DimPlotPlus(dat.combined, reduction = "umap", shape.choice = 21, pt.size = 1.75, st.size = 0.25, raster = FALSE, cols = stepped_pal)

Trying to change colors in ordinary DimPlot works nicely though: DimPlot(dat.combined, reduction = "umap", raster = FALSE, cols = stepped_pal)

I also unsuccesfully tried the following for DimPlotPlus: 1) DimPlotPlus_scCustom(dat.combined, reduction = "umap", shape.choice = 21, pt.size = 1.75, st.size = 0.25, raster = FALSE, cols = stepped_pal) results in: Error in DimPlotPlus_scCustom(dat.combined, reduction = "umap", shape.choice = 21, : could not find function "DimPlotPlus_scCustom" 2) DimPlotPlus(dat.combined, reduction = "umap", shape.choice = 21, pt.size = 1.75, st.size = 0.25, raster = FALSE, colors_use = DiscretePalette_scCustomize(num_colors = 24, palette = "stepped")) results in; Error in DimPlot(object, reduction = reduction, ...) : unused argument (colors_use = c("#990F26FF", "#B33E52FF", "#CC7A88FF", "#E6B8BFFF", "#99600FFF", "#B3823EFF", "#CCAA7AFF", "#E6D2B8FF", "#54990FFF", "#78B33EFF", "#A3CC7AFF", "#CFE6B8FF", "#0F8299FF", "#3E9FB3FF", "#7ABECCFF", "#B8DEE6FF", "#3D0F99FF", "#653EB3FF", "#967ACCFF", "#C7B8E6FF", "#333333FF", "#666666FF", "#999999FF", "#CCCCCCFF"))

Do you have any suggestions? Thank you for your time :-)

Charlotte

samuel-marsh commented 7 months ago

Hi Charlotte,

Thanks for info. From those error logs I believe the issue is with the input code and not DiscretePalette_scCustomize

The first error appears to be because you have the function name as DimPlotPlus_scCustom instead of DimPlotPlus. The second error appears to be because you specify the parameter name colors_use which is from scCustomize and not the cols parameter of Seurat.

Either of these should work:

# specify function inside of plotting function
DimPlotPlus(pbmc, cols = DiscretePalette_scCustomize(num_colors = 24, palette = "stepped"))

# specify palette as environment variable then provide to plotting
stepped_pal <- DiscretePalette_scCustomize(num_colors = 24, palette = "stepped")

DimPlotPlus(pbmc, cols = stepped_pal)

Best, Sam

charlotteharken commented 7 months ago

Thank you for your fast respons! Unfortunately it doesn´t seem to work for me. Both options return a plot with only black objects and the error: "Rasterizing points since number of points exceeds 100,000. To disable this behavior set raster=FALSE". I then included this (as I originally did), and for both options it resulted in the original DimPlot colors. Still, if I use the basic DimPlot function (and raster=FALSE), the "stepped" color palette works :-).

Kind regards, Charlotte

samuel-marsh commented 7 months ago

Can you paste the output of sessionInfo() here?

charlotteharken commented 7 months ago

R version 4.3.3 (2024-02-29) Platform: x86_64-apple-darwin20 (64-bit) Running under: macOS Sonoma 14.4.1

Matrix products: default BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib LAPACK: /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/lib/libRlapack.dylib; LAPACK version 3.11.0

locale: [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

time zone: Europe/Copenhagen tzcode source: internal

attached base packages: [1] stats graphics grDevices utils datasets methods
[7] base

other attached packages: [1] scCustomize_2.1.2 magrittr_2.0.3 tidyr_1.3.1
[4] ggplot2_3.4.4 pochi_0.1.0 cowplot_1.1.3
[7] patchwork_1.2.0 umap_0.2.10.0 dplyr_1.1.4
[10] mclust_6.1 Seurat_5.0.2 SeuratObject_5.0.1 [13] sp_2.1-3

loaded via a namespace (and not attached): [1] RColorBrewer_1.1-3 shape_1.4.6.1
[3] rstudioapi_0.15.0 jsonlite_1.8.8
[5] ggbeeswarm_0.7.2 spatstat.utils_3.0-4
[7] farver_2.1.1 GlobalOptions_0.1.2
[9] fs_1.6.3 vctrs_0.6.5
[11] ROCR_1.0-11 memoise_2.0.1
[13] spatstat.explore_3.2-6 paletteer_1.6.0
[15] askpass_1.2.0 janitor_2.2.0
[17] forcats_1.0.0 usethis_2.2.2
[19] htmltools_0.5.7 curl_5.2.0
[21] sctransform_0.4.1 parallelly_1.36.0
[23] KernSmooth_2.23-22 htmlwidgets_1.6.4
[25] ica_1.0-3 plyr_1.8.9
[27] lubridate_1.9.3 plotly_4.10.4
[29] zoo_1.8-12 cachem_1.0.8
[31] igraph_2.0.1.1 mime_0.12
[33] lifecycle_1.0.4 pkgconfig_2.0.3
[35] Matrix_1.6-5 R6_2.5.1
[37] fastmap_1.1.1 snakecase_0.11.1
[39] fitdistrplus_1.1-11 future_1.33.1
[41] shiny_1.8.0 digest_0.6.34
[43] colorspace_2.1-0 rematch2_2.1.2
[45] tensor_1.5 prismatic_1.1.1
[47] RSpectra_0.16-1 irlba_2.3.5.1
[49] pkgload_1.3.4 labeling_0.4.3
[51] progressr_0.14.0 timechange_0.3.0
[53] fansi_1.0.6 spatstat.sparse_3.0-3 [55] httr_1.4.7 polyclip_1.10-6
[57] abind_1.4-5 compiler_4.3.3
[59] remotes_2.4.2.1 withr_3.0.0
[61] fastDummies_1.7.3 pkgbuild_1.4.3
[63] MASS_7.3-60.0.1 openssl_2.1.1
[65] sessioninfo_1.2.2 tools_4.3.3
[67] vipor_0.4.7 lmtest_0.9-40
[69] beeswarm_0.4.0 httpuv_1.6.14
[71] future.apply_1.11.1 goftest_1.2-3
[73] glue_1.7.0 nlme_3.1-164
[75] promises_1.2.1 grid_4.3.3
[77] Rtsne_0.17 cluster_2.1.6
[79] reshape2_1.4.4 generics_0.1.3
[81] gtable_0.3.4 spatstat.data_3.0-4
[83] data.table_1.15.2 utf8_1.2.4
[85] spatstat.geom_3.2-8 RcppAnnoy_0.0.22
[87] ggrepel_0.9.5 RANN_2.6.1
[89] pillar_1.9.0 stringr_1.5.1
[91] ggprism_1.0.5 spam_2.10-0
[93] RcppHNSW_0.6.0 later_1.3.2
[95] circlize_0.4.16 splines_4.3.3
[97] lattice_0.22-5 survival_3.5-8
[99] deldir_2.0-2 tidyselect_1.2.0
[101] miniUI_0.1.1.1 pbapply_1.7-2
[103] gridExtra_2.3 scattermore_1.2
[105] devtools_2.4.5 matrixStats_1.2.0
[107] stringi_1.8.3 lazyeval_0.2.2
[109] codetools_0.2-19 tibble_3.2.1
[111] cli_3.6.2 uwot_0.1.16
[113] xtable_1.8-4 reticulate_1.35.0
[115] munsell_0.5.0 Rcpp_1.0.12
[117] globals_0.16.2 spatstat.random_3.2-2 [119] png_0.1-8 ggrastr_1.0.2
[121] parallel_4.3.3 ellipsis_0.3.2
[123] dotCall64_1.1-1 profvis_0.3.8
[125] urlchecker_1.0.1 listenv_0.9.1
[127] viridisLite_0.4.2 scales_1.3.0
[129] ggridges_0.5.6 leiden_0.4.3.1
[131] purrr_1.0.2 rlang_1.1.3

samuel-marsh commented 7 months ago

Can you try the following:

Start new R session.

library(Seurat)
library(scCustomize)

DimPlotPlus <- function(object,
                        reduction = "umap",
                        shape.choice = 21,
                        pt.size = 1.75,
                        st.size = 0.25,
                        ...){
  temp_dimplot <- DimPlot(object, reduction = reduction, ...)
  if(shape.choice == 21){
    temp_dimplot$layers[[1]]$mapping$fill <- temp_dimplot$layers[[1]]$mapping$colour
    temp_dimplot$layers[[1]]$mapping$colour <- NULL
    temp_dimplot$layers[[1]]$aes_params$shape <- 21
    temp_dimplot$layers[[1]]$aes_params$colour <- "black"
    temp_dimplot$layers[[1]]$aes_params$size <- pt.size
    temp_dimplot$layers[[1]]$aes_params$stroke <- st.size
  }
  temp_dimplot
}

pbmc_small <- UpdateSeuratObject(pbmc_small)

DimPlotPlus(pbmc_small, cols = DiscretePalette_scCustomize(num_colors = 20, palette = "stepped"), reduction = "tsne")
charlotteharken commented 7 months ago

This is the result?

test_CHJ

samuel-marsh commented 7 months ago

Ok so that works. What happens if you run your code again but set raster=FALSE?

charlotteharken commented 7 months ago

While addition of a line to the points worked fine for the pbmc´s above, the color was still the original I believe, not the stepped palette? Unfortunately, running my code also produced a plot that still presented with the original colors. I will have to live with the colors I guess :-)

samuel-marsh commented 7 months ago

Hi Charlotte,

You are right, sorry I didn’t look closely enough at resulting plot.

So after looking more closely at the DimPlotPlus function that is actually the source of the error. It is basically hacking the created ggplot object from Seurat::DimPlot and the way it is written it removes any provided color scale. That’s also why it doesn’t work when plot is rastered because it’s not using geom_point anymore.

You can see this if you run the pbmc_small example but provide cols = c(“black”, “red”, “blue”).

I will take a look at Seurat code and see if there is possibility of PR to add stroke parameter to default code.

But unfortunately as written DimPlotPlus will only ever use default palette.

Best, Sam

charlotteharken commented 7 months ago

Hi Sam,

Thank you for trying to help me with this - much appreciated! Would be so great if you find a solution for adding a border to the points :-)!

Kind regards,

Charlotte

samuel-marsh commented 7 months ago

Hi Charlotte,

~So in looking at things I don't think it's going to be something I can do with the current Seurat code base. The best way I can think to solve the issue would be for you build your own version of the plot. It is easy enough to pull necessary data using FetchData and then you can see the code for SingleDimPlot in Seurat code base. Depending on what functionality you are looking for will depend how much of that code you need to adapt or whether you just need to find the necessary ggplot sections to have your resulting plot mimic Seurat's.~

Best, Sam

samuel-marsh commented 7 months ago

Actually strike that.

I wasn't thinking clearly in any of this. All you need to do is:

DimPlotPlus(pbmc_small, reduction = "tsne", pt.size = 5) + scale_fill_manual(values = stepped)

Best, Sam

charlotteharken commented 7 months ago

Amazing! It works with both the PBMCs and my own data! Thank you so much!!! Have a wonderful day :-)! test2_CHJ

samuel-marsh commented 7 months ago

No problem! Ya I realized I was trying to be hacky like DimPlotPlus when really the solution was just normal ggplot2 theming.

Best, Sam