MDAnalysis / mdanalysis

MDAnalysis is a Python library to analyze molecular dynamics simulations.
https://mdanalysis.org
Other
1.3k stars 648 forks source link

read mdtraj hdf5-based trajectory format #3483

Open csu1505110121 opened 2 years ago

csu1505110121 commented 2 years ago

Expected behavior

Dear all, I am using OpenMM for the molecular dynamics simulations, and with the help of mdtraj.reporters.HDF5Reporter, I could save the velocities of each frame into a h5 format, when I convert the h5 file to trr that could be recognized by gromacs, errors encountered as in mdtraj.save() the velocities and forces are both set to be zero. So, I sought to mdanalysis, as it stores these information I need. However, when I try to load the h5 file generated by mdtraj with the api provided by MDanalysis, the errors were throw out. The code I tried was detailed as below

Actual behavior | codes | errors

code:

import MDAnalysis as mda
u = mda.Universe(params['dir']+str(smiles[0])+'/'+str(smiles[0])+'.pdb',params['dir']+str(smiles[0])+'/output.traj.h5',format='H5MD')

errors:

h5py/_objects.pyx in h5py._objects.with_phil.wrapper()

h5py/_objects.pyx in h5py._objects.with_phil.wrapper()

h5py/h5o.pyx in h5py.h5o.open()

KeyError: "Unable to open object (object 'particles' doesn't exist)"

Current version of MDAnalysis

>>> import MDAnalysis as mda
>>> print(mda.__version__)
2.0.0
>>>

the python version is Python 3.8.12 | packaged by conda-forge | (default, Oct 12 2021, 21:59:51) Operating system is centos 7

Best regards, Qiang

IAlibay commented 2 years ago

I believe that the H5MD and mdtraj HDF5 formats are actually two different things (annoyingly another pseudo naming clash).

It's been on my to-do list to add an mdtraj HDF5 parser for a long time but I never get around to it. I'll bump it up my priority list unless someone else gets to it first.

I'm not sure I have a temporary solution for your issue. Having a quick look around mdtraj, it seems that there's not that many options when it comes to velocity & forces writing (unless I'm looking in the wrong place). A "simple" hack could be to 1) load the TRR in MDAnalysis and the H5 trajectory in MDtraj, 2) iterate through both trajectories, 3) at each iteration step overwrite the MDAnalysis universe's velocities and forces with those in MDtraj for that time step (transformed appropriately to deal with the unit conversion) and then directly write that frame to a new TRR file.