zellkonverter icon indicating copy to clipboard operation
zellkonverter copied to clipboard

conversion between AnnData and SpatialExperiment object.

Open giovp opened this issue 2 years ago • 14 comments

pinging interested parties @lmweber @drighelli @HelenaLC @ivirshup

Nice to meet you all! I believe with some we crossed paths in some zoom dungeons or email thread. I'd like to use this issue to kick-off conversations about conversions between anndata and spatialexperiment obejcts.

I was trying out the SpatialExperiment object and think it's pretty low hanging fruit for conversion with anndata. I believe all the data types conversions are there (image are numpy.array and rest is string/scalar).

I should also mention that doing this I managed to plot annotations on spatial coordinates

path_adata <- "/data/day3_spatial_transcriptomics"
ad <- readH5AD(paste0(path_adata,"/V10F24-105_A1_destvi.h5ad"))

coords <- as.matrix(reducedDim(ad, "spatial"))
colnames(coords) = c("x","y")
spe <- SpatialExperiment(
    assay = assay(ad,"X"), 
    colData = ad@colData, 
    spatialCoords = coords,
)
spe[["sample_id"]] <- "V10F24-105_A1"

path_img <- "/home/rstudio/adv_singlecell_2022/day3_spatial_transcriptomics/V10F24-105_A1.png"
spe <- addImg(spe, 
    sample_id = "V10F24-105_A1", 
    image_id = "V10F24-105_A1",
    imageSource = path_img, 
    scaleFactor = 0.04155585, 
    load = TRUE)

