Vaishnav14220 commited on
Commit
820ef75
·
1 Parent(s): 3945aad

Add 3D SDF file download to molecule viewer

Browse files
Files changed (1) hide show
  1. app.py +24 -11
app.py CHANGED
@@ -50,8 +50,8 @@ 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) -> str:
54
- """Convert chemical name to 3D molecule visualization"""
55
  try:
56
  # Convert name to SMILES
57
  smiles = cirpy.resolve(name, 'smiles')
@@ -74,8 +74,17 @@ def name_to_3d_molecule(name: str) -> str:
74
  # Optimize geometry
75
  AllChem.MMFFOptimizeMolecule(mol)
76
 
77
- # Generate 2D image as fallback and show SMILES
78
- img = Draw.MolToImage(mol, size=(400, 400))
 
 
 
 
 
 
 
 
 
79
 
80
  # Convert image to base64
81
  import io
@@ -85,22 +94,23 @@ def name_to_3d_molecule(name: str) -> str:
85
  img_str = base64.b64encode(buffered.getvalue()).decode()
86
 
87
  # Get SMILES string
88
- canonical_smiles = Chem.MolToSmiles(mol)
89
 
90
- # Create HTML with 2D image and molecule info
91
  html_content = f"""
92
  <div style="text-align: center; padding: 20px;">
93
  <h3>{name}</h3>
94
  <p><strong>SMILES:</strong> {canonical_smiles}</p>
95
  <img src="data:image/png;base64,{img_str}" style="max-width: 100%; border: 1px solid #ddd; border-radius: 8px; padding: 10px; background: white;">
96
- <p style="margin-top: 10px; color: #666;">2D Structure Visualization</p>
 
97
  </div>
98
  """
99
 
100
- return html_content
101
 
102
  except Exception as e:
103
- raise gr.Error(f"Error creating 3D molecule: {str(e)}")
104
 
105
 
106
  smiles_interface = gr.Interface(
@@ -147,9 +157,12 @@ tpsa_interface = gr.Interface(
147
  molecule_3d_interface = gr.Interface(
148
  fn=name_to_3d_molecule,
149
  inputs=gr.Textbox(label="Chemical Name", placeholder="e.g., benzene, aspirin, caffeine"),
150
- outputs=gr.HTML(label="Molecule Viewer"),
 
 
 
151
  api_name="name_to_molecule",
152
- description="Convert a chemical name to a molecule visualization with SMILES.",
153
  cache_examples=False,
154
  )
155
 
 
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 file and 2D visualization"""
55
  try:
56
  # Convert name to SMILES
57
  smiles = cirpy.resolve(name, 'smiles')
 
74
  # Optimize geometry
75
  AllChem.MMFFOptimizeMolecule(mol)
76
 
77
+ # Save to SDF file
78
+ import tempfile
79
+ import os
80
+ sdf_file = tempfile.NamedTemporaryFile(mode='w', suffix='.sdf', delete=False)
81
+ writer = Chem.SDWriter(sdf_file.name)
82
+ writer.write(mol)
83
+ writer.close()
84
+
85
+ # Generate 2D image for preview
86
+ mol_2d = Chem.MolFromSmiles(smiles) # Get 2D version
87
+ img = Draw.MolToImage(mol_2d, size=(400, 400))
88
 
89
  # Convert image to base64
90
  import io
 
94
  img_str = base64.b64encode(buffered.getvalue()).decode()
95
 
96
  # Get SMILES string
97
+ canonical_smiles = Chem.MolToSmiles(mol_2d)
98
 
99
+ # Create HTML with 2D image and download info
100
  html_content = f"""
101
  <div style="text-align: center; padding: 20px;">
102
  <h3>{name}</h3>
103
  <p><strong>SMILES:</strong> {canonical_smiles}</p>
104
  <img src="data:image/png;base64,{img_str}" style="max-width: 100%; border: 1px solid #ddd; border-radius: 8px; padding: 10px; background: white;">
105
+ <p style="margin-top: 10px; color: #666;">2D Structure Preview</p>
106
+ <p style="margin-top: 10px; color: #28a745; font-weight: bold;">✓ 3D SDF file ready for download below</p>
107
  </div>
108
  """
109
 
110
+ return html_content, sdf_file.name
111
 
112
  except Exception as e:
113
+ raise gr.Error(f"Error creating molecule: {str(e)}")
114
 
115
 
116
  smiles_interface = gr.Interface(
 
157
  molecule_3d_interface = gr.Interface(
158
  fn=name_to_3d_molecule,
159
  inputs=gr.Textbox(label="Chemical Name", placeholder="e.g., benzene, aspirin, caffeine"),
160
+ outputs=[
161
+ gr.HTML(label="Molecule Viewer"),
162
+ gr.File(label="Download 3D SDF File")
163
+ ],
164
  api_name="name_to_molecule",
165
+ description="Convert a chemical name to a 2D visualization and download 3D SDF file with coordinates.",
166
  cache_examples=False,
167
  )
168