theislab / zellkonverter

Conversion between scRNA-seq objects
https://theislab.github.io/zellkonverter/
Other
137 stars 26 forks source link

conversion between AnnData and SpatialExperiment object. #61

Open giovp opened 2 years ago

giovp commented 2 years ago

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

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

lazappi commented 2 years ago

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?

giovp commented 2 years ago

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?

lazappi commented 2 years ago

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.

giovp commented 2 years ago

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.

drighelli commented 2 years ago

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 commented 2 years ago

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.

giovp commented 2 years ago

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?

nh012 commented 11 months ago

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. :)

lazappi commented 11 months ago

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.

spatts14 commented 4 months ago

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!

lazappi commented 4 months ago

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.

spatts14 commented 4 months ago

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

lazappi commented 4 months ago

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

spatts14 commented 4 months ago

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.