satijalab / seurat

R toolkit for single cell genomics
http://www.satijalab.org/seurat
Other
2.24k stars 902 forks source link

CellSelector error for spatial plots: "Cannot find a geom of class GeomPoint" #4089

Closed jeremycfd closed 3 years ago

jeremycfd commented 3 years ago

According to the resolution of #3611, the CellSelector function should work on spatial plots. However, CellSelector currently gives me an error "Cannot find a geom of class GeomPoint" when using a spatial plot:

brain <- LoadData("stxBrain", type = "anterior1")
brain <- SCTransform(brain, assay = "Spatial", verbose = FALSE)
p <- SpatialFeaturePlot(brain, features = c("Hpca"))
CellSelector(plot = p)
Error in GetXYAesthetics(plot = plot) : 
  Cannot find a geom of class GeomPoint

Similarly doesnt work with SpatialDimPlot:

brain <- RunPCA(brain, assay = "SCT", verbose = FALSE)
brain <- FindNeighbors(brain, reduction = "pca", dims = 1:30)
brain <- FindClusters(brain, verbose = FALSE)
brain <- RunUMAP(brain, reduction = "pca", dims = 1:30)

p2 <- SpatialDimPlot(brain, label = TRUE, label.size = 3)
CellSelector(plot = p2, object = brain)
Error in GetXYAesthetics(plot = plot) : 
  Cannot find a geom of class GeomPoint

However, it still works for normal UMAP plots without issue:

