This question has also been asked on Biostars and StackOverflow

I've been trying to code (in R) a way to convert gene accession numbers to gene names (from RNAseq data). I've looked at all the related questions and tried to modify my code such, but for some reason it's still not working. Here is my code, where charg is a character vector of the gene accession ID's of the data set resdata:

charg <- resdata$genes


ensembl = useMart("ensembl",dataset="hsapiens_gene_ensembl")

theBM <- getBM(attributes='ensembl_gene_id','hgnc_symbol', 
      filters = 'external_gene_name', 
      values = charg, 
      mart = ensembl)

resdata <- merge.data.frame(resdata, theBM, by.x="genes",by.y="ensembl_gene_id")

Here's some output (where I'm struggling):

> head(charg)
[1] "ENSG00000261150.2"  "ENSG00000164877.18" "ENSG00000120334.15"
[4] "ENSG00000100906.10" "ENSG00000182759.3"  "ENSG00000124145.6" 

> dim(theBM)
[1] 0 1

> head(theBM)
[1] ensembl_gene_id
<0 rows> (or 0-length row.names)

> dim(resdata)
[1] 20381    11
> resdata <- merge.data.frame(resdata, theBM, by.x="genes",by.y="ensembl_gene_id")
> dim(resdata) #after merge
[1]  0 11 #isn't correct -- just row names! where'd my genes go?

Thank you.


2 Answers 2


This is the code to get a look-up table to convert between Ensembl ID and HGNC:

ensembl = useMart("ensembl",dataset="hsapiens_gene_ensembl")
theBM <- getBM(attributes=c('ensembl_gene_id','hgnc_symbol'), 
               filters = c('ensembl_gene_id'),
               values = gsub("\\..*", "", charg),
               mart = ensembl)

What Devon was posting is correct but misses a c() around the attributes values. For further help please provide the content of resdata which you should always do when posting a question, since we cannot read minds. Does not work by the way is not a proper error description.

Once you have the output do:

resdata$genes <- gsub("\\..*", "", resdata$genes)

merge(x = theBM,
      by.x = "ensembl_gene_id",
      y = resdata,
      by.y = "genes")

Note that I had to go to that SE crosspost to get the content of resdata, this is not how this goes. Please post all relevant data up front in the future otherwise your questions might get downvoted and closed. Please also avoid cross-posting. if you provide proper information you usually get a good answer in time.

Edit: Just realized you also cross-posted this to Biostars even twice. Please stop this. I closed the Biostars posts and gave my two cents on this behaviour over there.

  • $\begingroup$ Truly, I apologize for any inconvenience my multiple posts caused, and it certainly will not happen again. Thank you an incredible amount for helping me, this worked beautifully :). Have a wonderful day. $\endgroup$
    – Equinox
    Jun 21, 2020 at 19:54
  • 1
    $\begingroup$ Glad to hear it works. Probably you were simply off to an unfortunate start, so don't worry too much. Good luck with your analysis :) $\endgroup$
    – user3051
    Jun 21, 2020 at 20:14
  • $\begingroup$ So, as I mentioned before, that worked beautifully for the human datasets. I'm trying with some mouse data and I'm getting a different error -- I'm not sure if I should make a new question, but I'll post it here: when I run ensembl = useMart("ensembl", dataset = "mmusculus_gene_ensembl") but then when I run mart, I get Object of class Mart: Using the ENSEMBL_MART_ENSEMBL BioMart database, Using the hsapiens_gene_ensembl dataset, instead mmusculus. I tried referencing the archive too, but that didn't work either. Any suggestions appreciated! $\endgroup$
    – Equinox
    Jun 21, 2020 at 21:45
  • $\begingroup$ turns out one of my input files was null! all good now thank you again. $\endgroup$
    – Equinox
    Jun 22, 2020 at 4:29

Those aren't external_gene_name's, they're ensembl_gene_id_versions:

theBM <- getBM(attributes='ensembl_gene_id','hgnc_symbol', 
               filters = 'ensembl_gene_id_version', 
               values = charg2, 
               mart = ensembl)

Note that you'll get more hits if you strip the gene ID versions off:

charg2 = sapply(strsplit(charg, '.', fixed=T), function(x) x[1])
theBM = getBM(attributes='ensembl_gene_id','hgnc_symbol', 
              filters = 'ensembl_gene_id', 
              values = charg2, 
              mart = ensembl)
  • $\begingroup$ Okay, thank you! So I tried this -- it's returning theBM (finally!) with the gene names, but when I merge it, the merge still doesn't work :(. Thank you immensely though :)))). $\endgroup$
    – Equinox
    Jun 20, 2020 at 17:08
  • 1
    $\begingroup$ Look at the first few rows of each and it should be apparent why there’s a mismatch. $\endgroup$
    – Devon Ryan
    Jun 20, 2020 at 19:59
  • $\begingroup$ Hm, maybe merge isn't the right tool. I'm trying to have the HGNC symbols replace my column of Ensembl Gene IDs -- the getBM worked beautifully, but what next? $\endgroup$
    – Equinox
    Jun 20, 2020 at 20:35
  • 1
    $\begingroup$ left_join from dplyr $\endgroup$
    – Devon Ryan
    Jun 20, 2020 at 20:40
  • $\begingroup$ Okay, please help, how would I implement that? $\endgroup$
    – Equinox
    Jun 20, 2020 at 20:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.