Converting GRO files to PDB using Python

I’ve started my journey into the world of molecular dynamics, and so I’m getting used to the file formats in this field of science.

I needed to convert my .gro files into .pdb and this seemed to be tricky to implement until I came across this package called MDAnalysis. It’s a pretty nifty package and is pip installable!

So to start off import MDAnalysis and initiate a universe with your Gromacs file as the path.

import MDAnalysis as mda
universe = mda.Universe(path_to_gromacs_file)

Next, we will use the Writer object from MDAnalysis with the output file specified with the extension .pdb . The software is smart enough to know what you’re going for.

with MDAnalysis.Writer("all.pdb") as pdb:
pdb.write(universe)

As simple as that! Took me a little while to find out.

Alternatively,

If you have multiple frames you can write in the multiframe

with MDAnalysis.Writer("all.pdb", multiframe=True) as pdb:
for _ in universe.trajectory:
pdb.write(universe)