前文提到Tangram在基于mapping的步驟,有兩種方式:
- mode = "cluster":速度快古徒,占用內(nèi)存少,適用于空轉和單細胞數(shù)據(jù)來自不同樣本情況下。
- mode = "cell": 占用內(nèi)存大撑刺,每個cell進行匹配,可以mapping到空轉沒有抓到的轉錄本握玛。
由于受限于空間轉錄組(包括 Visium 和 Slide-seq技術)的分辨度够傍,每個voxel(spot)通常包含多個單細胞,針對這一情況挠铲,采用去卷積的方法去推斷細胞組成顯得尤為重要冕屯。
去卷積是分析中亟需的技術,但很難獲取到確切的結果拂苹。如果你只是想推斷兩種細胞的空間分布共定位關系安聘,建議用Mapping的方法即可。如果你是想推斷細胞間的通訊連接乃至機制瓢棒,建議使用project_cell_annotations
來在空間上可視化相應的模塊(program usage)浴韭。以下是細胞去卷積的步驟:
tangram首先需要知道voxel中包含多少細胞,這可以從對應的組織學圖片上通過分割得到脯宿,squidpy通過兩句代碼完成:squidpy.im.process
進行平滑處理念颈,squidpy.im.segment
使用watershed算法進行分割。需要關注的是有些空轉技術连霉,比如Slide-seq舍肠,不能在對應的經(jīng)測序的切片上進行染色。針對這些數(shù)據(jù)窘面,我們用粗略的方式估計細胞密度翠语,傳入uniform 到density_prior
。最后需要注意的是财边,我們很難去驗證去卷積的結果肌括,因為我們得不到真實的單細胞空間分布情況。
sq.im.process(img=img, layer="image", method="smooth")
sq.im.segment(
img=img,
layer="image_smooth",
method="watershed",
channel=0,
)
顯示框選小圖的細胞分割情況
比較DAPI染色和Mask的結果確認分割的質量酣难。
inset_y = 1500
inset_x = 1700
inset_sy = 400
inset_sx = 500
fig, axs = plt.subplots(1, 3, figsize=(30, 10))
sc.pl.spatial(
adata_st, color="cluster", alpha=0.7, frameon=False, show=False, ax=axs[0], title=""
)
axs[0].set_title("Clusters", fontdict={"fontsize": 20})
sf = adata_st.uns["spatial"]["V1_Adult_Mouse_Brain_Coronal_Section_2"]["scalefactors"][
"tissue_hires_scalef"
]
rect = mpl.patches.Rectangle(
(inset_y * sf, inset_x * sf),
width=inset_sx * sf,
height=inset_sy * sf,
ec="yellow",
lw=4,
fill=False,
)
axs[0].add_patch(rect)
axs[0].axes.xaxis.label.set_visible(False)
axs[0].axes.yaxis.label.set_visible(False)
axs[1].imshow(
img["image"][inset_y : inset_y + inset_sy, inset_x : inset_x + inset_sx, 0, 0]
/ 65536,
interpolation="none",
)
axs[1].grid(False)
axs[1].set_xticks([])
axs[1].set_yticks([])
axs[1].set_title("DAPI", fontdict={"fontsize": 20})
crop = img["segmented_watershed"][
inset_y : inset_y + inset_sy, inset_x : inset_x + inset_sx
].values.squeeze(-1)
crop = skimage.segmentation.relabel_sequential(crop)[0]
cmap = plt.cm.plasma
cmap.set_under(color="black")
axs[2].imshow(crop, interpolation="none", cmap=cmap, vmin=0.001)
axs[2].grid(False)
axs[2].set_xticks([])
axs[2].set_yticks([])
axs[2].set_title("Nucleous segmentation", fontdict={"fontsize": 20});
接著我們需要去提前圖片中分割成的細胞核單元的特征:每個spot下的分割單元(segmentation object)以及分割單元質心對應的坐標谍夭。
# define image layer to use for segmentation
features_kwargs = {
"segmentation": {
"label_layer": "segmented_watershed",
"props": ["label", "centroid"],
"channels": [1, 2],
}
}
# calculate segmentation features
sq.im.calculate_image_features(
adata_st,
img,
layer="image",
key_added="image_features",
features_kwargs=features_kwargs,
features="segmentation",
mask_circle=True,
)
計算及顯示每個spot下segmentation object的數(shù)量
adata_st.obs["cell_count"] = adata_st.obsm["image_features"]["segmentation_label"]
sc.pl.spatial(adata_st, color=["cluster", "cell_count"], frameon=False)
Deconvolution via alignment
tangram進行去卷積的核心原理是限制每個voxel中mapped單細胞表達譜的數(shù)量,這是與其他去卷積方法的不同之處憨募。
實現(xiàn)方式是在map_cells_to_space
函數(shù)中傳入map_cells_to_space
紧索,這將在損失函數(shù)中加入過濾步驟,并進行布爾正則化處理菜谣,接著在``target_count中傳入所有細胞核(segmentation object)的數(shù)量珠漂,
density_prior```來傳入每個voxel中的segmentation object晚缩。
ad_map = tg.map_cells_to_space(
adata_sc,
adata_st,
mode="constrained",
target_count=adata_st.obs.cell_count.sum(),
density_prior=np.array(adata_st.obs.cell_count) / adata_st.obs.cell_count.sum(),
num_epochs=1000,
# device="cuda:0",
device='cpu',
)
同前面一樣,可以顯示不同細胞類型的空間分布媳危。
tg.project_cell_annotations(ad_map, adata_st, annotation="cell_subclass")
annotation_list = list(pd.unique(adata_sc.obs['cell_subclass']))
tg.plot_cell_annotation_sc(adata_st, annotation_list, perc=0.02)
同樣可以通過檢測test基因判斷mapping的情況
ad_ge = tg.project_genes(adata_map=ad_map, adata_sc=adata_sc)
df_all_genes = tg.compare_spatial_geneexp(ad_ge, adata_st, adata_sc)
tg.plot_auc(df_all_genes);
##################################################
接著是核心部分:
在前面去卷積部分中我們的都每個spot中獨特segmentation object的數(shù)目以及質心坐標荞彼。我們在數(shù)據(jù)框中顯示這些關鍵信息,每一行代表segmentation object (a cell)待笑,表格顯示每個細胞的unique centroid ID鸣皂,包含spot ID和數(shù)字index。
tg.create_segment_cell_df(adata_st)
adata_st.uns["tangram_cell_segmentation"].head()
使用tangram.count_cell_annotation()
暮蹂,其利用去卷積的結果來對每個segmentation ID映射相應的細胞類型寞缝。
tg.count_cell_annotations(
ad_map,
adata_sc,
adata_st,
annotation="cell_subclass",
)
adata_st.obsm["tangram_ct_count"].head()
將結果提取成新的AnnData
對象
adata_segment = tg.deconvolve_cell_annotations(adata_st)
adata_segment.obs.head()
該AnnData對象不包含細胞數(shù)量,只包含細胞類型的注釋仰泻,即tangram映射的結果荆陆,這更方便進行可視化。在下圖中你會發(fā)現(xiàn)我纪,每個點并不代表spot慎宾,而是代表每個segmentation object丐吓。
fig, ax = plt.subplots(1, 1, figsize=(20, 20))
sc.pl.spatial(
adata_segment,
color="cluster",
size=0.4,
show=False,
frameon=False,
alpha_img=0.2,
legend_fontsize=20,
ax=ax,
)