# Converting Gene Symbol to Ensembl ID in R

I'm trying to convert ~20,000 different human gene symbols to ensembl IDs. I've been trying to use biomaRt to do this, but continue getting the following error

getBM( attributes=c("ensembl_gene_id") , filters= "mgi_symbol"  ,mart=ensembl)
Error in martCheck(mart) :
No dataset selected, please select a dataset first.  You can see the available datasets by using the listDatasets function see ?listDatasets for more information.  Then you should create the Mart object by using the useMart function.  See ?useMart for more information


Is there a different format I should be using for this? I tried using BioConductor's description of how to use getBM but am a R novice and confused as to how to set this up to convert gene symbols into ensemble IDs.

• You say you have human gene symbols in your text, but in your code you say you are filtering by MGI symbols. Which is it? Oct 18 '18 at 7:40
• How can I do if I have gene accesion number as NM_? thank you very much Jul 22 at 3:38

You can use the standard annotation package for humans (you can also use biomaRt, but it can be more confusing, see below):

library("org.Hs.eg.db") # remember to install it if you don't have it already
symbols <- mapIds(org.Hs.eg.db, keys = ensemblsIDS, keytype = "ENSEMBL", column="SYMBOL")


The ensemblsIDS should be a character vector: c("ENSG001", "ENSG002", ..).

However, if your primary key is an ENSEMBL id it is preferred to use a ENSEMBL package centred around the ENSEMBL id :

library("EnsDb.Hsapiens.v86")
mapIds <- mapIds(EnsDb.Hsapiens.v86, keys = ensemblsIDS, keytype = "GENEID", columns = c("SYMBOL")


The solution with biomaRt:

library("biomaRt")
ensembl = useMart("ensembl",dataset="hsapiens_gene_ensembl")
getBM(attributes='hgnc_symbol',
filters = 'ensembl_gene_id',
values = ensemblsIDS,
mart = ensembl)

• Thanks much for the detailed response!! If I currently have ensembleIDs as a .csv file in columns, what is the best way to convert them to a character vector? Also, just to confirm, ensemblsIDs (defined as the value) is the input data set, correct? Oct 19 '18 at 23:02
• Well, read around the forums. Normally one would read the .csv file and then extract the values with something like as.character(csvData[, "ensemblColum"]). It should be correct, unless I understood something different.
– llrs
Oct 21 '18 at 14:42

I tried several R packages (mygene, org.Hs.eg.db, biomaRt, EnsDb.Hsapiens.v79) to convert Ensembl.gene to gene.symbol, and found that the EnsDb.Hsapiens.v79 package / gene database provides the best conversion quality (in terms of being able to convert most of Ensembl.gene to gene.symbol).

# Install the package if you have not installed by running this command:
BiocManager::install("EnsDb.Hsapiens.v79")

library(EnsDb.Hsapiens.v79)

# 1. Convert from ensembl.gene to gene.symbol
ensembl.genes <- c("ENSG00000150676", "ENSG00000099308", "ENSG00000142676", "ENSG00000180776", "ENSG00000108848", "ENSG00000277370", "ENSG00000103811", "ENSG00000101473")

geneIDs1 <- ensembldb::select(EnsDb.Hsapiens.v79, keys= ensembl.genes, keytype = "GENEID", columns = c("SYMBOL","GENEID"))

# 2. Convert from gene.symbol to ensembl.gene
geneSymbols <-  c('DDX26B','CCDC83',  'MAST3', 'RPL11', 'ZDHHC20',  'LUC7L3',  'SNORD49A',  'CTSH', 'ACOT8')

geneIDs2 <- ensembldb::select(EnsDb.Hsapiens.v79, keys= geneSymbols, keytype = "SYMBOL", columns = c("SYMBOL","GENEID"))


Other available R packages / gene database for the conversion can refer to this GitHub page.