drieslab / Giotto

Spatial omics analysis toolbox
https://drieslab.github.io/Giotto_website/
Other
240 stars 94 forks source link

Interactive selection tutorial needs more steps to have a saveable object with cosmx #880

Closed rbutleriii closed 5 months ago

rbutleriii commented 5 months ago

Describe the Error

The interactive selection is fantastic and really works well! However, when using it in my pipeline I noticed the tutorial is missing essential step for a cosmx object you can save. If you save it after addPolygonCells then it will error when you try to load it again:

Error Message

# 3.2 read Giotto spatial centroid information
# Error in basename(centroid_paths) : a character vector argument expected

To Reproduce

library(Giotto)
library(GiottoData)

# load prep dataset
loadGiottoMini(dataset="cosmx") %>%
  filterGiotto(
    feat_type = 'rna',
    feat_det_in_min_cells = 5,
    min_det_feats_per_cell = 5
  ) %>%
  normalizeGiotto(feat_type = 'rna') %>%
  normalizeGiotto(feat_type = 'neg_probe', library_size_norm = FALSE) %>%
  calculateHVF(show_plot = FALSE) %>%
  runPCA(ncp=10) %>%
  createNearestNetwork(dimensions_to_use = 1:5, k = 5) %>%
  doLeidenCluster() %>%
  createSpatialNetwork(minimum_k = 2) -> x

# Plot spatial cells/spots with tissue
x %>%
  spatPlot2D(
    cell_color = 'leiden_clus',
    point_size = 2,
    point_alpha = 1,
    show_image = TRUE,
    largeImage_name = names(x@largeImages),
    return_plot = TRUE,
    show_plot = FALSE
  ) %>%
  plotInteractivePolygons() -> my_polygon_coords

# Listening on http://127.0.0.1:3374

# > dput(my_polygon_coords)
my_polygon_coords <- structure(list(x = c(5302.82794711018, 5353.79105375725, 5384.3689177455,
5453.67874278552, 5575.9901987385, 5734.99509147738, 5741.11066427503,
5641.22297524676, 5500.56480090083), y = c(-156764.527735769,
-156864.415424798, -156917.417055711, -156903.147385849, -156846.068706405,
-156719.68020192, -156658.524473944, -156703.372007793, -156778.797405631
), name = c("polygon 1", "polygon 1", "polygon 1", "polygon 1",
"polygon 1", "polygon 1", "polygon 1", "polygon 1", "polygon 1"
)), row.names = c(NA, -9L), class = c("data.table", "data.frame"
))

# Add polygons as in tutorial
polygons = createGiottoPolygonsFromDfr(my_polygon_coords, name = 'subregion')

x %>% 
  addGiottoPolygons(gpolygons = list(polygons)) %>%
  addPolygonCells(polygon_name = 'subregion') -> x

saveGiotto(x, foldername = "test", method = 'qs', overwrite = TRUE)
loadGiotto("test")
# 1. read Giotto object
# 2. read Giotto feature information
# [1] "test/Features/neg_probe_feature_spatVector.shp"
# [1] "neg_probe"
# [1] "test/Features/rna_feature_spatVector.shp"
# [1] "rna"
# 3. read Giotto spatial information

# 3.1 read Giotto spatial shape information
# [1] "cell_spatInfo_spatVector.shp"      "subregion_spatInfo_spatVector.shp"
# cell
# subregion

# 3.2 read Giotto spatial centroid information
# Error in basename(centroid_paths) : a character vector argument expected

Expected behavior

It seems to be down to gobjects with subcellular features? In any case you can rescue it by calculating the spatial centroids. Maybe add that to the tutorial (Agg features seems optional).

# Calculate centroids too
x %>% 
  addGiottoPolygons(gpolygons = list(polygons)) %>%
  addPolygonCells(polygon_name = 'subregion') %>%
  addSpatialCentroidLocations(poly_info = 'subregion') -> x 
  # # Aggregate subcellular features
  # calculateOverlapRaster(spatial_info = 'subregion', feat_info = 'rna') %>%
  # calculateOverlapRaster(spatial_info = 'subregion', feat_info='neg_probe') %>%
  # overlapToMatrix(poly_info = 'subregion', feat_info='rna') %>%
  # overlapToMatrix(poly_info = 'subregion', feat_info='neg_probe') -> x

