Question

I managed to write a program using R to find out all the candidates genes in the database by biomaRt library. I am trying to convert the R script to a python file using rpy2.

Below is the R script written by me that works.

library(biomaRt) 
mart <- useMart(biomart="ensembl", dataset="hsapiens_gene_ensembl")

positions <- read.table("positions.txt")
names(positions) <- c("chr","start","stop")
positions$start <- positions$start - 650000
positions$stop <- positions$stop + 650000

filterlist <- list(positions$chr,positions$start,positions$stop,"protein_coding")

getBM(attributes = c("hgnc_symbol","entrezgene", "chromosome_name", 
"start_position", "end_position"), filters = c("chromosome_name", "start", 
"end", "biotype"), values = filterlist, mart = mart)

How do I convert the R script above to python script using rpy2?

from rpy2.robjects import r as R
R.library("biomaRt")
mart = R.useMart(biomart="ensembl",dataset="hsapiens_gene_ensembl")
position = R.list("7","110433484", "110433544")
filterlist = R.list(position[0],position[1],position[2],"protein_coding")

result = R.getBM(attributes = ("hgnc_symbol","entrezgene", "chromosome_name", 
"start_position", "end_position") ,filters = ("chromosome_name", 
"start", "end", "biotype"), values = filterlist, mart = mart)

The error in the last line:

result = R.getBM(attributes = ("hgnc_symbol","entrezgene", "chromosome_name", 
    "start_position", "end_position") ,filters = ("chromosome_name", 
    "start", "end", "biotype"), values = filterlist, mart = mart)

States that:

 Traceback (most recent call last):
  File "<stdin>", line 3, in <module>
  File "/Users/project/lib/python2.7/site-packages/rpy2/robjects/functions.py", line 86, in __call__
    return super(SignatureTranslatedFunction, self).__call__(*args, **kwargs)
  File "/Users/project/lib/python2.7/site-packages/rpy2/robjects/functions.py", line 34, in __call__
    new_kwargs[k] = conversion.py2ri(v)
  File "/Users/project/lib/python2.7/site-packages/rpy2/robjects/__init__.py", line 148, in default_py2ri
    raise(ValueError("Nothing can be done for the type %s at the moment." %(type(o))))
ValueError: Nothing can be done for the type <type 'tuple'> at the moment.

Anyone has any idea how can I convert to python using rpy2 and how can I embed the python code in django so that I can display the data?

Was it helpful?

Solution

As the error says, the Python type tuple is not converted automatically.

Try the following:

from rpy2.robjects.vectors import StrVector
result = R.getBM(attributes = StrVector(("hgnc_symbol","entrezgene", "chromosome_name", 
                                         "start_position", "end_position")),
                 filters = StrVector(("chromosome_name", 
                                      "start", "end", "biotype")),
                 values = filterlist,
                 mart = mart)

Notes: it would be possible to have a function that guesses what is the intent of the user might, but I consider this the potential source of too many issues. One can customize this by implementing his/her own additional conversion. Alternatively, one can let rpy2 turn a Python list into an R list and unlist it.

from rpy2.robjects.packages import importr
base = importr('base')
ul = base.unlist
result = R.getBM(attributes = ul(["hgnc_symbol","entrezgene", "chromosome_name", 
                                  "start_position", "end_position"]),
                 filters = ul(["chromosome_name", 
                               "start", "end", "biotype"]),
                 values = filterlist,
                 mart = mart)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top