I am learning about function in R recently. I successfully make this function work
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)
}
It will print this: Joining, by = "gene"
'The gene NKD1 is a marker gene for the cluster 8, but not expressed in the spot CTACCCTAAGGTCATA-1'
'The gene RPL8 is a marker gene for the cluster 8, but not expressed in the spot CTACCCTAAGGTCATA-1'
'The gene TSPAN13 is a marker gene for the cluster 8, but not expressed in the spot CTACCCTAAGGTCATA-1'
'The gene HSBP1 is a marker gene for the cluster 8, but not expressed in the spot CTACCCTAAGGTCATA-1'
'The gene BHLHE41 is a marker gene for the cluster 8, but not expressed in the spot CTACCCTAAGGTCATA-1'.
Then, I have tried to make a simpler function for this function using for statement to get more generalise number of samples, I try this
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)}
}}
When I call check_spot(), it does not show errors and does not show the output as well. Could please anyone help so I can get the similar result (but ore general, more number of markers) as shown by the first function? Even if I take the sample for marker only 5, it still does not work. Thank you very much.
Aucun commentaire:
Enregistrer un commentaire