This is usually done using AnnotationDbi in combination with the annotation database for human org.Hs.eg.db.
library(org.Hs.eg.db)
genes <- c("ENSG00000102055", "ENSG00000232605", "ENSG00000136828",
"ENSG00000159197", "ENSG00000201394", "ENSG00000278580",
"ENSG00000213440", "ENSG00000166816", "ENSG00000275895",
"ENSG00000105723")
select(org.Hs.eg.db, genes, c("ENSEMBL", "ALIAS"), "ENSEMBL")
This returns:
ENSEMBL ALIAS
1 ENSG00000102055 I-4
2 ENSG00000102055 PPP1R2P9
3 ENSG00000102055 PPP1R2C
4 ENSG00000232605 <NA>
5 ENSG00000136828 RALGEF2
6 ENSG00000136828 RALGPS1A
7 ENSG00000136828 RALGPS1
8 ENSG00000159197 ATFB4
9 ENSG00000159197 LQT5
10 ENSG00000159197 LQT6
11 ENSG00000159197 MIRP1
12 ENSG00000159197 KCNE2
13 ENSG00000201394 RN5S140
14 ENSG00000201394 RNA5SP140
15 ENSG00000278580 <NA>
16 ENSG00000213440 <NA>
17 ENSG00000166816 DLACD
18 ENSG00000166816 DLD
19 ENSG00000166816 LDHD
20 ENSG00000275895 U2AF1L5
21 ENSG00000105723 GSK3A
If you want to apply a 1:1 mapping of gene IDs to common names, you can use the "SYMBOL" column. In a data.frame
, you can do something like this:
ensembl_to_symbol <- function(e) {
hits <- select(org.Hs.eg.db, e, "SYMBOL", "ENSEMBL")$SYMBOL
# Gene IDs without symbols should just stay as IDs
no_hit <- is.na(hits)
hits[no_hit] <- e[no_hit]
hits
}
recode_symbols <- function(s) {
e <- ensembl_to_symbol(unlist(strsplit(s, split = ",")))
paste(e, collapse = ",")
}
df <- data.frame(
genes = c(paste(genes[1:5], collapse = ","),
paste(genes[6:10], collapse = ",")
)
)
df$genes <- sapply(df$genes, recode_symbols)
This isn't the most vectorized (fast) solution, but it's probably easier to understand this way.