Source code for gloryxr.utils
"""
Utility functions for GLORYxR metabolite prediction.
"""
from rdkit.Chem.rdchem import Mol
from rdkit.Chem.rdChemReactions import ChemicalReaction
from rdkit.Chem.rdmolfiles import MolToSmiles
__all__ = ["reactions_to_table", "extract_smiles_for_soms", "mol_without_mappings"]
# We import pandas inside of the function so that the package may
# continue working without a hard dependency on pandas.
[docs]
def reactions_to_table(
reactions: list[ChemicalReaction],
) -> "pandas.DataFrame": # pyright: ignore[reportUndefinedVariable] # noqa: F821
"""
Convert chemical reactions to a pandas DataFrame.
Args:
reactions: Iterable of chemical reactions
Returns:
DataFrame with Educt, Product, and Reaction columns, as well as additional columns for certain reaction properties.
"""
import pandas as pd
return pd.DataFrame(
[
{
"Educt": reaction.GetReactants()[0],
"Product": reaction.GetProducts()[0],
"Reaction": (
reaction.GetProp("_Name") if reaction.HasProp("_Name") else None
),
"Subset": reaction.GetProp("_Subset")
if reaction.HasProp("_Subset")
else None,
"Priority": reaction.GetProp("_Priority")
if reaction.HasProp("_Priority")
else None,
}
for reaction in reactions
]
).rename_axis("ID")
[docs]
def mol_without_mappings(mol: Mol) -> Mol:
"""
Remove atom mapping number information from a molecule.
Args:
mol: RDKit molecule object
Returns:
Copy of the given molecule with mapping information removed.
"""
mol_ = Mol(mol)
for atom in mol_.GetAtoms():
atom.SetAtomMapNum(0)
return mol_