alarv commited on
Commit
43b71ab
·
1 Parent(s): c39faa9

fix: fix broken pubchem api temporarily

Browse files
Files changed (1) hide show
  1. app.py +68 -15
app.py CHANGED
@@ -50,14 +50,60 @@ def retrieve_smiles_tool(compound_name: str) -> str | None:
50
  Args:
51
  compound_name: The compound name to retrieve the SMILES from described in natural language.
52
  """
53
-
54
- compounds = get_compounds(compound_name, namespace='name')
55
-
56
- if compounds:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
  compound = compounds[0]
58
- return compound.isomeric_smiles
59
- else:
60
- print(f"No compounds found for {compound_name}.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
  return None
62
 
63
  @tool
@@ -383,15 +429,22 @@ def qsartoolbox_get_property_value_tool(smiles:str, qsartoolbox_qsar_model: str)
383
  "VEGA - Adipose tissue - blood model (INERIS)"
384
  """
385
 
386
- qsar_model_guid = next(x for x in possible_qsar_guids if qsartoolbox_qsar_model == x["value"])["key"]
387
-
388
- results = jaqpot.qsartoolbox_qsar_model_predict_sync(smiles, qsar_model_guid)
389
- result = next((x for x in results if x["Value"] != ''), None)
 
 
390
 
391
- if(result != None):
392
- return result
393
- else:
394
- return None
 
 
 
 
 
395
 
396
  final_answer = FinalAnswerTool()
397
 
 
50
  Args:
51
  compound_name: The compound name to retrieve the SMILES from described in natural language.
52
  """
53
+
54
+ # Common chemical name mappings as fallback
55
+ common_smiles = {
56
+ "ethane": "CC",
57
+ "methane": "C",
58
+ "propane": "CCC",
59
+ "butane": "CCCC",
60
+ "benzene": "c1ccccc1",
61
+ "toluene": "Cc1ccccc1",
62
+ "water": "O",
63
+ "ethanol": "CCO",
64
+ "methanol": "CO"
65
+ }
66
+
67
+ try:
68
+ compounds = get_compounds(compound_name, namespace='name')
69
+
70
+ if not compounds:
71
+ # Try lowercase fallback for common compounds
72
+ if compound_name.lower() in common_smiles:
73
+ print(f"PubChem search failed, using known SMILES for {compound_name}")
74
+ return common_smiles[compound_name.lower()]
75
+
76
+ print(f"No compounds found for {compound_name}.")
77
+ return None
78
+
79
  compound = compounds[0]
80
+
81
+ # Workaround for PubChemPy issue #101 - extract SMILES from record properties
82
+ smiles_props = [i for i in compound.to_dict().get("record").get("props") if i["urn"]["label"] == "SMILES"]
83
+
84
+ if smiles_props:
85
+ # Prefer "Absolute" SMILES (isomeric), fall back to "Connectivity" (canonical)
86
+ for prop in smiles_props:
87
+ if prop["urn"]["name"] == "Absolute":
88
+ return prop["value"]["sval"]
89
+
90
+ # If no Absolute found, use the first SMILES available
91
+ return smiles_props[0]["value"]["sval"]
92
+ else:
93
+ # Fall back to common compounds if no SMILES found
94
+ if compound_name.lower() in common_smiles:
95
+ print(f"No SMILES found in PubChem, using fallback for {compound_name}")
96
+ return common_smiles[compound_name.lower()]
97
+ return None
98
+
99
+ except Exception as e:
100
+ print(f"PubChem API error for {compound_name}: {e}")
101
+
102
+ # Fallback to common compounds
103
+ if compound_name.lower() in common_smiles:
104
+ print(f"Using fallback SMILES for {compound_name}")
105
+ return common_smiles[compound_name.lower()]
106
+
107
  return None
108
 
109
  @tool
 
429
  "VEGA - Adipose tissue - blood model (INERIS)"
430
  """
431
 
432
+ try:
433
+ qsar_model_guid = next(x for x in possible_qsar_guids if qsartoolbox_qsar_model == x["value"])["key"]
434
+
435
+ print(f"Attempting QSAR prediction for SMILES: {smiles} using model: {qsartoolbox_qsar_model}")
436
+ results = jaqpot.qsartoolbox_qsar_model_predict_sync(smiles, qsar_model_guid)
437
+ result = next((x for x in results if x["Value"] != ''), None)
438
 
439
+ if(result != None):
440
+ return result
441
+ else:
442
+ return "No prediction results available for this compound with the selected model."
443
+
444
+ except Exception as e:
445
+ error_msg = f"QSAR prediction failed: {str(e)}"
446
+ print(error_msg)
447
+ return error_msg
448
 
449
  final_answer = FinalAnswerTool()
450