This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Help with converting gene symbols to gene IDs

I am trying to convert gene symbols in one column to gene IDs. I am using the following code to achieve this:

for (i in 1:nrow(data_gsea2)){   
data_gsea2$ID[i] <-  mapIds(org.Hs.eg.db, (data_gsea2$ID[i]), "ENTREZID", "SYMBOL") 
}

I have a dataframe of 7386 rows/genes and this is taking forever to complete. I'm sure there is a smarter way to do this but I'm not sure how to. Anyone can help?

Thanks very much!

r gsea

If you've verified this command works as you expect it to (by, say, running it on ten rows instead of the entire data.frame), you'll probably just have to wait for the process to complete.

Yes, it works but takes a long time.

Try using apply (or even better, mclapply) instead of using a loop.

1 answer

mapIds manual indicates it allows submission of multiple keys, as opposed to one-by-one, and using a different test case that is on hand here, that appears to be much faster:

require(AnnotationDbi)
require(hgu95av2.db)
keys <- head(keys(hgu95av2.db, 'ENTREZID'), 100)

# sapply - one key at a time, 100 mapIds() calls
system.time(sapply(keys, function(z) mapIds(hgu95av2.db, keys=z, column="ALIAS", keytype="ENTREZID")))
<snip>
   user  system elapsed 
   5.35    0.64    6.11 

# send all keys at once, 1 mapIds() call
system.time(mapIds(hgu95av2.db, keys, column="ALIAS", keytype="ENTREZID"))
<snip>
   user  system elapsed 
   0.05    0.01    0.06

Good catch, Ahill! Can't believe I missed this - OP is submitting keys one by one in a loop instead of submitting a vector of keys!

Log in to answer this question.