Skip to content

Commit

Permalink
Add function to check whether a compound SMILES has atom-mapping info (
Browse files Browse the repository at this point in the history
  • Loading branch information
avaucher authored Jul 31, 2023
1 parent 121d09c commit 1f439f4
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/rxn/chemutils/miscellaneous.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,3 +317,28 @@ def merge_reactions(*reactions: ReactionEquation) -> ReactionEquation:
agents.extend(reaction.agents)
products.extend(reaction.products)
return ReactionEquation(reactants=reactants, agents=agents, products=products)


def mol_has_atom_mapping(mol: Mol) -> bool:
"""
Whether at least one atom of an RDKit Mol contains an atom map number.
Args:
mol: RDKit Mol.
"""
atom: Atom
for atom in mol.GetAtoms():
if atom.GetAtomMapNum() != 0:
return True
return False


def smiles_has_atom_mapping(smiles: str) -> bool:
"""
Whether at least one atom of a compound SMILES contains an atom map number.
Args:
smiles: compound SMILES.
"""
mol = smiles_to_mol(smiles, sanitize=False)
return mol_has_atom_mapping(mol)
18 changes: 18 additions & 0 deletions tests/test_miscellaneous.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
merge_reactions,
remove_chiral_centers,
remove_double_bond_stereochemistry,
smiles_has_atom_mapping,
sort_any,
)
from rxn.chemutils.reaction_equation import ReactionEquation
Expand Down Expand Up @@ -260,3 +261,20 @@ def test_merge_reactions() -> None:
assert merge_reactions(r1, r2, r3, r4) == ReactionEquation(
["A", "B", "F"], ["C", "G", "H"], ["D", "I"]
)


def test_smiles_has_atom_mapping() -> None:
assert smiles_has_atom_mapping("[CH3:9][CH:8]([CH3:10])c1ccccc1")
assert smiles_has_atom_mapping("CCO[CH3:9]")
assert smiles_has_atom_mapping("C[CH3:9].O")

assert not smiles_has_atom_mapping("CC")
assert not smiles_has_atom_mapping("C[CH3]")
assert not smiles_has_atom_mapping("C[12CH3]")

# Note: does not care about valence, but SMILES must be valid
# invalid valence -> call succeeds:
assert smiles_has_atom_mapping("CF(C)CC[C:9]")
# invalid SMILES -> call raises:
with pytest.raises(InvalidSmiles):
_ = canonicalize_any("[CH3:9]C(")

0 comments on commit 1f439f4

Please sign in to comment.