Question: Help with converting gene symbols to gene IDs
0
gravatar for nattzy94
9 months ago by
nattzy9420
nattzy9420 wrote:

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!

gsea R • 300 views
ADD COMMENTlink modified 9 months ago by Ahill1.9k • written 9 months ago by nattzy9420
1

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.

ADD REPLYlink written 9 months ago by RamRS30k

Yes, it works but takes a long time.

ADD REPLYlink written 9 months ago by nattzy9420

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

ADD REPLYlink written 9 months ago by RamRS30k
1
gravatar for Ahill
9 months ago by
Ahill1.9k
United States
Ahill1.9k wrote:

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
ADD COMMENTlink written 9 months ago by Ahill1.9k

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!

ADD REPLYlink written 9 months ago by RamRS30k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 621 users visited in the last hour