def read_molecule_file(input_file, mol_container): """Read input file (PDB or PROPKA) for a molecular container Args input_file: input file to read mol_container: MolecularContainer object Returns updated MolecularContainer object Raises ValuError if invalid input given """ input_path = Path(input_file) mol_container.name = input_path.stem input_file_extension = input_path.suffix if input_file_extension.lower() == '.pdb': # input is a pdb file. read in atoms and top up containers to make # sure that all atoms are present in all conformations conformations, conformation_names = read_pdb( input_path, mol_container.version.parameters, mol_container) if len(conformations) == 0: str_ = ('Error: The pdb file does not seems to contain any ' 'molecular conformations') raise ValueError(str_) mol_container.conformations = conformations mol_container.conformation_names = conformation_names mol_container.top_up_conformations() # make a structure precheck protein_precheck(mol_container.conformations, mol_container.conformation_names) # set up atom bonding and protonation mol_container.version.setup_bonding_and_protonation(mol_container) # Extract groups mol_container.extract_groups() # sort atoms for name in mol_container.conformation_names: mol_container.conformations[name].sort_atoms() # find coupled groups mol_container.find_covalently_coupled_groups() elif input_file_extension.lower() == '.propka_input': # input is a propka_input file conformations, conformation_names = read_propka( input_file, mol_container.version.parameters, mol_container) mol_container.conformations = conformations mol_container.conformation_names = conformation_names # Extract groups - this merely sets up the groups found in the # input file mol_container.extract_groups() # do some additional set up mol_container.additional_setup_when_reading_input_file() else: str_ = "Unknown input file type {0!s} for file {1!s}".format( input_file_extension, input_path) raise ValueError(str_) return mol_container
def read_molecule_file(filename: str, mol_container, stream=None): """Read input file or stream (PDB or PROPKA) for a molecular container Args: filename(str): name of input file. If not using a filestream via the ``stream`` argument, should be a path to the file to be read. mol_container: :class:`~propka.molecular_container.MolecularContainer` object. stream: optional filestream handle. If ``None``, then open ``filename`` as a local file for reading. Returns: updated :class:`~propka.molecular_container.MolecularContainer` object. Raises: ValuError: if invalid input given Examples: There are two main cases for using ``read_molecule_file``. The first (and most common) is to pass the input file (``filename``) as a string which gives the path of the molecule file to be read (here we also pass a :class:`~propka.molecular_container.MolecularContainer` object named ``mol_container``). >>> read_molecule_file('test.pdb', mol_container) <propka.molecular_container.MolecularContainer at 0x7f6e0c8f2310> The other use case is when passing a file-like object, e.g. a :class:`io.StringIO` class, instance. This is done by passing the object via the ``stream`` argument. Since file-like objects do not usually have an associated file name, an appropirate file name should be passed to the ``filename`` argument. In this case, ``filename`` is not opened for reading, but instead is used to help recognise the file type (based on the extension being `.pdb`) and also uses that given ``filename`` to assign a name to the input :class:`~propka.molecular_container.MolecularContainer` object. >>> read_molecule_file('test.pdb', mol_container, stream=string_io_object) <propka.molecular_container.MolecularContainer at 0x7f6e0c8f2310> .. versionchanged:: 3.4.0 PROPKA input files (extension: `.propka_input`) are no longer read. """ input_path = Path(filename) mol_container.name = input_path.stem input_file_extension = input_path.suffix if stream is not None: input_file = stream else: input_file = filename if input_file_extension.lower() == '.pdb': # input is a pdb file. read in atoms and top up containers to make # sure that all atoms are present in all conformations conformations, conformation_names = read_pdb( input_file, mol_container.version.parameters, mol_container) if len(conformations) == 0: str_ = ('Error: The pdb file does not seem to contain any ' 'molecular conformations') raise ValueError(str_) mol_container.conformations = conformations mol_container.conformation_names = conformation_names mol_container.top_up_conformations() # make a structure precheck protein_precheck(mol_container.conformations, mol_container.conformation_names) # set up atom bonding and protonation mol_container.version.setup_bonding_and_protonation(mol_container) # Extract groups mol_container.extract_groups() # sort atoms for name in mol_container.conformation_names: mol_container.conformations[name].sort_atoms() # find coupled groups mol_container.find_covalently_coupled_groups() else: str_ = "Unknown input file type {0!s} for file {1!s}".format( input_file_extension, input_path) raise ValueError(str_) return mol_container