Limour

Limour

临床医学在读。

【Migration】CellTypist Annotation of Immune Cell Subtypes

Installing CellTypist#

conda create -n celltypist -c conda-forge r-base=4.1.2
conda activate celltypist
conda install -c conda-forge r-seurat=4.1.0 -y
conda install -c conda-forge r-irkernel=1.3 -y
IRkernel::installspec(name='celltypist', displayname='r-celltypist')
conda install -c conda-forge scanpy=1.8.2 -y
/opt/conda/envs/celltypist/bin/pip3 install celltypist -i https://pypi.tuna.tsinghua.edu.cn/simple
conda install -c conda-forge r-reticulate=1.24 -y
python3
import celltypist
celltypist.models.download_models(force_update = False)

Loading packages#

Sys.setenv(RETICULATE_PYTHON = "/opt/conda/envs/celltypist/bin/python3.8")
library(reticulate)
scanpy = import("scanpy")
celltypist = import("celltypist")
pandas <- import("pandas")
numpy = import("numpy")
py_config()

Loading data#

library(Seurat)
sce <- readRDS("~/upload/yy_zhang_data/scRNA-seq/pca.celltype.rds")
Myeloid <- subset(sce, cell_type=='Myeloid')

Converting Seurat to scanpy#

# Data matrix, with row and column definitions reversed between scanpy and Seurat
adata.X = numpy$array(t(as.matrix(Myeloid[['RNA']]@counts)))
# Observation for each cell
adata.obs = pandas$DataFrame([email protected][colnames(Myeloid[['RNA']]@counts),])
# Annotation for the gene matrix
adata.var = pandas$DataFrame(data.frame(gene = rownames(Myeloid[['RNA']]@counts), row.names = rownames(Myeloid[['RNA']]@counts)))
 
# Assembling AnnData object
adata = scanpy$AnnData(X = adata.X, obs=adata.obs, var=adata.var)

Performing prediction#

model = celltypist$models$Model$load(model = 'Immune_All_AddPIP.pkl')
model$cell_types
scanpy$pp$normalize_total(adata, target_sum=1e4)
scanpy$pp$log1p(adata)
predictions = celltypist$annotate(adata, model = 'Immune_All_AddPIP.pkl', majority_voting = T)

Adding predictions to seurat object#

Myeloid = AddMetaData(Myeloid, predictions$predicted_labels)
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.