[FIX] Fixed Search Output, Legacy API Model Endpoint, Handle ObjectsDoesNotExists in views (#297)

Co-authored-by: Tim Lorsbach <tim@lorsba.ch>
Reviewed-on: enviPath/enviPy#297
This commit is contained in:
2026-01-15 20:39:54 +13:00
parent 6499a0c659
commit 54f8302104
9 changed files with 111 additions and 103 deletions

View File

@ -2,14 +2,12 @@ import logging
import re
from abc import ABC
from collections import defaultdict
from typing import List, Optional, Dict, TYPE_CHECKING, Union
from typing import TYPE_CHECKING, Dict, List, Optional, Union
from indigo import Indigo, IndigoException, IndigoObject
from indigo.renderer import IndigoRenderer
from rdkit import Chem, rdBase
from rdkit.Chem import MACCSkeys, Descriptors, rdFingerprintGenerator
from rdkit.Chem import rdchem
from rdkit.Chem import rdChemReactions
from rdkit.Chem import Descriptors, MACCSkeys, rdchem, rdChemReactions, rdFingerprintGenerator
from rdkit.Chem.Draw import rdMolDraw2D
from rdkit.Chem.MolStandardize import rdMolStandardize
from rdkit.Chem.rdmolops import GetMolFrags
@ -335,9 +333,14 @@ class FormatConverter(object):
# Inplace
if preprocess_smiles:
# from rdkit.Chem.rdmolops import AROMATICITY_RDKIT
# Chem.SetAromaticity(mol, AROMATICITY_RDKIT)
Chem.SanitizeMol(mol)
mol = Chem.AddHs(mol)
# for std in BASIC:
# mol = std.standardize(mol)
# Check if reactant_filter_smarts matches and we shouldn't apply the rule
if reactant_filter_smarts and FormatConverter.smarts_matches(
mol, reactant_filter_smarts
@ -376,29 +379,6 @@ class FormatConverter(object):
prods.append(p)
# if kekulize:
# # from rdkit.Chem import MolStandardize
# #
# # # Attempt re-sanitization via standardizer
# # cleaner = MolStandardize.rdMolStandardize.Cleanup()
# # mol = cleaner.cleanup(product)
# # # Fixes
# # # [2025-01-30 23:00:50] ERROR chem - Sanitizing and converting failed:
# # # non-ring atom 3 marked aromatic
# # # But does not improve overall performance
# # # for a in product.GetAtoms():
# # # if (not a.IsInRing()) and a.GetIsAromatic():
# # # a.SetIsAromatic(False)
# # #
# # # for b in product.GetBonds():
# # # if (not b.IsInRing()) and b.GetIsAromatic():
# # # b.SetIsAromatic(False)
# # for atom in product.GetAtoms():
# # atom.SetIsAromatic(False)
# # for bond in product.GetBonds():
# # bond.SetIsAromatic(False)
# Chem.Kekulize(product)
except ValueError as e:
logger.error(f"Sanitizing and converting failed:\n{e}")
continue
@ -524,8 +504,8 @@ class Standardizer(ABC):
def __init__(self, name):
self.name = name
def standardize(self, smiles: str) -> str:
return FormatConverter.normalize(smiles)
def standardize(self, mol: rdchem.Mol) -> rdchem.Mol:
return mol
class RuleStandardizer(Standardizer):
@ -533,18 +513,20 @@ class RuleStandardizer(Standardizer):
super().__init__(name)
self.smirks = smirks
def standardize(self, smiles: str) -> str:
standardized_smiles = list(set(FormatConverter.apply(smiles, self.smirks)))
def standardize(self, mol: rdchem.Mol) -> rdchem.Mol:
rxn = rdChemReactions.ReactionFromSmarts(self.smirks)
sites = rxn.RunReactants((mol,))
if len(standardized_smiles) > 1:
logger.warning(f"{self.smirks} generated more than 1 compound {standardized_smiles}")
print(f"{self.smirks} generated more than 1 compound {standardized_smiles}")
standardized_smiles = standardized_smiles[:1]
if len(sites) == 1:
sites = sites[0]
if standardized_smiles:
smiles = standardized_smiles[0]
if len(sites) > 1:
logger.warning(f"{self.smirks} generated more than 1 compound {sites}")
print(f"{self.smirks} generated more than 1 compound {sites}")
return super().standardize(smiles)
mol = sites[0]
return mol
class RegExStandardizer(Standardizer):
@ -552,19 +534,20 @@ class RegExStandardizer(Standardizer):
super().__init__(name)
self.replacements = replacements
def standardize(self, smiles: str) -> str:
smi = smiles
mod_smi = smiles
for k, v in self.replacements.items():
mod_smi = smi.replace(k, v)
while mod_smi != smi:
mod_smi = smi
for k, v in self.replacements.items():
smi = smi.replace(k, v)
return super().standardize(smi)
def standardize(self, mol: rdchem.Mol) -> rdchem.Mol:
# smi = smiles
# mod_smi = smiles
#
# for k, v in self.replacements.items():
# mod_smi = smi.replace(k, v)
#
# while mod_smi != smi:
# mod_smi = smi
# for k, v in self.replacements.items():
# smi = smi.replace(k, v)
#
# return super().standardize(smi)
raise ValueError("Not implemented yet!")
FLATTEN = [RegExStandardizer("Remove Stereo", {"@": ""})]