How to include spatial coordinate data into anndata within scanpy
1
4
Entering edit mode
2.1 years ago

Hello I am working on 10X visium spatial transcriptome data which was processed in seurat pacakge with image data. For my downstream analysis, I am trying to import seurat normalized data into scanpy. For this I converted seurat object to h5ad using these steps.

SaveH5Seurat(test_object, overwrite = TRUE, filename = "A1")

Convert("A1.h5seurat", dest = "h5ad", overwrite = TRUE)

Next, imported h5ad format file into scanpy :

adata1 = sc.read_h5ad("A1.h5ad")

But it does not contain any spatial and image information.

obs: 'orig.ident', 'nCount_Spatial', 'nFeature_Spatial', 'cell.ids', 'nCount_SCT', 'nFeature_SCT', 'seurat_clusters', var: 'features', 'SCT_features', 'genename' obsm: 'X_pca', 'X_umap' varm: 'PCs' layers: 'SCT'

raise KeyError( KeyError: "Could not find entry in obsm for 'spatial'.\nAvailable keys are: ['X_pca', 'X_umap']."

How can I add image and coordinate information in 'obsm' for spatial information ?

I would appreciate all the suggestion .

Thanks in advance

scanpy visium transcriptome spatial 10X • 4.2k views
ADD COMMENT
0
Entering edit mode

Hi, were you able to figure this out? I found that I couldn't even transfer pca information using Convert, the result only had umap information.

ADD REPLY
2
Entering edit mode
2.1 years ago
Aljes ▴ 90

I haven't found a direct way to convert the "spatial" information into a h5ad file (in my case, it was lost when I saved the Seurat object to a .h5seurat file). But you can try to save all the information from the VisiumV1 object manually enter image description here

ADD COMMENT
2
Entering edit mode

I tried to separately include the "spatial" cooridnates and images into adata.

1) Adding tissue coodinates :

c = scanpy.read_h5ad("p.h5ad")

all = pd.read_csv('spatial.csv', delimiter = ',',index_col=0) # " three Colum data
label imagerow imagecol

0 AAACCGGGTAGGTACC-1_1 261.297067 313.946997

1 AAACCGTTCGTCCAGG-1_1 233.577403 348.256620

2 AAACCTCATGAAGTTG-1_1 279.114362 296.827053

del all[all. columns[0]] # removed barcodes 
all = all.to_numpy()
c.obsm["spatial"] = all

2) Reading image ##

import matplotlib.pyplot as plt

import PIL

import numpy as np

from PIL import Image

source_image_path="tissue_hires_image.png"

im = Image.open(source_image_path)


image = numpy.array(im)

image=image

spatial_key = "spatial"

library_id = "A1"

c.uns[spatial_key] = {library_id: {}}

c.uns[spatial_key][library_id]["images"] = {}

c.uns['spatial'][library_id]['images']['hires'] = image.astype(np.float32)

3) Plotting spatial cluster

sq.pl.spatial_scatter(c, color="integrated_snn_res.0.7")

It shows a image to one side. How can I fix this issue ?

enter image description here

I would appreciate all the suggestion.

ADD REPLY
0
Entering edit mode

If you got this to work, could you please add this as a tutorial. It's very helpful.

ADD REPLY
0
Entering edit mode

Hi Aljes, Thanks for the code. If we have 2 images/ slices (integrated object), how could i add these images to anndata? Thanks

ADD REPLY
0
Entering edit mode

Value passed for key 'spatial' is of incorrect shape. Values of obsm must match dimensions (0,) of parent. Value had shape (14335,) while it should have had (19701,).

ADD REPLY

Login before adding your answer.

Traffic: 2364 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6