Skip to content

Commit 4321172

Browse files
author
Oliver Scott
committed
Update discharge functions to use rdMolStandardize
1 parent c6ce17c commit 4321172

File tree

2 files changed

+63
-7
lines changed

2 files changed

+63
-7
lines changed

scaffoldgraph/core/fragment.py

Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
BondType,
1818
GetSymmSSSR,
1919
RemoveHs,
20+
Descriptors,
2021
MurckoDecompose,
22+
MolStandardize,
2123
CHI_UNSPECIFIED,
2224
SANITIZE_ALL,
2325
SANITIZE_CLEANUP,
@@ -459,19 +461,73 @@ def keep_largest_fragment(mol):
459461
return max(frags, key=lambda x: x.GetNumAtoms())
460462

461463

462-
def discharge_and_deradicalize(mol):
463-
"""Discharge and remove radicals (brute force).
464+
def convert_radicals_to_hydrogen(mol):
465+
"""Converts radical electrons in a molecule into bonds to hydrogens.
464466
465-
This operation is performed in-place.
467+
Parameters
468+
----------
469+
mol : rdkit.Chem.rdchem.Mol
470+
471+
Returns
472+
-------
473+
mol : rdkit.Chem.rdchem.Mol
474+
New molecule with radical electrons converted into
475+
bonds to hydrogens.
476+
477+
"""
478+
m = Mol(mol)
479+
if Descriptors.NumRadicalElectrons(m) == 0:
480+
return m
481+
else:
482+
for atom in m.GetAtoms():
483+
nradicals = atom.GetNumRadicalElectrons()
484+
if nr > 0:
485+
atom.SetNumRadicalElectrons(0)
486+
atom.SetNumExplicitHs(nradicals)
487+
return m
488+
489+
490+
@suppress_rdlogger()
491+
def discharge_molecule(mol):
492+
"""Discharge molecules using the RDKit Uncharger class.
493+
494+
Uncharges molecules by adding and/or removing hydrogens.
495+
For zwitterions, hydrogens are moved to eliminate charges where possible.
496+
In cases where there is a positive charge that is not neutralizable,
497+
an attempt is made to also preserve the corresponding negative charge.
466498
467499
Parameters
468500
----------
469501
mol : rdkit.Chem.rdchem.Mol
470502
503+
Returns
504+
-------
505+
mol : rdkit.Chem.rdchem.Mol
506+
New molecule with charges eliminated where possible.
507+
471508
"""
472-
for atom in mol.GetAtoms():
473-
atom.SetFormalCharge(0)
474-
atom.SetNumRadicalElectrons(0)
509+
uncharger = MolStandardize.charge.Uncharger()
510+
out = uncharger.uncharge(mol)
511+
return out
512+
513+
514+
def discharge_and_deradicalize(mol):
515+
"""Discharge and remove radicals.
516+
517+
Parameters
518+
----------
519+
mol : rdkit.Chem.rdchem.Mol
520+
521+
Returns
522+
-------
523+
mol : rdkit.Chem.rdchem.Mol
524+
New molecule with charges eliminated where possible
525+
and radicals removed.
526+
527+
"""
528+
out = convert_radicals_to_hydrogen(mol)
529+
out = discharge_molecule(out)
530+
return out
475531

476532

477533
def remove_exocyclic_attachments(mol):

scaffoldgraph/core/graph.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ def _preprocess_scaffold(self, scaffold_rdmol, init_args):
266266
if init_args.get('keep_largest', False) is True:
267267
scaffold_rdmol = keep_largest_fragment(scaffold_rdmol)
268268
if init_args.get('discharge', False) is True:
269-
discharge_and_deradicalize(scaffold_rdmol)
269+
scaffold_rdmol = discharge_and_deradicalize(scaffold_rdmol)
270270
return scaffold_rdmol
271271

272272
def _process_no_top_level(self, molecule):

0 commit comments

Comments
 (0)