####################################################################### BioSimSpace: Making biomolecular simulation a breeze!## Copyright: 2017-2024## Authors: Lester Hedges <lester.hedges@gmail.com>## BioSimSpace is free software: you can redistribute it and/or modify# it under the terms of the GNU General Public License as published by# the Free Software Foundation, either version 3 of the License, or# (at your option) any later version.## BioSimSpace is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the# GNU General Public License for more details.## You should have received a copy of the GNU General Public License# along with BioSimSpace. If not, see <http://www.gnu.org/licenses/>.#####################################################################"""Utility functions."""__author__="Lester Hedges"__email__="lester.hedges@gmail.com"__all__=["formalCharge"]importtempfileas_tempfilefrom..import_is_notebookfrom..importIOas_IOfrom..import_Utilsfrom..Units.Chargeimportelectron_chargeas_electron_chargefrom.._SireWrappersimportMoleculeas_Molecule
[docs]defformalCharge(molecule):""" Compute the formal charge on a molecule. This function requires that the molecule has explicit hydrogen atoms. Parameters ---------- molecule : :class:`Molecule <BioSimSpace._SireWrappers.Molecule>` A molecule object. Returns ------- formal_charge : :class:`Charge <BioSimSpace.Types.Charge>` The total formal charge on the molecule. """ifnotisinstance(molecule,_Molecule):raiseTypeError("'molecule' must be of type 'BioSimSpace._SireWrappers.Molecule'")fromrdkitimportChemas_ChemfromrdkitimportRDLoggeras_RDLogger# Disable RDKit warnings._RDLogger.DisableLog("rdApp.*")# Create a temporary working directory.tmp_dir=_tempfile.TemporaryDirectory()work_dir=tmp_dir.name# Zero the total formal charge.formal_charge=0# Run in the working directory.with_Utils.cd(work_dir):# Save the molecule to a PDB file._IO.saveMolecules("tmp",molecule,"PDB")# Read the ligand PDB into an RDKit molecule.mol=_Chem.MolFromPDBFile("tmp.pdb")# Compute the formal charge.formal_charge=_Chem.rdmolops.GetFormalCharge(mol)returnformal_charge*_electron_charge