我最近正在学习 R 中的函数。我成功地使这个功能工作
check_spot = function(one_spot, cluster_id, marker_gene){
one_spot = sample(glio_spatial@assays$SCT@misc$vst.out$cells_step1, 1)
cluster_id = sample(as.data.frame(Idents(glioblastoma))[,1], 1)
marker = FindMarkers(glioblastoma, ident.1 = cluster_id)
marker = cbind(gene = rownames(marker), marker)
rownames(marker) = 1:nrow(marker)
marker_gene = sample(marker[,1], 5)
a = as.data.frame(glio_spatial@assays$Spatial@counts)
b = a[one_spot]
b = rename(b,c("count_spot" = all_of(one_spot)))
b = subset(b, b[,1] != 0, )
b = cbind(gene = rownames(b), b)
rownames(b) = 1:nrow(b)
intersection = inner_join(b, marker)
if (marker_gene[1] %in% intersection[1,]) sprintf("The gene %s is a marker gene for the cluster %s and is expressed in the spot %s", marker_gene, cluster_id, one_spot)
else if
(marker_gene[2] %in% intersection[1,]) sprintf("The gene %s is a marker gene for the cluster %s and is expressed in the spot %s", marker_gene, cluster_id, one_spot)
else if
(marker_gene[3] %in% intersection[1,]) sprintf("The gene %s is a marker gene for the cluster %s and is expressed in the spot %s", marker_gene, cluster_id, one_spot)
else if
(marker_gene[4] %in% intersection[1,]) sprintf("The gene %s is a marker gene for the cluster %s and is expressed in the spot %s", marker_gene, cluster_id, one_spot)
else if
(marker_gene[5] %in% intersection[1,]) sprintf("The gene %s is a marker gene for the cluster %s and is expressed in the spot %s", marker_gene, cluster_id, one_spot)
else
sprintf("The gene %s is a marker gene for the cluster %s, but not expressed in the spot %s", marker_gene, cluster_id, one_spot)
}
它将打印: Joining, by = "gene"
'基因 NKD1 是簇 8 的标记基因,但在 CTACCCTAAGGTCATA-1 位点中不表达'
'基因 RPL8 是簇 8 的标记基因,但不在 CTACCCTAAGGTCATA-1 位点中表达'
'基因 TSPAN13 是簇 8 的标记基因,但在 CTACCCTAAGGTCATA-1 位点中不表达'
'基因 HSBP1 是簇 8 的标记基因,但在 CTACCCTAAGGTCATA-1 位点中不表达'
'基因 BHLHE41 是簇 8 的标记基因,但不在 CTACCCTAAGGTCATA-1 位点中表达'。
然后,我尝试使用 for 语句为这个函数创建一个更简单的函数来获得更通用的样本数量,我试试这个
check_spot = function(one_spot, cluster_id, marker_gene){
one_spot = sample(glio_spatial@assays$SCT@misc$vst.out$cells_step1, 1)
cluster_id = sample(as.data.frame(Idents(glioblastoma))[,1], 1)
marker = FindMarkers(glioblastoma, ident.1 = cluster_id)
marker = cbind(gene = rownames(marker), marker)
rownames(marker) = 1:nrow(marker)
marker_gene = sample(marker[,1], length(marker[,1]))
a = as.data.frame(glio_spatial@assays$Spatial@counts)
b = a[one_spot]
b = rename(b,c("count_spot" = all_of(one_spot)))
b = subset(b, b[,1] != 0, )
b = cbind(gene = rownames(b), b)
rownames(b) = 1:nrow(b)
intersection = inner_join(b, marker)
for (i in 1:length(marker_gene)){
if (marker_gene[i] %in% intersection[1,]) {sprintf("The gene %s is a marker gene for the cluster %s and is expressed in the spot %s", marker_gene, cluster_id, one_spot)}
else {sprintf("The gene %s is a marker gene for the cluster %s, but not expressed in the spot %s", marker_gene, cluster_id, one_spot)}
}}
当我调用 check_spot() 时,它不会显示错误,也不会显示输出。可以请任何人帮忙,这样我就可以获得第一个函数所示的类似结果(但一般来说,更多的标记)?即使我只为标记 5 取样,它仍然不起作用。非常感谢。