Python gmxapi.error

GROMACS version:Version 2020
GROMACS modification: No
Here post your question

Dear all,

I am trying to incorporate python in order to simplify my work, this is the code that I have written:

import gmxapi as gmx

traj_comp= open(‘traj_comp.xtc’, ‘r’)
rama_file = open(‘rama.xvg’,‘w’)

grompp = gmx.commandline_operation(executable =[‘gmx_mpi’], arguments =[‘rama’, ‘-f’,‘traj_comp’,’-o’, ‘rama_file’],
input_files ={’-f’: ‘traj_comp’}, output_files = {’-o’: ‘rama_file’})

grompp.run()

However this error comes up:

raise exceptions.ApiError(message) from e
gmxapi.exceptions.ApiError: Uncaught {} while providing output-publishing resources for {}.

What am I doing wrong and can someone enlighten me? Thank you very much.

Best,

Ben

In the script you provide, you define python variables traj_comp and rama_file, but you provide file arguments as the strings “traj_comp” and “rama_file”.

Also, you should not duplicate arguments in arguments and input_files / output_files.

I think what you intend is

grompp = gmx.commandline_operation(executable =[‘gmx_mpi’],
    arguments =[‘rama’],
    input_files ={’-f’: traj_comp}, output_files = {’-o’: rama_file})

The next version of the software provides more informative error messages, but I have filed https://gitlab.com/gromacs/gromacs/-/issues/3693 to specifically check for the input file existence.

Hi Eric,

Thank you very much, now it works. So you don’t need the ending like “.xtc” or “.mdp”?

Furthermore, I have extra questions:
If I want to

echo 15 | gmx genion -neutral yes -p

how do I write that in the python code?

Best,

Ben

So you don’t need the ending like “.xtc” or “.mdp”?

That depends on the tool. Some programs are sensitive to the suffix on the filename.

To be clear: In the example you gave you created a variable called rama_file and assigned to it a string value of “rama.xvg”.

To refer to the variable, you don’t use quotes.

rama_file is the variable.

'rama_file' is a typo.

For your other question, commandline_operation accepts a stdin key word argument in gmxapi 0.2. You would write

commandline_operation(‘gmx_mpi’,
    arguments=[‘genion', '-neutral', 'yes', '-p’],
    stdin='15')

gmxapi 0.2 is available pre-release in the master GROMACS branch and in the upcoming 2021 beta release, or from PyPI with the --pre flag to pip.
E.g.

pip install --upgrade "gmxapi>=0.2.0a1"

Thank you very much!

I tried to do that without the quotation mark, however that caused error in my code. With the ‘’, I can run it. Is it some error or am I not setting it up correctly?

Best,

Ben

Hi Eric,

Thanks for the help. I have no problem doing that with editconf, pdb2gmx etc commands. However, it seems like genion doesn’t work at all as you describe above. Do you have any idea? there is no error, it doesn’t produce a new out.gro file.

Best,

Ben

You were able to update to gmxapi 0.2 alpha okay? Note that the updated documentation is available at http://manual.gromacs.org/nightly/gmxapi/userguide/pythonreference.html

Can you please share an example script that you are having trouble with?

Also, check the erroroutput result.

I think I gave you incomplete advice. I think you will need to add at least one “newline” to the stdin.

Try

cmd = commandline_operation(
    ‘gmx_mpi’,
    arguments=[‘genion', '-neutral', 'yes', '-p’],
    stdin='15\n')
cmd.run()

or

cmd = commandline_operation(
    ‘gmx_mpi’,
    arguments=[‘genion', '-neutral', 'yes', '-p’],
    stdin='15\n\n')
cmd.run()

and maybe

if cmd.output.returncode.result() != 0:
    print(cmd.output.erroroutput.result())