<p>You can use the standard annotation package for humans (you can also use biomaRt, but it can be more confusing, see below):</p>
<pre><code>library(&quot;org.Hs.eg.db&quot;) # remember to install it if you don't have it already
symbols &lt;- mapIds(org.Hs.eg.db, keys = ensemblsIDS, keytype = &quot;ENSEMBL&quot;, column=&quot;SYMBOL&quot;)
</code></pre>
<p>The <code>ensemblsIDS</code> should be a character vector: <code>c(&quot;ENSG001&quot;, &quot;ENSG002&quot;, ..)</code>.</p>
<p>However, if your primary key is an ENSEMBL id it is preferred to use a ENSEMBL package centred around the ENSEMBL id :</p>
<pre><code>library(&quot;EnsDb.Hsapiens.v86&quot;)
mapIds &lt;- mapIds(EnsDb.Hsapiens.v86, keys = ensemblsIDS, keytype = &quot;GENEID&quot;, columns = c(&quot;SYMBOL&quot;)
</code></pre>
<p>The solution with biomaRt:</p>
<pre class="lang-r prettyprint-override"><code>library(&quot;biomaRt&quot;)
ensembl = useMart(&quot;ensembl&quot;,dataset=&quot;hsapiens_gene_ensembl&quot;)
getBM(attributes='hgnc_symbol', 
      filters = 'ensembl_gene_id', 
      values = ensemblsIDS, 
      mart = ensembl)
</code></pre>

CC BY-SA 4.0
2018-10-19T23:02:48.707Z
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?
Mea R.
CC BY-SA 4.0
2018-10-21T14:42:39.903Z
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
CC BY-SA 4.0
2018-10-18T07:12:57.810Z
2021-09-28T08:26:31.590Z
2021-09-28T08:26:31.590Z
llrs
<p>I tried several R packages (<code>mygene</code>, <code>org.Hs.eg.db</code>, <code>biomaRt</code>, <code>EnsDb.Hsapiens.v79</code>) to convert Ensembl.gene to gene.symbol, and found that the <code>EnsDb.Hsapiens.v79</code> package / gene database provides the best conversion quality (in terms of being able to convert most of Ensembl.gene to gene.symbol).</p>

<pre><code># 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 &lt;- c("ENSG00000150676", "ENSG00000099308", "ENSG00000142676", "ENSG00000180776", "ENSG00000108848", "ENSG00000277370", "ENSG00000103811", "ENSG00000101473")

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

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

geneIDs2 &lt;- ensembldb::select(EnsDb.Hsapiens.v79, keys= geneSymbols, keytype = "SYMBOL", columns = c("SYMBOL","GENEID"))
</code></pre>

<p>Other available R packages / gene database for the conversion can refer to <a href="https://shiring.github.io/genome/2016/10/23/AnnotationDbi" rel="noreferrer">this GitHub page</a>.</p>

CC BY-SA 4.0
2019-11-28T17:12:10.733Z
2019-11-29T16:15:43.367Z
2019-11-29T16:15:43.367Z
Good Fit
<p>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 </p>

<pre><code>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
</code></pre>

<p>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. </p>

CC BY-SA 4.0
2018-10-18T07:40:59.463Z
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?
Emily_Ensembl
CC BY-SA 4.0
2021-07-22T03:38:21.010Z
How can I do if I have gene accesion number as NM_? thank you very much
Irene CE
<r><gene><ensembl><biomart>
Converting Gene Symbol to Ensembl ID in R
# Converting Gene Symbol to Ensembl ID in R

(Asked by: username_0 on 2018-10-17)

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.

**Tags:** r, gene, ensembl, biomart
> **Comments:**
> **username_1**: 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?  
> **username_2**: How can I do if I have gene accesion number as NM_? thank you very much  

---

## 2 answers

### Answer by username_3 on 2018-10-18. Score: 13

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)

> **Comments:**
> **username_0**: 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?  
> **username_3**: 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.  

---

### Answer by username_4 on 2019-11-28. Score: 6

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.


---

