pachterlab / voyager

From geospatial to spatial -omics
https://pachterlab.github.io/voyager/
Artistic License 2.0
70 stars 8 forks source link

error in `geom_spi_rgb()` when using ` image_id` in `plotSpatialFeature()` #11

Closed alikhuseynov closed 11 months ago

alikhuseynov commented 11 months ago
imgData(sfe)
DataFrame with 5 rows and 4 columns
     sample_id    image_id   data scaleFactor
   <character> <character> <list>   <numeric>
1 test.merfish  Cellbound1   ####           1
2 test.merfish  Cellbound2   ####           1
3 test.merfish  Cellbound3   ####           1
4 test.merfish        DAPI   ####           1
5 test.merfish       PolyT   ####           1

options(repr.plot.height = 5, repr.plot.width = 10)
pl1 <- 
plotSpatialFeature(sfe, features = "sum", 
                   size = 4, colGeometryName = "cellSeg", 
                   scattermore = TRUE, # will plot only centroids!
                   image_id = "DAPI" # 
                  ) & DarkTheme()
pl1

scattermore and binning only apply to points. Using centroids.
ERROR while rich displaying an object: Error in `geom_spi_rgb()`:
! Problem while converting geom to grob.
Error occurred in the 1st layer.
Caused by error in `rgb()`:
! color intensity 47180, not in 0:255

This error happen when using image_id. Something to do with max_col_value in Voyager:::geom_spi_rgb(., max_col_value = 255) any suggestions? Thanks

alikhuseynov commented 11 months ago

Many Thanks! I can confirm that it works now for the whole image. However it won't work with the bbox_use, any suggestions?

bbox_use <- c(xmin = 8300, xmax = 8000, ymin = -8300, ymax = -8000)
plotSpatialFeature(sfe, features = "LAMA2", bbox = bbox_use,
                   size = 4, colGeometryName = "cellSeg", 
                   image_id = "DAPI" #
                  ) & DarkTheme()
Error: [ext] invalid extent
Traceback:

1. plotSpatialFeature(sfe, features = "LAMA2", bbox = bbox_use, 
 .     size = 4, colGeometryName = "cellSeg", image_id = "DAPI")
2. .plotSpatialFeature(sfe, values, colGeometryName, sample_id, 
 .     ncol, ncol_sample, annotGeometryName, annot_aes, annot_fixed, 
 .     bbox, image_id, aes_use, divergent, diverge_center, annot_divergent, 
 .     annot_diverge_center, size, shape, linewidth, linetype, alpha, 
 .     color, fill, scattermore, pointsize, bins, summary_fun, hex, 
 .     maxcell, dark, ...)
3. .get_img_df(sfe, sample_id, image_id, bbox)
4. lapply(sample_id, function(s) {
 .     img_data <- img_df$data[img_df$sample_id == s]
 .     bbox_use <- ext(bbox[c("xmin", "xmax", "ymin", "ymax"), s])
 .     bb <- as.vector(bbox_use)
 .     lapply(img_data, function(img) {
 .         img_cropped <- terra::crop(imgRaster(img), bbox_use, 
 .             snap = "out")
 .         img_cropped <- terra::shift(img_cropped, dx = -bb["xmin"], 
 .             dy = -bb["ymin"])
 .         new("SpatRasterImage", image = img_cropped)
 .     })
 . })
5. FUN(X[[i]], ...)
6. ext(bbox[c("xmin", "xmax", "ymin", "ymax"), s])
7. ext(bbox[c("xmin", "xmax", "ymin", "ymax"), s])
8. .local(x, ...)
9. error("ext", "invalid extent")
10. stop("[", f, "] ", emsg, ..., call. = FALSE)
lambdamoses commented 11 months ago

The bbox is unrelated to the maximum value in the image. Using a bounding box would still work with my 16 bit test image. Your bbox is

bbox_use <- c(xmin = 8300, xmax = 8000, ymin = -8300, ymax = -8000)

xmax is smaller than xmin, and that's the reason why it's invalid extent.

lambdamoses commented 11 months ago

Plus there's an argument in plotSpatialFeature for dark theme: dark = TRUE; it uses different default palettes from light theme so lighter color stands for higher value and stand out from the dark background.

alikhuseynov commented 11 months ago

ahh I see, Thanks! it works now. The confusion came from flip in ReadVizgen() since I was trying different combinations. By default plotSpatialFeature() doesn't show the axes, so I was adding Seurat::DarkTheme() which does the trick with less code. dark = TRUE is awesome 👍

alikhuseynov commented 11 months ago

is it easy to implement multiple overlapping images (eg, terra::aggregate()) to show, say image_id = ("DAPI", "Cellbound1") together? I could take a look at some point as well.

lambdamoses commented 11 months ago

That's a good point. I can implement that. Doesn't seem difficult.

alikhuseynov commented 11 months ago

Thanks, especially, if one could use > 2 images, like image_id = ("DAPI", "Cellbound1", "Cellbound2")