单细胞中的细胞类型划分

cell type classify

正好最近课题有些新东西,就做一下总结,以后估计都会用到吧.

单细胞测序中对细胞的分类

一般在单细胞测序数据的处理中,都会涉及到对细胞类型的分类,不管有用没用都得统计一下做一个展示.
比较常见的方法是用聚类的方式将单细胞划分到最相似的细胞类型,通常这样操作的时候细胞类型的数量都比较少.HPCA项目的数据带来了更加丰富的细胞类型,利用该数据进行细胞类型的划分可能会更有帮助(也许吧).

HPCA

HPCA项目收集了不同类型的细胞表达数据,并公开发表在GEO上: GSE49910.
UCSF可是好人啊,把整理后的数据公开发布了他们的singleR项目中,那么自然是盗亦有道,把下载链接分享一下,嘻嘻.

细胞类型划分

接下去就开始正式处理数据,导入hpca.rda以及需要进行细胞类型划分的单细胞数据.

setwd("/home/wang/Documents/subclone_anlysis/GSE118389/data/")
library(dplyr)
load("hpca.rda")
GSE118389 <- read.table(
  "GSE118389_norm_data.txt",
  header = T, 
  row.names = 1, 
  sep = "\t", stringsAsFactors = F, quote = "\"")

简单说明一下hpca.rda中的数据存储方式:

变量含义
data 存储HPCA项目中所有的细胞的表达数据,行名为基因名,列名为GSM样本号
type GSM样本的细胞具体类型
main_types 相对于type更为宽泛
de.gene 与type对应,存储了每两种细胞之间的差异基因
de.gene.main 与de.gene类似,其中的细胞为main_type

以type为例,我们将未知细胞划分到具体的细胞类型中去

cell_list <- names(hpca$de.genes)

构建计算细胞样本之间距离的函数:

cell_dist <- function(cell_1_exp, cell_2_exp){
  cell_1_exp - cell_2_exp
}

然后构建细胞划分类型函数,其中需要一个未知细胞,两个已知细胞类型,通过与该两种细胞之间的距离,来确定未知细胞与哪一类更为接近:

type_compare <- function(unknow_cell, cell_type_1, cell_type_2){
  de_gene = eval(parse(text = paste0("hpca$de.genes$`", cell_type_1, "`$`", cell_type_2, "`")))
  de_gene_index = which(de_gene %in% tolower(rownames(hpca$data)))
  need_gene = intersect(rownames(hpca$data)[de_gene_index], rownames(GSE118389))
  cell_1_exp = hpca$data[need_gene, which(hpca$types == cell_type_1)] 
  if(class(cell_1_exp)== "matrix"){
    cell_1_exp = apply(cell_1_exp, 1, mean) %>% scale() %>% as.vector()
  }else {
    cell_1_exp = unlist(cell_1_exp) %>% scale() %>% as.vector()
  }
  cell_2_exp = hpca$data[need_gene, which(hpca$types == cell_type_2)]
  if(class(cell_2_exp) == "matrix"){
    cell_2_exp = apply(cell_2_exp, 1, mean) %>% scale() %>% as.vector()
  }else {
    cell_2_exp = unlist(cell_2_exp)  %>% scale() %>% as.vector()
  }
  unknow_cell_exp = unlist(GSE118389[need_gene, unknow_cell])
  dist = as.matrix(dist(rbind(unknow_cell_exp, cell_1_exp, cell_2_exp)))[-1,1]
  return(dist)
}

利用冒泡法迭代寻找未知细胞最相似的细胞类型:

cell_type_classify <- function(unknow_cell){
  temp = cell_list[1]
  for(i in 2: length(cell_list)){
    cell_type_2 = cell_list[i]
    need_dist = type_compare(unknow_cell, temp, cell_type_2)
    if(need_dist[1] > need_dist[2]){
      temp = cell_type_2
    }
  }
  return(temp)
}

最后就是循环的过程了:

GSE118389_cell_type <- apply(
  matrix(colnames(GSE118389), ncol = 1),
  1,
  cell_type_classify
)

虽然可以把每个未知细胞都划分到具体的细胞类型,但是还是有些问题,应该还是会有些细胞是无法划分到HPCA中的细胞类型的,毕竟其中的细胞类型也只是人体内的一部分.
如何解决这个问题还要再考虑一下,就这样.

posted @ 2019-03-14 20:13  PeRl`  阅读(2104)  评论(0编辑  收藏  举报