前文提到Tangram在基于mapping的步驟,有兩種方式:
- mode = "cluster":速度快,占用內(nèi)存少,適用于空轉(zhuǎn)和單細(xì)胞數(shù)據(jù)來(lái)自不同樣本情況下。
- mode = "cell": 占用內(nèi)存大,每個(gè)cell進(jìn)行匹配,可以mapping到空轉(zhuǎn)沒(méi)有抓到的轉(zhuǎn)錄本。
由于受限于空間轉(zhuǎn)錄組(包括 Visium 和 Slide-seq技術(shù))的分辨度,每個(gè)voxel(spot)通常包含多個(gè)單細(xì)胞,針對(duì)這一情況,采用去卷積的方法去推斷細(xì)胞組成顯得尤為重要。
去卷積是分析中亟需的技術(shù),但很難獲取到確切的結(jié)果。如果你只是想推斷兩種細(xì)胞的空間分布共定位關(guān)系,建議用Mapping的方法即可。如果你是想推斷細(xì)胞間的通訊連接乃至機(jī)制,建議使用project_cell_annotations來(lái)在空間上可視化相應(yīng)的模塊(program usage)。以下是細(xì)胞去卷積的步驟:
tangram首先需要知道voxel中包含多少細(xì)胞,這可以從對(duì)應(yīng)的組織學(xué)圖片上通過(guò)分割得到,squidpy通過(guò)兩句代碼完成:squidpy.im.process進(jìn)行平滑處理,squidpy.im.segment使用watershed算法進(jìn)行分割。需要關(guān)注的是有些空轉(zhuǎn)技術(shù),比如Slide-seq,不能在對(duì)應(yīng)的經(jīng)測(cè)序的切片上進(jìn)行染色。針對(duì)這些數(shù)據(jù),我們用粗略的方式估計(jì)細(xì)胞密度,傳入uniform 到density_prior。最后需要注意的是,我們很難去驗(yàn)證去卷積的結(jié)果,因?yàn)槲覀兊貌坏秸鎸?shí)的單細(xì)胞空間分布情況。
sq.im.process(img=img, layer="image", method="smooth")
sq.im.segment(
img=img,
layer="image_smooth",
method="watershed",
channel=0,
)
顯示框選小圖的細(xì)胞分割情況
比較DAPI染色和Mask的結(jié)果確認(rèn)分割的質(zhì)量。
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});
接著我們需要去提前圖片中分割成的細(xì)胞核單元的特征:每個(gè)spot下的分割單元(segmentation object)以及分割單元質(zhì)心對(duì)應(yīng)的坐標(biāo)。
# 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,
)
計(jì)算及顯示每個(gè)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進(jìn)行去卷積的核心原理是限制每個(gè)voxel中mapped單細(xì)胞表達(dá)譜的數(shù)量,這是與其他去卷積方法的不同之處。
實(shí)現(xiàn)方式是在map_cells_to_space函數(shù)中傳入map_cells_to_space,這將在損失函數(shù)中加入過(guò)濾步驟,并進(jìn)行布爾正則化處理,接著在``target_count中傳入所有細(xì)胞核(segmentation object)的數(shù)量,density_prior```來(lái)傳入每個(gè)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',
)
同前面一樣,可以顯示不同細(xì)胞類型的空間分布。
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)

同樣可以通過(guò)檢測(cè)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);

##################################################
接著是核心部分:
在前面去卷積部分中我們的都每個(gè)spot中獨(dú)特segmentation object的數(shù)目以及質(zhì)心坐標(biāo)。我們?cè)跀?shù)據(jù)框中顯示這些關(guān)鍵信息,每一行代表segmentation object (a cell),表格顯示每個(gè)細(xì)胞的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(),其利用去卷積的結(jié)果來(lái)對(duì)每個(gè)segmentation ID映射相應(yīng)的細(xì)胞類型。
tg.count_cell_annotations(
ad_map,
adata_sc,
adata_st,
annotation="cell_subclass",
)
adata_st.obsm["tangram_ct_count"].head()
將結(jié)果提取成新的AnnData對(duì)象
adata_segment = tg.deconvolve_cell_annotations(adata_st)
adata_segment.obs.head()
該AnnData對(duì)象不包含細(xì)胞數(shù)量,只包含細(xì)胞類型的注釋,即tangram映射的結(jié)果,這更方便進(jìn)行可視化。在下圖中你會(huì)發(fā)現(xiàn),每個(gè)點(diǎn)并不代表spot,而是代表每個(gè)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,
)