In anndata we have

  • adata.obsm["spatial"] for spatial coordinates
  • adata.uns["spatial"][<library_id>]["images"] store the images (with prob different keys than what can be used/plot in SpatialExp?
  • adata.uns["spatial"][<library_id>]["scalefactors"] store diameters and scale factors of the object.

Where do you think this data should be stored in the SpatialExperiment object?

giovp avatar Apr 26 '22 14:04 giovp

I don't know much about SpatialExperiment so I won't comment on that part but from my side I am happy to add this to {zellkonverter}. From this example it looks as though reading the file into R should work ok already (pending more testing) so the question is where things should be stored and some details of how that is implemented.

I do have a question for @giovp and @ivirshup. Do you plan to keep using this special "spatial" name in existing slots going forward or do you think there will be an AnnData extension with custom slots at some stage?

lazappi avatar Apr 27 '22 06:04 lazappi

I do have a question for @giovp and @ivirshup. Do you plan to keep using this special "spatial" name in existing slots going forward or do you think there will be an AnnData extension with custom slots at some stage?

There will be an anndata extension where a lot will change in the way it interacts with images and the actual object format will change. However, we will for sure have methods to just return an anndata with current level of spatial info (small image, scalefactors) in order to be shared/converted. So I believe the spatial slot will stay for a while?

giovp avatar Apr 27 '22 07:04 giovp

I do have a question for @giovp and @ivirshup. Do you plan to keep using this special "spatial" name in existing slots going forward or do you think there will be an AnnData extension with custom slots at some stage?

There will be an anndata extension where a lot will change in the way it interacts with images and the actual object format will change. However, we will for sure have methods to just return an anndata with current level of spatial info (small image, scalefactors) in order to be shared/converted. So I believe the spatial slot will stay for a while?

Cool. I think we can put together something for what you have currently but longer term I would prefer to support the new spatial format directly when that is settled on. I'm guessing what you are doing currently is only really specific to squidpy? That's probably ok but we need to clear about documenting that.

lazappi avatar Apr 28 '22 08:04 lazappi

I'm guessing what you are doing currently is only really specific to squidpy? That's probably ok but we need to clear about documenting that.

I think scvi-tools also uses visualization and same anndata slots, as well as e.g. eggplant.

Agree with supporting new anndata but don't have schedule on that yet in terms of release.

giovp avatar Apr 28 '22 13:04 giovp

Hi @giovp and @lazappi ,

sorry for my late answer and thanks for involving me in this discussion, I think it's a relevant issue to solve for our SpatialExperiment class and the python interoperability with Squidpy and other methods.

Indeed, with a collaborator ( @davidecrs ) we already tried to implement a first very naive attempt here to this conversion.

At the moment, our implementation doesn't include the image storing into the AnnData object during the conversion, so maybe we can start to discuss about this aspect and then extend the conversion for molecule based data that in SpatialExperiment are stored into BumpyMatrix objects.

Looking forward to talk more about this.

drighelli avatar May 03 '22 14:05 drighelli

About your questions @giovp ,

In anndata we have

  • adata.obsm["spatial"] for spatial coordinates

In SpE we have spatialCoords setter/getter method for the coordinates. They are in a matrix form.

  • adata.uns["spatial"][<library_id>]["images"] store the images (with prob different keys than what can be used/plot in SpatialExp?

I think you already stored an image in the SpE with addImg accessor, is that right?

  • adata.uns["spatial"][<library_id>]["scalefactors"] store diameters and scale factors of the object.

This is still an open issue for us, we had several thoughts and implementations about that, but at the moment we decided to not store them in any place. Future versions could include the scale factors stored in the object metadata.

drighelli avatar May 03 '22 14:05 drighelli

At the moment, our implementation doesn't include the image storing into the AnnData object during the conversion, so maybe we can start to discuss about this aspect and then extend the conversion for molecule based data that in SpatialExperiment are stored into BumpyMatrix objects.

indeed we don't have a way to store molecule based data yet so we don't really read it as well. Think starting from the image in uns would be a good starting point.

In SpE we have spatialCoords setter/getter method for the coordinates. They are in a matrix form. I think you already stored an image in the SpE with addImg accessor, is that right?

Indeed that was the solution.

This is still an https://github.com/drighelli/SpatialExperiment/issues/25 for us, we had several thoughts and implementations about that, but at the moment we decided to not store them in any place. Future versions could include the scale factors stored in the object metadata.

sounds good, where would unstructured metadata be stored in SpaExperiemnt?

giovp avatar May 04 '22 13:05 giovp

Hello, I hope that I am not disturbing any of you. Since I see that this issue is still open, I like to ask for some guidance.

My name in Naime and I work as an scientific employee at Meier's Lab (Helmholtz HPC)

I am reading MS imaging data in Anndata format (annotated maldi images) and I want to store the high resolution H&E image (which is from a consecutive tissue layer) in the same Anndata without it being included for spatial computation. (MALDI has its own spatial info already in adata.obsm["spatial"] for spatial coordinates, and registration with H&E image has been done using other libraries)

Is there a spesific way or data structure that you suggest for sroting the optical image in Anndata? (an optical image that does not take part in spatial transcriptomics computations)

Thank you for your time. :)

nh012 avatar Aug 07 '23 14:08 nh012

Hi @nh012

I haven't worked much with spatial data so I'm not sure I can answer much. Probably the only place image information can be stored is in uns which allows any kind of object. {zellkonverter} will only be able to convert it if it is an object type that is understands though.

I know there has been a lot of work on developing new spatial objects so there may be a better option than using AnnData now/soon. Maybe @giovp can give an update on what is currently recommended.

lazappi avatar Aug 08 '23 06:08 lazappi

Hi all,

Any updates on using this to convert a spatial experiment to an anndata object? The data is IMC data. I tried to use

 writeH5AD(spe, file = out_path)

but got this error

ℹ Using the 'counts' assay as the X matrix
Error in if (ncol(m)) { : argument is of length zero

Thanks!

spatts14 avatar Mar 14 '24 21:03 spatts14

It's not something we have really looked into. I'm not sure how similar SpatialExperiment is, but if it is close enough to SingleCellExperiment/SummarizedExperiment it should be possible (at least for the non-spatial specific parts).

If this is something you would like to push, we would need some example data and more information about which parts do/don't work. A PR would be even better 😸. There is only a couple of weeks to get changes into the next release so the timeline for that is pretty tight.

lazappi avatar Mar 15 '24 07:03 lazappi

Thanks for the quick response! :) Let me check with my supervisor on sharing the data and Ill get back to you

spatts14 avatar Mar 15 '24 07:03 spatts14

A small public example dataset would probably be better than something private. Maybe even whatever SpatialExperiment uses as an example.

lazappi avatar Mar 15 '24 12:03 lazappi

Makes sense! here is a good resource that has the spe object: https://github.com/BodenmillerGroup/ImagingWorkshop2023/tree/main/data

https://github.com/BodenmillerGroup/IMCDataAnalysis/tree/main https://bodenmillergroup.github.io/IMCDataAnalysis/index.html

This gives the pipeline as well so will provide details on what is in the spatial experiment. Our ultimate aim is to convert this into an anndata so we can use GraphCompass to compare disease states. Would be happy to chat more via email or teams, happy to chat.

spatts14 avatar Mar 15 '24 13:03 spatts14