-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
104 lines (80 loc) · 2.58 KB
/
main.py
File metadata and controls
104 lines (80 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
"""
SMARTSViewer - Molecular Structure Analysis Tool
Main entry point for the Streamlit application.
"""
from rdkit import Chem
from rdkit.Chem.rdchem import Mol
from rdkit.Chem import AllChem, Draw, rdDepictor
def get_mol(smiles: str) -> Mol:
"""
Create a sanitized RDKit molecule from SMILES string.
Args:
smiles: SMILES string
Returns:
RDKit Mol object with 2D coordinates
"""
mol = Chem.MolFromSmiles(smiles)
Chem.SanitizeMol(mol)
Chem.SetAromaticity(mol)
mol.RemoveAllConformers()
rdDepictor.Compute2DCoords(mol)
return mol
def get_matched_atoms_and_bonds(smiles: str, smarts: str):
"""
Find atoms and bonds matching a SMARTS pattern.
Args:
smiles: SMILES string of the molecule
smarts: SMARTS pattern to match
Returns:
Tuple of (atoms, bonds) lists
"""
mol = get_mol(smiles)
pattern = Chem.MolFromSmarts(smarts)
# Get matching atoms
matches = mol.GetSubstructMatches(pattern)
atoms = []
bonds = []
for match in matches:
for i in match:
atoms.append(i)
for bond in mol.GetBonds():
a1 = bond.GetBeginAtomIdx()
a2 = bond.GetEndAtomIdx()
for match in matches:
if a1 in match and a2 in match:
bonds.append(bond.GetIdx())
return atoms, bonds
def _get_highlighted_mol_image(mol: Mol, atoms: list[int], bonds: list[int]):
"""
Create a highlighted molecule image.
Args:
mol: RDKit Mol object
atoms: List of atom indices to highlight
bonds: List of bond indices to highlight
Returns:
PIL Image object
"""
img = Draw.MolToImage(mol, highlightAtoms=atoms, highlightBonds=bonds)
return img
def perform_reaction(reaction_smarts: str, *reactants: Mol):
"""
Perform a chemical reaction using reaction SMARTS.
Args:
reaction_smarts: Reaction SMARTS pattern
reactants: Variable number of reactant molecules
Returns:
Tuple of product tuples
"""
rxn = AllChem.ReactionFromSmarts(reaction_smarts)
prods = rxn.RunReactants(reactants)
return prods
def main():
"""Main entry point - runs the Streamlit application."""
print("Starting SMARTSViewer Streamlit application...")
print("Please use 'streamlit run app.py' to run the web application.")
print("Or use 'streamlit run main.py' for direct execution.")
# Import and run the Streamlit app
from app import main as app_main
app_main()
if __name__ == "__main__":
main()