Spaces:
Runtime error
Runtime error
Vaishnav14220
commited on
Commit
Β·
8e9a87a
1
Parent(s):
4a8bfe4
Improve reaction SVG extraction and rendering from NIST data
Browse files
app.py
CHANGED
|
@@ -333,15 +333,21 @@ def _render_smiles_to_svg(smiles_text: str) -> str | None:
|
|
| 333 |
return None
|
| 334 |
|
| 335 |
try:
|
|
|
|
| 336 |
reaction = rdChemReactions.ReactionFromSmarts(smiles_text, useSmiles=True)
|
| 337 |
except Exception:
|
| 338 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 339 |
|
| 340 |
if reaction is None or (reaction.GetNumReactantTemplates() == 0 and reaction.GetNumProductTemplates() == 0):
|
| 341 |
return None
|
| 342 |
|
| 343 |
try:
|
| 344 |
-
|
|
|
|
| 345 |
except Exception:
|
| 346 |
return None
|
| 347 |
|
|
@@ -394,16 +400,32 @@ def fetch_detail(selected_url: str, manual_url: str):
|
|
| 394 |
# Try to render the reaction title as SVG
|
| 395 |
reaction_svg = ""
|
| 396 |
if detail.title:
|
| 397 |
-
# Extract SMILES-like patterns from title (heuristic)
|
| 398 |
title = detail.title.strip()
|
| 399 |
-
|
|
|
|
|
|
|
| 400 |
if " β " in title:
|
|
|
|
| 401 |
parts = title.split(" β ")
|
| 402 |
if len(parts) == 2:
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 407 |
|
| 408 |
return markdown, table, plot_fig, reaction_svg
|
| 409 |
|
|
|
|
| 333 |
return None
|
| 334 |
|
| 335 |
try:
|
| 336 |
+
# Try parsing as SMILES reaction first
|
| 337 |
reaction = rdChemReactions.ReactionFromSmarts(smiles_text, useSmiles=True)
|
| 338 |
except Exception:
|
| 339 |
+
try:
|
| 340 |
+
# Fall back to SMARTS parsing
|
| 341 |
+
reaction = rdChemReactions.ReactionFromSmarts(smiles_text, useSmiles=False)
|
| 342 |
+
except Exception:
|
| 343 |
+
return None
|
| 344 |
|
| 345 |
if reaction is None or (reaction.GetNumReactantTemplates() == 0 and reaction.GetNumProductTemplates() == 0):
|
| 346 |
return None
|
| 347 |
|
| 348 |
try:
|
| 349 |
+
# Generate SVG with better sizing
|
| 350 |
+
svg = Draw.ReactionToImage(reaction, subImgSize=(250, 200), useSVG=True)
|
| 351 |
except Exception:
|
| 352 |
return None
|
| 353 |
|
|
|
|
| 400 |
# Try to render the reaction title as SVG
|
| 401 |
reaction_svg = ""
|
| 402 |
if detail.title:
|
|
|
|
| 403 |
title = detail.title.strip()
|
| 404 |
+
smiles_attempt = None
|
| 405 |
+
|
| 406 |
+
# Try different reaction format conversions
|
| 407 |
if " β " in title:
|
| 408 |
+
# Format: "A + B β C"
|
| 409 |
parts = title.split(" β ")
|
| 410 |
if len(parts) == 2:
|
| 411 |
+
reactants = parts[0].replace(" + ", ".").strip()
|
| 412 |
+
products = parts[1].replace(" + ", ".").strip()
|
| 413 |
+
smiles_attempt = f"{reactants}>>{products}"
|
| 414 |
+
elif " β " in title and " β " in title:
|
| 415 |
+
# Reversible reaction
|
| 416 |
+
smiles_attempt = title.replace(" β ", ">>").replace(" + ", ".")
|
| 417 |
+
elif "->" in title:
|
| 418 |
+
# Alternative arrow format
|
| 419 |
+
parts = title.split("->")
|
| 420 |
+
if len(parts) == 2:
|
| 421 |
+
reactants = parts[0].replace(" + ", ".").strip()
|
| 422 |
+
products = parts[1].replace(" + ", ".").strip()
|
| 423 |
+
smiles_attempt = f"{reactants}>>{products}"
|
| 424 |
+
|
| 425 |
+
if smiles_attempt:
|
| 426 |
+
svg = _render_smiles_to_svg(smiles_attempt)
|
| 427 |
+
if svg:
|
| 428 |
+
reaction_svg = svg
|
| 429 |
|
| 430 |
return markdown, table, plot_fig, reaction_svg
|
| 431 |
|