This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Make org.db dynamic in AnnotationDbi::mapIds()

I am trying to make the org.db (x) argument in AnnotationDbi::mapIds() as variable, But

genelist <- c("ENSG00000074800","ENSG00000116285","ENSG00000171603","ENSG00000049245")  
org_pkg <- "org.Hs.eg.db"

library(AnnotationDbi)
library(org_pkg)
entrez_Ids <- mapIds(org_pkg, as.character(genelist), 'ENTREZID', 'ENSEMBL')

Getting an Error message:

Error in (function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ‘mapIds’ for signature ‘"character"’

Tried with as.symbol(org_pkg) But no help.

r bioconductor

1 answer

You are not loading your packages correctly. org_pkg is a string (i.e. class character).

Try this instead:

library(AnnotationDbi)
library(org.Hs.eg.db)

genelist <- c("ENSG00000074800","ENSG00000116285","ENSG00000171603","ENSG00000049245")  
org_pkg <- org.Hs.eg.db
entrez_Ids <- mapIds(org_pkg, as.character(genelist), 'ENTREZID', 'ENSEMBL')

Resulting in:

head(entrez_Ids)
ENSG00000074800 ENSG00000116285 ENSG00000171603 ENSG00000049245 
         "2023"         "54206"         "22883"          "9341"

EDIT: Another simpler fix

genelist <- c("ENSG00000074800","ENSG00000116285","ENSG00000171603","ENSG00000049245")  
org_pkg <- "org.Hs.eg.db"

library(AnnotationDbi)
library(org_pkg,character.only=TRUE)
entrez_Ids <- mapIds(eval(parse(text = org_pkg)), as.character(genelist), 'ENTREZID', 'ENSEMBL')

Thanks for the answer. Yes this gives an output.

But suppose I am receiving a string object in org_pkg (because its coming from an Rscript argument, and its always a string). Then how to convert that string object to an AnnotationDbi class.

Good question!

library(org_pkg,character.only=TRUE)

I have no problem with loading package if the org_pkg is a string, Problem comes with mapIds()

entrez_Ids <- mapIds(eval(parse(text = org_pkg)), as.character(genelist), 'ENTREZID', 'ENSEMBL')

Log in to answer this question.