single2spatial-spatial-mapping
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseSingle2Spatial spatial mapping
Single2Spatial空间映射
Overview
概述
Apply this skill when converting single-cell references into spatially resolved profiles. It follows , demonstrating how Single2Spatial trains on PDAC scRNA-seq and Visium data, reconstructs spot-level proportions, and visualises marker expression.
t_single2spatial.ipynb当你需要将单细胞参考数据转换为空间解析图谱时,可以使用该技能。本内容遵循教程,演示了Single2Spatial如何在PDAC scRNA-seq和Visium数据上进行训练、重建斑点水平的细胞比例并可视化标记基因表达。
t_single2spatial.ipynbInstructions
操作步骤
- Import dependencies and style
- Load ,
omicverse as ov,scanpy as sc,anndata,pandas as pd, andnumpy as np.matplotlib.pyplot as plt - Call (or
ov.utils.ov_plot_set()in older versions) to align plots with omicverse styling.ov.plot_set()
- Load
- Load single-cell and spatial datasets
- Read processed matrices with then create AnnData objects (
pd.read_csv(...)).anndata.AnnData(raw_df.T) - Attach metadata: and
single_data.obs = pd.read_csv(...)[['Cell_type']]containing coordinates and slide metadata.spatial_data.obs = pd.read_csv(... )
- Read processed matrices with
- Initialise Single2Spatial
- Instantiate .
ov.bulk2single.Single2Spatial(single_data=single_data, spatial_data=spatial_data, celltype_key='Cell_type', spot_key=['xcoord','ycoord'], gpu=0) - Note that inputs should be normalised/log-scaled scRNA-seq matrices; ensure matches spatial coordinate columns.
spot_key
- Instantiate
- Train the deep-forest model
- Execute to fit the mapper and generate reconstructed spatial AnnData (
st_model.train(spot_num=500, cell_num=10, df_save_dir='...', df_save_name='pdac_df', k=10, num_epochs=1000, batch_size=1000, predicted_size=32)).sp_adata - Explain that defines sampled pseudo-spots per iteration and
spot_numcontrols per-spot cell draws.cell_num
- Execute
- Load pretrained weights
- Use when checkpoints already exist to skip training.
st_model.load(modelsize=14478, df_load_dir='.../pdac_df.pth', k=10, predicted_size=32)
- Use
- Assess spot-level outputs
- Call to compute aggregated spot AnnData (
st_model.spot_assess()) for QC.sp_adata_spot - Plot marker genes with .
sc.pl.embedding(sp_adata, basis='X_spatial', color=['REG1A', 'CLDN1', ...], frameon=False, ncols=4)
- Call
- Visualise proportions and cell-type maps
- Use to highlight per-spot cell fractions.
sc.pl.embedding(sp_adata_spot, basis='X_spatial', color=['Acinar cells', ...], frameon=False) - Plot coloured by
sp_adatawithCell_typeto show reconstructed assignments.palette=ov.utils.ov_palette()[11:]
- Use
- Export results
- Encourage saving generated AnnData objects (,
sp_adata.write_h5ad(...)) and derived CSV summaries for downstream reporting.sp_adata_spot.write_h5ad(...)
- Encourage saving generated AnnData objects (
- Troubleshooting tips
- If training diverges, reduce via keyword arguments or decrease
learning_rateto stabilise the forest.predicted_size - Ensure scRNA-seq inputs are log-normalised; raw counts can lead to scale mismatches and poor spatial predictions.
- Verify GPU availability when is non-zero; fallback to CPU by omitting the argument or setting
gpu.gpu=-1
- If training diverges, reduce
- 导入依赖项和样式
- 导入、
omicverse as ov、scanpy as sc、anndata、pandas as pd和numpy as np。matplotlib.pyplot as plt - 调用(旧版本中为
ov.utils.ov_plot_set())使绘图样式与omicverse保持一致。ov.plot_set()
- 导入
- 加载单细胞和空间数据集
- 使用读取处理后的矩阵,然后创建AnnData对象(
pd.read_csv(...))。anndata.AnnData(raw_df.T) - 附加元数据:,
single_data.obs = pd.read_csv(...)[['Cell_type']]需包含坐标和切片元数据。spatial_data.obs = pd.read_csv(... )
- 使用
- 初始化Single2Spatial
- 实例化。
ov.bulk2single.Single2Spatial(single_data=single_data, spatial_data=spatial_data, celltype_key='Cell_type', spot_key=['xcoord','ycoord'], gpu=0) - 注意:输入应为归一化/对数转换后的scRNA-seq矩阵;确保与空间坐标列匹配。
spot_key
- 实例化
- 训练深度森林模型
- 执行来拟合映射器并生成重建后的空间AnnData(
st_model.train(spot_num=500, cell_num=10, df_save_dir='...', df_save_name='pdac_df', k=10, num_epochs=1000, batch_size=1000, predicted_size=32))。sp_adata - 说明:定义每次迭代中采样的伪斑点数量,
spot_num控制每个斑点抽取的细胞数。cell_num
- 执行
- 加载预训练权重
- 当检查点已存在时,使用跳过训练步骤。
st_model.load(modelsize=14478, df_load_dir='.../pdac_df.pth', k=10, predicted_size=32)
- 当检查点已存在时,使用
- 评估斑点水平输出结果
- 调用计算聚合后的斑点AnnData(
st_model.spot_assess())以进行质量控制。sp_adata_spot - 使用绘制标记基因。
sc.pl.embedding(sp_adata, basis='X_spatial', color=['REG1A', 'CLDN1', ...], frameon=False, ncols=4)
- 调用
- 可视化细胞比例和细胞类型图谱
- 使用突出显示每个斑点的细胞比例。
sc.pl.embedding(sp_adata_spot, basis='X_spatial', color=['Acinar cells', ...], frameon=False) - 使用绘制按
palette=ov.utils.ov_palette()[11:]着色的Cell_type,展示重建后的细胞分配结果。sp_adata
- 使用
- 导出结果
- 建议保存生成的AnnData对象(、
sp_adata.write_h5ad(...))和衍生的CSV汇总文件,用于后续分析报告。sp_adata_spot.write_h5ad(...)
- 建议保存生成的AnnData对象(
- 故障排除提示
- 如果训练过程出现异常,可通过关键字参数降低,或减小
learning_rate来稳定模型。predicted_size - 确保scRNA-seq输入数据经过对数归一化;原始计数会导致尺度不匹配,进而影响空间预测效果。
- 当参数非0时,需验证GPU是否可用;可通过省略该参数或设置
gpu回退到CPU运行。gpu=-1
- 如果训练过程出现异常,可通过关键字参数降低
Examples
示例
- "Train Single2Spatial on PDAC scRNA-seq and Visium slides, then visualise REG1A and CLDN1 spatial expression."
- "Load a saved Single2Spatial checkpoint to regenerate spot-level cell-type proportions for reporting."
- "Plot reconstructed cell-type maps with omicverse palettes to compare against histology."
- "在PDAC scRNA-seq和Visium切片上训练Single2Spatial,然后可视化REG1A和CLDN1的空间表达。"
- "加载已保存的Single2Spatial检查点,重新生成斑点水平的细胞类型比例用于报告。"
- "使用omicverse调色板绘制重建后的细胞类型图谱,与组织学图像进行对比。"
References
参考资料
- Tutorial notebook:
t_single2spatial.ipynb - Example datasets and models:
omicverse_guide/docs/Tutorials-bulk2single/data/pdac/ - Quick copy/paste commands:
reference.md
- 教程笔记本:
t_single2spatial.ipynb - 示例数据集和模型:
omicverse_guide/docs/Tutorials-bulk2single/data/pdac/ - 快速复制命令:
reference.md