spatial transcriptomics 3d visualization | Word Count: 344 | Reading Time: 2mins | Post Views:
With development of spatial transfriptomics, we can get the spatial
information of gene expression. However, the data is 2D, and it is hard
to visualize the data in 3D. Here, I will introduce some methods to
visualize the data in 3D.
But for the first time, there are only some visualization method,
next i will introduce some methods.
here is the code, which is easy for understanding.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 def plot_cloud_point (adata: ad.AnnData, spatial_key: str ='3d_align_spatial' , anno: str ='region' , color_anno: str ='color_anno' , color_map: Optional [dict ] = None , point_size: Union [float , list ] = 20 , save_path: Optional [str ] = None ): if color_map is None : color_map = adata.uns[color_anno] print (color_map) annotation = list (color_map.keys()) colors = np.array(list (color_map.values())) color_arr = np.array([int (c[1 :], 16 ) for c in colors], dtype=np.uint32) color_map = dict (zip (annotation, color_arr)) pts_map = {} point_cloud = np.empty((0 ,3 ), float ) point_cloud = np.append(point_cloud, adata.obsm[spatial_key], axis=0 ) for i in range (len (adata.obs)): annot = adata.obs[anno][i] if annot not in pts_map: pts_map[annot] = np.empty((0 ,3 ), float ) pts_map[annot] = np.append(pts_map[annot], [adata.obsm[spatial_key][i]], axis=0 ) plot = k3d.plot() for key, val in sorted (pts_map.items()): plt_points = k3d.points(positions=val, colors = [color_map[key]]*val.shape[0 ], point_size=point_size, shader='dot' , opacity= 1 , name = key, ) plot += plt_points with open (save_path,'w' ) as fp: fp.write(plot.get_snapshot()) return plot
for this code, i used the k3d package, which is a 3D visualization
package. but it's helpful for spatial transcriptomics data.
for the package import, you can use the following code:
1 2 3 4 5 6 import scanpy as scimport pandas as pdimport numpy as npimport anndata as adfrom typing import Optional ,Union import k3d
you can download some 3d spatial transcriptomics data to make a
visualization.