5
5
# operations.
6
6
7
7
import requests
8
+ import os
8
9
from rdkit import Chem
9
10
from reactions .utils .to_smiles import smiles_with_explicit_hydrogens
10
11
from django .core .files .temp import NamedTemporaryFile
@@ -288,14 +289,30 @@ def get_vmh_miriam(abbr):
288
289
- abbr (str): The molecule abbreviation.
289
290
290
291
Output:
291
- - (str): The MIRIAM ID for the molecule.
292
+ - (str): The MIRIAM ID for the molecule is found, else empty string .
292
293
"""
293
294
BASE_URL = settings .OLD_VMH_BASE_URL
294
295
encoded_abbr = quote (abbr )
295
296
endpoint = f"{ BASE_URL } _api/metabolites/?abbreviation={ encoded_abbr } "
296
- response = requests .get (endpoint , verify = False )
297
- miriam = response .json ().get ('results' , [[]])[0 ].get ('miriam' , '' )
298
- return miriam
297
+ try :
298
+ response = requests .get (endpoint , verify = False , timeout = 10 )
299
+ if response .status_code == 200 :
300
+ results = response .json ().get ('results' , [])
301
+ if results :
302
+ return results [0 ].get ('miriam' , '' )
303
+ except Exception :
304
+ pass
305
+
306
+ # Fallback: local mol file
307
+ mol_path = os .path .join (settings .MOL_FILE_PATH , f"{ abbr } .mol" )
308
+ if os .path .exists (mol_path ):
309
+ try :
310
+ mol = Chem .MolFromMolFile (mol_path , sanitize = False , removeHs = False )
311
+ found , miriam = search_vmh (mol )
312
+ return miriam if found else ''
313
+ except Exception :
314
+ return ''
315
+ return ''
299
316
300
317
301
318
def search_metabolites_vmh (
@@ -322,8 +339,8 @@ def search_metabolites_vmh(
322
339
file_idx = 0 # Initialize file index
323
340
for idx , this_type in enumerate (types ):
324
341
if this_type == 'VMH' :
325
- found = True
326
342
miriam = get_vmh_miriam (mols [idx ])
343
+ found = bool (miriam )
327
344
elif this_type == 'SwissLipids' :
328
345
base_url = 'https://www.swisslipids.org/api/index.php/entity/'
329
346
endpoint = f"{ base_url } { mols [idx ]} "
0 commit comments