theMILOlab / SPATA2

A Toolbox for Spatial Gene Expression Analysis
77 stars 15 forks source link

ggplot2 error with ias_layer_bins #81

Closed dst53 closed 12 months ago

dst53 commented 1 year ago

Hello,

Thank you for the great work. I have followed your tutorial on Image Annotation Screening with no issues. However, I run into an issue when using my own SPATA object and annotation, specifically when attempting to visualize gene expression in the same plot with the image annotation screening circle bins overlaid. Every other plotting functionality works as expected.

plotSurfaceComparison() works as expected, but when I try to add the ias_layer_bins I get the following error:

spata2_object@images$sample@annotations$img_ann_1

An object of class 'ImageAnnotation' named 'img_ann_1'. Tags: no_tags.`


# using ten circle bins to visualize the concept

ias_layer_bins_new <- ggpLayerEncirclingIAS( object = spata2_object, distance = "2.25mm", n_bins_circle = 10, id = "img_ann_1", line_size = 1, verbose = TRUE )

ias_layer_bins_new

>[[1]]
[[1]][[1]]
mapping: x = ~x, y = ~y 
geom_polygon: na.rm = FALSE, rule = evenodd
stat_identity: na.rm = FALSE
position_identity 
>
>[[1]][[2]]
mapping: x = ~x, y = ~y, group = ~plot_group 
geom_path: lineend = butt, linejoin = round, linemitre = 10, arrow = NULL, na.rm = FALSE
stat_identity: na.rm = FALSE
position_identity 

plotSurfaceComparison( object = spata2_object, color_by = 'EPCAM', nrow = 1) + ias_layer_bins_new


>09:59:41 Using matrix 'scaled'.
09:59:41 Normalizing values.
09:59:41 Done.
09:59:41 Plotting 1 different variables. (This can take a few seconds.)
Error in `ggplot2::geom_path()`:
! Problem while computing aesthetics.
ℹ Error occurred in the 3rd layer.
Caused by error in `FUN()`:
! object 'plot_group' not found
Run `rlang::last_trace()` to see where the error occurred.
> rlang::last_trace()
<error/rlang_error>
Error in `ggplot2::geom_path()`:
! Problem while computing aesthetics.
ℹ Error occurred in the 3rd layer.
Caused by error in `FUN()`:
! object 'plot_group' not found
---
>Backtrace:

  1. ├─base (local) `<fn>`(x)
  2. └─ggplot2:::print.ggplot(x)
  3.   ├─ggplot2::ggplot_build(x)
  4.   └─ggplot2:::ggplot_build.ggplot(x)
  5.     └─ggplot2:::by_layer(...)
  6.       ├─rlang::try_fetch(...)
  7.       │ ├─base::tryCatch(...)
  8.       │ │ └─base (local) tryCatchList(expr, classes, parentenv, handlers)
  9.       │ │   └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
 10.       │ │     └─base (local) doTryCatch(return(expr), name, parentenv, handler)
 11.       │ └─base::withCallingHandlers(...)
 12.       └─ggplot2 (local) f(l = layers[[i]], d = data[[i]])
 13.         └─l$compute_aesthetics(d, plot)
 14.           └─ggplot2 (local) compute_aesthetics(..., self = self)
 15.             └─ggplot2:::scales_add_defaults(...)
 16.               └─base::lapply(aesthetics[new_aesthetics], eval_tidy, data = data)
 17.                 └─rlang (local) FUN(X[[i]], ...)
>Run rlang::last_trace(drop = FALSE) to see 5 hidden frames.`

This works fine when I add the ias_layer_bins object from the tutorial to my SPATA object's plotSurfaceComparison(), although obviously the bins do not correspond to my annotation of interest.

ias_layer_bins <- ggpLayerEncirclingIAS( object = object_t313, distance = "2.25mm", n_bins_circle = 10, id = "necrotic_center", line_size = 1 )

plotSurfaceComparison( object = spata2_object, color_by = 'EPCAM', nrow = 1) + ias_layer_bins


![image](https://github.com/theMILOlab/SPATA2/assets/75229061/795e54d2-b88a-4139-a56c-2d3e621b3e5c)

sessionInfo()



>R version 4.2.1 (2022-06-23)
>Platform: x86_64-pc-linux-gnu (64-bit)
>Running under: CentOS Linux 7 (Core)
>
>Matrix products: default
>BLAS:   /share/pkg.7/r/4.2.1/install/lib64/R/lib/libRblas.so
>LAPACK: /share/pkg.7/r/4.2.1/install/lib64/R/lib/libRlapack.so
>
>locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8     LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8    LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
>
>attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     
>
>other attached packages:
[1] purrr_1.0.1  SPATA2_2.0.4

Do you know what could be the issue?

Thank you
kueckelj commented 1 year ago

Hello dst53, thanks for using SPATA2. Unfortunately I can not reproduce the error. Weird, indeed, that it seems to work for one spata2 object and not for the other. Does the error occur when you try to add ias_layer_bins_new to surface plots plotted with plotSurface() instead of plotSurfaceComparison()? If not this might work as a work around for you untill we have solved the problem:

# example variables
var_names <- c("EPCAM")

# 
purrr::map(
  .x = var_names, 
  .f = ~ plotSurface(spata2_object, color_by = .x) + ias_layer_new_bins
) %>% 
  patchwork::wrap_plots()

Furthermore, does the same problem occur with other spata2 objects as well? And does the error appear if you set inc_outline = FALSE? Can you show me a surface plot with the outline of the image annotation img_ann_1 alone using ggpLayerImgAnnOutline(). And lastly, can you give me the output of getPixelScaleFactor(spata2_object, unit = "mm")?

Best regards

Jan

dst53 commented 1 year ago

Hi Jan,

Thank you for your help. I believe I have found the source of the error. I had been converting my Seurat objects to SPATA2 objects using SPATA2::asSPATA2().

If instead I load my 10x matrix using initiateSpataObject_10X() the error no longer occurs and I can plot ias_layer_bins on top of the marker expression.

Therefore this seems to be an issue only with Seurat objects that have been converted to SPATA2 objects?

A little more about the error:

The error occurs regardless of whether plotSurface() or plotSurfaceComparison() is used and with other spata2 objects that have been converted from Seurat objects.

I'm not sure what function uses the inc_outline argument? But if I try to create a tissue outline like so on the converted seurat spata2 objects I get the following error:

tissue_outline <- ggpLayerTissueOutline(object = spata2_object, line_color = "grey")

11:04:18 Using center to center distance to compute pixel scale factor. 11:04:18 Computing barcode spot distances. 11:04:22 Done. Error in getTissueOutlineDf(object) : tissueOutlineIdentified(object) is not TRUE

getPixelScaleFactor(spata2_object, unit = "mm")

[1] 0.01475554 attr(,"unit") [1] "mm/px"

Thank you

kueckelj commented 12 months ago

Hello dst53, sorry for the late reply. I am facing a unusual busy week. Can you try to run object <- identifyTissueSections(object) followed by object <- identifyTissueOutline(object) ? Does it work afterwards? inc_outline is an argument of the function ggpLayerEncirclingIAS(). It requires, however, an identified tissue outline. It seems as if this processing step is missing in the asSPATA2() method for Seurat objects. Therefore, if inc_outline = FALSE the tissue outline is not included and thus not needed. I assume that the function works if you set it to FALSE.

dst53 commented 12 months ago

Hi Jan,

No worries, thank you! That fixed the problem.

Cheers