Skip to content

Instantly share code, notes, and snippets.

@theavey
Created January 28, 2019 19:10
Show Gist options
  • Save theavey/e4ff34e63256a66a1cdf67168b5379f3 to your computer and use it in GitHub Desktop.
Save theavey/e4ff34e63256a66a1cdf67168b5379f3 to your computer and use it in GitHub Desktop.
Convert trajectory of normal modes to nmd format for visualizing with NMWiz
def convert_univ_to_nmd(u, nmd_name, title='generic_title'):
"""
Convert MDAnalysis.Universe normal mode trajectory to nmd format
u should be a MDAnalysis.Universe with each frame of the trajectory
being the cartesian displacements for a single normal mode.
For example, the trajectory written by the nmeig tool of GROMACS
(http://manual.gromacs.org/documentation/2018/onlinehelp/gmx-nmeig.html)
gives an appropriate trajectory with the -v option.
The file written with nmd_name will be in the nmd format that
can be read by the NMWiz plugin to VMD
(https://www.ks.uiuc.edu/Research/vmd/plugins/nmwiz/).
"""
with open(nmd_name, 'w') as f_out:
w = f_out.write
w(f'title {title}\nnames')
for a in u.atoms:
w(f' {a.name}')
w('\nresnames ')
for a in u.atoms:
w(f' {a.resname}')
w('\nresnums ')
for a in u.atoms:
w(f' {a.resid}')
w('\nchids ')
for a in u.atoms:
w(f' {a.segid}')
w('\ncoordinates ')
u.trajectory[0]
for a in u.atoms:
pos = a.position
w(f' {pos[0]} {pos[1]} {pos[2]}')
for i in range(1, len(u.trajectory)):
u.trajectory[i]
inv_freq = 1. / np.sqrt(u.trajectory.time)
inv_freq = inv_freq if not np.isnan(inv_freq) else 1.
w(f'\nmode {i} {inv_freq} ')
for a in u.atoms:
pos = a.position
w(f' {pos[0]} {pos[1]} {pos[2]}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment