diff --git a/pygmx/__init__.py b/pygmx/__init__.py index b064c28..aeab992 100644 --- a/pygmx/__init__.py +++ b/pygmx/__init__.py @@ -5,3 +5,21 @@ Python wrapper for the gromacs library. from .tpxio import TPXReader from .xtcio import XTCReader # from .enxio import EDRFile +from .errors import FileTypeError +from .gromacs.reader import index_filename_for_xtc + +FILE_EXTENSIONS = { + 'xtc': XTCReader, + 'tpr': TPXReader +} + + +def open(filename): + """Open a supported gromacs file with the appropiate reader.""" + ext = filename.split('.')[-1] + if ext in FILE_EXTENSIONS: + if ext in ['xtc']: + indexfile = index_filename_for_xtc(filename) + return FILE_EXTENSIONS[ext](filename, indexfile) + else: + raise FileTypeError('Filetype {} not supported by pygmx.'.format(ext)) diff --git a/pygmx/errors.py b/pygmx/errors.py new file mode 100644 index 0000000..3be2849 --- /dev/null +++ b/pygmx/errors.py @@ -0,0 +1,16 @@ +"""Exceptions of the pygmx package.""" + + +class InvalidMagicException(Exception): pass + + +class InvalidIndexException(Exception): pass + + +class UnknownLenError(Exception): pass + + +class FileTypeError(Exception): pass + + +class XTCError(Exception): pass diff --git a/pygmx/xtcio.pyx b/pygmx/xtcio.pyx index 8bf334b..2960d15 100644 --- a/pygmx/xtcio.pyx +++ b/pygmx/xtcio.pyx @@ -11,7 +11,8 @@ import os from utility cimport * from math cimport * from fileio cimport * -from gromacs.reader import InvalidIndexException, InvalidMagicException, INDEX_MAGIC, SubscriptableReader, XTCFrame +from .gromacs.reader import INDEX_MAGIC, SubscriptableReader, XTCFrame +from .errors import InvalidIndexException, InvalidMagicException, XTCError cdef extern from "gromacs/fileio/xtcio.h": @@ -67,9 +68,6 @@ cdef array get_xtc_index(t_fileio *fio): return cache[:-1] -class XTCError(Exception): pass - - cdef class XTCReader: cdef: t_fileio *fio