使用 rpy2 调用 R 包函数

Calling an R package function with rpy2

我是 R 的新手,需要将字符串数据从 pandas 数据帧传递到 R 中的函数。此函数接受嵌套的字符串列表,例如:

> list(c("HP:0001315", "HP:0011343"), c("HP:0007164", "HP:0030810"), c( "HP:0030133", "HP:0040082"))
[[1]]
[1] "HP:0001315" "HP:0011343"

[[2]]
[1] "HP:0007164" "HP:0030810"

[[3]]
[1] "HP:0030133" "HP:0040082"  

代码

# Importing the package `ontologySimilarity`:

from rpy2.robjects.packages import importr
import rpy2.robjects as robjects
utils = importr('utils')
utils.data('hpo')
ontology_similarity = importr('ontologySimilarity')
   

我尝试了两种方法:

1)

lists = numbers_and_ids.HPO_ID.str.split('\t')
ontology_similarity.get_sim_grid(ontology='hpo', term_sets=lists)

产生 KeyError: <class 'list'> 错误消息。

2)

# numbers_and_ids dataframe from which the data will be taken:

data = {'dna_#': {0: '25246', 1: '29244', 2: '6409'},
        'HPO_ID': {0: 'HP:0001263\tHP:0001508\tHP:0000252\tHP:0001875\tHP:0001627', 1: 'HP:0011344\tHP:0008936\tHP:0001257\tHP:0005305\tHP:0002188\tHP:0040187\tHP:0000365\tHP:0001999', 2: 'HP:0001263\tHP:0000252\tHP:0001629\tHP:0001875\tHP:0001999'}}

numbers_and_ids = pandas.DataFrame(data}

numbers_and_ids
Out[89]:19:00
   dna_#                                             HPO_ID
0  25246  HP:0001263\tHP:0001508\tHP:0000252\tHP:0001875...
1  29244  HP:0011344\tHP:0008936\tHP:0001257\tHP:0005305...
2   6409  HP:0001263\tHP:0000252\tHP:0001629\tHP:0001875...

# Converting the data in the dataframe into tuples:

_1 = tuple(numbers_and_ids.HPO_ID.str.split('\t')[0])
_2 = tuple(numbers_and_ids.HPO_ID.str.split('\t')[1])
_3 = tuple(numbers_and_ids.HPO_ID.str.split('\t')[2])

# Creating the nested list d and calling the function:

robjects.r(f'd<-list(c{_1}, c{_2}, c{_3})')
ontology_similarity.get_sim_grid(ontology='hpo', term_sets='d')

产生了错误信息:

rpy2.rinterface_lib.embedded.RRuntimeError: Error in (function (ontology, information_content, term_sim_method, term_sim_mat,  : 
  is.list(term_sets) & is.list(term_sets) is not TRUE

我检查了 d 是否是一个列表:

robjects.r(f'print(is.list(d))')

[1] TRUE
Out[92]: 
<rpy2.robjects.vectors.BoolVector object at 0x7f591524a900> [RTYPES.LGLSXP]
R classes: ('logical',)
[       1]

如果能就如何调用 ontology_similarity.get_sim_grid() 提供任何建议,我将不胜感激。
谢谢。

最终我使用 robjects.r 从 R 而不是从 Python 调用 R 包并且成功了。