Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions chebi_utils/sdf_extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,24 @@
import gzip
import warnings
from pathlib import Path
from typing import Optional

import pandas as pd
from rdkit import Chem

from chebi_utils.obo_extractor import _chebi_id_to_str


def _sanitize_molecule(mol: Chem.Mol) -> Chem.Mol:
"""Sanitize molecule, mirroring the ChEBI molecule processing."""
def _sanitize_molecule(mol: Chem.Mol) -> Optional[Chem.Mol]:
"""Sanitize molecule"""
from chembl_structure_pipeline.standardizer import update_mol_valences

mol = update_mol_valences(mol)
Chem.SanitizeMol(
mol,
sanitizeOps=Chem.SanitizeFlags.SANITIZE_FINDRADICALS
| Chem.SanitizeFlags.SANITIZE_KEKULIZE
| Chem.SanitizeFlags.SANITIZE_SETAROMATICITY
| Chem.SanitizeFlags.SANITIZE_SETCONJUGATION
| Chem.SanitizeFlags.SANITIZE_SETHYBRIDIZATION
| Chem.SanitizeFlags.SANITIZE_SYMMRINGS,
catchErrors=True,
)
try:
Chem.SanitizeMol(mol)
except Exception as e:
warnings.warn(f"Failed to sanitize molecule: {e}", stacklevel=2)
mol = None
return mol


Expand All @@ -53,6 +49,9 @@ def _parse_molblock(molblock: str, chebi_id: str | None = None) -> Chem.Mol | No
warnings.warn(f"Failed to parse molblock for {chebi_id}", stacklevel=2)
return None
mol = _sanitize_molecule(mol)
if mol is None:
warnings.warn(f"Failed to sanitize molblock for {chebi_id}", stacklevel=2)

return mol


Expand Down
Loading