From 819a94aced7cb65b089d508fac7f8aba1bbab1a4 Mon Sep 17 00:00:00 2001 From: jebus Date: Wed, 22 Oct 2025 01:13:06 +1300 Subject: [PATCH] [Fix] Catch Exception for Adding Structures / Show PubChem Substances (#168) Fixes #163 Fixes #165 Co-authored-by: Tim Lorsbach Reviewed-on: https://git.envipath.com/enviPath/enviPy/pulls/168 --- epdb/models.py | 13 ++++++++----- epdb/views.py | 11 ++++++++++- templates/objects/compound.html | 20 ++++++++++++++++++-- 3 files changed, 36 insertions(+), 8 deletions(-) diff --git a/epdb/models.py b/epdb/models.py index a03fcb6d..998503e2 100644 --- a/epdb/models.py +++ b/epdb/models.py @@ -310,7 +310,7 @@ class ExternalDatabase(TimeStampedModel): }, { "database": ExternalDatabase.objects.get(name="ChEBI"), - "placeholder": "ChEBI ID without prefix e.g. 12345", + "placeholder": "ChEBI ID without prefix e.g. 10576", }, ], "structure": [ @@ -328,7 +328,7 @@ class ExternalDatabase(TimeStampedModel): }, { "database": ExternalDatabase.objects.get(name="ChEBI"), - "placeholder": "ChEBI ID without prefix e.g. 12345", + "placeholder": "ChEBI ID without prefix e.g. 10576", }, ], "reaction": [ @@ -342,7 +342,7 @@ class ExternalDatabase(TimeStampedModel): }, { "database": ExternalDatabase.objects.get(name="UniProt"), - "placeholder": "Query ID for UniPro e.g. rhea:12345", + "placeholder": "Query ID for UniProt e.g. rhea:12345", }, ], } @@ -477,7 +477,7 @@ class ChemicalIdentifierMixin(ExternalIdentifierMixin): return self.add_external_identifier("CAS", cas_number) def get_pubchem_identifiers(self): - return self.get_external_identifier("PubChem Compound") or self.get_external_identifier( + return self.get_external_identifier("PubChem Compound") | self.get_external_identifier( "PubChem Substance" ) @@ -3043,6 +3043,7 @@ class EnviFormer(PackageBasedModel): @cached_property def model(self): from enviformer import load + ckpt = os.path.join(s.MODEL_DIR, "enviformer", str(self.uuid), f"{self.uuid}.ckpt") mod = load(device=s.ENVIFORMER_DEVICE, ckpt_path=ckpt) return mod @@ -3062,7 +3063,9 @@ class EnviFormer(PackageBasedModel): start = datetime.now() products_list = self.model.predict_batch(canon_smiles) end = datetime.now() - logger.info(f"Prediction took {(end - start).total_seconds():.2f} seconds. Got results {products_list}") + logger.info( + f"Prediction took {(end - start).total_seconds():.2f} seconds. Got results {products_list}" + ) results = [] for products in products_list: diff --git a/epdb/views.py b/epdb/views.py index 4844d3be..1f73027a 100644 --- a/epdb/views.py +++ b/epdb/views.py @@ -1251,7 +1251,16 @@ def package_compound_structures(request, package_uuid, compound_uuid): structure_smiles = request.POST.get("structure-smiles") structure_description = request.POST.get("structure-description") - cs = current_compound.add_structure(structure_smiles, structure_name, structure_description) + try: + cs = current_compound.add_structure( + structure_smiles, structure_name, structure_description + ) + except ValueError: + return error( + request, + "Adding structure failed!", + "The structure could not be added as normalized structures don't match!", + ) return redirect(cs.url) diff --git a/templates/objects/compound.html b/templates/objects/compound.html index 98083ca6..90e3e1db 100644 --- a/templates/objects/compound.html +++ b/templates/objects/compound.html @@ -183,7 +183,7 @@
- {% if compound.get_pubchem_identifiers %} + {% if compound.get_pubchem_compound_identifiers %}

@@ -193,12 +193,28 @@

- {% for eid in compound.get_pubchem_identifiers %} + {% for eid in compound.get_pubchem_compound_identifiers %} CID{{ eid.identifier_value }} {% endfor %}
{% endif %} + {% if compound.get_pubchem_substance_identifiers %} + +
+ {% for eid in compound.get_pubchem_substance_identifiers %} + SID{{ eid.identifier_value }} + {% endfor %} +
+ {% endif %} {% if compound.get_chebi_identifiers %}