使用 MetaTiME 的细胞类型自动注释#
MetaTiME 学习数据驱动的, interpretable, and reproducible gene programs by integrating millions of single cells from hundreds of tumor scRNA-seq data. The idea is to learn a map of single-cell space with biologically meaningful directions from large-scale data, which helps understand functional cell states and transfers knowledge to new data analysis. MetaTiME provides pretrained meta-components (MeCs) to automatically annotate fine-grained cell states and plot signature continuum for new single-cells of tumor microenvironment.
在这里,我们将 MetaTiME 集成到 omicverse 中. This tutorial demonstrates how to use MetaTiME (original code) to annotate celltype in TME
Colab 可重现性:https://colab.research.google.com/drive/1isvjTfSFM2cy6GzHWAwbuvSjveEJijzP?usp=sharing

import omicverse as ov
ov.utils.ov_plot_set()
/mnt/data/env/pyomic/lib/python3.8/site-packages/phate/__init__.py
数据标准化和批次纠正#
样本数据有多个患者 , 我们可以对患者进行批次纠正. 在这里,我们使用 scVI 来移除批次效应.
注意
如果您的数据包含计数矩阵, 我们提供一个包装函数 for pre-processing the data. 否则,如果数据已经深度标准化, 对数变换,细胞被过滤, 我们可以跳过这一步.
'''
import scvi
scvi.model.SCVI.setup_anndata(adata, layer="counts", batch_key="patient")
vae = scvi.model.SCVI(adata, n_layers=2, n_latent=30, gene_likelihood="nb")
vae.train()
adata.obsm["X_scVI"] = vae.get_latent_representation()
'''
示例数据可以从 figshare 获得: https://figshare.com/ndownloader/files/41440050
import scanpy as sc
adata=sc.read('TiME_adata_scvi.h5ad')
adata
AnnData object with n_obs × n_vars = 40911 × 2000
obs: 'RNA_snn_res_1', 'assign_ident', 'assign_score', 'nCount_RNA', 'nFeature_RNA', 'orig_ident', 'patient', 'seurat_clusters', 'treatment', 'n_counts', 'log_counts', 'n_genes', 'n_genes_by_counts', 'total_counts', 'total_counts_mt', 'pct_counts_mt', 'isTME'
var: 'Selected', 'vst_mean', 'vst_variable', 'vst_variance', 'vst_variance_expected', 'vst_variance_standardized', 'n_cells', 'mt', 'n_cells_by_counts', 'mean_counts', 'pct_dropout_by_counts', 'total_counts', 'highly_variable', 'means', 'dispersions', 'dispersions_norm', 'highly_variable_rank', 'variances', 'variances_norm', 'highly_variable_nbatches'
uns: 'hvg'
obsm: 'X_scVI'
varm: 'PCs'
layers: 'counts'
建议首先识别恶性细胞 and removed for best practice in cell state annotation.
In the BCC data, the cluster of malignant cells are identified with inferCNV. We can use the pre-saved column ‘isTME’ to keep Tumor Microenvironment cells.
These are the authors’ exact words, but tests have found that the difference in annotation effect is not that great even without removing the malignant cells
But I think this step is not necessary
#adata = adata[adata.obs['isTME']]
计算邻域图#
We note that scVI was used earlier to remove the batch effect from the data, so we need to recalculate the neighbourhood map based on what is stored in adata.obsm['X_scVI']. 注意 that if you are not using scVI but using another method to calculate the neighbourhood map, such as X_pca, then you need to change X_scVI to X_pca to complete the calculation
#Example
#sc.tl.pca(adata)
#sc.pp.neighbors(adata, use_rep="X_pca")
sc.pp.neighbors(adata, use_rep="X_scVI")
computing neighbors
finished: added to `.uns['neighbors']`
`.obsp['distances']`, distances for each pair of neighbors
`.obsp['connectivities']`, weighted adjacency matrix (0:00:18)
To visualize the PCA’s embeddings, we use the pymde package wrapper in omicverse. This is an alternative to UMAP that is GPU-accelerated.
adata.obsm["X_mde"] = ov.utils.mde(adata.obsm["X_scVI"])
#adata.write_h5ad('adata_mde.h5ad',compression='gzip')
#adata=sc.read('adata_mde.h5ad')
MetaTiME 模型初始化#
接下来,让我们加载预先计算的 MetaTiME 元组件 (MeCs) 及其功能注释.
TiME_object=ov.single.MetaTiME(adata,mode='table')
...load pre-trained MeCs
...load functional annotation for MetaTiME-TME
我们可以对细胞进行过度聚类 which is useful for fine-grained cell state annotation.
当分辨率变大时, 聚类数量变大
TiME_object.overcluster(resolution=8,clustercol = 'overcluster',)
...overclustering using leiden
running Leiden clustering
finished: found 111 clusters and added
'overcluster', the cluster labels (adata.obs, categorical) (0:00:11)
TME 细胞类型预测#
我们使用 TiME_object.predictTiME() 来预测 TME 中的潜在细胞类型.
次要细胞类型将存储在
adata.obs['MetaTiME']主要细胞类型将存储在
adata.obs['Major_MetaTiME']
TiME_object.predictTiME(save_obs_name='MetaTiME')
...projecting MeC scores
......The predicted celltype have been saved in obs.MetaTiME
......The predicted major celltype have been saved in obs.Major_MetaTiME
AnnData object with n_obs × n_vars = 38836 × 2000
obs: 'RNA_snn_res_1', 'assign_ident', 'assign_score', 'nCount_RNA', 'nFeature_RNA', 'orig_ident', 'patient', 'seurat_clusters', 'treatment', 'n_counts', 'log_counts', 'n_genes', 'n_genes_by_counts', 'total_counts', 'total_counts_mt', 'pct_counts_mt', 'isTME', 'MeC_0', 'MeC_1', 'MeC_2', 'MeC_3', 'MeC_4', 'MeC_5', 'MeC_6', 'MeC_7', 'MeC_8', 'MeC_9', 'MeC_11', 'MeC_12', 'MeC_13', 'MeC_14', 'MeC_15', 'MeC_16', 'MeC_17', 'MeC_18', 'MeC_19', 'MeC_20', 'MeC_21', 'MeC_22', 'MeC_23', 'MeC_24', 'MeC_25', 'MeC_26', 'MeC_27', 'MeC_28', 'MeC_29', 'MeC_30', 'MeC_31', 'MeC_32', 'MeC_33', 'MeC_34', 'MeC_35', 'MeC_36', 'MeC_37', 'MeC_38', 'MeC_39', 'MeC_40', 'MeC_41', 'MeC_42', 'MeC_43', 'MeC_45', 'MeC_46', 'MeC_47', 'MeC_48', 'MeC_49', 'MeC_50', 'MeC_51', 'MeC_52', 'MeC_53', 'MeC_54', 'MeC_55', 'MeC_56', 'MeC_57', 'MeC_58', 'MeC_59', 'MeC_61', 'MeC_63', 'MeC_64', 'MeC_65', 'MeC_66', 'MeC_67', 'MeC_68', 'MeC_69', 'MeC_74', 'MeC_75', 'MeC_76', 'MeC_77', 'MeC_78', 'MeC_81', 'MeC_83', 'overcluster', 'MetaTiME_overcluster', 'MetaTiME', 'Major_MetaTiME'
var: 'Selected', 'vst_mean', 'vst_variable', 'vst_variance', 'vst_variance_expected', 'vst_variance_standardized', 'n_cells', 'mt', 'n_cells_by_counts', 'mean_counts', 'pct_dropout_by_counts', 'total_counts', 'highly_variable', 'means', 'dispersions', 'dispersions_norm', 'highly_variable_rank', 'variances', 'variances_norm', 'highly_variable_nbatches'
uns: 'hvg', 'neighbors', 'patient_colors', 'leiden'
obsm: 'X_mde', 'X_scVI'
varm: 'PCs'
layers: 'counts'
obsp: 'connectivities', 'distances'
可视化#
原作者提供了一个绘图函数 that effectively avoids overlapping labels. Here I have expanded its parameters so that it can be visualised using parameters other than X_umap
我们也可以使用 sc.pl.embedding to visualize the celltype