Hello,
This is probably really simple but I can't work out how to change my cluster IDs from their numbers to a cell type. ie, cluster "1" to "T cell", for example.
I used scran for my clustering:
suppressPackageStartupMessages(library(SingleCellExperiment))
#/ example data
ncells <- 100
u <- matrix(rpois(20000, 5), ncol=ncells)
sce <- SingleCellExperiment(assays=list(counts=u))
sce$cluster <- factor(sample(c(1:4), size=ncells, replace=TRUE))
colData(sce)
#> DataFrame with 100 rows and 1 column
#> cluster
#> <factor>
#> 1 1
#> 2 1
#> 3 4
#> 4 3
#> 5 4
#> ... ...
#> 96 3
#> 97 4
#> 98 1
#> 99 1
#> 100 2
#/ make a translation table
translation <- data.frame(cluster=factor(c(1:4)),
replacement=c("Tcell", "Bcell", "NKcell", "Macrophage"))
translation
#> cluster replacement
#> 1 1 Tcell
#> 2 2 Bcell
#> 3 3 NKcell
#> 4 4 Macrophage
#/ now match colData against translation
matched <- match(sce$cluster, translation$cluster)
#/ translate
translated <- translation[matched,]$replacement
#/ feed that back into the sce
sce$translated <- factor(translated)
colData(sce)
#> DataFrame with 100 rows and 2 columns
#> cluster translated
#> <factor> <factor>
#> 1 1 Tcell
#> 2 1 Tcell
#> 3 4 Macrophage
#> 4 3 NKcell
#> 5 4 Macrophage
#> ... ... ...
#> 96 3 NKcell
#> 97 4 Macrophage
#> 98 1 Tcell
#> 99 1 Tcell
#> 100 2 Bcell
Created on 2022-08-05 by the reprex package (v2.0.1)
You can use other functions rather than match to match the two data.frames such as dplyr::left_join as long as that preserves the order of the clusters in sce$cluster.
Thank you so much - that worked perfectly!
Changing the levels of a factor is a simple one liner. Using ATpoint's code to get the fake
SingleCellExperiment
, it's easy to change in place.