YosefLab / Hotspot

https://hotspot.readthedocs.io/en/latest/
MIT License
93 stars 14 forks source link

spatial distances #21

Closed wangjiawen2013 closed 2 years ago

wangjiawen2013 commented 2 years ago

Hi, I have an anndata object, and the spatial distances are stored in the obsp key: https://squidpy.readthedocs.io/en/stable/auto_examples/graph/compute_spatial_neighbors.html

Then I run: hs = hotspot.Hotspot(adata,layer_key="counts",latent_obsm_key=None,distances_obsp_key="spatial_distances") hs.create_knn_graph(weighted_graph=False, n_neighbors=4, neighborhood_factor=3, approx_neighbors=False) hs_results = hs.compute_autocorrelations(jobs=5) , an error occurred:

Traceback (most recent call last): File "/home/wangjw/data/work/spatial/.snakemake/scripts/tmp7qyuf46i.hotspot.py", line 14, in hs = hotspot.Hotspot(adata,layer_key="counts",latent_obsm_key=None,distances_obsp_key="spatial_distances") File "/home/wangjw/programs/customized/Hotspot/hotspot/hotspot.py", line 77, in init assert not issparse(distances) AssertionError

I have integrated several different spatial transcriptome objects in my anndata, how does hotspot compute the neighbors ?

adamgayoso commented 2 years ago

Indeed this is a bug, should be fixed by tomorrow.

wangjiawen2013 commented 2 years ago

Hi,

I have construct the spatial neighborhood graph according to squidpy tutorial: https://squidpy.readthedocs.io/en/stable/auto_examples/graph/compute_spatial_neighbors.html

Here is my anndata object: In [17]: adata Out[17]: AnnData object with n_obs × n_vars = 34554 × 23825 obs: 'library_id', 'n_genes_by_counts', 'total_counts', 'total_counts_mt', 'pct_counts_mt', '_scvi_batch', '_scvi_labels', 'leiden' var: 'mt', 'n_cells_by_counts', 'mean_counts', 'pct_dropout_by_counts', 'total_counts', 'mean', 'std' uns: '_scvi_manager_uuid', '_scvi_uuid', 'leiden', 'leiden_co_occurrence', 'leiden_colors', 'leiden_nhood_enrichment', 'leiden_ripley_L', 'library_id_colors', 'log1p', 'moranI', 'neighbors', 'rank_genes_groups', 'spatial', 'spatial_neighbors', 'umap' obsm: 'X_scVI', 'X_umap', 'spatial' layers: 'counts', 'scvi_normalized' obsp: 'connectivities', 'distances', 'spatial_connectivities', 'spatial_distances'

The graph are generated by: sq.gr.spatial_neighbors(adata, n_rings=1, coord_type="grid", n_neighs=4, library_key="library_id"), and there are four libraries in my anndata objects.

It is said in the tutorial: The function builds a spatial graph and saves its adjacency matrix to adata.obsp['spatial_connectivities'] and weighted adjacency matrix to adata.obsp['spatial_distances'] by default. Note that it can also build a a graph from a square grid, just set n_neighs = 4. Here is the graph structure of my anndata object: output

Then when I ran: hs = hotspot.Hotspot(adata,layer_key="counts",latent_obsm_key=None,distances_obsp_key="spatial_distances") hs.create_knn_graph(weighted_graph=False, n_neighbors=4, neighborhood_factor=3, approx_neighbors=False)

An error occurred: ValueError: 4 neighbors per samples are required, but some samples have only 1 neighbors in precomputed graph matrix. Decrease number of neighbors used or recompute the graph with more neighbors. So how to use the spatial_distances stored in an anndata object ?

wangjiawen2013 commented 2 years ago

There are four libraries in the anndata object, so I compute the neighbors corresponding to each library with the parameter "library_key="library_id" using squidpy gr.spatial_neighbors. I think hostpot will treat them separately, and that's why I use "distances_obsp_key="spatial_distances", not latent_obsm_key="spatial".

adamgayoso commented 2 years ago

It makes sense that spots on the edges or corners will have fewer than 4 neighbors. You might consider removing those spots to make hotspot work temporarily.