作者,Evil Genius
今天我们分享方法论。
参考文献
其中分析内容很多,但是这一篇我们主要关注距离分析。
现在基本上空转分析全部是python代码,大家最好还是上手一下,2025番外--linux、R、python培训,任何时候,都不如自己会分析来的实在。
import spacec as sp
#import standard packages
import os
import pandas as pd
import scanpy as sc
# silencing warnings
import warnings
warnings.filterwarnings('ignore')
# set plotting parameters
sc.settings.set_figure_params(dpi=80, facecolor='white')
# Specify the path to the data
root_path = "/home/user/path/SPACEc/" # inset your own path
data_path = root_path + 'example_data/raw/' # where the data is stored
# where you want to store the output
output_dir = root_path + 'example_data/output/'
os.makedirs(output_dir, exist_ok=True)
# Load data
adata = sc.read("adata_nn_demo_annotated_cn.h5ad")
adata
AnnData object with n_obs × n_vars = 46789 × 59
obs: 'DAPI', 'x', 'y', 'area', 'region_num', 'unique_region', 'condition', 'leiden_1', 'leiden_1_subcluster', 'cell_type_coarse', 'cell_type_coarse_subcluster', 'cell_type_coarse_f', 'cell_type_coarse_f_subcluster', 'cell_type_coarse_f_f', 'cell_type', 'CN_k20_n40', 'CN_k20_n30', 'CN_k20_n20', 'CN_k20_n25', 'CN_k20_n6', 'CN_k20_n6_annot'
uns: 'CN_k20_n6_colors', 'Centroid_k20_n20', 'Centroid_k20_n25', 'Centroid_k20_n30', 'Centroid_k20_n40', 'Centroid_k20_n6', 'cell_type_coarse_f_colors', 'cell_type_colors', 'dendrogram_cell_type_coarse_f_subcluster', 'leiden', 'leiden_1_colors', 'neighbors', 'umap', 'unique_region_colors'
obsm: 'X_pca', 'X_umap'
layers: 'scaled'
obsp: 'connectivities', 'distances'
Identify potential interactions
# compute the potential interactions
distance_pvals, results_dict = sp.tl.identify_interactions(
adata = adata, # AnnData object
cellid = "index", # column that contains the cell id (set index if the cell id is the index of the dataframe)
x_pos = "x", # x coordinate column
y_pos = "y", # y coordinate column
cell_type = "cell_type", # column that contains the cell type information
region = "unique_region", # column that contains the region information
num_iterations=1000, # number of iterations for the permutation test
num_cores=12, # number of CPU threads to use
min_observed = 10, # minimum number of observed interactions to consider a cell type pair
comparison = 'condition', # column that contains the condition information we want to compare
distance_threshold=20/0.5085) # distance threshold in px (20 µm)
# the results_dict contains the results of the permutation test as well as the observed and shuffled distances
results_dict.keys()
dict_keys(['distance_pvals', 'triangulation_distances_observed', 'triangulation_distances_iterated'])
# the distance_pvals contains the p-values for each cell type pair and is automatically added to the adata.uns
adata.uns['triDist']
distance_pvals_filt = sp.tl.remove_rare_cell_types(adata,
distance_pvals,
cell_type_column="cell_type",
min_cell_type_percentage=1)
# Identify significant cell-cell interactions
# dist_table_filt is a simplified table used for plotting
# dist_data_filt contains the filtered raw data with more information about the pairs
# The function outputs two dataframes: and dist_data_filt that contains all filtered interactions and dist_table_filt that contains a table for all interactions that show a significant value in both tissues
dist_table_filt, dist_data_filt = sp.tl.filter_interactions(
distance_pvals = distance_pvals_filt,
pvalue = 0.05,
logfold_group_abs = 0.1,
comparison = 'condition')
print(dist_table_filt.shape)
dist_data_filt
sp.pl.plot_top_n_distances(
dist_table_filt,
dist_data_filt,
n=5,
colors=None,
dodge=False,
savefig=False,
output_fname="",
output_dir="./",
figsize=(5, 5),
unit="px",
errorbars=True,
)
sp.pl.dumbbell(data = dist_table_filt, figsize=(8,12), colors = ['#DB444B', '#006BA2'])
sp.pl.distance_graph(dist_table = dist_data_filt, # the (filtered) distance data table you want to plot
distance_pvals = distance_pvals, # the full distance data table
condition_pair=['tonsil', 'tonsillitis'],
node_size=1600, font_size=6,
palette=None,
dpi = 600,
savefig=False,
output_fname="",
output_dir=output_dir,)
生活很好,有你更好