Coverage for SynRBL/SynChemImputer/peroxide_imputer.py: 90%
50 statements
« prev ^ index » next coverage.py v7.4.3, created at 2024-04-11 11:17 +0200
« prev ^ index » next coverage.py v7.4.3, created at 2024-04-11 11:17 +0200
1from rdkit import Chem
4class PeroxidGroupImputer:
5 """A class for imputing peroxide and peracid groups in chemical reactions."""
7 @staticmethod
8 def check_peroxide(smiles: str) -> bool:
9 """Check for the presence of a peroxide substructure in a molecule."""
10 try:
11 peroxide_pattern = Chem.MolFromSmarts("OO")
12 mol = Chem.MolFromSmiles(smiles)
13 return (
14 mol.HasSubstructMatch(peroxide_pattern)
15 if mol and not PeroxidGroupImputer.check_peracid(smiles)
16 else False
17 )
18 except Exception:
19 return False
21 @staticmethod
22 def check_peracid(smiles: str) -> bool:
23 """Check for the presence of a peracid substructure in a molecule."""
24 try:
25 peracid_pattern = Chem.MolFromSmarts("C(OO)=O")
26 mol = Chem.MolFromSmiles(smiles)
27 return mol.HasSubstructMatch(peracid_pattern) if mol else False
28 except Exception:
29 return False
31 @staticmethod
32 def impute_peroxide(reactant_smiles: str, product_smiles: str) -> tuple:
33 """Impute a peroxide functional group in the reactant SMILES string and update the product SMILES accordingly."""
34 updated = False
35 reactant_components = reactant_smiles.split(".")
36 for smiles in reactant_components:
37 if PeroxidGroupImputer.check_peroxide(smiles):
38 updated_smiles = smiles.replace("OO", "O.O")
39 updated = True
41 if updated:
42 updated_product_smiles = (
43 product_smiles + "." + Chem.CanonSmiles(updated_smiles)
44 )
45 return reactant_smiles, updated_product_smiles
46 else:
47 return reactant_smiles, product_smiles
49 @staticmethod
50 def impute_peracid(reactant_smiles: str, product_smiles: str) -> tuple:
51 """Impute a peracid functional group in the reactant SMILES string and update the product SMILES accordingly."""
52 updated = False
53 reactant_components = reactant_smiles.split(".")
54 for smiles in reactant_components:
55 if PeroxidGroupImputer.check_peracid(smiles):
56 updated_smiles = Chem.CanonSmiles(
57 smiles.replace("O=C(OO)", "O=C(O)").replace("C(=O)OO", "C(=O)O")
58 )
59 updated = True
61 if updated and updated_smiles not in product_smiles.split("."):
62 updated_product_smiles = (
63 product_smiles + "." + Chem.CanonSmiles(updated_smiles)
64 )
65 return reactant_smiles, updated_product_smiles
66 else:
67 return reactant_smiles, product_smiles
69 def fix(self, reactions_dict: dict, rsmi_col: str, symbol: str = ">>") -> dict:
70 """Process and update reactions containing peroxide or peracid groups."""
71 reactants, products = reactions_dict[rsmi_col].split(symbol)
72 if PeroxidGroupImputer.check_peroxide(reactants):
73 reactants, products = PeroxidGroupImputer.impute_peroxide(
74 reactants, products
75 )
76 elif PeroxidGroupImputer.check_peracid(reactants):
77 reactants, products = PeroxidGroupImputer.impute_peracid(
78 reactants, products
79 )
80 reactions_dict[rsmi_col] = symbol.join([reactants, products])
81 return reactions_dict