[docs]defget_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 Readerifinspect.isclass(format):returnformat# ChainReader gets returned even if format is specifiedifnotisinstance(filename,np.ndarray)andutil.iterable(filename)andnotisstream(filename):format='CHAIN'# Only guess if format is not specifiedelifformatisNone:# Checks for specialised formatsifisinstance(filename,np.ndarray):# memoryreader slurps numpy arraysformat='MEMORY'elifisinstance(filename,mmtf.MMTFDecoder):# mmtf slurps mmtf objectformat='MMTF'else:# else let the guessing begin!format=util.guess_format(filename)format=format.upper()try:return_READERS[format]exceptKeyError:raiseValueError("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()))

[docs]defget_writer_for(filename,format=None,multiframe=None):"""Return an appropriate trajectory or frame writer class for `filename`. The format is determined by the `format` argument or the extension of `filename`. If `format` is provided, it takes precedence over the extension of `filename`. Parameters ---------- filename : str or ``None`` If no *format* is supplied, then the filename for the trajectory is examined for its extension and the Writer is chosen accordingly. If ``None`` is provided, then :class:`~MDAnalysis.coordinates.null.NullWriter` is selected (and all output is discarded silently). format : str (optional) Explicitly set a format. multiframe : bool (optional) ``True``: write multiple frames to the trajectory; ``False``: only write a single coordinate frame; ``None``: first try trajectory (multi frame writers), then the single frame ones. Default is ``None``. Returns ------- :class:`Writer` A Writer object Raises ------ ValueError: The format could not be deduced from `filename` or an unexpected value was provided for the `multiframe` argument. TypeError: No writer was found for the required format or the required `filename` argument was omitted. .. versionchanged:: 0.7.6 Added `multiframe` keyword; the default ``None`` reflects the previous behaviour. .. versionchanged:: 0.14.0 Removed the default value for the `format` argument. Now, the value provided with the `format` parameter takes precedence over the extension of `filename`. A :exc:`ValueError` is raised if the format cannot be deduced from `filename`. .. versionchanged:: 0.16.0 The `filename` argument has been made mandatory. """iffilenameisNone:format='NULL'elifformatisNone:try:root,ext=util.get_ext(filename)except(TypeError,AttributeError):# An AttributeError is raised if filename cannot# be manipulated as a string.# A TypeError is raised in py3.6# "TypeError: expected str, bytes or os.PathLike object"raiseValueError('File format could not be guessed from "{0}"'.format(filename))else:format=util.check_compressed_format(root,ext)format=format.upper()ifmultiframeisNone:# Multiframe takes priority, else use singleframeoptions=copy.copy(_SINGLEFRAME_WRITERS)# do copy to avoid changing in placeoptions.update(_MULTIFRAME_WRITERS)# update overwrites existing entrieserrmsg="No trajectory or frame writer for format '{0}'"elifmultiframeisTrue:options=_MULTIFRAME_WRITERSerrmsg="No trajectory writer for format '{0}'"elifmultiframeisFalse:options=_SINGLEFRAME_WRITERSerrmsg="No single frame writer for format '{0}'"else:raiseValueError("Unknown value '{0}' for multiframe,"" only True, False, None allowed""".format(multiframe))try:returnoptions[format]exceptKeyError:raiseTypeError(errmsg.format(format))

defget_parser_for(filename,format=None):"""Return the appropriate topology parser for `filename`. Automatic detection is disabled when an explicit `format` is provided. Parameters ---------- filename : str or mmtf.MMTFDecoder name of the topology file; if this is an instance of :class:`mmtf.MMTFDecoder` then directly use the MMTF format. format : str description of the file format Raises ------ ValueError If no appropriate parser could be found. """ifinspect.isclass(format):returnformat# Only guess if format is not providedifformatisNone:ifisinstance(filename,mmtf.MMTFDecoder):format='mmtf'else:format=util.guess_format(filename)format=format.upper()try:return_PARSERS[format]exceptKeyError:try:rdr=get_reader_for(filename)exceptValueError:raiseValueError("'{0}' isn't a valid topology format, nor a coordinate format\n"" from which a topology can be minimally inferred.\n"" You can use 'Universe(topology, ..., topology_format=FORMAT)'\n"" to explicitly specify the format and\n"" override automatic detection. Known FORMATs are:\n"" {1}\n"" See https://docs.mdanalysis.org/documentation_pages/topology/init.html#supported-topology-formats\n"" For missing formats, raise an issue at \n"" http://issues.mdanalysis.org".format(format,_PARSERS.keys()))else:return_PARSERS['MINIMAL']