MIBI-TOF human colorectal carcinoma

import scanpy as sc
import spacec as sp
import warnings
warnings.filterwarnings("ignore")
2025-04-14 15:47:34.894290: 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_proteomics/'
output_dir = '/Users/yuqitan/Nolan Lab Dropbox/Yuqi Tan/analysis_pipeline/Manuscript/NatComm_091624/revision_031225/analysis/app_spatial_proteomics/output/'
# trying to read the imc
adata = sc.read(data_dir + 'mibitof_adata.h5ad')
adata
AnnData object with n_obs × n_vars = 3309 × 36
    obs: 'row_num', 'point', 'cell_id', 'X1', 'center_rowcoord', 'center_colcoord', 'cell_size', 'category', 'donor', 'Cluster', 'batch', 'library_id'
    var: 'mean-0', 'std-0', 'mean-1', 'std-1', 'mean-2', 'std-2'
    uns: 'Cluster_colors', 'batch_colors', 'neighbors', 'spatial', 'umap'
    obsm: 'X_scanorama', 'X_umap', 'spatial'
    obsp: 'connectivities', 'distances'
adata.obs['x'] = [sublist[0] for sublist in adata.obsm['spatial']]
adata.obs['y'] = [sublist[1] for sublist in adata.obsm['spatial']]

Scatter plot

df = sp.pl.catplot(
    adata, 
    color = "Cluster", # specify group column name here (e.g. celltype_fine)
    unique_region = "batch", # 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/7327d17025f1953e3a3bd09b24872c3b19a21fcfc2cd7e826dcc35fa02926f6f.png

Cell type composition

sp.pl.create_pie_charts(
    adata,
    color = "Cluster", 
    grouping = "batch", 
    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/f0fa40a5c8349538540430098bfe65a14db3208aa7b216453a997e8d0f25fb47.png

Neighborhood analysis

sc.pl.umap(adata, color = ['Cluster', 'batch'], wspace=0.5) 
../_images/20a12bd3a82bc3f4d84c273c0699668f009136fea3fa194c285edd12cd8bf7a7.png
adata = sp.tl.neighborhood_analysis(
    adata, 
    unique_region = "batch", 
    cluster_col = "Cluster", 
    X = 'x', Y = 'y',
    k = 20, # k nearest neighbors
    n_neighborhoods = 8, #number of CNs
    elbow = False)
Starting: 1/3 : 0
Finishing: 1/3 : 0 0.008551836013793945 0.008671045303344727
Starting: 2/3 : 1
Finishing: 2/3 : 1 0.0064699649810791016 0.015212059020996094
Starting: 3/3 : 2
Finishing: 3/3 : 2 0.005615949630737305 0.020841121673583984
sp.pl.cn_exp_heatmap(
    adata, # anndata
    cluster_col = "Cluster", # 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/ef96de29c335d93060b0b3a210f2955b5b16e97bd5f9171e2069bb73ff53e7f0.png
df = sp.pl.catplot(
    adata, 
    color = "CN_k20_n8", # specify group column name here (e.g. celltype_fine)
    unique_region = "batch", # 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/a444bc76da71862a60ba6df7d570ea46f8fcb37c9555524574db944bf5fe2fd0.png

Spatial context map

cnmap_dict_mibitof = sp.tl.build_cn_map(
    adata = adata, # adata object
    cn_col = "CN_k20_n8",# column with CNs
    palette = None, # color dictionary
    unique_region = 'Cluster',# 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: 7/8 : Endothelial
Finishing: 7/8 : Endothelial 0.006070852279663086 0.006963014602661133
Starting: 1/8 : Epithelial
Finishing: 1/8 : Epithelial 0.011197090148925781 0.01810622215270996
Starting: 5/8 : Fibroblast
Finishing: 5/8 : Fibroblast 0.004419803619384766 0.022513866424560547
Starting: 2/8 : Imm_other
Finishing: 2/8 : Imm_other 0.005689859390258789 0.028171062469482422
Starting: 3/8 : Myeloid_CD11c
Finishing: 3/8 : Myeloid_CD11c 0.002629995346069336 0.03075885772705078
Starting: 8/8 : Myeloid_CD68
Finishing: 8/8 : Myeloid_CD68 0.003142118453979492 0.03382086753845215
Starting: 4/8 : Tcell_CD4
Finishing: 4/8 : Tcell_CD4 0.00849294662475586 0.04222679138183594
Starting: 6/8 : Tcell_CD8
Finishing: 6/8 : Tcell_CD8 0.004423856735229492 0.04657578468322754
27 0.010879419764279197
../_images/6ef2c64e34e4342bf0b5c461af9b727360b2e3a10241f2e8f27c77c38d1cf2a8.png ../_images/c9e3aefb5276bf31cf1e727c770a5bc550cbff01841e2d36d6c0c4cbccb061a9.png
sp.pl.cn_map(cnmap_dict = cnmap_dict_mibitof, # dictionary from the previous step
        adata = adata, # adata object
        cn_col = "CN_k20_n8", # 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/9bf9e06ecadcd748612cf821c3e3dde9ec213e8c0f6bd36e9c20ff986e1b5dc3.png

Barycentric coordinate plots

sp.pl.BC_projection(adata=adata,
    cnmap_dict = cnmap_dict_mibitof, # dictionary from the previous step
    cn_col = "CN_k20_n8", # column with CNs
    plot_list = [1, 3, 7], # list of CNs to plot (three for the corners)
    cn_col_annt = "CN_k20_n8", # 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/340402b42625cb92df2f9a812880929fa2bc67f750d024ea92f685d4bcf4b465.png

Patch proximity analysis

adata.obs["CN_k20_n10"] = adata.obs["CN_k20_n10"].astype(str)
region_results = sp.tl.patch_proximity_analysis(
    adata, 
    region_column = "batch", # column with the region information
    patch_column = "CN_k20_n10", # column with the patch information (derive patches from this column)
    group='7', # group to consider
    min_cluster_size=20, # minimum cluster size to consider
    x_column='x', y_column='y', # spatial coordinates
    radius = 20, # to get the distance in µm
    edge_neighbours = 2, # number of neighbours to consider for edge detection
    key_name = 'ppa_result_20', # key name to store the result in adata.uns
    plot = True) # plot detection for demonstration purposes
Estimated number of clusters: 1
Estimated number of noise points: 83
../_images/3884d5dc0f73d98026217dd3b9120884619e3681b82e76b8548d873b460e86f8.png
2025-04-14 17:43:06.931194: 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
Figure(1500x500)
Finished 0_7
No 7 in 1
Estimated number of clusters: 1
Estimated number of noise points: 162
../_images/f4a9ff2f1a4a865ff64437f63d5271fb67a680527a80ce7a87ae5dc7b9fa40d6.png
2025-04-14 17:43:18.449883: 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
Figure(1500x500)
Finished 2_7
# Donut plots for CNs around Germinal Center
sp.pl.ppa_res_donut(adata, 
                palette=None,
                cat_col = "CN_k20_n10",
                key_names = ['ppa_result_20'],
                radii = [20],
                unit = 'µm',
                figsize = (10,10),  
                add_guides = True,
                text = 'CN 7',
                label_color='white',
                subset_column = None,
                subset_condition = 'batch',
                title='mibitof patch proximity analysis')
Key 0: ppa_result_20
Key 0 has 25 rows.
../_images/c1083bab9f2bb8fff9cb3d4fefd39b80cd47ad18f38d6165c8980cfd39a28d76.png