R: run a python function with reticulate passing arguments as R variables (need r_to_py()?)

64 Views Asked by At

I am using the Python program stitchr inside an R package, so I run it with reticulate.

I follow the instructions listed here.

I create a file "run_stitchr.py" with these contents:

# import stitchr
from Stitchr import stitchrfunctions as fxn
from Stitchr import stitchr as st

# specify details about the locus to be stitched
chain = 'TRB'
species = 'HUMAN'

# initialise the necessary data
tcr_dat, functionality, partial = fxn.get_imgt_data(chain, st.gene_types, species)
codons = fxn.get_optimal_codons('', species)

# provide details of the rearrangement to be stitched
tcr_bits = {'v': 'TRBV7-3*01', 'j': 'TRBJ1-1*01', 'cdr3': 'CASSYLQAQYTEAFF',
            'l': 'TRBV7-3*01', 'c': 'TRBC1*01',
            'skip_c_checks': False, 'species': species, 'seamless': False,
            '5_prime_seq': '', '3_prime_seq': '', 'name': 'TCR'}

# then run stitchr on that rearrangement
stitched = st.stitch(tcr_bits, tcr_dat, functionality, partial, codons, 3, '')

And I just run it like:

stitchr_results <- reticulate::py_run_file("run_stitchr.py")

This works perfect, but I want to try and get rid of that .py file, and instead produce the inputs needed in R, and pass them as arguments to the main function st.stitch() (as specified in the .py file).

This is my best attempt:

fxn <- reticulate::import("Stitchr.stitchrfunctions")
st <- reticulate::import("Stitchr.stitchr")
chain <- "TRB"
species <- "HUMAN"
output <- fxn$get_imgt_data(chain, st$gene_types, species)
tcr_dat <- output[[1]]
functionality <- output[[2]]
partial <- output[[3]]
codons <- fxn$get_optimal_codons('', species)
tcr_bits <- list(v = 'TRBV7-3*01',
                 j = 'TRBJ1-1*01',
                 cdr3 = 'CASSYLQAQYTEAFF',
                 l = 'TRBV7-3*01',
                 c = 'TRBC1*01',
                 skip_c_checks = 'False',
                 species = species,
                 seamless = 'False',
                 '5_prime_seq' = '',
                 '3_prime_seq' = '',
                 name = 'TCR')
stitched <- st$stitch(tcr_bits, tcr_dat, functionality, partial, codons, 3, '')

... but I get this:

Error in py_call_impl(callable, call_args$unnamed, call_args$named) : 
  KeyError: 'TRBV7-3'

I'm not very proficient with Python, but I suspect I need to convert the R variables into Python types (reticulate::r_to_py()?), but I tried every possibility I could think of, and still it does not work (note that all arguments are python lists, like tcr_bits in the .py file).

If anyone can shed some light here, I would appreciate it! Many thanks.

0

There are 0 best solutions below