【问题标题】:Using rpy2 and biomaRt in Django在 Django 中使用 rpy2 和 biomaRt
【发布时间】:2014-04-11 18:46:55
【问题描述】:

我设法使用 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 以及如何在 django 中嵌入 python 代码以便显示数据?

【问题讨论】:

    标签: python django r bioinformatics rpy2


    【解决方案1】:

    正如错误所说,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 list 转换为 R 列表并 unlist 它。

    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)
    

    【讨论】:

      猜你喜欢
      • 2019-01-31
      • 2021-01-19
      • 2018-12-30
      • 1970-01-01
      • 1970-01-01
      • 2016-05-11
      • 1970-01-01
      • 2019-02-03
      • 1970-01-01
      相关资源
      最近更新 更多