Visium mouse brain

import scanpy as sc
import spacec as sp
import warnings
warnings.filterwarnings("ignore")
2025-04-14 15:45:26.003866: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  SSE4.1 SSE4.2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
INFO:root: * TissUUmaps version: 3.1.1.6
data_dir = '/Users/yuqitan/Nolan Lab Dropbox/Yuqi Tan/analysis_pipeline/Manuscript/NatComm_091624/revision_031225/analysis/app_spatial_transcriptomics/'
output_dir = '/Users/yuqitan/Nolan Lab Dropbox/Yuqi Tan/analysis_pipeline/Manuscript/NatComm_091624/revision_031225/analysis/app_spatial_transcriptomics/output/'
# trying to read the imc
adata = sc.read(data_dir + 'visium_ffpe_mouse_brain_adata.h5ad')
adata
AnnData object with n_obs × n_vars = 2264 × 19465
    obs: 'in_tissue', 'array_row', 'array_col'
    var: 'gene_ids', 'feature_types', 'genome'
    uns: 'spatial'
    obsm: 'spatial'
adata.obs['x'] = [sublist[0] for sublist in adata.obsm['spatial']]
adata.obs['y'] = [sublist[1] for sublist in adata.obsm['spatial']]
adata.obs['in_tissue'] = adata.obs['in_tissue'].astype(str)

Clustering

sc.pp.scale(adata)
sc.pp.pca(adata, n_comps=10)
sc.pp.neighbors(adata)
sc.tl.leiden(adata, resolution=0.5)
adata
AnnData object with n_obs × n_vars = 2264 × 19465
    obs: 'in_tissue', 'array_row', 'array_col', 'x', 'y', 'leiden', 'CN_k20_n6', 'CN_k20_n10', 'CN_k20_n20'
    var: 'gene_ids', 'feature_types', 'genome', 'mean', 'std'
    uns: 'spatial', 'pca', 'neighbors', 'leiden', 'leiden_colors', 'Centroid_k20_n6', 'Centroid_k20_n10', 'Centroid_k20_n20', 'CN_k20_n20_colors', 'CN_k20_n10_colors'
    obsm: 'spatial', 'X_pca'
    varm: 'PCs'
    obsp: 'distances', 'connectivities'
sc.pl.pca(adata, color = ['leiden'], wspace=0.5) 
../_images/0173bcff935db717cf3fc8ab21f5b07ee617d0ab62d973d6c7a4388b0699eb00.png

Scatter plot

df = catplot(
    adata, 
    color = "leiden", # specify group column name here (e.g. celltype_fine)
    unique_region = "in_tissue", # specify unique_regions here
    X='x', Y='y', # specify x and y columns here
    n_columns=2, # adjust the number of columns for plotting here (how many plots do you want in one row?)
    palette=None, #default is None which means the color comes from the anndata.uns that matches the UMAP
    savefig=False, # save figure as pdf
    output_fname = "", # change it to file name you prefer when saving the figure
    output_dir= output_dir, # specify output directory here (if savefig=True)
)
../_images/a6e2379f48a5afa53b6a19c7cfecacf9049641f97110fb37b04a96ba86bf85e1.png

Cell type composition

sp.pl.create_pie_charts(
    adata,
    color = "leiden", 
    grouping = "in_tissue", 
    show_percentages=False,
    palette=None, #default is None which means the color comes from the anndata.uns that matches the UMAP
    savefig=False, # change it to true if you want to save the figure
    output_fname = "", # change it to file name you prefer when saving the figure
    output_dir = output_dir #output directory for the figure
)
../_images/4c2bc399534f48420e539c7ae60adee1cce2230f882e689cb05dcba96792cc18.png

Neighborhood analysis

adata = sp.tl.neighborhood_analysis(
    adata, 
    unique_region = "in_tissue", 
    cluster_col = "leiden", 
    X = 'x', Y = 'y',
    k = 20, # k nearest neighbors
    n_neighborhoods = 10, #number of CNs
    elbow = False)
Starting: 1/1 : 1
Finishing: 1/1 : 1 0.016676902770996094 0.01679396629333496
sp.pl.cn_exp_heatmap(
    adata, # anndata
    cluster_col = "leiden", # cell type column
    cn_col = "CN_k20_n10", # CN column
    palette=None, # color palette for CN
    savefig = False, # save the figure
    output_dir = output_dir, # output directory
    rand_seed = 1 # random seed for reproducibility
)
../_images/0318b248cc6a1f70cd31392b57cd476324b40309513d3aa8dba77cf93d3e195d.png
df = catplot(
    adata, 
    color = "CN_k20_n10", # specify group column name here (e.g. celltype_fine)
    unique_region = "in_tissue", # specify unique_regions here
    X='x', Y='y', # specify x and y columns here
    n_columns=2, # adjust the number of columns for plotting here (how many plots do you want in one row?)
    palette=None, #default is None which means the color comes from the anndata.uns that matches the UMAP
    savefig=False, # save figure as pdf
    output_fname = "", # change it to file name you prefer when saving the figure
    output_dir= output_dir, # specify output directory here (if savefig=True)
)
../_images/724a15bfccd1881fa003a235d9049eb31d7039a7d6941f2b84cebfa2c11044d0.png

Spatial context map

cnmap_dict_visium = sp.tl.build_cn_map(
    adata = adata, # adata object
    cn_col = "CN_k20_n10",# column with CNs
    palette = None, # color dictionary
    unique_region = 'in_tissue',# column with unique regions
    k = 70, # number of neighbors
    X='x', Y='y',  # coordinates
    threshold = 0.85, # threshold for percentage of cells in CN
    per_keep_thres = 0.85,) # threshold for percentage of cells in CN
Starting: 1/1 : 1
Finishing: 1/1 : 1 0.03047490119934082 0.030478715896606445
18 0.017226148409894004
../_images/c7f9641959f05cc5ed23233e930ce1540240e21ec360462d29ad3c5eb773eeda.png ../_images/a8515554dd2c184ac3b76321cb4de8855869aa5de2b8b619e46381247cba8317.png
sp.pl.cn_map(cnmap_dict = cnmap_dict_visium, # dictionary from the previous step
        adata = adata, # adata object
        cn_col = "CN_k20_n10", # column with CNs used to color the plot
        palette = None, # color dictionary
        figsize=(25, 15), # figure size
        savefig=False, # save figure as pdf
        output_fname = "", # change it to file name you prefer when saving the figure
        output_dir= output_dir # specify output directory here (if savefig=True)
    )
../_images/b54b293d303f849d563a5563ef7bf6d615c0d2fdde08ceae84d5aa380a6b07d4.png

Barycentric coordinate plots

sp.pl.BC_projection(adata=adata,
    cnmap_dict = cnmap_dict_visium, # dictionary from the previous step
    cn_col = "CN_k20_n10", # column with CNs
    plot_list = [0, 5, 6], # list of CNs to plot (three for the corners)
    cn_col_annt =  "CN_k20_n10", # column with CNs used to color the plot
    palette = None, # color dictionary
    figsize=(5, 5), # figure size
    rand_seed = 1, # random seed for reproducibility
    n_num = None, # number of neighbors
    threshold = 0.6) # threshold for percentage of cells in CN
../_images/65b28bfd7872b24b1a5fb7148ace78f13d7cf00fae538f8d3405ca8eb2feb74c.png