Make org.db dynamic in AnnotationDbi::mapIds()
1
1
Entering edit mode
5.3 years ago

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 • 3.2k views
ADD COMMENT
1
Entering edit mode
5.3 years ago

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')
ADD COMMENT
0
Entering edit mode

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.

ADD REPLY
0
Entering edit mode

Good question!

library(org_pkg,character.only=TRUE)
ADD REPLY
0
Entering edit mode

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

ADD REPLY
1
Entering edit mode
entrez_Ids <- mapIds(eval(parse(text = org_pkg)), as.character(genelist), 'ENTREZID', 'ENSEMBL')
ADD REPLY
0
Entering edit mode

Hey thank you so much. It solved :)

ADD REPLY

Login before adding your answer.

Traffic: 1555 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6