Nanny7 commited on
Commit
d3c3431
·
1 Parent(s): 42f5484

Fix reaction SMILES parsing and examples

Browse files
Files changed (1) hide show
  1. app.py +45 -8
app.py CHANGED
@@ -190,16 +190,53 @@ def smiles_to_name(smiles: str) -> str:
190
  def reaction_smiles_to_svg(reaction_smiles: str) -> str:
191
  """Convert reaction SMILES to SVG image"""
192
  try:
193
- # Parse the reaction SMILES
194
- reaction = rdChemReactions.ReactionFromSmarts(reaction_smiles)
195
- if reaction is None:
196
- raise gr.Error(f"Invalid reaction SMILES: {reaction_smiles}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
197
 
198
  # Draw the reaction as SVG
199
  svg = Draw.ReactionToImage(reaction, subImgSize=(300, 300), useSVG=True)
200
 
201
  return svg
202
 
 
 
203
  except Exception as e:
204
  raise gr.Error(f"Error generating reaction image: {str(e)}")
205
 
@@ -1301,7 +1338,7 @@ reaction_interface = gr.Interface(
1301
  fn=reaction_smiles_to_svg,
1302
  inputs=gr.Textbox(
1303
  label="Reaction SMILES",
1304
- placeholder="CCO.O=C=O>>CC(=O)O.CO2 (Esterification reaction)",
1305
  info="Use '>>' to separate reactants from products"
1306
  ),
1307
  outputs=gr.HTML(label="Reaction Visualization"),
@@ -1309,9 +1346,9 @@ reaction_interface = gr.Interface(
1309
  description="Visualize chemical reactions from SMILES notation.",
1310
  api_name="reaction_visualizer",
1311
  examples=[
1312
- ["CCO.O=C=O>>CC(=O)O.CO2"],
1313
- ["c1ccccc1.ClC(Cl)Cl>>c1ccccc1Cl.HCl"],
1314
- ["CC(=O)O.[Na]>>CC(=O)O[Na]"]
1315
  ],
1316
  )
1317
 
 
190
  def reaction_smiles_to_svg(reaction_smiles: str) -> str:
191
  """Convert reaction SMILES to SVG image"""
192
  try:
193
+ # Parse the reaction SMILES - try ReactionFromSmarts first, fallback to manual parsing
194
+ try:
195
+ reaction = rdChemReactions.ReactionFromSmarts(reaction_smiles)
196
+ except:
197
+ # If ReactionFromSmarts fails, try to parse manually
198
+ if ">>" not in reaction_smiles:
199
+ raise gr.Error("Reaction SMILES must contain '>>' to separate reactants from products")
200
+
201
+ parts = reaction_smiles.split(">>")
202
+ if len(parts) != 2:
203
+ raise gr.Error("Reaction SMILES must have exactly one '>>' separator")
204
+
205
+ reactant_smiles = parts[0].strip()
206
+ product_smiles = parts[1].strip()
207
+
208
+ # Create reaction from individual molecules
209
+ reactant_mols = []
210
+ for smi in reactant_smiles.split('.'):
211
+ smi = smi.strip()
212
+ if smi:
213
+ mol = Chem.MolFromSmiles(smi)
214
+ if mol is None:
215
+ raise gr.Error(f"Invalid SMILES in reactants: {smi}")
216
+ reactant_mols.append(mol)
217
+
218
+ product_mols = []
219
+ for smi in product_smiles.split('.'):
220
+ smi = smi.strip()
221
+ if smi:
222
+ mol = Chem.MolFromSmiles(smi)
223
+ if mol is None:
224
+ raise gr.Error(f"Invalid SMILES in products: {smi}")
225
+ product_mols.append(mol)
226
+
227
+ reaction = rdChemReactions.ChemicalReaction()
228
+ for mol in reactant_mols:
229
+ reaction.AddReactantTemplate(mol)
230
+ for mol in product_mols:
231
+ reaction.AddProductTemplate(mol)
232
 
233
  # Draw the reaction as SVG
234
  svg = Draw.ReactionToImage(reaction, subImgSize=(300, 300), useSVG=True)
235
 
236
  return svg
237
 
238
+ except gr.Error:
239
+ raise
240
  except Exception as e:
241
  raise gr.Error(f"Error generating reaction image: {str(e)}")
242
 
 
1338
  fn=reaction_smiles_to_svg,
1339
  inputs=gr.Textbox(
1340
  label="Reaction SMILES",
1341
+ placeholder="CCO.O=C=O>>CC(=O)O.O=C=O (Esterification reaction)",
1342
  info="Use '>>' to separate reactants from products"
1343
  ),
1344
  outputs=gr.HTML(label="Reaction Visualization"),
 
1346
  description="Visualize chemical reactions from SMILES notation.",
1347
  api_name="reaction_visualizer",
1348
  examples=[
1349
+ ["CC(=O)O.CO>>CC(=O)OC.O"],
1350
+ ["c1ccccc1.ClCl>>c1ccccc1Cl.Cl"],
1351
+ ["Cl.[Na+].[OH-]>>[Na+].[Cl-].O"]
1352
  ],
1353
  )
1354