[Feature] Implemented SMARTS filtering for Rules (#246)

Reactant Filter SMARTS as well as Product Filter SMARTS are now reflected when applying rules.

Fixes #245

Co-authored-by: Tim Lorsbach <tim@lorsba.ch>
Reviewed-on: enviPath/enviPy#246
This commit is contained in:
2025-11-28 23:28:41 +13:00
parent fd2e2c2534
commit e8ae494c16
4 changed files with 90 additions and 3 deletions

View File

@ -5,6 +5,7 @@ from django.test import TestCase
from epdb.logic import PackageManager
from epdb.models import SimpleAmbitRule, User
from utilities.chem import FormatConverter
class SimpleAmbitRuleTest(TestCase):
@ -189,7 +190,9 @@ class SimpleAmbitRuleTest(TestCase):
test_smiles = "CCO"
result = rule.apply(test_smiles)
mock_apply.assert_called_once_with(test_smiles, rule.smirks)
mock_apply.assert_called_once_with(
test_smiles, rule.smirks, reactant_filter_smarts=None, product_filter_smarts=None
)
self.assertEqual(result, ["product1", "product2"])
def test_reactants_smarts_property(self):
@ -338,3 +341,41 @@ class SimpleAmbitRuleTest(TestCase):
self.assertEqual(
rule._meta.get_field("product_filter_smarts").verbose_name, "Product Filter SMARTS"
)
def test_reactant_filter_smarts(self):
rule = SimpleAmbitRule.create(
package=self.package,
smirks="[#6,#8,#16:6]-[#6:4](=[O:5])[#6:2]=[#6;!$(CO)!$(C=CN[H])!$(C(=C)N[H]):1]>>[#8]([H])-[#6:1]-[#6:2]-[#6:4](-[#6,#8,#16:6])=[O:5]",
reactant_filter_smarts="[$(OC(=O)C=CC=CC(O)=O),$(OC1C=CC=CC1O)]",
product_filter_smarts=None,
)
SMILES = "OC(=O)C=CC=CC(O)=O"
res = FormatConverter.apply(SMILES, rule.smirks)
# Assert Rule triggers, if we use FormatConverter without reactant_filter_smarts passed along
self.assertTrue(len(res) > 0)
res = rule.apply(SMILES)
# Check if reactant_filter_smarts prevents rule from application
self.assertTrue(len(res) == 0)
def test_product_filter_smarts(self):
rule = SimpleAmbitRule.create(
package=self.package,
smirks="[#6,#8,#16:6]-[#6:4](=[O:5])[#6:2]=[#6;!$(CO)!$(C=CN[H])!$(C(=C)N[H]):1]>>[#8]([H])-[#6:1]-[#6:2]-[#6:4](-[#6,#8,#16:6])=[O:5]",
reactant_filter_smarts=None,
product_filter_smarts="[$(O=C(O)C=CC=CC(O)CC(=O)O)]",
)
SMILES = "OC(=O)C=CC=CC=CC(O)=O"
res = FormatConverter.apply(SMILES, rule.smirks)
# Assert Rule triggers, if we use FormatConverter without product_filter_smarts passed along
self.assertTrue(len(res) > 0)
res = rule.apply(SMILES)
# Check if reactant_filter_smarts prevents rule from application
self.assertTrue(len(res) == 0)