GSE270583单细胞实战palantir拟时序分析

发布时间:2026/7/31 22:52:39
GSE270583单细胞实战palantir拟时序分析 写在前面本教程可视化集锦单细胞发育轨迹分析Pseudotime Analysis是揭开细胞分化、状态转换及发育命运谜团的核心技术。传统的分析往往通过聚类识别离散的细胞群但难以捕捉从祖细胞到成熟谱系之间连续的转录组流形演变。Palantir算法的出现为解析这种复杂的发育动态提供了系统性的视角——它不再局限于线性的轨迹推导而是通过构建扩散映射Diffusion Maps和多尺度空间模拟细胞在分化潜能Entropy驱动下的随机游走。Palantir 能够精准计算细胞在发育轴上的拟时间Pseudotime位置并量化其向不同终端谱系分化的概率从而将零散的单细胞快照串联成一部完整的“发育电影”识别驱动谱系特异性分化的核心枢纽基因。本文主要涵盖以下十大核心实操模块① 数据读入与初始化演示已注释 AnnData 对象的标准化加载并完成 Counts 层备份以保障原始表达数据在后续插补中的溯源② 高变基因筛选与降维通过高度变异基因HVGs的精准筛选与 PCA 降维剔除背景噪声聚焦核心生物学差异信号③ 扩散映射Diffusion Map构建深度解析 Palantir 算法基石构建扩散映射空间以捕捉单细胞数据中复杂的非线性发育流形④ 多尺度空间Multiscale Space确定基于扩散组件进一步精炼特征空间增强流形结构的鲁棒性为拟时间计算奠定数学基础⑤ 聚类与 UMAP 可视化结合邻近图构建与 Leiden 聚类在二维 UMAP 空间中直观勾勒前体细胞与各分化分支的分布轮廓⑥ MAGIC 算法表达量插补利用马尔可夫亲和矩阵修复单细胞测序中的数据缺失Dropout实现基因表达水平的流形平滑处理⑦ 拟时间核心推断指定分化起点Root Cell与多个谱系终点运行 Palantir 计算拟时间进度、分化熵及命运决策概率⑧ 分支细胞提取与谱系分析根据分化概率矩阵精准识别属于特定分支的细胞子集并可视化各路径内部的发育流向⑨ 基因动态表达趋势拟合拟合关键 Marker 基因随拟时间轴演变的表达曲线及趋势热图识别驱动各谱系分化的特征基因⑩ 功能富集与多维统计展示联合 GSEA/GO 分析揭示分支背后的生物学功能转变并通过拟时间四分位占比统计实现对发育全景的深度解析。更多教程可见学前tips:本教程基于Linux环境的jupyter演示计算资源不足的同学可参考足够支持你完成硕博生涯的生信环境忘记宣传了独享用户连技术支持都是独享的RTX5090、4080S、5070显卡上机访问链接https://biomamba.xiyoucloud.net/欢迎滴滴[Biomamba_zhushou]获取帮助如果需要单细胞数据分析教学、生信热点全文复现、生信个性化分析、常态化实验学习欢迎滴滴[Biomamba_zhushou]获取帮助。实操流程资料下载见文末如果你对下面的教程比较迷茫那么你可以先行学习编程教程十小时学会Linux生信Linux及服务器使用技巧生信Python速查手册1.环境导入与依赖准备importosimportscanpyasscimportpalantirimportpandasaspdimportnumpyasnpimportmatplotlib.pyplotaspltimportseabornassns2.数据预处理# 载入已注释的单细胞数据 adata sc.read(/data1/home/zouhuren/data/GSE270583_adata_annotated_v2.h5ad) adataAnnData object with n_obs × n_vars 19528 × 23055obs: sample, n\_genes\_by\_counts, log1p\_n\_genes\_by\_counts, total\_counts, log1p\_total\_counts, pct\_counts\_in\_top\_50\_genes, pct\_counts\_in\_top\_100\_genes, pct\_counts\_in\_top\_200\_genes, pct\_counts\_in\_top\_500\_genes, total\_counts\_mt, log1p\_total\_counts\_mt, pct\_counts\_mt, total\_counts\_ribo, log1p\_total\_counts\_ribo, pct\_counts\_ribo, total\_counts\_hb, log1p\_total\_counts\_hb, pct\_counts\_hb, leiden, cell\_type var: mt, ribo, hb, n\_cells\_by\_counts, mean\_counts, log1p\_mean\_counts, pct\_dropout\_by\_counts, total\_counts, log1p\_total\_counts, n\_cells, highly\_variable, means, dispersions, dispersions\_norm, highly\_variable\_nbatches, highly\_variable\_intersection uns: cell\_type\_colors, hvg, leiden, leiden\_colors, log1p, neighbors, pca, rank\_genes\_groups, sample\_colors, tsne, umap obsm: X\_pca, X\_pca\_harmony, X\_tsne, X\_umap varm: PCs layers: counts obsp: connectivities, distances# 备份原始 counts后续 MAGIC 插补和趋势分析需调用原始计数信息 adata.layers[counts] adata.X.copy() # 筛选 1500 个高变基因捕捉细胞间的主要差异信号 sc.pp.highly_variable_genes(adata, n_top_genes1500) # 执行 PCA 降维为构建发育流形提供基础特征空间 sc.pp.pca(adata)3. 构建扩散映射与多尺度空间# 计算扩散映射 (Diffusion Map)Palantir 捕捉非线性发育流形的核心步骤 dm_res palantir.utils.run_diffusion_maps(adata, n_components5) # 确定多尺度空间基于扩散组件消除噪声增强流形结构的稳定性 ms_data palantir.utils.determine_multiscale_space(adata)4. 聚类与 UMAP 可视化sc.pp.neighbors(adata) # Leiden 聚类初步划分细胞群体设置分辨率为 0.2 sc.tl.leiden(adata, resolution0.2) sc.tl.umap(adata) # UMAP 展示检查细胞类型在降维空间中的分布情况 sc.pl.umap(adata, colorcell_type, frameonFalse)5. MAGIC 插补填补数据缺失# MAGIC 插补填补 Dropout 数据缺失使基因表达在流形上更加连续、平滑 imputed_X palantir.utils.run_magic_imputation(adata) # 基因表达可视化展示插补后关键 Marker 基因的表达丰度 genes_to_plot [Alb, Lyz2, S100a8, Col1a1] sc.pl.embedding( adata, basisumap, layerMAGIC_imputed_data, colorgenes_to_plot, frameonFalse) plt.show()6. Palantir 核心轨迹推断6.1 起始与终点细胞选择# 查看 Monocytes 的细胞数量adata.obs[cell_type].value_counts()[Monocytes]# 查看 Monocytes 的前几个细胞 barcodemonocytes_barcodesadata.obs_names[adata.obs[cell_type]Monocytes]print(monocytes_barcodes[:10])# 打印前10个# 随机选择一个作为 Palantir 起点importrandom start_cellrandom.choice(monocytes_barcodes)print(Monocytes 起点细胞 barcode:,start_cell)Index([‘AAACGAACATAAGATG-1’, ‘AAACGAAGTGAGCGAT-1’, ‘AAACGAATCAACACCA-1’,AAAGAACGTTGCCGCA-1, AAAGAACTCGCAATGT-1, AAAGGATTCGAAGCCC-1, AAAGGATTCGACGCGT-1, AAAGGGCCATGACAGG-1, AAAGGTATCCATCGTC-1, AAAGTGATCCCGTAAA-1\], dtypeobject)Monocytes 起点细胞 barcode: CTGCAGGGTTCCGCTT-1# Kupffer cells 终点kupffer_cellsadata.obs_names[adata.obs[cell_type]Kupffer cells]print(kupffer_cells[:10])# 打印前10个# cDC 终点cdc_cellsadata.obs_names[adata.obs[cell_type].str.contains(cDC)]print(cdc_cells[:10])# LSECs 终点lsecs_cellsadata.obs_names[adata.obs[cell_type]LSECs]print(lsecs_cells[:10])# Hepatocytes 终点hepatocytes_cellsadata.obs_names[adata.obs[cell_type]Hepatocytes]print(hepatocytes_cells[:10])Index([‘AAACGAACACGTCATA-1’, ‘AAACGCTGTTAAGGAT-1’, ‘AAACGCTTCCTGGGAC-1’,AAAGAACCATTGAAAG-1, AAAGAACTCGCCTCTA-1, AAAGGATAGGTGGCTA-1, AAAGGATCATGCGGTC-1, AAAGGATTCATCGACA-1, AAAGGGCAGTCACACT-1, AAAGGGCGTAATTAGG-1\], dtypeobject)Index([‘AAACCCACAGCACGAA-1’, ‘AAACGAACAGCTGAAG-1’, ‘AAACGCTGTTCAACGT-1’,AAAGAACAGAGCCGAT-1, AAAGAACCAGAAGCTG-1, AAAGGATGTTTAAGGA-1, AAAGGTAAGGATGCGT-1, AAAGTCCCACTTACAG-1, AAATGGACAATCGCAT-1, AAATGGACAATCTCTT-1\], dtypeobject)Index([‘AAACGAAAGAACCGCA-1’, ‘AAACGAACACACCAGC-1’, ‘AAACGAATCGTAACCA-1’,AAACGCTAGTTAACGA-1, AAAGAACCAACGATTC-1, AAAGAACTCTCGGTAA-1, AAAGGTAGTAGTGTGG-1, AAAGTGAGTTGTGGAG-1, AACAAAGAGTGCACTT-1, AACAACCAGTGTCATC-1\], dtypeobject)Index([‘AAACCCAAGGACGCAT-1’, ‘AAAGGATTCTGGCCTT-1’, ‘AAAGTCCCAGACCGCT-1’,AAAGTGATCGCTTGCT-1, AACGTCACAACTCCCT-1, AAGACAAAGTTGTCGT-1, AAGCGAGAGCGATGCA-1, AAGTCGTAGACTTCGT-1, AATGACCTCTCTGCTG-1, AATGGAACATAATGAG-1\], dtypeobject)# 起点细胞Monocytes被认为是 Kupffer cells 和树突状细胞的前体 start_cell GTCCTCACAGCACAGA-1 # 替换成你数据里 Monocytes 中任意代表性细胞的 barcode # 终点细胞成熟细胞类型的代表性 barcode terminal_states pd.Series( [Kupffer cells, cDC, LSECs, Hepatocytes], index[ AAACGAACACGTCATA-1, # 替换为实际 barcode AAACCCACAGCACGAA-1, # 替换为实际 barcode AAACGAAAGAACCGCA-1, # 替换为实际 barcode AAACCCAAGGACGCAT-1 # 替换为实际 barcode ] )# 检查起点start_cellGTCCTCACAGCACAGA-1print(start_cellinadata.obs_names)# True 才行# 检查终点forcellinterminal_states.index: print(cell, cellinadata.obs_names)# 都应该是 TrueTrueAAACGAACACGTCATA-1 TrueAAACCCACAGCACGAA-1 TrueAAACGAAAGAACCGCA-1 TrueAAACCCAAGGACGCAT-1 True6.2 运行palantir核心函数# 运行 Palantir计算拟时间 (Pseudotime)、分化熵 (Entropy) 及分化概率 pr_res palantir.core.run_palantir( adata, start_cell, num_waypoints500, terminal_statesterminal_states, eigvec_keyDM_EigenVectors_multiscaled)Sampling and flocking waypoints…Time for determining waypoints: 0.016318086783091226 minutesDetermining pseudotime…Shortest path distances using 30-nearest neighbor graph…Time for shortest paths: 0.23866192897160848 minutesIteratively refining the pseudotime…Correlation at iteration 1: 1.0000Entropy and branch probabilities…Markov chain construction…Computing fundamental matrix and absorption probabilities…Project results to all cells…6.3 可视化拟时间和分化概率# 展示拟时间进度图、命运不确定性(熵)图以及各谱系分化概率图palantir.plot.plot_palantir_results(adata,s1,embedding_basisX_umap,# UMAP 展示pseudo_time_keypalantir_pseudotime,# 拟时间颜色渐变 → 细胞从干细胞到成熟细胞的轨迹entropy_keypalantir_entropy,# 分化熵Entropy命运不确定性高熵细胞通常在干细胞或早期祖细胞区域低熵细胞通常在成熟分支末端fate_prob_keypalantir_fate_probabilities# 分化概率矩阵颜色表示该细胞向该终点分化的概率)plt.show()7. 分支提取与动态演变# 选择分支细胞 masks palantir.presults.select_branch_cells(adata, q.01, eps.01)# 假设 masks 的列顺序与 terminal_states 中终点顺序一致 branch_Kupffer_cells adata.obs_names[masks[:, 0]] branch_cDC_cells adata.obs_names[masks[:, 1]] # cDC1 cDC2 branch_LSECs_cells adata.obs_names[masks[:, 2]] branch_Hepatocytes_cells adata.obs_names[masks[:, 3]]# 可视化单分支示例 for branch_name,cells inzip([Kupffer cells,cDC,LSECs,Hepatocytes],[branch_Kupffer_cells,branch_cDC_cells,branch_LSECs_cells,branch_Hepatocytes_cells]):ad_branch adata[cells,:].copy()sc.pl.umap(ad_branch,color[total_counts,palantir_pseudotime],# 用 total_counts 替代 n_counts frameonFalse,cmapviridis,titlebranch_name)8. 基因表达趋势拟合# 计算基因随拟时间变化 gene_trends palantir.presults.compute_gene_trends( adata, expression_keyMAGIC_imputed_data )Hepatocytes[2026-01-08 19:14:50,006] [INFO ] Using sparse Gaussian Process since n_landmarks (500) n_samples (8,666) and rank 1.0.[2026-01-08 19:14:50,008] [INFO ] Using covariance function Matern52(ls1.0).[2026-01-08 19:14:52,690] [INFO ] Sigma interpreted as element-wise standard deviation.cDC[2026-01-08 19:14:55,105] [INFO ] Using sparse Gaussian Process since n_landmarks (500) n_samples (4,440) and rank 1.0.[2026-01-08 19:14:55,106] [INFO ] Using covariance function Matern52(ls1.0).[2026-01-08 19:14:56,225] [INFO ] Sigma interpreted as element-wise standard deviation.LSECs[2026-01-08 19:14:59,494] [INFO ] Using sparse Gaussian Process since n_landmarks (500) n_samples (10,790) and rank 1.0.[2026-01-08 19:14:59,495] [INFO ] Using covariance function Matern52(ls1.0).[2026-01-08 19:15:00,696] [INFO ] Sigma interpreted as element-wise standard deviation.Kupffer cells[2026-01-08 19:15:02,903] [INFO ] Using sparse Gaussian Process since n_landmarks (500) n_samples (2,557) and rank 1.0.[2026-01-08 19:15:02,904] [INFO ] Using covariance function Matern52(ls1.0).[2026-01-08 19:15:03,973] [INFO ] Sigma interpreted as element-wise standard deviation.# 绘制趋势 # Alb → 肝细胞特异性标记 # Lyz2 → 单核/巨噬细胞标记 # S100a8 → 中性粒细胞标记 # Col1a1 → 纤维细胞/星状细胞标记 genes_of_interest [Alb, Lyz2, S100a8, Col1a1] palantir.plot.plot_gene_trends(adata, genes_of_interest) palantir.plot.plot_gene_trend_heatmaps(adata, genes_of_interest) plt.show()# 单分支基因趋势聚类 top_genes adata.var_names[:200] branch_list [Hepatocytes, Kupffer cells, cDC, LSECs] branch_communities {} for branch in branch_list: communities palantir.presults.cluster_gene_trends(adata, branch, top_genes) branch_communities[branch] communities palantir.plot.plot_gene_trend_clusters(adata, branch) plt.show()9.富集分析GSEA/GO/KEGGimportgseapyasgpfromgseapy.plotimportdotplot# 针对每个分支 cluster 1 基因做富集分析go_bpgp.get_library(nameGO_Biological_Process_2025,organismMouse)forbranchinbranch_list:communitiesbranch_communities[branch]cluster_genescommunities[communities1].index.tolist()iflen(cluster_genes)0:print(f{branch}分支 cluster 1 没有基因跳过富集分析)continueenrgp.enrichr(gene_listcluster_genes,gene_setsgo_bp,organismHuman)print(f{branch}分支 cluster 1 富集 top5:)display(enr.results.head(5))Hepatocytes 分支 cluster1富集 top5:Kupffer cells 分支 cluster1富集 top5:cDC 分支 cluster1富集 top5:LSECs 分支 cluster1富集 top5:# 分支列表branch_list[Hepatocytes,Kupffer cells,cDC,LSECs]# GO librarygo_bpgp.get_library(nameGO_Biological_Process_2025,organismMouse)forbranchinbranch_list:# cluster 1 的基因communitiesbranch_communities[branch]cluster_genescommunities[communities1].index.tolist()iflen(cluster_genes)0:print(f{branch}分支 cluster 1 没有基因跳过富集分析)continue# 富集分析enrgp.enrichr(gene_listcluster_genes,gene_setsgo_bp,organismHuman)ifenr.res2d.empty:print(f{branch}分支 cluster 1 富集无结果跳过)continue# 新增 -log10(FDR) 列可以作为颜色参考enr.res2d[log10_FDR]-np.log10(enr.res2d[Adjusted P-value]1e-300)# -------------------- 绘图 --------------------# dotplot 返回 matplotlib Axesaxdotplot(enr.res2d,titlef{branch}GO Biological Process,cmapviridis_r,size12,# 放大圆圈figsize(6,8),# 图大小cutoff0.25,# 富集比例阈值fontsize9,# 坐标轴字体showFalse,legend_titleSignificance (-log10 FDR)# 图例标题)# 调整坐标轴字体大小ax.tick_params(axisy,labelsize9)ax.tick_params(axisx,labelsize9)# 调整图例标题部分 gseapy 版本需要这样设置ifax.get_legend()isnotNone:ax.get_legend().set_title(Significance (-log10 FDR))# 自动调整布局plt.tight_layout()plt.show()10. Scanpy联合分析10.1 UMAP 可视化 pseudotime 和熵fig,axesplt.subplots(1,3,figsize(18,5))# 1️⃣ cell_type有 legendsc.pl.umap(adata,colorcell_type,axaxes[0],legend_locright margin,# 关键showFalse)axes[0].set_title(Cell type)# 2️⃣ pseudotime无 legendsc.pl.umap(adata,colorpalantir_pseudotime,axaxes[1],showFalse)axes[1].set_title(Palantir pseudotime)# 3️⃣ entropy无 legendsc.pl.umap(adata,colorpalantir_entropy,axaxes[2],showFalse)axes[2].set_title(Palantir entropy)plt.tight_layout()plt.show()10.2 小提琴图fig,axesplt.subplots(1,2,figsize(14,4))# 左pseudotimesc.pl.violin(adata,keyspalantir_pseudotime,groupbycell_type,rotation90,axaxes[0],showFalse)axes[0].set_title(Palantir pseudotime)# 右entropysc.pl.violin(adata,keyspalantir_entropy,groupbycell_type,rotation90,axaxes[1],showFalse)axes[1].set_title(Palantir entropy)plt.tight_layout()plt.show()10.3 pseudotime 四等分与堆积图adata.obs[pseudotime_quartile]pd.qcut(adata.obs[palantir_pseudotime],4,labels[Q1,Q2,Q3,Q4])# 饼图quartile_countsadata.obs[pseudotime_quartile].value_counts().sort_index()plt.figure(figsize(6,6))plt.pie(quartile_counts,labelsquartile_counts.index,autopct%.1f%%,startangle90)plt.title(Pseudotime Quartiles)plt.show()# 堆积柱形图按 cell_type 显示 pseudotime 四分位占比 cellnum pd.crosstab(adata.obs[cell_type], adata.obs[pseudotime_quartile]) cellprop cellnum.div(cellnum.sum(axis0), axis1).T.reset_index() cellprop.plot(kindbar, stackedTrue, figsize(8,5)) plt.title(Celltype Proportion per Pseudotime Quartile) plt.ylabel(Proportion) plt.xticks(rotation45) plt.legend(bbox_to_anchor(1,1)) plt.tight_layout() plt.show()10.4 密度分布plt.figure(figsize(8,5))axsns.kdeplot(dataadata.obs,xpalantir_pseudotime,huecell_type,fillTrue,alpha0.3)# 把 legend 移到图外右侧sns.move_legend(ax,center left,bbox_to_anchor(1.02,0.5),frameonFalse)plt.tight_layout()plt.show()# 保存到当前目录文件名自定义adata.write(adata_palantir_full_analysis.h5ad)print(已保存为 adata_pseudotime.h5ad)已保存为 adata_pseudotime.h5ad资料领取一切不给测试文件和分析环境版本的教程都是耍流氓本推送的代码和测试文件可以在以下链接中下载通过网盘文件分享的文件https://pan.baidu.com/s/1X9xklW5hNCBT0m2EKouzwA提取码请滴滴我们领取欢迎致谢如果以上内容对你有帮助欢迎在文章的Acknowledgement中加上这一段联系客服微信可以发放奖励Since Biomamba and his wechat public account team produce bioinformatics tutorials and share code with annotation, we thank Biomambafortheir guidanceinbioinformatics and data analysisforthe current study.欢迎在发文/毕业时向我们分享你的喜悦~已致谢文章鼻咽癌的Bulk RNA-Seq与scRNA-Seq联合分析13分文章利用scRNA-Seq揭示地铁细颗粒物引起肺部炎症的分子机制除了铁死亡还有铜死亡IF14.3| scRNA-seq脂质组多组学分析揭示宫内生长受限导致肝损伤的性别差异银屑病和脂肪肝病中共同病理和免疫特征《Advanced Science》新型Arf1抑制剂促进癌症干细胞衰老并增强抗肿瘤免疫scRNA-seq揭示脓毒症预后水平预测的关键靶点机器学习生信多组学联合构建牙周炎线粒体功能障碍与免疫微环境关联网络KIF18A在肝细胞癌转移中的双重角色bulkscRNA-seq挖掘BCL2-MAPK14-TXN氧化应激诊断模型鉴定脓毒症中氧化应激关键基因致谢文章1中科院1区scRNA-seq揭示麻黄-甘草配对治疗呼吸系统症状和多(I:C)诱发肺炎模型机制欢迎大家致谢~