Error while using rpy2
0
0
Entering edit mode
14 months ago

Hello everyone, I am trying to do some data analysis which requires both Python and R code chunks. So I am using R in Python using rpy2. Till now, the analysis worked fine, but when I tried doing the Differential Gene expression code, it gave me this error:

RParsingError: Parsing status not OK - PARSING_STATUS.PARSE_ERROR

The error starts from here:

RParsingError                             Traceback (most recent call last)
/var/folders/5y/5gmkj36s11v1y774zthcythh0000gn/T/ipykernel_18714/2325256096.py in <module>
----> 1 get_ipython().run_cell_magic(
      2 "R",
      3 "-i adata_test -o end_de fib_de", #%%R -i adata_test -o end_de -o fib_de
      4 '''
      5 #convert singlecellexperiment to singlecellassay type as required by MAST
~/opt/anaconda3/lib/python3.9/site-packages/IPython/core/interactiveshell.py in run_cell_magic(self, magic_name, line, cell)
   2417             with self.builtin_trap:
   2418                 args = (magic_arg_s, cell)
-> 2419                 result = fn(*args, **kwargs)
   2420             return result
   2421 
~/opt/anaconda3/lib/python3.9/site-packages/decorator.py in fun(*args, **kw)
    230             if not kwsyntax:
    231                 args, kw = fix(args, kw, sig)
--> 232             return caller(func, *(extras + args), **kw)
    233     fun.__name__ = func.__name__
    234     fun.__doc__ = func.__doc__*

I tried the solution given parsing error but that also did not work.

Can anyone please help me here and suggest me the possible solution.

Thank you in advance.

R rpy2 Python • 1.4k views
ADD COMMENT
0
Entering edit mode

You don't show what is in the cell that is triggering this issue. And what in particular do you mean by "Till now, the analysis worked fine, "? Meaning the cells in the notebook all worked up to then? Or that notebook and that cell has worked as written before on the same machine and no updates have been done? Then you say you tried the workaround suggested but specifically what you tried isn't shown either, and so it is hard to judge if you implemented it. Does the case like there in the original post throw an error similar to the original post so you can share a more minimal, reproducible example? That would make things much easier to debug.

ADD REPLY
0
Entering edit mode

Thanks for your comment. The code that causes this is :

%%R -i adata_test -o ent_de -o paneth_de

#Convert SingleCellExperiment to SingleCellAssay type as required by MAST
sca <- SceToSingleCellAssay(adata_test, class = "SingleCellAssay")

#Scale Gene detection rate
colData(sca)$n_genes = scale(colData(sca)$n_genes)

#Create data subsets for paneth and Enterocyte subpopulations
sca_ent <- subset(sca, with(colData(sca), louvain_r0.5=='Enterocyte'))
sca_paneth <- subset(sca, with(colData(sca), louvain_r0.5=='Paneth'))


#Filter out non-expressed genes in the subsets
print("Dimensions before subsetting:")
print(dim(sca_ent))
print(dim(sca_paneth))
print("")

sca_ent_filt = sca_ent[rowSums(assay(sca_ent)) != 0, ]
sca_paneth_filt = sca_paneth[rowSums(assay(sca_paneth)) != 0, ]

print("Dimensions after subsetting:")
print(dim(sca_ent_filt))
print(dim(sca_paneth_filt))


#Define & run hurdle model - Enterocytes
zlmCond_ent <- zlm(formula = ~prox_dist + donor + n_genes, sca=sca_ent_filt)
summaryCond_ent <- summary(zlmCond_ent, doLRT='prox_distProximal')
summaryDt_ent <- summaryCond_ent$datatable

result_ent <- merge(summaryDt_ent[contrast=='prox_distProximal' & component=='H',.(primerid, `Pr(>Chisq)`)], #P-vals
                 summaryDt_ent[contrast=='prox_distProximal' & component=='logFC', .(primerid, coef)],
                 by='primerid') #logFC coefficients

#Correct for multiple testing (FDR correction) and filtering
result_ent[,FDR:=p.adjust(`Pr(>Chisq)`, 'fdr')]
ent_de = result_ent[result_ent$FDR<0.01,, drop=F]
ent_de = ent_de[order(ent_de$FDR),]


#Define & run hurdle model - paneth cells
zlmCond_paneth <- zlm(formula = ~prox_dist + donor + n_genes, sca=sca_paneth_filt)
summaryCond_paneth <- summary(zlmCond_paneth, doLRT='prox_distProximal')
summaryDt_paneth <- summaryCond_paneth$datatable

result_paneth <- merge(summaryDt_paneth[contrast=='prox_distProximal' & component=='H',.(primerid, `Pr(>Chisq)`)], #P-vals
                 summaryDt_paneth[contrast=='prox_distProximal' & component=='logFC', .(primerid, coef)],
                 by='primerid') #logFC coefficients

#Correct for multiple testing (FDR correction) and filtering
result_paneth[,FDR:=p.adjust(`Pr(>Chisq)`, 'fdr')]
paneth_de = result_paneth[result_paneth$FDR<0.01,, drop=F]
paneth_de = paneth_de[order(paneth_de$FDR),]

"Till now, the analysis worked fine, " means yes the cells in the notebook all worked fine as well as no updates have been made. And yes the error in the example shows similar error while using:

get_ipython().run_cell_magic(
"R",
"-i adata_test -o end_de fib_de", #%%R -i adata_test -o end_de -o fib_de
'''
#convert singlecellexperiment to singlecellassay type as required by MAST
sca <- SceToSingleCellAssay(adata_test, class = "SingleCellAssay")

#scale gene detection rate
colData(sca)$n_genes = scale(colData(sca)$n_genes)

I hope it gives a better idea of the problem.

Thank you for any help!

ADD REPLY
0
Entering edit mode

Separating out weird characters and parsing from the %%R magic command input not working can be difficult. (I thought I had seen a recent StackOverflow post that had a weird character that had creeped in, but I cannot find it right now and maybe it got deleted.) One direction to go would be seeing if you can run a simpler variation on that command in pure R. Not necessarily your data even. Just some toy test. Then see if you can convert it to work in the notebook using the %%R magic command variation. (Or the workaround.)

Another direction would be to keep trying what you are trying to run in the notebook with the %%R magic. Two directions to pursue there. First option: Simplify as much as you can. Remove comments. Move as many commands as you can to the next cell for now. Second option: The other direction along that line of continuing to try in the notebook is to keep trying the SO workaround option. What you posted in your last reply didn't implement that adequately. That is not valid python. Python is very syntax and whitespace sensitive. Keep it very simple, too. Remove comments, blank lines, and anything not absolutely essential.

ADD REPLY

Login before adding your answer.

Traffic: 1997 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6