Controlling external program with python

771 Views Asked by At

for my studies I need to process a lot of data. The data needs to be analyzed by a program called VMD and should finally be stored in an excel sheet. My aim is to automate the whole process instead of copy pasting all the different files.

I am a complete beginner with python and at the very start of writing my own program. I managed to open VMD with a python script with:

import subprocess

result = subprocess.run(['C:Program Files (x86)\\University of Illinois\\VMD\\vmd.exe', "-c", "C:\\Users\\MrTemper\\Documents\\HiWi\\Trajektorien\\Traj_2_5_to_10ps_IFR_MD_298K_1.pdb"], shell=True)

but now I want the program to load the .pdb file into the VMD program. If I run the program an error occurs: "The system could not find the given path".

How do I solve this problem? And what is the general way to control the GUI VMD program with a python script?

Answers in german language are also possible. Thanks.

1

There are 1 best solutions below

1
On

I had the same problem and created a little program to help me run vmd.

VMD accepts a script when starting with the flag -e <script>. So we can use this to pass the routine we need it to do.

That means we will have to program in tcl and use python to load our script into vmd. Then vmd can run our analysis and give an output.

In my case I created junks of tcl scripts and loaded them dynamically with python, depending on my problem, to create one analysis script and pass it as an arg. Something like this:

script = prepare_tcl_script() # create script
script_file = save_script(script) # save it to a tcl file
cmd = ['vmd', '-e', script_file]
subprocess.Popen(cmd)

So in your case you will need to create a tcl script where you pass all the information, including the pdb file, that you want vmd to analyze. Then you can call it with python.

Or alternatively, if you don't want to create a script, you can just pass information as stdin to vmd. This was my solution:

vmd_process = subprocess.Popen('vmd', stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.STDOUT)
grep_stdout = vmd_process.communicate(input=b'puts "Hi!"')

Then you can even read it with grep_stdout.decode().

Either way you won't escape from programming in tcl, but think of this as another lang on your cv :)