Skip to content

Commit

Permalink
Merge pull request #768 from padix-key/rdkit
Browse files Browse the repository at this point in the history
Block RDKit logs if sanitation problems appear
  • Loading branch information
padix-key authored Mar 8, 2025
2 parents a750ac9 + 88fa196 commit e426358
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/biotite/interface/rdkit/mol.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
__author__ = "Patrick Kunzmann, Simon Mathis"
__all__ = ["to_mol", "from_mol"]

import copy
import numbers
import warnings
from collections import defaultdict
import numpy as np
import rdkit.Chem.AllChem as Chem
from rdkit.Chem import SanitizeFlags
from rdkit.rdBase import BlockLogs
from biotite.interface.version import requires_version
from biotite.interface.warning import LossyConversionWarning
from biotite.structure.atoms import AtomArray, AtomArrayStack
Expand Down Expand Up @@ -320,7 +322,10 @@ def from_mol(mol, conformer_id=None, add_hydrogen=None):
if add_hydrogen is None:
add_hydrogen = not _has_explicit_hydrogen(mol)
if add_hydrogen:
Chem.SanitizeMol(mol, SanitizeFlags.SANITIZE_ADJUSTHS)
mol = copy.deepcopy(mol)
with BlockLogs():
# Avoid modifying the input molecule
Chem.SanitizeMol(mol, SanitizeFlags.SANITIZE_ADJUSTHS)
mol = Chem.AddHs(mol, addCoords=False, addResidueInfo=False)

rdkit_atoms = mol.GetAtoms()
Expand Down Expand Up @@ -427,7 +432,8 @@ def from_mol(mol, conformer_id=None, add_hydrogen=None):
# Copy as 'Kekulize()' modifies the molecule in-place
mol = Chem.Mol(mol)
try:
Chem.Kekulize(mol)
with BlockLogs():
Chem.Kekulize(mol)
except Chem.KekulizeException:
warnings.warn(
"Kekulization failed, "
Expand Down

0 comments on commit e426358

Please sign in to comment.