Skip to content

Commit aef1758

Browse files
authored
Merge pull request #12 from opencobra/constructor_dev
fix: search vmh using local mol file
2 parents 3933c5d + 13781bd commit aef1758

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

curationTool/reactions/utils/search_vmh.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# operations.
66

77
import requests
8+
import os
89
from rdkit import Chem
910
from reactions.utils.to_smiles import smiles_with_explicit_hydrogens
1011
from django.core.files.temp import NamedTemporaryFile
@@ -288,14 +289,30 @@ def get_vmh_miriam(abbr):
288289
- abbr (str): The molecule abbreviation.
289290
290291
Output:
291-
- (str): The MIRIAM ID for the molecule.
292+
- (str): The MIRIAM ID for the molecule is found, else empty string.
292293
"""
293294
BASE_URL = settings.OLD_VMH_BASE_URL
294295
encoded_abbr = quote(abbr)
295296
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 ''
299316

300317

301318
def search_metabolites_vmh(
@@ -322,8 +339,8 @@ def search_metabolites_vmh(
322339
file_idx = 0 # Initialize file index
323340
for idx, this_type in enumerate(types):
324341
if this_type == 'VMH':
325-
found = True
326342
miriam = get_vmh_miriam(mols[idx])
343+
found = bool(miriam)
327344
elif this_type == 'SwissLipids':
328345
base_url = 'https://www.swisslipids.org/api/index.php/entity/'
329346
endpoint = f"{base_url}{mols[idx]}"

0 commit comments

Comments
 (0)