theMILOlab / SPATA2

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

What is the input image for initiateSpataObject_CountMtr? #82

Closed yeswzc closed 11 months ago

yeswzc commented 1 year ago

Hi, I am trying to use spatial gene expression count matrix that processed from other package. I am trying to use initiateSpataObject_CountMtr to create the SPATA2 object. My coordinates is from "tissue_positions.csv"

I tried the images in 10x spaceranger output folder, but both "tissue_lowres_image.png" and "tissue_hires_image.png" does not work well.

I tried plotSurface(spata2.obj, pt_alpha = 0.2). But no background image was shown with the lowres image. Tough with hires image it showed something, but it is not in the correct position.

Can you let me know which image should I use?

Thanks! Zee

simonmfr commented 1 year ago

Hi @yeswzc, thank you for reaching out.

Which package did you use for preprocessing? In case you use Seurat/Scanpy/Giotto please check https://themilolab.github.io/SPATA2/articles/spata-v2-platform-compatibility.html for converting objects.

SPATA2 relies on the EBImage format, therefore please first load and convert the input for image using magick::as_EBImage(magick::image_read("image.png")).

Alternatively, you can add an image to a SPAT2 object post hoc as described here (also in EBImage format): https://github.com/theMILOlab/SPATA2/blob/9e25808f2f93284d66861151dc84b073e0342f35/R/adj-aZ.R#L2029-L2040

Incorrect positions can be fixed using flipImage() or rotateImage().

Does that help?

yeswzc commented 1 year ago

Thanks. I used SpotClean package to pre-process the data. The output count matrix was later imported to SPATA2 using initiateSpataObject_CountMtr.

Can you let me know what image should I use as input for [EBImage](magick::as_EBImage)? Can I use the images in the cellranger output folder?

I used tissue_positions.csv as input for the coordinates. I tried both tissue_hires_image.png and tissue_lowres_image.png, but neither worked. The hires image is cut at wrong place and spots are at incorrect positions, the lowres does not appear at all.

Here is my code:

sp.data is a seurat object

spata.coord.file = "spaceranger_v201/BY/outs/spatial/tissue_positions.csv" spata.image = "spaceranger_v201/BY/outs/spatial/tissue_lowres_image.png" #spata.image = "spaceranger_v201/BY/outs/spatial/tissue_hires_image.png" spata.coord = read.csv(spata.coord.file, head = T, check.names = F) colnames(spata.coord)[c(1,5:6)] <- c("barcodes","x", "y")

image_ebi <- magick::as_EBImage(magick::image_read(spata.image)) image_obj <- createHistologyImaging(image = image_ebi, id = "BY", coordinates = spata.coord, image_class = "HistologyImage") #image_obj <- flipImage(image_obj) #image_obj <- rotateImage(image_obj) ### spata.coord <- spata.coord[match(colnames(sp.data), spata.coord$barcodes),] #required to be in same order spata.obj <- initiateSpataObject_CountMtr(coords_df = spata.coord, count_mtr = sp.data@assays$Spatial@counts, sample_name = "BY",image_object = image_obj) #setImageDirLowres(spata.obj) <- spata.image ### #spata.obj <- initiateSpataObject_10X("/data/wuz6/data/aldape_lab/spatial10xg/spaceranger_v201/BY52/outs/", sample_name = "BY52")

plotSurface(object = spata.obj, pt_alpha = 0.1, display_image = T)

simonmfr commented 1 year ago

The coordinates from spaceranger are refering to the image used as spaceranger input, that's why I assume your field of view is distorted.

So you can either use the raw image or, if you want to add the hires or lowres images, you need to scale down the coordinates using tissue_hires_scalef or tissue_lowres_scalef from the outs/spatial/scalefactors_json.json file.

Does that help?

yeswzc commented 1 year ago

Thank you. But it is still not clear which command I should use to input the image. Can you send me a code using spaceranger's output files ?

simonmfr commented 1 year ago

You can set the image of a SPATA2 object using setImageObject(). When using createHistologyImaging() for loading the image you can skip magick. This should work for you:

spata.coord.file = "spaceranger_v201/BY/outs/spatial/tissue_positions.csv"
spata.image = "spaceranger_v201/BY/outs/spatial/tissue_lowres_image.png"
#spata.image = "spaceranger_v201/BY/outs/spatial/tissue_hires_image.png"
spata.coord = read.csv(spata.coord.file, head = T, check.names = F)
colnames(spata.coord)[c(1,5:6)] <- c("barcodes","x", "y")

image_obj <- createHistologyImaging(image = spata.image, 
                                    id = "BY", 
                                    coordinates = spata.coord, 
                                    image_class = "HistologyImage",
                                    img_scale_fct = 1)

spata.obj <- setImageObject(object = spata.obj,
                            image_object = image_obj) 

Plus some adjustment to img_scale_fct depending on which image you use

yeswzc commented 12 months ago

Thank you. But it still doesn't work,same as before. Here is how the plot from plotSurface(object = spata.obj, pt_alpha = 0.3, display_image = T) test BY52

Do you know how to solve this? This is the tissue_lowres_image tissue_lowres_image

kueckelj commented 11 months ago

Hello yeswzc, until we fix the problem: May I suggest that you use addExpressionMatrix() on a spata2 object that you created with initiateSpataObject_10X()? Given the directories you posted I assume that you have a normal space ranger output folder, correct? Is the alignment of barcoded spots and image mismatched, too, if you use initiateSpataObject_10x()? If not, is there any other particular reason why you want to create the spata2 object from scratch?

Or is the plot you posted the output of plotSurface() from a spata2 object created with initiateSpataObject_10X()?

Best regards

Jan

yeswzc commented 11 months ago

Thank you. I am just trying. I will use initiateSpataObject_10x().