feat: add detailed docstrings for rotatable bond functions and utility methods
Browse files- rotatable_bonds.py +43 -0
- utils.py +33 -3
rotatable_bonds.py
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from rdkit import Chem
|
| 2 |
from utils import mol_to_svg, highlight_by_patterns, IMAGE_SIZE
|
| 3 |
|
|
@@ -8,6 +14,21 @@ rotatable_patterns = {
|
|
| 8 |
}
|
| 9 |
|
| 10 |
def get_rotatable_bond_indices(mol):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
rot_bond_indices = []
|
| 12 |
for bond in mol.GetBonds():
|
| 13 |
if bond.GetBondType() != Chem.BondType.SINGLE:
|
|
@@ -24,6 +45,16 @@ def get_rotatable_bond_indices(mol):
|
|
| 24 |
return rot_bond_indices
|
| 25 |
|
| 26 |
def highlight_rotatable_bonds(smiles: str):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
mol = Chem.MolFromSmiles(smiles)
|
| 28 |
if mol is None:
|
| 29 |
return None
|
|
@@ -34,6 +65,18 @@ def highlight_rotatable_bonds(smiles: str):
|
|
| 34 |
return img
|
| 35 |
|
| 36 |
def process_rotatable(smiles: str):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
images = []
|
| 38 |
img = highlight_rotatable_bonds(smiles)
|
| 39 |
if img:
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Module for identifying and visualizing rotatable bonds in molecular structures.
|
| 3 |
+
Provides functionality to detect single bonds that can rotate using both
|
| 4 |
+
a local algorithm and SMARTS pattern matching approaches.
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
from rdkit import Chem
|
| 8 |
from utils import mol_to_svg, highlight_by_patterns, IMAGE_SIZE
|
| 9 |
|
|
|
|
| 14 |
}
|
| 15 |
|
| 16 |
def get_rotatable_bond_indices(mol):
|
| 17 |
+
"""
|
| 18 |
+
Identifies rotatable bonds in a molecule using local structural analysis.
|
| 19 |
+
|
| 20 |
+
A bond is considered rotatable if it:
|
| 21 |
+
- Is a single bond
|
| 22 |
+
- Is not in a ring
|
| 23 |
+
- Neither atom is a hydrogen
|
| 24 |
+
- Both atoms have at least 2 neighbors
|
| 25 |
+
|
| 26 |
+
Args:
|
| 27 |
+
mol: RDKit molecule object
|
| 28 |
+
|
| 29 |
+
Returns:
|
| 30 |
+
list: Indices of rotatable bonds in the molecule
|
| 31 |
+
"""
|
| 32 |
rot_bond_indices = []
|
| 33 |
for bond in mol.GetBonds():
|
| 34 |
if bond.GetBondType() != Chem.BondType.SINGLE:
|
|
|
|
| 45 |
return rot_bond_indices
|
| 46 |
|
| 47 |
def highlight_rotatable_bonds(smiles: str):
|
| 48 |
+
"""
|
| 49 |
+
Creates an SVG visualization of a molecule with rotatable bonds highlighted.
|
| 50 |
+
|
| 51 |
+
Args:
|
| 52 |
+
smiles: SMILES string representation of the molecule
|
| 53 |
+
|
| 54 |
+
Returns:
|
| 55 |
+
str: SVG string of the molecule with rotatable bonds highlighted, or
|
| 56 |
+
None if no rotatable bonds are found or if SMILES is invalid
|
| 57 |
+
"""
|
| 58 |
mol = Chem.MolFromSmiles(smiles)
|
| 59 |
if mol is None:
|
| 60 |
return None
|
|
|
|
| 65 |
return img
|
| 66 |
|
| 67 |
def process_rotatable(smiles: str):
|
| 68 |
+
"""
|
| 69 |
+
Processes a molecule to identify rotatable bonds using multiple methods
|
| 70 |
+
and generates visualizations for each method.
|
| 71 |
+
|
| 72 |
+
Args:
|
| 73 |
+
smiles: SMILES string representation of the molecule
|
| 74 |
+
|
| 75 |
+
Returns:
|
| 76 |
+
tuple: (list of (image, caption) tuples, status message string)
|
| 77 |
+
Images show the molecule with rotatable bonds highlighted using
|
| 78 |
+
different detection methods
|
| 79 |
+
"""
|
| 80 |
images = []
|
| 81 |
img = highlight_rotatable_bonds(smiles)
|
| 82 |
if img:
|
utils.py
CHANGED
|
@@ -1,11 +1,30 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
from rdkit import Chem
|
| 3 |
from rdkit.Chem.Draw import MolDraw2DSVG
|
| 4 |
import tempfile
|
| 5 |
|
| 6 |
IMAGE_SIZE = (400, 400)
|
| 7 |
|
| 8 |
-
def mol_to_svg(mol, size, highlightAtoms=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
drawer = MolDraw2DSVG(size[0], size[1])
|
| 10 |
opts = drawer.drawOptions()
|
| 11 |
|
|
@@ -23,7 +42,18 @@ def mol_to_svg(mol, size, highlightAtoms=None, highlightBonds=None, legend="", a
|
|
| 23 |
tmp.close()
|
| 24 |
return tmp.name
|
| 25 |
|
| 26 |
-
def highlight_by_patterns(smiles: str, pattern_dict: dict):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
mol = Chem.MolFromSmiles(smiles)
|
| 28 |
if mol is None:
|
| 29 |
return None
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Utility functions for molecular visualization and pattern matching using RDKit.
|
| 3 |
+
Provides SVG rendering capabilities and substructure highlighting functionality.
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
from rdkit import Chem
|
| 7 |
from rdkit.Chem.Draw import MolDraw2DSVG
|
| 8 |
import tempfile
|
| 9 |
|
| 10 |
IMAGE_SIZE = (400, 400)
|
| 11 |
|
| 12 |
+
def mol_to_svg(mol: Chem.Mol, size: tuple[int, int], highlightAtoms: list[int] = None,
|
| 13 |
+
highlightBonds: list[int] = None, legend: str = "", atomLabels: dict[int, str] = None) -> str:
|
| 14 |
+
"""
|
| 15 |
+
Converts an RDKit molecule to an SVG image file with optional highlighting and labels.
|
| 16 |
+
|
| 17 |
+
Args:
|
| 18 |
+
mol: RDKit molecule object to render
|
| 19 |
+
size: Tuple of (width, height) for the output image
|
| 20 |
+
highlightAtoms: List of atom indices to highlight
|
| 21 |
+
highlightBonds: List of bond indices to highlight
|
| 22 |
+
legend: Text to display as image legend
|
| 23 |
+
atomLabels: Dictionary mapping atom indices to label strings
|
| 24 |
+
|
| 25 |
+
Returns:
|
| 26 |
+
str: Path to the temporary SVG file containing the rendered molecule
|
| 27 |
+
"""
|
| 28 |
drawer = MolDraw2DSVG(size[0], size[1])
|
| 29 |
opts = drawer.drawOptions()
|
| 30 |
|
|
|
|
| 42 |
tmp.close()
|
| 43 |
return tmp.name
|
| 44 |
|
| 45 |
+
def highlight_by_patterns(smiles: str, pattern_dict: dict[str, Chem.Mol]) -> list[tuple[str, str]]:
|
| 46 |
+
"""
|
| 47 |
+
Generates visualizations of a molecule with substructures matching SMARTS patterns highlighted.
|
| 48 |
+
|
| 49 |
+
Args:
|
| 50 |
+
smiles: SMILES string representation of the molecule
|
| 51 |
+
pattern_dict: Dictionary mapping pattern names to RDKit molecule objects representing SMARTS patterns
|
| 52 |
+
|
| 53 |
+
Returns:
|
| 54 |
+
list: List of tuples containing (image_path, pattern_name) for each matching pattern.
|
| 55 |
+
Returns None if the SMILES string is invalid.
|
| 56 |
+
"""
|
| 57 |
mol = Chem.MolFromSmiles(smiles)
|
| 58 |
if mol is None:
|
| 59 |
return None
|