前言
近期,张泽民教授团队开发了一种在cell cluster里面计算纯度的算法ROGUE,什么意思呢?经过传统的降维方式(t-SNE,UMAP)降维聚类后的cell cluster,其每一个cell cluster里面并不会是100%的同一类型的细胞,里面可能含有一些其他类型的细胞也分到同一个cell cluster里面,因此有必要计算每一个cell cluster的纯度
文章链接:An entropy-based metric for assessing the purity of single cell populations
测试算法
首先,我们模拟创建一个单细胞数据中某cell cluster的表达矩阵
exper = data.frame(abs(matrix(rnorm(10000),nrow=100)))
row.names(exper) = paste(rep('gene'),1:100,sep = '_')
colnames(exper) = paste(rep('cell'),1:100,sep = '_')
然后计算该矩阵的平均表达量和香浓熵,文章是这么定义每个基因的香浓熵的:
那么从代码中,我们可以看到,其香浓熵即为每行的均值,我们称之为真实entropy,实现由函数:Entropy:
library(tibble)
Entropy <- function(expr, r = 1){
tmp <- log(expr+1)
entropy <- Matrix::rowMeans(tmp) #计算香浓熵
mean.expr <- log(Matrix::rowMeans(expr)+r) #计算表达均值的log值
ent_res <- tibble(
Gene = rownames(expr),
mean.expr = mean.expr,
entropy = entropy
)
return(ent_res)
}
结果为:
计算好每个基因表达量均值的log值和每个基因的香浓熵以后,作者利用loess来拟合基因表达量均值的log值和每个基因的香浓熵之间的关系,实现由函数:entropy_fit:
entropy_fit <- function(.x, span = 0.5, mt.method = "fdr"){
.x <- .x %>% dplyr::filter(is.finite(mean.expr)) %>% dplyr::filter(entropy > 0)
fit <- loess(entropy~mean.expr, data = .x, span=span)
prd <- predict(fit, .x$mean.expr)
.x %>%
dplyr::mutate(fit = prd) %>%
dplyr::mutate(ds = fit - entropy) %>%
dplyr::mutate(pv = 1-pnorm(.$ds, mean = mean(.$ds), sd = sd(.$ds))) %>%
dplyr::filter(pv > 0.1) -> tmp
fit <- loess(entropy~mean.expr, data = tmp, span=span)
prd <- predict(fit, .x$mean.expr)
.x %>%
dplyr::mutate(fit = prd) %>%
dplyr::mutate(ds = fit - entropy) %>%
dplyr::filter(is.finite(ds)) %>%
dplyr::mutate(pv = 1-pnorm(.$ds, mean = mean(.$ds), sd = sd(.$ds))) %>%
dplyr::filter(pv > 0.1) -> tmp
fit <- loess(entropy~mean.expr, data = tmp, span=span)
prd <- predict(fit, .x$mean.expr)
.x %>%
dplyr::mutate(fit = prd) %>%
dplyr::mutate(ds = fit - entropy) %>%
dplyr::filter(is.finite(ds)) -> .x
.x <- .x %>% dplyr::mutate(p.value = 1-pnorm(.x$ds, mean = mean(.x$ds), sd = sd(.x$ds)))
p.adj <- p.adjust(.x$p.value, method = mt.method)
.x <- .x %>% dplyr::mutate(p.adj = p.adj) %>% dplyr::arrange(desc(ds))
}
结果为:
这里用loess(局部加权回归)来预测基因平均表达量的log值与熵的关系,预测的模型为fit,而后者需要利用基因表达量均值的log值作为决策变量,来根据模型fit预测响应变量的值,我们称之为预测entropy
并且作者定义ds如下:
由代码可知
dplyr::mutate(fit = prd) %>%
dplyr::mutate(ds = fit - entropy)
ds为预测entropy减去真实entropy,ds越大说明该cluster内某基因的熵变很大,即真实表达水平与预测的(与cluster内部大部分基因不一样)相差很大,说明很有可能是其他类型的细胞(杂质)
最后要计算的是Rogue值
该值定义如下:
其中而ROGUE介于0-1之间;当平台是UMI的,那么K=45,若平台为full-length,则K=500
SE_fun <- function(expr, span = 0.5, r = 1, mt.method = "fdr", if.adj = T){
ent_res <- ROGUE::Entropy(expr, r = r)
ent_res <- ROGUE::entropy_fit(ent_res, span = span, mt.method = mt.method)
if(!isTRUE(if.adj)){
ent_res <- ent_res %>% dplyr::mutate(p.adj = p.value)
}
return(ent_res)
}
## .x即为函数SE_fun()的返回值
CalculateRogue <- function(.x, platform = NULL, cutoff = 0.05, k = NULL, features = NULL){
if(is.null(k)){
if(is.null(platform)){
warning("Please provide a \"platform\" argument or specify a k value")
}else if(platform == "UMI"){
k = 45
}else if(platform == "full-length"){
k = 500
}else if(!is.null(platform) & !(platform %in% c("UMI","full-length"))){
warning("Please provide valid \"platform\" argument")
}
}else if(!is.null(k)){
k <- k
}
if(!is.null(features)){
.x <- .x %>% dplyr::filter(Gene %in% features)
sig_value <- sum(abs(.x$ds))
Rogue <- 1-sig_value/(sig_value+k)
return(Rogue)
}else{
sig_value <- abs(.x$ds[.x$p.adj < cutoff & .x$p.value < cutoff])
sig_value <- sum(sig_value)
Rogue <- 1-sig_value/(sig_value+k)
return(Rogue)
}
}
其中.x即为函数SE_fun()的返回值,在计算sig_value时,用的是该cell cluster中所有基因的ds值的总和;ROGUE值越高,说明该cell cluster细胞纯度越高,反之越低;
Github:https://github.com/PaulingLiu/ROGUE/blob/master/R/ROGUE.R