p1 <- DimPlot(brain, reduction = "umap", label = TRUE)
CellSelector(plot = p1)
jeremycfd commented 3 years ago
> sessionInfo()
R version 4.0.3 (2020-10-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                           LC_TIME=English_United States.1252    

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

other attached packages:
[1] shiny_1.6.0               stxBrain.SeuratData_0.1.1 SeuratData_0.2.1          RColorBrewer_1.1-2        dplyr_1.0.3              
[6] patchwork_1.1.1           ggplot2_3.3.3             SeuratObject_4.0.0        Seurat_4.0.0             

loaded via a namespace (and not attached):
  [1] Rtsne_0.15           colorspace_2.0-0     deldir_0.2-9         ellipsis_0.3.1       rio_0.5.16           ggridges_0.5.3      
  [7] rprojroot_2.0.2      fs_1.5.0             rstudioapi_0.13      spatstat.data_2.0-0  farver_2.0.3         leiden_0.3.7        
 [13] listenv_0.8.0        remotes_2.2.0        ggrepel_0.9.1        RSpectra_0.16-0      codetools_0.2-18     splines_4.0.3       
 [19] cachem_1.0.1         polyclip_1.10-0      pkgload_1.1.0        jsonlite_1.7.2       ica_1.0-2            cluster_2.1.0       
 [25] png_0.1-7            uwot_0.1.10          sctransform_0.3.2    compiler_4.0.3       httr_1.4.2           assertthat_0.2.1    
 [31] Matrix_1.2-18        fastmap_1.1.0        lazyeval_0.2.2       cli_2.3.0            later_1.1.0.1        htmltools_0.5.1.1   
 [37] prettyunits_1.1.1    tools_4.0.3          igraph_1.2.6         gtable_0.3.0         glue_1.4.2           RANN_2.6.1          
 [43] reshape2_1.4.4       rappdirs_0.3.3       spatstat_1.64-1      Rcpp_1.0.6           scattermore_0.7      carData_3.0-4       
 [49] jquerylib_0.1.3      cellranger_1.1.0     vctrs_0.3.6          nlme_3.1-151         lmtest_0.9-38        stringr_1.4.0       
 [55] globals_0.14.0       ps_1.5.0             openxlsx_4.2.3       testthat_3.0.1       mime_0.9             miniUI_0.1.1.1      
 [61] lifecycle_0.2.0      irlba_2.3.3          devtools_2.3.2       goftest_1.2-2        future_1.21.0        MASS_7.3-53         
 [67] zoo_1.8-8            scales_1.1.1         spatstat.utils_2.0-0 hms_1.0.0            promises_1.1.1       parallel_4.0.3      
 [73] curl_4.3             memoise_2.0.0        reticulate_1.18      pbapply_1.4-3        gridExtra_2.3        sass_0.3.1          
 [79] rpart_4.1-15         stringi_1.5.3        desc_1.2.0           pkgbuild_1.2.0       zip_2.1.1            rlang_0.4.10        
 [85] pkgconfig_2.0.3      matrixStats_0.58.0   lattice_0.20-41      tensor_1.5           ROCR_1.0-11          purrr_0.3.4         
 [91] labeling_0.4.2       htmlwidgets_1.5.3    cowplot_1.1.1        processx_3.4.5       tidyselect_1.1.0     parallelly_1.23.0   
 [97] RcppAnnoy_0.0.18     plyr_1.8.6           magrittr_2.0.1       R6_2.5.0             generics_0.1.0       mgcv_1.8-33         
[103] pillar_1.4.7         haven_2.3.1          foreign_0.8-81       withr_2.4.1          fitdistrplus_1.1-3   survival_3.2-7      
[109] abind_1.4-5          tibble_3.0.6         future.apply_1.7.0   crayon_1.4.1         car_3.0-10           KernSmooth_2.23-18  
[115] plotly_4.9.3         usethis_2.0.1        grid_4.0.3           readxl_1.3.1         data.table_1.13.6    callr_3.5.1         
[121] forcats_0.5.1        digest_0.6.27        xtable_1.8-4         tidyr_1.1.2          httpuv_1.5.5         munsell_0.5.0       
[127] viridisLite_0.3.0    bslib_0.2.4          sessioninfo_1.1.1   
mojaveazure commented 3 years ago

Hi Jeremy, the previous answer is wrong. CellSelector only supports ggplot2-based plots using GeomPoint as their layer. Spatial plots in Seurat utilize a custom GeomSpatial to ensure that points on the plot line up with an underlying image and to apply axis transformations to ensure that plots are correct.

These axis transformations are the reason why we haven't implemented CellSelector to work on spatial plots: the coordinates returned by Shiny would not line up with the coordinates present in the Seurat object.

l-magnificence commented 3 years ago

Because of that CellSelector only supports ggplot2-based plots which are different from spatial plots , so how can I manually select a spot or group of spots from a spatial dataset in SpatialDimPlot, like loupe browser do, to do further analysis?

timoast commented 3 years ago

@l-magnificence this is not currently supported. We are working on updates to better support the spatial functionality in Seurat.

daccachejoe commented 2 years ago

Hello-

I was looking into this myself as a subsetting method and I believe I found a workaround. @jeremycfd I'm sure by now you figured out a way but it could help others who have the same question.

The 10X spatial image coordinates are stored in the spatial_object@images[["slice1"]]@coordinates slot. If you feed that into a ggplot and then CellSelector like such CellSelector(spatial_object_list@images[["slice1"]]@coordinates %>% ggplot(aes(x = imagerow, y = imagecol)) + geom_point())

You can interact with the points and extract the barcodes of interest to a vector that you can reference in your Seurat object.

I hope this helps!

cvw462 commented 2 years ago

Hey @jad362, I'm sorry for such a novice question, but how do you access coordinates (command wise) in your seurat object? **

spatial_object@images[["slice1"]]@coordinates

** I understand the coordinates are stored in the tissue_positions_list.csv file.

I'm trying to interact and eventually subset visium spots. Any help is much appreciated Github friends.

NicolaasVanRenne commented 2 years ago

@jad362 yeah this is a step forward, but still cumbersome

I can only select a square at a time instead of cherry-picking the dots. On top of that, the histology image is not underneath to facilitate the cherry picking. Now you just get dizzy of all the dots without the tissue image :(

ivingan01 commented 1 year ago

I am struggling with something similar. I am not nearly experienced enough to come up with some work around. Is there some way to show the coordinates of a given point in a standard interactive plot? Currently an interactive plot can show the cell identity and its cluster (or the expression of a feature). Could you have it show the coordinates of the point you are hovering over? That would be atleast helpful to report the bounds of an ROI.

dkcoxie commented 8 months ago

For Visium data, @weishwu 's workaround for this kind of request is to have areas of interest selected via Loupe Browser (using the lasso or the square selection tool), export the spot ids for selected area(s) (via CSV export), and then read in those data to subset the Seurat object using those ids. If the cluster identify from an initial analysis is required, LoupeR should allow for export of a Seurat object to .cloupe format to be loaded into Loupe Browser.

Not as elegant as CellSelector sounds or some other within R/Seurat solution, but this approach does allow the underlying H&E image to be referenced.

alishamay1305 commented 7 months ago

Hi @dkcoxie, would you be able to link me to @weishwu 's workaround? I would like to try it. Thank you!

weishwu commented 6 months ago

Hi @alishamay1305, as @dkcoxie said, you just need to load the cloupe file (one of the space ranger outputs) into the Loupe Browser and then use the lasso function to select any area you like. After naming that as a new cluster you can export the spot barcodes as a csv file. You can then load that csv file into R add add them to the @metadata slot in your Seurat object, so that you can use them to group/split the spots.

Screenshot 2024-03-07 at 3 21 09 PM
wykpenguin commented 3 weeks ago

When I run this after changing spatial_object_list to my own object name (xenium.obj),

spatial_object_list@images[["slice1"]]@coordinates %>% ggplot(aes(x = imagerow, y = imagecol)) + geom_point())

I get the following error"

Error in xenium.obj@images[["slice1"]]@coordinates : no applicable method for @ applied to an object of class "NULL"

What did I miss?