scverse / spatialdata

An open and interoperable data framework for spatial omics data
https://spatialdata.scverse.org/
BSD 3-Clause "New" or "Revised" License
174 stars 34 forks source link

Overlay custom shape onto cell boundaries from Xenium #547

Open EST09 opened 3 weeks ago

EST09 commented 3 weeks ago

Hi,

This may be a question for SOPA but I thought it was also worth asking here as well please.

I have custom polygon segmentation for a particular cell type derived from H&E. I've added this as a shape to my spatialdata object in the same coordinate system as cell boundaries (the default Xenium segmentation). I'd love to overlay this custom segmentation onto the default Xenium cell segmentation (cell boundaries) so that any cells/transcripts in the default under the custom segmentation get merged into the custom segmentation but the other cells from the default are kept.

In an ideal world I'd end up a new segmentation which is mostly cell_boundaries with the custom segmentation replacing cells it overlaid.

I've been struggling a bit with how to achieve this and was wondering if the functionalities already present in spatialdata/SOPA. I've been looking at https://spatialdata.scverse.org/en/latest/tutorials/notebooks/notebooks/examples/tables.html, a use case for table, but haven't managed to implement it yet.

Best wishes, Emily

giovp commented 3 weeks ago

hi @EST09 thank you for the question, this is a very interesting use case that we currently do not support. I quickly googled at potential solutions in shapely/geopandas and found these two that it is worth to take a look:

  1. https://shapely.readthedocs.io/en/stable/manual.html#shapely.ops.unary_union
  2. https://geopandas.org/en/stable/docs/reference/api/geopandas.GeoSeries.union.html

I think if the two shapes have the same index (i.e. a row refers to the same cells, in the two segmentation view), the second option could be relatively straightforward, that would output a new shape (union) that you could parse and make the default for the table annotations. HTH

EST09 commented 2 weeks ago

Thank you so much for getting back to me. I ended up initially using the GeoPandas overlay function but also asked SOPA the same question and he's very kindly implemented a new feature which I'm hoping to try soon. Thank you for spatial data - it's a really fantastic package and I'm v much enjoying using it