Cell segmentation demo
%time
exec('import warnings; warnings.filterwarnings("ignore")')
import matplotlib.pyplot as plt
import spateo as st
import numpy as np
import skimage
import sklearn
st.config.n_threads = 8
圈细胞分析需要准备的数据有空间 UMI 计数矩阵和ssDNA染色图像,测试数据可以从数据库网站MOSTA: Mouse Organogenesis Spatiotemporal Transcriptomic Atlas 下载使用。
Load data
### 加载 UMI 计数矩阵和细胞核染色图像到 AnnData 对象
adata = st.io.read_bgi_agg("./E12.5_E1S3_GEM_bin1.tsv.gz", "./E12.5_E1S3.tif")
print(adata.layers["stain"].shape)
adata
|-----> Constructing count matrices.
|-----> <insert> __type to uns in AnnData Object.
|-----> <insert> pp to uns in AnnData Object.
|-----> <insert> spatial to uns in AnnData Object.
(22834, 17384)
AnnData object with n_obs × n_vars = 22834 × 17384
uns: '__type', 'pp', 'spatial'
layers: 'stain'
显示细胞核染色图像
st.pl.imshow(adata, 'stain')
stain segmentation
Watershed-based approach to segment and label nuclei from the staining image.
%time
st.cs.mask_nuclei_from_stain(adata) ### 创建染色图像细胞核蒙版
st.pl.imshow(adata, 'stain_mask')
plt.savefig("stain_mask.png",dpi=600)
plt.close()
st.cs.find_peaks_from_mask(adata, 'stain', 7)
st.cs.watershed(adata, 'stain', 5, out_layer='watershed_labels')
fig, ax = st.pl.imshow(adata, 'stain', save_show_or_return='return')
st.pl.imshow(adata, 'watershed_labels', labels=True, alpha=0.5, ax=ax)
plt.savefig("waterfall.png",dpi=600)
plt.close()
Deep learning-based approach
%time
st.cs.stardist(adata, tilesize=2000, equalize=2.0, out_layer='stardist_labels')
fig, ax = st.pl.imshow(adata, 'stain', save_show_or_return='return')
st.pl.imshow(adata, 'stardist_labels', labels=True, alpha=0.5, ax=ax)
plt.savefig("fig5_stardist.png",dpi=600)
plt.close()
Augment labels
st.cs.augment_labels(adata, 'watershed_labels', 'stardist_labels', out_layer='augmented_labels')
fig, ax = st.pl.imshow(adata, 'stain', save_show_or_return='return')
st.pl.imshow(adata, 'augmented_labels', labels=True, alpha=0.5, ax=ax)
plt.savefig("Argmented_labels.png",dpi=600)
plt.close()
Expand labels to cytoplasm
st.cs.mask_cells_from_stain(adata, out_layer='stain_cell_mask')
st.cs.watershed(adata,'stain',mask_layer='stain_cell_mask', markers_layer='augmented_labels',out_layer='cell_labels')
fig, ax = st.pl.imshow(adata, 'stain', save_show_or_return='return')
st.pl.imshow(adata, 'cell_labels', labels=True, alpha=0.5, ax=ax)
st.cs.expand_labels(adata, 'augmented_labels', distance=5, max_area=400)
st.cs.expand_labels(adata, 'cell_labels', distance=2, out_layer='cell_labels_expanded')
fig, ax = st.pl.imshow(adata, 'stain', save_show_or_return='return')
st.pl.imshow(adata, 'cell_labels_expanded', labels=True, alpha=0.5, ax=ax)
Obtain a cell x gene matrix
cell_adata = st.io.read_bgi("./E12.5_E1S3_GEM_bin1.tsv.gz",segmentation_adata=adata, labels_layer='cell_labels_expanded',)
cell_adata
|-----> <select> cell_labels_expanded layer in AnnData Object
|-----> Using labels provided with `segmentation_adata` and `labels_layer` arguments.
|-----> Constructing count matrices.
|-----> <insert> __type to uns in AnnData Object.
|-----> <insert> pp to uns in AnnData Object.
|-----> <insert> spatial to uns in AnnData Object.
AnnData object with n_obs × n_vars = 70475 × 26274
obs: 'area'
uns: '__type', 'pp', 'spatial'
obsm: 'spatial', 'contour', 'bbox'
layers: 'spliced', 'unspliced'
import joblib
joblib.dump(cell_adata,"cell_adata_segmentation.pkl")
圈细胞后获得的表达矩阵就可以进行后续的降维聚类差异分析…参考这篇文章spateo 数据分析demo进行数据分析。