我用R编写了一个程序,通过biomaRt库找出数据库中的所有候选基因。我正在尝试使用rpy2将R脚本转换为python文件。
下面是我写的R脚本。
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)如何使用rpy2将上面的R脚本转换为python脚本?
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)最后一行中的错误:
result = R.getBM(attributes = ("hgnc_symbol","entrezgene", "chromosome_name",
"start_position", "end_position") ,filters = ("chromosome_name",
"start", "end", "biotype"), values = filterlist, mart = mart)国家:
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.任何人都不知道如何使用rpy2将python转换为python,以及如何在django中嵌入python代码以便显示数据。
发布于 2014-03-09 15:22:26
正如错误所述,Python tuple不会自动转换。
尝试以下几点:
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)备注:可能有一个函数来猜测用户的意图,但我认为这是造成太多问题的潜在原因。您可以通过实现自己的额外转换来自定义这一点。或者,可以让rpy2将Python转换为R列表,并将其转化为rpy2。
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)https://stackoverflow.com/questions/22270119
复制相似问题