【发布时间】:2021-06-24 08:07:16
【问题描述】:
我最近正在学习 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)
}
它会打印这个: 加入, 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 取样,它仍然不起作用。非常感谢。
【问题讨论】:
-
你需要一个语言标签
-
已经编辑了。
-
你的函数没有返回值。通常在 R 中,
function范围内的最后一行代码被视为返回值。但一个好的做法是使用return()。话虽如此,请考虑包含一个数据集样本,以帮助您发现任何问题 -
@ChrissPaul 我想我已经用 sprintf() 为函数创建了一个返回值,因为 sprintf 是执行的最后一行?
-
一个函数应该在参数中包含所有输入,但是在你的函数中,你没有正确声明参数,实际上所有输入参数都将被后续代码覆盖。正确声明输入 --
标签: r function loops if-statement seurat