SingleCellExperiment: Edit cluster IDs in colLabels
1
0
Entering edit mode
achoppe • 0
@375a1478
Last seen 4 days ago
Australia

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:

g <- buildSNNGraph(sce, use.dimred="PCA")

cluster <- igraph::cluster_louvain(g)$membership

colLabels(sce) <- factor(cluster) table(colLabels(sce))

Can someone please help me? 🙏🙏🙏

scran Clustering SingleCellExperiment annotate cluster • 124 views
ADD COMMENT
2
Entering edit mode
ATpoint ★ 1.4k
@atpoint-13662
Last seen 54 minutes ago
Germany

It's really just a simple matching operation:

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.

ADD COMMENT
0
Entering edit mode

Thank you so much - that worked perfectly!

ADD REPLY
2
Entering edit mode

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.

> colData(sce)

colData(sce)
DataFrame with 100 rows and 1 column
     cluster
    <factor>
1          4
2          1
3          4
4          1
5          4
...      ...
96         3
97         4
98         2
99         1
100        4
> levels(sce$cluster) <- c("Tcell","Bcell","NKcell","Macrophage")
> colData(sce)
DataFrame with 100 rows and 1 column
       cluster
      <factor>
1   Macrophage
2   Tcell     
3   Macrophage
4   Tcell     
5   Macrophage
...        ...
96  NKcell    
97  Macrophage
98  Bcell     
99  Tcell     
100 Macrophage

Login before adding your answer.

Traffic: 220 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6