Coverage for SynRBL/SynChemImputer/appel_reaction.py: 97%

39 statements  

« prev     ^ index     » next       coverage.py v7.4.3, created at 2024-04-11 11:17 +0200

1from typing import Dict 

2from rdkit import Chem 

3 

4 

5class AppelReaction: 

6 TCM = "ClC(Cl)(Cl)Cl" 

7 TCM_product = "ClC(Cl)Cl" 

8 TBM = "BrC(Br)(Br)Br" 

9 TBM_product = "BrC(Br)Br" 

10 TPP = "c1ccccc1P(c2ccccc2)c3ccccc3" 

11 TPPO = "O=P(c1ccccc1)(c2ccccc2)c3ccccc3" 

12 

13 def __init__(self): 

14 pass 

15 

16 @staticmethod 

17 def check_alcohol_group(smiles: str) -> bool: 

18 """ 

19 Check for the presence of an alcohol (hydroxyl, -OH) group in a molecule. 

20 

21 Args: 

22 smiles (str): A SMILES string representing the molecule. 

23 

24 Returns: 

25 bool: True if the alcohol group is present, False otherwise. 

26 """ 

27 alcohol_pattern = Chem.MolFromSmarts("[OH]") 

28 mol = Chem.MolFromSmiles(smiles) 

29 return mol.HasSubstructMatch(alcohol_pattern) if mol else False 

30 

31 @staticmethod 

32 def check_appel_reaction(reactants: str) -> bool: 

33 """ 

34 Check if the Appel reaction conditions are met in the reactants. 

35 

36 Args: 

37 reactants (str): SMILES string of the reactants. 

38 

39 Returns: 

40 bool: True if Appel reaction conditions are met, False otherwise. 

41 """ 

42 return any( 

43 reagent in reactants for reagent in [AppelReaction.TCM, AppelReaction.TBM] 

44 ) and AppelReaction.check_alcohol_group(reactants) 

45 

46 @staticmethod 

47 def check_missing_reagent(reactants: str) -> bool: 

48 """ 

49 Check if the key reagent (Triphenylphosphine) is missing in the reactants. 

50 

51 Args: 

52 reactants (str): SMILES string of the reactants. 

53 

54 Returns: 

55 bool: True if Triphenylphosphine is missing, False otherwise. 

56 """ 

57 return AppelReaction.TPP not in reactants 

58 

59 @staticmethod 

60 def check_missing_products(products: str) -> bool: 

61 """ 

62 Check if the expected product (Triphenylphosphine oxide) is missing in the products. 

63 

64 Args: 

65 products (str): SMILES string of the products. 

66 

67 Returns: 

68 bool: True if Triphenylphosphine oxide is missing, False otherwise. 

69 """ 

70 return AppelReaction.TPPO not in products 

71 

72 def fit( 

73 self, reaction_dict: Dict[str, str], rmsi_col: str, symbol: str = ">>" 

74 ) -> Dict[str, str]: 

75 """ 

76 Modify the reaction dictionary to include missing reactants or products for Appel reaction. 

77 

78 Args: 

79 reaction_dict (Dict[str, str]): Dictionary containing reaction information. 

80 rmsi_col (str): Key for the reaction SMILES string in the dictionary. 

81 symbol (str): Symbol used to separate reactants and products. Default is '>>'. 

82 

83 Returns: 

84 Dict[str, str]: Updated reaction dictionary with modified SMILES string. 

85 """ 

86 reaction = reaction_dict[rmsi_col] 

87 reactants, products = reaction.split(symbol) 

88 

89 if self.check_appel_reaction(reactants): 

90 if self.check_missing_reagent(reactants): 

91 reactants += "." + AppelReaction.TPP 

92 if self.check_missing_products(products): 

93 products += "." + AppelReaction.TPPO 

94 

95 # Add TCM or TBM products if they are missing 

96 if ( 

97 AppelReaction.TCM in reactants 

98 and AppelReaction.TCM_product not in products 

99 ): 

100 products += "." + AppelReaction.TCM_product 

101 elif ( 

102 AppelReaction.TBM in reactants 

103 and AppelReaction.TBM_product not in products 

104 ): 

105 products += "." + AppelReaction.TBM_product 

106 

107 reaction_dict[rmsi_col] = symbol.join([reactants, products]) 

108 

109 return reaction_dict