def test2Params(self): """ tests handling of Issue187 """ m1 = Chem.MolFromSmiles('C(=O)[O-]') rdPartialCharges.ComputeGasteigerCharges(m1) m2 = Chem.MolFromSmiles('C(=O)[O-].[Na+]') rdPartialCharges.ComputeGasteigerCharges(m2) for i in range(m1.GetNumAtoms()): c1 = float(m1.GetAtomWithIdx(i).GetProp('_GasteigerCharge')) c2 = float(m2.GetAtomWithIdx(i).GetProp('_GasteigerCharge')) self.assertTrue(feq(c1, c2, 1e-4))
def test2Params(self): """ tests handling of Issue187 """ m1 = Chem.MolFromSmiles('C(=O)[O-]') rdPartialCharges.ComputeGasteigerCharges(m1) m2 = Chem.MolFromSmiles('C(=O)[O-].[Na+]') try: rdPartialCharges.ComputeGasteigerCharges(m2) except: self.fail('should not have hit an exception') for i in range(m1.GetNumAtoms()): c1 = float(m1.GetAtomWithIdx(i).GetProp('_GasteigerCharge')) c2 = float(m2.GetAtomWithIdx(i).GetProp('_GasteigerCharge')) self.failUnless(feq(c1, c2, 1e-4))
def CalculateMeanNCharge(mol): """ ################################################################# The average negative charge -->Mnc Usage: result=CalculateMeanNCharge(mol) Input: mol is a molecule object. Output: result is a numeric value. ################################################################# """ Hmol = Chem.AddHs(mol) GMCharge.ComputeGasteigerCharges(Hmol, iter_step) res = [] for atom in Hmol.GetAtoms(): res.append(float(atom.GetProp("_GasteigerCharge"))) if res == []: return 0 else: cc = numpy.array(res, "d") return round(numpy.mean(cc[cc < 0]), 3)
def compute_charges(self): chem = self.to_chem() rdPartialCharges.ComputeGasteigerCharges(chem) charges = [(atom.GetSymbol(), atom.GetDoubleProp('_GasteigerCharge')) for atom in chem.GetAtoms()] return charges
def CalculateMeanAbsoulteCharge(mol): """ ################################################################# The average absolute charge -->Mac Usage: result=CalculateMeanAbsoulteCharge(mol) Input: mol is a molecule object. Output: result is a numeric value. ################################################################# """ Hmol = Chem.AddHs(mol) GMCharge.ComputeGasteigerCharges(Hmol, iter_step) res = [] for atom in Hmol.GetAtoms(): res.append(float(atom.GetProp('_GasteigerCharge'))) if res == []: return 0 else: cc = numpy.array(res, 'd') return round(numpy.mean(numpy.absolute(cc)), 3)
def CalculateAllMaxNCharge(mol): """ ################################################################# Most negative charge on all atoms -->Qmin Usage: result=CalculateAllMaxNCharge(mol) Input: mol is a molecule object. Output: result is a numeric value. ################################################################# """ Hmol = Chem.AddHs(mol) GMCharge.ComputeGasteigerCharges(Hmol, iter_step) res = [] for atom in Hmol.GetAtoms(): res.append(float(atom.GetProp('_GasteigerCharge'))) if res == []: return 0 else: return round(min(res), 3)
def CalculateTotalNCharge(mol): """ ################################################################# The total negative charge -->Tnc Usage: result=CalculateTotalNCharge(mol) Input: mol is a molecule object. Output: result is a numeric value. ################################################################# """ Hmol = Chem.AddHs(mol) GMCharge.ComputeGasteigerCharges(Hmol, iter_step) res = [] for atom in Hmol.GetAtoms(): res.append(float(atom.GetProp('_GasteigerCharge'))) if res == []: return 0 else: cc = numpy.array(res, 'd') return round(sum(cc[cc < 0]), 3)
def CalculateLocalDipoleIndex(mol): """ ################################################################# Calculation of local dipole index (D) -->LDI Usage: result=CalculateLocalDipoleIndex(mol) Input: mol is a molecule object. Output: result is a numeric value. ################################################################# """ GMCharge.ComputeGasteigerCharges(mol, iter_step) res = [] for atom in mol.GetAtoms(): res.append(float(atom.GetProp('_GasteigerCharge'))) cc = [ numpy.absolute(res[x.GetBeginAtom().GetIdx()] - res[x.GetEndAtom().GetIdx()]) for x in mol.GetBonds() ] B = len(mol.GetBonds()) return round(sum(cc) / B, 3)
def CalculateAllSumSquareCharge(mol): """ ################################################################# The sum of square charges on all atoms -->Qass Usage: result=CalculateAllSumSquareCharge(mol) Input: mol is a molecule object. Output: result is a numeric value. ################################################################# """ Hmol = Chem.AddHs(mol) GMCharge.ComputeGasteigerCharges(Hmol, iter_step) res = [] for atom in Hmol.GetAtoms(): res.append(float(atom.GetProp('_GasteigerCharge'))) if res == []: return 0 else: return round(sum(numpy.square(res)), 3)
def atom_level_descriptors(mol, include=['functional'], asOneHot=False, ORIGINAL_VERSION=False): ''' Given an RDKit mol, returns an N_atom-long list of lists, each of which contains atom-level descriptors and their names returns: (label, attributes) ''' attributes = [[] for i in mol.GetAtoms()] labels = [] if 'functional' in include: [attributes[i].append(x[0]) \ for (i, x) in enumerate(rdMolDescriptors._CalcCrippenContribs(mol))] labels.append('Crippen contribution to logp') [attributes[i].append(x[1]) \ for (i, x) in enumerate(rdMolDescriptors._CalcCrippenContribs(mol))] labels.append('Crippen contribution to mr') [attributes[i].append(x) \ for (i, x) in enumerate(rdMolDescriptors._CalcTPSAContribs(mol))] labels.append('TPSA contribution') [attributes[i].append(x) \ for (i, x) in enumerate(rdMolDescriptors._CalcLabuteASAContribs(mol)[0])] labels.append('Labute ASA contribution') [attributes[i].append(x) \ for (i, x) in enumerate(EState.EStateIndices(mol))] labels.append('EState Index') rdPartialCharges.ComputeGasteigerCharges(mol) [attributes[i].append(float(a.GetProp('_GasteigerCharge'))) \ for (i, a) in enumerate(mol.GetAtoms())] labels.append('Gasteiger partial charge') # Gasteiger partial charges sometimes gives NaN for i in range(len(attributes)): if np.isnan(attributes[i][-1]): attributes[i][-1] = 0.0 [attributes[i].append(float(a.GetProp('_GasteigerHCharge'))) \ for (i, a) in enumerate(mol.GetAtoms())] labels.append('Gasteiger hydrogen partial charge') # Gasteiger partial charges sometimes gives NaN for i in range(len(attributes)): if np.isnan(attributes[i][-1]): attributes[i][-1] = 0.0 if 'structural' in include: [attributes[i].extend(atom_structural(mol.GetAtomWithIdx(i), asOneHot = asOneHot, ORIGINAL_VERSION = ORIGINAL_VERSION)) \ for i in range(len(attributes))] labels.append('--many structural--') return (labels, attributes)
def CalculateMolPartialCharges(Mol, MolCount): """Calculate partial atomic charges for a molecule.""" PartialCharges = [] if OptionsInfo["MMFFChargesMode"]: if AllChem.MMFFHasAllMoleculeParams(Mol): MMFFProp = AllChem.MMFFGetMoleculeProperties(Mol) PartialCharges = [ MMFFProp.GetMMFFPartialCharge(AtomIndex) for AtomIndex in range(Mol.GetNumAtoms()) ] else: MolName = RDKitUtil.GetMolName(Mol, MolCount) MiscUtil.PrintWarning( "Failed to calculate MMFF partial charges for molecule, %s: Missing forcefield parameters" % MolName) return (False, PartialCharges) else: rdPartialCharges.ComputeGasteigerCharges( Mol, nIter=OptionsInfo["NumIters"], throwOnParamFailure=OptionsInfo["AllowParamFailure"]) PartialCharges = [ Atom.GetProp("_GasteigerCharge") for Atom in Mol.GetAtoms() ] # Format charges... PartialCharges = [ "%.*f" % (OptionsInfo["Precision"], float(Value)) for Value in PartialCharges ] return (True, PartialCharges)
def test0HalgrenSet(self): smiSup = Chem.SmilesMolSupplier(os.path.join(RDConfig.RDBaseDir, 'Code', 'GraphMol', 'PartialCharges', 'Wrap', 'test_data', 'halgren.smi'), delimiter='\t') #parse the original file with open( os.path.join(RDConfig.RDBaseDir, 'Code', 'GraphMol', 'PartialCharges', 'Wrap', 'test_data', 'halgren_out.txt'), 'r') as infil: lines = infil.readlines() tab = Chem.GetPeriodicTable() olst = [] for mol in smiSup: rdPartialCharges.ComputeGasteigerCharges(mol) tstr = "Molecule: " tstr += mol.GetProp("_Name") olst.append(tstr) for i in range(mol.GetNumAtoms()): at = mol.GetAtomWithIdx(i) en = tab.GetElementSymbol(at.GetAtomicNum()) chg = float(at.GetProp("_GasteigerCharge")) tstr = "%i %s %6.4f" % (i, en, chg) olst.append(tstr) i = 0 for line in lines: self.assertTrue(line.strip() == olst[i]) i += 1
def CalculateRelativeNCharge(mol): """ ################################################################# The partial charge of the most negative atom divided by the total negative charge. -->Rnc Usage: result=CalculateRelativeNCharge(mol) Input: mol is a molecule object. Output: result is a numeric value. ################################################################# """ Hmol = Chem.AddHs(mol) GMCharge.ComputeGasteigerCharges(Hmol, iter_step) res = [] for atom in Hmol.GetAtoms(): res.append(float(atom.GetProp('_GasteigerCharge'))) if res == []: return 0 else: cc = numpy.array(res, 'd') if sum(cc[cc < 0]) == 0: return 0 else: return round(min(res) / sum(cc[cc < 0]), 3)
def test1PPDataset(self): fileN = os.path.join(RDConfig.RDBaseDir, 'Code', 'GraphMol', 'PartialCharges', 'Wrap', 'test_data', 'PP_descrs_regress.2.csv') infil = open(fileN, 'r') lines = infil.readlines() infil.close() infile = os.path.join(RDConfig.RDBaseDir, 'Code', 'GraphMol', 'PartialCharges', 'Wrap', 'test_data', 'PP_combi_charges.pkl') with open(infile, 'r') as cchtFile: buf = cchtFile.read().replace('\r\n', '\n').encode('utf-8') cchtFile.close() with io.BytesIO(buf) as cchFile: combiCharges = pickle.load(cchFile) for lin in lines: if (lin[0] == '#'): continue tlst = lin.strip().split(',') smi = tlst[0] rdmol = Chem.MolFromSmiles(smi) rdPartialCharges.ComputeGasteigerCharges(rdmol) nat = rdmol.GetNumAtoms() failed = False for ai in range(nat): rdch = float( rdmol.GetAtomWithIdx(ai).GetProp('_GasteigerCharge')) if not feq(rdch, combiCharges[smi][ai], 1.e-2): failed = True print(smi, ai, rdch, combiCharges[smi][ai]) if failed: rdmol.Debug() self.assertFalse(failed)
def graph_from_smiles(smiles): graph = MolGraph() mol = MolFromSmiles(smiles) if not mol: raise ValueError("Could not parse SMILES string:", smiles) atoms_by_rd_idx = {} rdPartialCharges.ComputeGasteigerCharges(mol) for atom in mol.GetAtoms(): add_Gasteiger = float(atom.GetProp('_GasteigerCharge')) if np.isnan(add_Gasteiger) or np.isinf(add_Gasteiger): add_Gasteiger = 0.0 new_atom_node = graph.new_node('atom', features=atom_features( atom, add_Gasteiger), rdkit_ix=atom.GetIdx()) atoms_by_rd_idx[atom.GetIdx()] = new_atom_node for bond in mol.GetBonds(): atom1_node = atoms_by_rd_idx[bond.GetBeginAtom().GetIdx()] atom2_node = atoms_by_rd_idx[bond.GetEndAtom().GetIdx()] new_bond_node = graph.new_node('bond', features=bond_features(bond)) new_bond_node.add_neighbors((atom1_node, atom2_node)) atom1_node.add_neighbors((atom2_node, )) mol_node = graph.new_node('molecule') mol_node.add_neighbors(graph.nodes['atom']) return graph
def get_charges(self, estimate=False): """ Get the partial atomic charges using either XTB or estimate with RDKit using the Gasteiger charge scheme :param estimate: (bool) :param guess: (bool) :return: """ if estimate and self.mol_obj is None: raise CgbindCritical( 'Cannot estimate charges without a rdkit molecule object') if estimate: try: rdPartialCharges.ComputeGasteigerCharges(self.mol_obj) charges = [ float( self.mol_obj.GetAtomWithIdx(i).GetProp( '_GasteigerCharge')) for i in range(self.n_atoms) ] except: logger.error('RDKit failed to generate charges') return None else: charges = calculations.get_charges(self) return charges
def testGithubIssue577(self): """ tests handling of Github issue 577 """ m1 = Chem.MolFromSmiles('CCO') from locale import setlocale, LC_NUMERIC try: setlocale(LC_NUMERIC, "de_DE") except: # can't set the required locale, might as well just return return rdPartialCharges.ComputeGasteigerCharges(m1) for at in m1.GetAtoms(): float(at.GetProp('_GasteigerCharge')) setlocale(LC_NUMERIC, "C") rdPartialCharges.ComputeGasteigerCharges(m1) for at in m1.GetAtoms(): float(at.GetProp('_GasteigerCharge'))
def testGithubIssue20(self): """ tests handling of Github issue 20 """ m1 = Chem.MolFromSmiles('CB(O)O') rdPartialCharges.ComputeGasteigerCharges(m1) chgs = [-0.030, 0.448, -0.427, -0.427] for i in range(m1.GetNumAtoms()): c1 = float(m1.GetAtomWithIdx(i).GetProp('_GasteigerCharge')) self.assertAlmostEqual(c1, chgs[i], 3)
def test3Params(self): """ tests handling of Issue187 """ m2 = Chem.MolFromSmiles('C(=O)[O-].[Na+]') try: rdPartialCharges.ComputeGasteigerCharges(m2, 12, 1) except: pass else: self.fail('should have hit an exception')
def CalculateLocalDipoleIndex(mol: Chem.Mol) -> float: """Calculate the local dipole index (D).""" GMCharge.ComputeGasteigerCharges(mol, iter_step) res = [] for atom in mol.GetAtoms(): res.append(float(atom.GetProp('_GasteigerCharge'))) cc = [numpy.absolute(res[x.GetBeginAtom().GetIdx()] - res[x.GetEndAtom().GetIdx()]) for x in mol.GetBonds()] B = len(mol.GetBonds()) return 0 if len(cc) == 0.0 else round(sum(cc) / B, 3)
def getAtomPartialChargeVector(mol): """ Atom Partial charge """ from rdkit.Chem import rdPartialCharges rdPartialCharges.ComputeGasteigerCharges(mol) return np.array( [float(a.GetProp('_GasteigerHCharge')) for a in mol.GetAtoms()], dtype=np.float16)
def process_molecule(self, pdb_file, use_esp=False): """ Processes a molecule from the passed PDB file if the file contents has no errors. :param pdb_file: path to the PDB file to process the molecule from. :return: a ProcessedMolecule object """ # NOTE: Gasteiger is an inappropriate algorithm for ESP calculation of proteins! # read a molecule from the PDB file try: mol = Chem.MolFromPDBFile(molFileName=pdb_file, removeHs=False, sanitize=True) except IOError: log.warning("Could not read PDB file.") return None if mol is None: log.warning("Bad pdb file found.") return None if use_esp: try: # add missing hydrogen atoms mol = rdMO.AddHs(mol, addCoords=True) # compute partial charges rdPC.ComputeGasteigerCharges(mol, throwOnParamFailure=True) except ValueError: log.warning("Bad Gasteiger charge evaluation.") return None # get the conformation of the molecule conformer = mol.GetConformer() # calculate the center of the molecule center = rdMT.ComputeCentroid(conformer, ignoreHs=False) atoms_count = mol.GetNumAtoms() atoms = mol.GetAtoms() def get_coords(i): coord = conformer.GetAtomPosition(i) return np.asarray([coord.x, coord.y, coord.z]) # set the coordinates, charges, VDW radii and atom count res = { "coords": np.asarray( [get_coords(i) for i in range(0, atoms_count)]) - np.asarray( [center.x, center.y, center.z]), "vdwradii": np.asarray( [self.periodic_table.GetRvdw(atom.GetAtomicNum()) for atom in atoms]) } if use_esp: res['charges'] = np.asarray([float(atom.GetProp("_GasteigerCharge")) for atom in atoms]) return res
def CalculateAllMaxNCharge(mol): Hmol = Chem.AddHs(mol) GMCharge.ComputeGasteigerCharges(Hmol, iter_step) res = [] for atom in Hmol.GetAtoms(): res.append(float(atom.GetProp('_GasteigerCharge'))) if res == []: return 0 else: return round(min(res), 3)
def gasteiger_charge(a, force_calc=False): """ Hacky way of getting gasteiger charge """ res = a.props.get('_GasteigerCharge', None) if res and not force_calc: return float(res) else: m = a.GetOwningMol() rdPartialCharges.ComputeGasteigerCharges(m) return float(a.props['_GasteigerCharge'])
def _CalculateElementMaxNCharge(mol, AtomicNum=6): Hmol = Chem.AddHs(mol) GMCharge.ComputeGasteigerCharges(Hmol, iter_step) res = [] for atom in Hmol.GetAtoms(): if atom.GetAtomicNum() == AtomicNum: res.append(float(atom.GetProp('_GasteigerCharge'))) if res == []: return 0 else: return round(min(res), 3)
def CalculateAllSumSquareCharge(mol: Chem.Mol): """Get the sum of square charges of all atoms.""" Hmol = Chem.AddHs(mol) GMCharge.ComputeGasteigerCharges(Hmol, iter_step) res = [] for atom in Hmol.GetAtoms(): res.append(float(atom.GetProp('_GasteigerCharge'))) if res == []: return 0 else: return round(sum(numpy.square(res)), 3)
def CalculateAllMaxNCharge(mol: Chem.Mol) -> float: """Get most negative charge of all atoms.""" Hmol = Chem.AddHs(mol) GMCharge.ComputeGasteigerCharges(Hmol, iter_step) res = [] for atom in Hmol.GetAtoms(): res.append(float(atom.GetProp('_GasteigerCharge'))) if res == []: return 0 else: return round(min(res), 3)
def CalculateTotalNCharge(mol: Chem.Mol) -> float: """Ge the total negative charge.""" Hmol = Chem.AddHs(mol) GMCharge.ComputeGasteigerCharges(Hmol, iter_step) res = [] for atom in Hmol.GetAtoms(): res.append(float(atom.GetProp('_GasteigerCharge'))) if res == []: return 0 else: cc = numpy.array(res, 'd') return round(sum(cc[cc < 0]), 3)
def CalculateMeanAbsoulteCharge(mol: Chem.Mol) -> float: """Get the average absolute charge.""" Hmol = Chem.AddHs(mol) GMCharge.ComputeGasteigerCharges(Hmol, iter_step) res = [] for atom in Hmol.GetAtoms(): res.append(float(atom.GetProp('_GasteigerCharge'))) if res == []: return 0 else: cc = numpy.array(res, 'd') return round(numpy.mean(numpy.absolute(cc)), 3)
def _CalculateElementMaxNCharge(mol: Chem.Mol, AtomicNum: int = 6) -> float: """Get the most negative charge of atom with specified atomic number.""" Hmol = Chem.AddHs(mol) GMCharge.ComputeGasteigerCharges(Hmol, iter_step) res = [] for atom in Hmol.GetAtoms(): if atom.GetAtomicNum() == AtomicNum: res.append(float(atom.GetProp('_GasteigerCharge'))) if res == []: return 0 else: return round(min(res), 3)