-
Notifications
You must be signed in to change notification settings - Fork 824
Description
Is your feature request related to a problem?
I'm trying to pass an h5py.File object into mda.Universe using H5MDReader (#2787), but have been running into funky errors and I think the issue is that h5py.File attributes aren't suited for MDAnalysis.lib.util.isstream and MDAnalysis.lib.util.iterable, due to h5py files not passing the isstream method and being classified as "iterable" due to having a len(h5py.File)
If I don't pass format="H5MD:
u = mda.Universe(TPR_xvf, h5py.File(H5MD_xvf, 'r'))
MDAnalysis.lib.util.guess_format() looks at isstream(h5py.File) and iterable(h5py.File, however, these return unexpected values for an h5py.File:

which makes guess_format think there are multiple trajectories that get passed to MDAnalysis.coordinates.chain.py which THEN gets passed to MDAnalysis.coordinates.core.py which fails and gives ValueError: Unknown coordinate trajectory format '' for 'h5md'., so it looks like it did try to iterate through the groups in the h5py.File:

If I do pass `format="H5MD":
u = mda.Universe(TPR_xvf, h5py.File(H5MD_xvf, 'r'), format="H5MD")
Again, it ends up calling MDAnalysis.coordinates.chain.py, but then passes it to H5MDReader where it fails with OSError: Unable to open file (unable to open file: name = 'h5md', errno = 2, error message = 'No such file or directory', flags = 0, o_flags = 0) . So, again, it looks like it tried to load the first group 'h5md' from the file as an individual trajectory
Describe the solution you'd like
I'd like h5py files to also pass the isstream method, even though it doesn't fit the criteria.
Describe alternatives you've considered
I've tried doing something like adding an if clause at the start of MDAnalysis.lib.util.guess_format with something like
import h5py
if isinstance(filename, h5py.File):
format = format_from_filename_extension(filename.filename)
but the same issues occur, so I'm not sure what to do to fix it.