merv merv - 3 months ago 7
R Question

Annotation DB has no 'select' method in rpy2

I have the following code in R:

require(hgu133a.db)

entrezIDs <- select(hgu133a.db, probeNames, "ENTREZID")


where
probeNames
is a list of strings corresponding to probes found in this database.

I am attempting to translate it to Python using rpy2:

from rpy2.robjects.packages import importr
hgu133a_db = importr('hgu133a.db')

entrez_ids = hgu133a_db.select(hgu133a_db, probe_names, 'ENTREZID')


But receive the error:


AttributeError: module 'hgu133a.db' has no attribute 'select'


I've searched the documentation (
?select
) and as far as I can tell the database hgu133a.db inherits a
select
method from the AnnotationDbi class.

How do I properly resolve the library where
select()
is coming from, so I can use it in Python?

Answer

[should have been a comment to @merv 's answer, but exceeded the number characters]

rpy2's importr() is trying to help a being specific about which package namespace an R object is coming from, while R's common usage is much less so (and can lead to annoyances such as the loading order of R packages having an influence on which one of the functions with the same name is executed).

The tradeoff with importr is that one has to know where an R symbol is coming from. There is a less-known function in rpy2 that can help finding where a given R symbol is defined(*): https://rpy2.readthedocs.io/en/version_2.8.x/robjects_rpackages.html#finding-where-an-r-symbol-is-coming-from .

Otherwise, one can also use r() to retrieve the object that would be picked(*) in an R session.

from rpy2.robjects import r
r('select')

(*: as mentioned earlier, the order in which R packages were loaded earlier in the session can have an influence on which R object is picked).

Comments