I’m new to R and need to pass string data from a pandas dataframe to a function in R. This function accepts nested lists of strings, such as:
JavaScript
x
10
10
1
> list(c("HP:0001315", "HP:0011343"), c("HP:0007164", "HP:0030810"), c( "HP:0030133", "HP:0040082"))
2
[[1]]
3
[1] "HP:0001315" "HP:0011343"
4
5
[[2]]
6
[1] "HP:0007164" "HP:0030810"
7
8
[[3]]
9
[1] "HP:0030133" "HP:0040082"
10
The code
JavaScript
1
9
1
# Importing the package `ontologySimilarity`:
2
3
from rpy2.robjects.packages import importr
4
import rpy2.robjects as robjects
5
utils = importr('utils')
6
utils.data('hpo')
7
ontology_similarity = importr('ontologySimilarity')
8
9
I tried two approaches:
1)
JavaScript
1
3
1
lists = numbers_and_ids.HPO_ID.str.split('t')
2
ontology_similarity.get_sim_grid(ontology='hpo', term_sets=lists)
3
That yielded KeyError: <class 'list'>
error message.
2)
JavaScript
1
25
25
1
# numbers_and_ids dataframe from which the data will be taken:
2
3
data = {'dna_#': {0: '25246', 1: '29244', 2: '6409'},
4
'HPO_ID': {0: 'HP:0001263tHP:0001508tHP:0000252tHP:0001875tHP:0001627', 1: 'HP:0011344tHP:0008936tHP:0001257tHP:0005305tHP:0002188tHP:0040187tHP:0000365tHP:0001999', 2: 'HP:0001263tHP:0000252tHP:0001629tHP:0001875tHP:0001999'}}
5
6
numbers_and_ids = pandas.DataFrame(data}
7
8
numbers_and_ids
9
Out[89]:19:00
10
dna_# HPO_ID
11
0 25246 HP:0001263tHP:0001508tHP:0000252tHP:0001875...
12
1 29244 HP:0011344tHP:0008936tHP:0001257tHP:0005305...
13
2 6409 HP:0001263tHP:0000252tHP:0001629tHP:0001875...
14
15
# Converting the data in the dataframe into tuples:
16
17
_1 = tuple(numbers_and_ids.HPO_ID.str.split('t')[0])
18
_2 = tuple(numbers_and_ids.HPO_ID.str.split('t')[1])
19
_3 = tuple(numbers_and_ids.HPO_ID.str.split('t')[2])
20
21
# Creating the nested list d and calling the function:
22
23
robjects.r(f'd<-list(c{_1}, c{_2}, c{_3})')
24
ontology_similarity.get_sim_grid(ontology='hpo', term_sets='d')
25
That yielded the error message:
JavaScript
1
3
1
rpy2.rinterface_lib.embedded.RRuntimeError: Error in (function (ontology, information_content, term_sim_method, term_sim_mat, :
2
is.list(term_sets) & is.list(term_sets) is not TRUE
3
I checked if d is a list or not:
JavaScript
1
8
1
robjects.r(f'print(is.list(d))')
2
3
[1] TRUE
4
Out[92]:
5
<rpy2.robjects.vectors.BoolVector object at 0x7f591524a900> [RTYPES.LGLSXP]
6
R classes: ('logical',)
7
[ 1]
8
I’d be grateful to get any suggestion regarding how to call ontology_similarity.get_sim_grid()
.
Thanks.
Advertisement
Answer
Eventually I used robjects.r
to call the R package from R instead of from Python, and it worked.