-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split
mol_to_smiles
into a util (#48)
- Loading branch information
1 parent
9120b99
commit f35492f
Showing
3 changed files
with
65 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import pytest | ||
from rdkit import Chem | ||
|
||
from descent.utils.molecule import mol_to_smiles | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"input_smiles, expected_smiles, canonical", | ||
[ | ||
("OC", "[H:1][C:4]([H:2])([O:3][H:5])[H:6]", True), | ||
("OC", "[O:1]([C:2]([H:4])([H:5])[H:6])[H:3]", False), | ||
], | ||
) | ||
def test_mol_to_smiles(input_smiles, expected_smiles, canonical): | ||
mol = Chem.MolFromSmiles(input_smiles) | ||
actual_smiles = mol_to_smiles(mol, canonical) | ||
|
||
assert actual_smiles == expected_smiles |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import typing | ||
|
||
if typing.TYPE_CHECKING: | ||
from rdkit import Chem | ||
|
||
|
||
def mol_to_smiles(mol: "Chem.Mol", canonical: bool = True) -> str: | ||
"""Convert a molecule to a SMILES string with atom mapping. | ||
Args: | ||
mol: The molecule to convert. | ||
canonical: Whether to canonicalize the atom ordering prior to assigning | ||
map indices. | ||
Returns: | ||
The SMILES string. | ||
""" | ||
from rdkit import Chem | ||
|
||
mol = Chem.AddHs(mol) | ||
|
||
if canonical: | ||
order = Chem.CanonicalRankAtoms(mol, includeChirality=True) | ||
mol = Chem.RenumberAtoms(mol, list(order)) | ||
|
||
for atom in mol.GetAtoms(): | ||
atom.SetAtomMapNum(atom.GetIdx() + 1) | ||
|
||
return Chem.MolToSmiles(mol) |