最近有小伙伴问起Scissor工具的报错,因此就再次回顾一下。目前在很多分析中都会用到Scissor法,但由于Scissor法目前没有更新还是最兼容Seurat V4版本。如果要在Seurat V5中使用时,需要注意和修改两句代码。
既往流程:Scissor算法-从含有表型的bulkRNA数据中提取信息去鉴别单细胞亚群 https://mp.weixin.qq.com/s/yUaeJKpuF1NzaD0fnhhUuQ
1.没有提取到矩阵
2.preprocessCore::normalize.quantiles() 要求输入是一个数值型矩阵
主要注意和修改如下两句话即可
sc_exprs <- as.matrix(sc_dataset@assays$RNA@data)
network <- as.matrix(sc_dataset@graphs$RNA_snn)
改成
sc_exprs <- as.matrix(GetAssayData(sc_dataset, assay="RNA", layer="data"))
network <- as.matrix(sc_dataset@graphs$RNA_snn)
当然如果使用了其他整合方式的时候,也需要适时的修改assay
runScissor <- function (bulk_dataset, sc_dataset, phenotype, tag = NULL, alpha = NULL,
cutoff = 0.2, family = c("gaussian", "binomial", "cox"),
Save_file = "Scissor_inputs.RData", Load_file = NULL)
{
library(Seurat)
library(Matrix)
library(preprocessCore)
if (is.null(Load_file)) {
common <- intersect(rownames(bulk_dataset), rownames(sc_dataset))
if (length(common) == 0) {
stop("There is no common genes between the given single-cell and bulk samples.")
}
if (class(sc_dataset) == "Seurat") {
sc_exprs <- as.matrix(GetAssayData(sc_dataset, assay="RNA", layer="data"))
network <- as.matrix(sc_dataset@graphs$RNA_snn)
}
else {
sc_exprs <- as.matrix(sc_dataset)
Seurat_tmp <- CreateSeuratObject(sc_dataset)
Seurat_tmp <- FindVariableFeatures(Seurat_tmp, selection.method = "vst",
verbose = F)
Seurat_tmp <- ScaleData(Seurat_tmp, verbose = F)
Seurat_tmp <- RunPCA(Seurat_tmp, features = VariableFeatures(Seurat_tmp),
verbose = F)
Seurat_tmp <- FindNeighbors(Seurat_tmp, dims = 1:10,
verbose = F)
network <- as.matrix(Seurat_tmp@graphs$RNA_snn)
}
diag(network) <- 0
network[which(network != 0)] <- 1
dataset0 <- cbind(bulk_dataset[common, ], sc_exprs[common,
])
dataset1 <- normalize.quantiles(dataset0)
rownames(dataset1) <- rownames(dataset0)
colnames(dataset1) <- colnames(dataset0)
Expression_bulk <- dataset1[, 1:ncol(bulk_dataset)]
Expression_cell <- dataset1[, (ncol(bulk_dataset) +
1):ncol(dataset1)]
X <- cor(Expression_bulk, Expression_cell)
quality_check <- quantile(X)
print("|**************************************************|")
print("Performing quality-check for the correlations")
print("The five-number summary of correlations:")
print(quality_check)
print("|**************************************************|")
if (quality_check[3] < 0.01) {
warning("The median correlation between the single-cell and bulk samples is relatively low.")
}
if (family == "binomial") {
Y <- as.numeric(phenotype)
z <- table(Y)
if (length(z) != length(tag)) {
stop("The length differs between tags and phenotypes. Please check Scissor inputs and selected regression type.")
}
else {
print(sprintf("Current phenotype contains %d %s and %d %s samples.",
z[1], tag[1], z[2], tag[2]))
print("Perform logistic regression on the given phenotypes:")
}
}
if (family == "gaussian") {
Y <- as.numeric(phenotype)
z <- table(Y)
if (length(z) != length(tag)) {
stop("The length differs between tags and phenotypes. Please check Scissor inputs and selected regression type.")
}
else {
tmp <- paste(z, tag)
print(paste0("Current phenotype contains ",
paste(tmp[1:(length(z) - 1)], collapse = ", "),
", and ", tmp[length(z)], " samples."))
print("Perform linear regression on the given phenotypes:")
}
}
if (family == "cox") {
Y <- as.matrix(phenotype)
if (ncol(Y) != 2) {
stop("The size of survival data is wrong. Please check Scissor inputs and selected regression type.")
}
else {
print("Perform cox regression on the given clinical outcomes:")
}
}
save(X, Y, network, Expression_bulk, Expression_cell,
file = Save_file)
}
else {
load(Load_file)
}
if (is.null(alpha)) {
alpha <- c(0.005, 0.01, 0.05, 0.1, 0.2, 0.3, 0.4, 0.5,
0.6, 0.7, 0.8, 0.9)
}
for (i in 1:length(alpha)) {
set.seed(123)
fit0 <- APML1(X, Y, family = family, penalty = "Net",
alpha = alpha[i], Omega = network, nlambda = 100,
nfolds = min(10, nrow(X)))
fit1 <- APML1(X, Y, family = family, penalty = "Net",
alpha = alpha[i], Omega = network, lambda = fit0$lambda.min)
if (family == "binomial") {
Coefs <- as.numeric(fit1$Beta[2:(ncol(X) + 1)])
}
else {
Coefs <- as.numeric(fit1$Beta)
}
Cell1 <- colnames(X)[which(Coefs > 0)]
Cell2 <- colnames(X)[which(Coefs < 0)]
percentage <- (length(Cell1) + length(Cell2))/ncol(X)
print(sprintf("alpha = %s", alpha[i]))
print(sprintf("Scissor identified %d Scissor+ cells and %d Scissor- cells.",
length(Cell1), length(Cell2)))
print(sprintf("The percentage of selected cell is: %s%%",
formatC(percentage * 100, format = "f", digits = 3)))
if (percentage < cutoff) {
break
}
cat("\n")
}
print("|**************************************************|")
return(list(para = list(alpha = alpha[i], lambda = fit0$lambda.min,
family = family), Coefs = Coefs, Scissor_pos = Cell1,
Scissor_neg = Cell2))
}
注:若对内容有疑惑或者有发现明确错误的朋友,请联系后台。更多相关内容可关注公众号:生信方舟 。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。