System Information

R version 4.3.2 (2023-10-31)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 22.04.3 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.10.0
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.10.0

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C
 [9] LC_ADDRESS=C               LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

time zone: America/Los_Angeles
tzcode source: system (glibc)

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

other attached packages:
[1] shiny_1.8.0        GiottoData_0.2.6.2 GiottoUtils_0.1.3  Giotto_4.0.2
[5] GiottoClass_0.1.3

loaded via a namespace (and not attached):
 [1] tidyselect_1.2.0      dplyr_1.1.4           farver_2.1.1
 [4] GiottoVisuals_0.1.4   fastmap_1.1.1         promises_1.2.1
 [7] rsvd_1.0.5            stringfish_0.16.0     digest_0.6.34
[10] mime_0.12             lifecycle_1.0.4       ellipsis_0.3.2
[13] terra_1.7-65          dbscan_1.1-12         magrittr_2.0.3
[16] compiler_4.3.2        rlang_1.1.3           sass_0.4.8
[19] tools_4.3.2           igraph_2.0.1.1        utf8_1.2.4
[22] data.table_1.15.0     S4Arrays_1.2.0        labeling_0.4.3
[25] reticulate_1.34.0     DelayedArray_0.28.0   RColorBrewer_1.1-3
[28] abind_1.4-5           BiocParallel_1.36.0   miniUI_0.1.1.1
[31] withr_3.0.0           BiocGenerics_0.48.1   grid_4.3.2
[34] stats4_4.3.2          fansi_1.0.6           beachmat_2.18.0
[37] xtable_1.8-4          colorspace_2.1-0      future_1.32.0
[40] ggplot2_3.4.4         globals_0.16.2        scales_1.3.0
[43] gtools_3.9.5          cli_3.6.2             crayon_1.5.2
[46] ragg_1.2.5            generics_0.1.3        RcppParallel_5.1.7
[49] future.apply_1.11.0   cachem_1.0.8          RApiSerialize_0.1.2
[52] zlibbioc_1.48.0       parallel_4.3.2        XVector_0.42.0
[55] matrixStats_1.2.0     vctrs_0.6.5           Matrix_1.6-3
[58] jsonlite_1.8.8        BiocSingular_1.18.0   IRanges_2.36.0
[61] S4Vectors_0.40.2      irlba_2.3.5.1         listenv_0.9.0
[64] systemfonts_1.0.4     jquerylib_0.1.4       glue_1.7.0
[67] parallelly_1.36.0     codetools_0.2-19      gtable_0.3.4
[70] deldir_2.0-2          later_1.3.2           ScaledMatrix_1.10.0
[73] munsell_0.5.0         tibble_3.2.1          pillar_1.9.0
[76] rappdirs_0.3.3        htmltools_0.5.7       R6_2.5.1
[79] textshaping_0.3.6     lattice_0.22-5        Biobase_2.60.0
[82] png_0.1-8             backports_1.4.1       qs_0.25.7
[85] memoise_2.0.1         httpuv_1.6.11         bslib_0.6.1
[88] Rcpp_1.0.12           SparseArray_1.2.3     checkmate_2.3.1
[91] colorRamp2_0.1.0      MatrixGenerics_1.14.0 pkgconfig_2.0.3
josschavezf commented 5 months ago

Hi @rbutleriii, thanks for detecting this issue. I looked at it and the error comes from the lack of centroids in the polygons object. To fix the error, indicate calc_centroids = TRUE in the createGiottoPolygonsFromDfr( ) function. It won't affect the downstream analysis using your selected regions. I'll update the tutorial on the website immediately.

polygons = createGiottoPolygonsFromDfr(my_polygon_coords, name = 'subregion', calc_centroids = TRUE)

x %>% addGiottoPolygons(gpolygons = list(polygons)) %>% addPolygonCells(polygon_name = 'subregion') -> x

saveGiotto(x, foldername = "test", method = 'qs', overwrite = TRUE) loadGiotto("test")