Spaces:
Runtime error
Runtime error
Vaishnav14220
commited on
Commit
·
e2cbfc6
1
Parent(s):
32797f8
Remove atom numbers, show only element symbols and add orbital visualization toggle
Browse files
app.py
CHANGED
|
@@ -50,7 +50,7 @@ def name_to_smiles(name: str) -> str:
|
|
| 50 |
raise gr.Error(f"Error converting name to SMILES: {str(e)}")
|
| 51 |
|
| 52 |
|
| 53 |
-
def name_to_3d_molecule(name: str) -> tuple:
|
| 54 |
"""Convert chemical name to 3D molecule SDF and 2D visualization"""
|
| 55 |
try:
|
| 56 |
# Convert name to SMILES
|
|
@@ -115,7 +115,7 @@ def name_to_3d_molecule(name: str) -> tuple:
|
|
| 115 |
z_coords.append(pos.z)
|
| 116 |
element = atom.GetSymbol()
|
| 117 |
elements.append(element)
|
| 118 |
-
atom_labels.append(
|
| 119 |
|
| 120 |
# Define color scheme for common elements (Jmol colors)
|
| 121 |
color_map = {
|
|
@@ -162,8 +162,43 @@ def name_to_3d_molecule(name: str) -> tuple:
|
|
| 162 |
hoverinfo='skip'
|
| 163 |
)
|
| 164 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
# Create figure
|
| 166 |
-
fig = go.Figure(data=
|
| 167 |
|
| 168 |
fig.update_layout(
|
| 169 |
title=dict(text=f"{name}<br>SMILES: {canonical_smiles}", x=0.5, xanchor='center'),
|
|
@@ -236,16 +271,19 @@ tpsa_interface = gr.Interface(
|
|
| 236 |
|
| 237 |
molecule_3d_interface = gr.Interface(
|
| 238 |
fn=name_to_3d_molecule,
|
| 239 |
-
inputs=
|
|
|
|
|
|
|
|
|
|
| 240 |
outputs=[
|
| 241 |
gr.HTML(label="2D Structure"),
|
| 242 |
gr.Plot(label="3D Interactive Molecule Viewer - Rotate and Zoom!"),
|
| 243 |
gr.Textbox(label="3D SDF Content (Optional - for external viewers)", lines=10, max_lines=20, visible=False)
|
| 244 |
],
|
| 245 |
api_name="name_to_molecule",
|
| 246 |
-
description="View 2D structure and interactive 3D molecule with atom labels. Click and drag to rotate, scroll to zoom!",
|
| 247 |
cache_examples=False,
|
| 248 |
-
examples=[["benzene"], ["aspirin"], ["caffeine"], ["glucose"]],
|
| 249 |
)
|
| 250 |
|
| 251 |
|
|
|
|
| 50 |
raise gr.Error(f"Error converting name to SMILES: {str(e)}")
|
| 51 |
|
| 52 |
|
| 53 |
+
def name_to_3d_molecule(name: str, show_orbitals: bool = False) -> tuple:
|
| 54 |
"""Convert chemical name to 3D molecule SDF and 2D visualization"""
|
| 55 |
try:
|
| 56 |
# Convert name to SMILES
|
|
|
|
| 115 |
z_coords.append(pos.z)
|
| 116 |
element = atom.GetSymbol()
|
| 117 |
elements.append(element)
|
| 118 |
+
atom_labels.append(element) # Only element symbol, no numbers
|
| 119 |
|
| 120 |
# Define color scheme for common elements (Jmol colors)
|
| 121 |
color_map = {
|
|
|
|
| 162 |
hoverinfo='skip'
|
| 163 |
)
|
| 164 |
|
| 165 |
+
# Create orbital clouds if requested
|
| 166 |
+
data_traces = [bonds_trace, atoms_trace]
|
| 167 |
+
|
| 168 |
+
if show_orbitals:
|
| 169 |
+
import numpy as np
|
| 170 |
+
# Add semi-transparent spheres around atoms to represent electron orbitals
|
| 171 |
+
orbital_radius = {
|
| 172 |
+
'H': 0.6, 'C': 0.9, 'N': 0.8, 'O': 0.75,
|
| 173 |
+
'F': 0.7, 'Cl': 1.0, 'Br': 1.15, 'I': 1.4,
|
| 174 |
+
'P': 1.1, 'S': 1.05, 'B': 0.95, 'Si': 1.2
|
| 175 |
+
}
|
| 176 |
+
|
| 177 |
+
# Create mesh for each atom's orbital
|
| 178 |
+
for i, (x, y, z, elem) in enumerate(zip(x_coords, y_coords, z_coords, elements)):
|
| 179 |
+
radius = orbital_radius.get(elem, 0.8)
|
| 180 |
+
|
| 181 |
+
# Create sphere mesh
|
| 182 |
+
u = np.linspace(0, 2 * np.pi, 20)
|
| 183 |
+
v = np.linspace(0, np.pi, 15)
|
| 184 |
+
sphere_x = x + radius * np.outer(np.cos(u), np.sin(v))
|
| 185 |
+
sphere_y = y + radius * np.outer(np.sin(u), np.sin(v))
|
| 186 |
+
sphere_z = z + radius * np.outer(np.ones(np.size(u)), np.cos(v))
|
| 187 |
+
|
| 188 |
+
color = color_map.get(elem, '#FF1493')
|
| 189 |
+
|
| 190 |
+
orbital_trace = go.Surface(
|
| 191 |
+
x=sphere_x, y=sphere_y, z=sphere_z,
|
| 192 |
+
colorscale=[[0, color], [1, color]],
|
| 193 |
+
showscale=False,
|
| 194 |
+
opacity=0.15,
|
| 195 |
+
name=f'{elem} orbital',
|
| 196 |
+
hoverinfo='skip'
|
| 197 |
+
)
|
| 198 |
+
data_traces.append(orbital_trace)
|
| 199 |
+
|
| 200 |
# Create figure
|
| 201 |
+
fig = go.Figure(data=data_traces)
|
| 202 |
|
| 203 |
fig.update_layout(
|
| 204 |
title=dict(text=f"{name}<br>SMILES: {canonical_smiles}", x=0.5, xanchor='center'),
|
|
|
|
| 271 |
|
| 272 |
molecule_3d_interface = gr.Interface(
|
| 273 |
fn=name_to_3d_molecule,
|
| 274 |
+
inputs=[
|
| 275 |
+
gr.Textbox(label="Chemical Name", placeholder="e.g., benzene, aspirin, caffeine, glucose"),
|
| 276 |
+
gr.Checkbox(label="Show Electron Orbitals", value=False)
|
| 277 |
+
],
|
| 278 |
outputs=[
|
| 279 |
gr.HTML(label="2D Structure"),
|
| 280 |
gr.Plot(label="3D Interactive Molecule Viewer - Rotate and Zoom!"),
|
| 281 |
gr.Textbox(label="3D SDF Content (Optional - for external viewers)", lines=10, max_lines=20, visible=False)
|
| 282 |
],
|
| 283 |
api_name="name_to_molecule",
|
| 284 |
+
description="View 2D structure and interactive 3D molecule with atom labels. Click and drag to rotate, scroll to zoom! Toggle 'Show Electron Orbitals' to visualize electron clouds around atoms.",
|
| 285 |
cache_examples=False,
|
| 286 |
+
examples=[["benzene", False], ["aspirin", False], ["caffeine", True], ["glucose", False]],
|
| 287 |
)
|
| 288 |
|
| 289 |
|