Skip to content

Instantly share code, notes, and snippets.

@matteoferla
Last active March 15, 2024 19:13
Show Gist options
  • Save matteoferla/94eb8e4f8441ddfb458bfc45722469b8 to your computer and use it in GitHub Desktop.
Save matteoferla/94eb8e4f8441ddfb458bfc45722469b8 to your computer and use it in GitHub Desktop.
A small function to fix the pH protonation/deprotonation of RDKit Chem objects —fixes the charge of simple, i.e. biological cases. So amino acid is fine, but complex artificial drug won't be.
from rdkit import mol
def set_to_neutral_pH(mol: Chem):
"""
Not great, but does the job.
* Protonates amines, but not aromatic bound amines.
* Deprotonates carboxylic acid, phosphoric acid and sulfuric acid, without ruining esters.
"""
protons_added = 0
protons_removed = 0
for indices in mol.GetSubstructMatches(Chem.MolFromSmarts('[N;D1]')):
atom = mol.GetAtomWithIdx(indices[0])
if atom.GetNeighbors()[0].GetIsAromatic():
continue # aniline
atom.SetFormalCharge(1)
protons_added += 1
for indices in mol.GetSubstructMatches(Chem.MolFromSmarts('C(=O)[O;D1]')):
atom = mol.GetAtomWithIdx(indices[2])
# benzoic acid pKa is low.
atom.SetFormalCharge(-1)
protons_removed += 1
for indices in mol.GetSubstructMatches(Chem.MolFromSmarts('P(=O)[Oh1]')):
atom = mol.GetAtomWithIdx(indices[2])
# benzoic acid pKa is low.
atom.SetFormalCharge(-1)
protons_removed += 1
for indices in mol.GetSubstructMatches(Chem.MolFromSmarts('S(=O)(=O)[Oh1]')):
atom = mol.GetAtomWithIdx(indices[3])
# benzoic acid pKa is low.
atom.SetFormalCharge(-1)
protons_removed += 1
return (protons_added, protons_removed)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment