def get_reader_for(filename, format=None):
    """Return the appropriate trajectory reader class for `filename`.

    Parameters
    ----------
    filename
        filename of the input trajectory or coordinate file.  Also can
        handle a few special cases, see notes below.
    format : str or :class:`Reader` (optional)
        Define the desired format.  Can be a string to request a given
        Reader.
        If a class is passed, it will be assumed that this is
        a Reader and will be returned.

    Returns
    -------
    :class:`Reader`
        A Reader object


    Raises
    ------
    ValueError
        If no appropriate Reader is found


    Notes
    -----
    There are a number of special cases that can be handled:

    - If `filename` is a numpy array,
      :class:`~MDAnalysis.coordinates.memory.MemoryReader` is returned.
    - If `filename` is an MMTF object,
      :class:`~MDAnalysis.coordinates.MMTF.MMTFReader` is returned.
    - If `filename` is an iterable of filenames,
      :class:`~MDAnalysis.coordinates.chain.ChainReader` is returned.

    Automatic detection is disabled when an explicit `format` is provided,
    unless a list of filenames is given, in which case
    :class:`~MDAnalysis.coordinates.chain.ChainReader` is returned and `format`
    passed to the :class:`~MDAnalysis.coordinates.chain.ChainReader`.

    """
    # check if format is actually a Reader
    if inspect.isclass(format):
        return format

    # ChainReader gets returned even if format is specified
    if not isinstance(filename, np.ndarray) and util.iterable(
            filename) and not isstream(filename):
        format = 'CHAIN'
    # Only guess if format is not specified
    elif format is None:
        # Checks for specialised formats
        if isinstance(filename, np.ndarray):
            # memoryreader slurps numpy arrays
            format = 'MEMORY'
        elif isinstance(filename, mmtf.MMTFDecoder):
            # mmtf slurps mmtf object
            format = 'MMTF'
        else:
            # else let the guessing begin!
            format = util.guess_format(filename)
    format = format.upper()
    try:
        return _READERS[format]
    except KeyError:
        raise ValueError(
            "Unknown coordinate trajectory format '{0}' for '{1}'. The FORMATs \n"
            "           {2}\n"
            "           are implemented in MDAnalysis.\n"
            "           See https://docs.mdanalysis.org/documentation_pages/coordinates/init.html#id1\n"
            "           Use the format keyword to explicitly set the format: 'Universe(...,format=FORMAT)'\n"
            "           For missing formats, raise an issue at "
            "http://issues.mdanalysis.org".format(format, filename,
                                                  _READERS.keys()))
Beispiel #2
0
 def test_StringIO_read(self):
     with open(datafiles.PSF, "r") as f:
         obj = StringIO(f.read())
     assert_equal(util.isstream(obj), True)
     obj.close()
Beispiel #3
0
 def test_StringIO_write(self):
     obj = StringIO()
     assert_equal(util.isstream(obj), True)
     obj.close()
Beispiel #4
0
 def test_iterator(self):
     obj = (i for i in range(3))
     assert_equal(util.isstream(obj), False)
Beispiel #5
0
 def test_file(self):
     with open(datafiles.PSF) as obj:
         assert_equal(util.isstream(obj), True)
Beispiel #6
0
 def test_string(self):
     obj = datafiles.PSF  # filename
     assert_equal(util.isstream(obj), False)
Beispiel #7
0
 def test_list(self):
     obj = [1, 2, 3]
     assert_equal(util.isstream(obj), False)
Beispiel #8
0
 def test_StringIO_read(self):
     with open(datafiles.PSF, "r") as f:
         obj = StringIO.StringIO(f)
     assert_equal(util.isstream(obj), True)
     obj.close()
Beispiel #9
0
 def test_StringIO_write(self):
     obj = StringIO.StringIO()
     assert_equal(util.isstream(obj), True)
     obj.close()
Beispiel #10
0
 def test_file(self):
     with open(datafiles.PSF) as obj:
         assert_equal(util.isstream(obj), True)
Beispiel #11
0
 def test_iterator(self):
     obj = (i for i in xrange(3))
     assert_equal(util.isstream(obj), False)
Beispiel #12
0
 def test_list(self):
     obj = [1, 2, 3]
     assert_equal(util.isstream(obj), False)
Beispiel #13
0
 def test_string(self):
     obj = datafiles.PSF  # filename
     assert_equal(util.isstream(obj), False)
Beispiel #14
0
def get_reader_for(filename, format=None):
    """Return the appropriate trajectory reader class for `filename`.

    Parameters
    ----------
    filename
        filename of the input trajectory or coordinate file.  Also can
        handle a few special cases, see notes below.
    format : str or :class:`Reader` (optional)
        Define the desired format.  Can be a string to request a given
        Reader.
        If a class is passed, it will be assumed that this is
        a Reader and will be returned.

    Returns
    -------
    :class:`Reader`
        A Reader object


    Raises
    ------
    ValueError
        If no appropriate Reader is found


    Notes
    -----
    There are a number of special cases that can be handled:

    - If `filename` is a numpy array,
      :class:`~MDAnalysis.coordinates.memory.MemoryReader` is returned.
    - If `filename` is an MMTF object,
      :class:`~MDAnalysis.coordinates.MMTF.MMTFReader` is returned.
    - If `filename` is an iterable of filenames,
      :class:`~MDAnalysis.coordinates.chain.ChainReader` is returned.

    Automatic detection is disabled when an explicit `format` is provided,
    unless a list of filenames is given, in which case
    :class:`~MDAnalysis.coordinates.chain.ChainReader` is returned and `format`
    passed to the :class:`~MDAnalysis.coordinates.chain.ChainReader`.

    """
    # check if format is actually a Reader
    if inspect.isclass(format):
        return format

    # ChainReader gets returned even if format is specified
    if not isinstance(filename, np.ndarray) and util.iterable(filename) and not isstream(filename):
        format = 'CHAIN'
    # Only guess if format is not specified
    elif format is None:
        # Checks for specialised formats
        if isinstance(filename, np.ndarray):
            # memoryreader slurps numpy arrays
            format = 'MEMORY'
        elif isinstance(filename, mmtf.MMTFDecoder):
            # mmtf slurps mmtf object
            format = 'MMTF'
        else:
            # else let the guessing begin!
            format = util.guess_format(filename)
    format = format.upper()
    try:
        return _READERS[format]
    except KeyError:
        raise ValueError(
            "Unknown coordinate trajectory format '{0}' for '{1}'. The FORMATs \n"
            "           {2}\n"
            "           are implemented in MDAnalysis.\n"
            "           See https://docs.mdanalysis.org/documentation_pages/coordinates/init.html#id1\n"
            "           Use the format keyword to explicitly set the format: 'Universe(...,format=FORMAT)'\n"
            "           For missing formats, raise an issue at "
            "http://issues.mdanalysis.org".format(
                format, filename, _READERS.keys()))