0

I am building a toolbox for bioinformatical analysis of DNA sequences. All the statistical analysis is done in R. I have written the R scripts for these.

I am now building a GUI with Tkinter so users can choose a file with DNA information and run my R scripts on them. I am at the very beginning of the Tkinter script and I am stuck on getting the information (directory path) the user selects into my R script, which is then executed on that file.

Here is the test code if you want to run the GUI yourself:

import Tkinter
import tkMessageBox
import tkFileDialog
import os
import sys
import subprocess

top = Tkinter.Tk()
CDR3Merge = "PHB_forR.R"

#####----------Definitions------------####

#Definition to open file browser and select file.
def FileOpen():
    var.set(tkFileDialog.askopenfilename(defaultextension=".txt"))

#Definition to start R-Script
def CDR3Extract():
    os.popen("R CMD BATCH PHB_forR.R").read()


####----------Buttons and their Functions-------------####

#Button to browse and open/select file
ButtonOpenFile = Tkinter.Button(top, text="Open File", command = FileOpen)
ButtonOpenFile.grid(row=1, column=1)

#Button to Execute R-Script
ButtonCDR3Extract = Tkinter.Button(top, text="CDR3 Extraction", command = CDR3Extract)
ButtonCDR3Extract.grid(row=2,column=0)

#####----------Entry Bars and their Definition--------####
#Label at the very top
TopLabel = Tkinter.Label(text="CDR3 Extraction")
TopLabel.grid(row=0, column=0)

#Entry for file selection
var = Tkinter.StringVar()
FileEntry = Tkinter.Entry(top, width=60, textvariable= var)
FileEntry.grid(row=1,column=0)


####---------------------------------------------------####

top.mainloop()

And Im guessing we need to focus on this part:

  #Definition to open file browser and select file.
def FileOpen():
    var.set(tkFileDialog.askopenfilename(defaultextension=".txt"))

#Definition to start R-Script
def CDR3Extract():
    os.popen("R CMD BATCH PHB_forR.R").read()

I don´t know if it is necessary to post my R script, but the method/code with which I read my .txt file in R is following:

read.delim("file.txt",header=TRUE, sep="\t")

How can I turn the output of the askopenfile command from Tkinter into a variable used in my R script? Is this even possible or should I consider a different method of doing this?

Thank you.

Edit: After Dason recommended to check out commandArgs() in R I wanted to print the filepath together with the command to run the Rscript. So now the button CDR3 Extract will do folliwng:

def CDR3Extract():
        os.popen("Rscript PHB_forR.R" + " " + str(var).read()

var still being

var = Tkinter.SrtingVar()

The problem I am having now is that var is returned as PY_VAR0 instead of the filepath that is printed in my entry box.

I have tried var.get() and using that but it also returns as PY_VAR0.

Any suggestions? I am probably looking at this the wrong way I can´t find or come up with something more simple.

2
  • 1
    Check this out in R: help(commandArgs) Commented Dec 12, 2015 at 19:16
  • Thank you! I think I'm almost there, I have set up the R script to use as commandArgs() so when I execute Rscript PHB_forR.R filename.Rit will use filename.R as the file to read. However, this does not seem to work with the file in a different directory as the script and neither have I been able to get the path of the file into the definition/command in my python script. I thought os.popen("R CMD BATCH PHB_forR.R", var).read()would do it, as var is the path directory, however I get a error message that says TypeError: must be string, not instance" Commented Dec 12, 2015 at 21:02

1 Answer 1

0

Alright so here is what works:

def CDR3Extract():
os.popen("Rscript test_forR.R" + " " + var.get()).read()

as varis set to return the file path selected by the user it had to be added as var.get(). This way it does not return as PY_VAR0 but as the path. To recap how varis set:

def FileOpen():
    var.set(tkFileDialog.askopenfilename(defaultextension=".txt"))
var = Tkinter.StringVar()

Running Rscript script.R /path/to/file.txtworks, however, in order to get only the filepath when using commandArgs()I had to get rid of script.Rin R, because commandArgs()would give me:

filepath <- commandArgs()
filepath
[1] script.R
[2] /path/to/file.txt

so I used

filepath2 <- filepath[-1]
filepath2
[1] /path/to/file.txt

The file can then be read in R with

read.delim(filepath2,header=TRUE, sep="\t")

I hope I answered the question in a way outsiders can follow? Please let me know if I should go into more detail.

Thank you for your help.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.