Coverage for SynRBL/SynMCSImputer/structure.py: 89%

132 statements  

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

1from __future__ import annotations 

2 

3import rdkit.Chem.rdchem as rdchem 

4import rdkit.Chem.rdmolfiles as rdmolfiles 

5 

6 

7class Boundary: 

8 def __init__( 

9 self, 

10 compound: Compound, 

11 index: int, 

12 symbol: str | None = None, 

13 neighbor_index: int | None = None, 

14 neighbor_symbol: str | None = None, 

15 ): 

16 self.compound = compound 

17 self.index = index 

18 self.neighbor_index = neighbor_index 

19 self.symbol = symbol 

20 if self.symbol is None: 

21 self.symbol = self.compound.mol.GetAtomWithIdx(index).GetSymbol() 

22 self.neighbor_symbol = neighbor_symbol 

23 if self.neighbor_index is not None and self.neighbor_symbol is None: 

24 src_mol = self.promise_src() 

25 self.neighbor_symbol = src_mol.GetAtomWithIdx(neighbor_index).GetSymbol() 

26 self.is_merged = False 

27 

28 def __str__(self) -> str: 

29 return "Boundary '{}' @ {} in '{}' from '{}'.".format( 

30 self.symbol, self.index, self.compound.smiles, self.compound.src_smiles 

31 ) 

32 

33 def promise_src(self) -> rdchem.Mol: 

34 mol = self.compound.src_mol 

35 if mol is None: 

36 raise ValueError("Missing src_mol in compound.") 

37 return mol 

38 

39 def promise_neighbor_index(self) -> int: 

40 if self.neighbor_index is None: 

41 raise ValueError("Missing neighbor index.") 

42 return self.neighbor_index 

43 

44 def verify(self): 

45 if self.symbol is not None: 

46 mol = self.compound.mol 

47 sym = mol.GetAtomWithIdx(self.index).GetSymbol() 

48 if sym != self.symbol: 

49 raise ValueError( 

50 ( 

51 "Invalid boundary atom symbol. " 

52 + "Expected '{}' but found '{}' at index {}." 

53 ).format(self.symbol, sym, self.index) 

54 ) 

55 if self.neighbor_index is not None: 

56 src_mol = self.promise_src() 

57 sym = src_mol.GetAtomWithIdx(self.neighbor_index).GetSymbol() 

58 if sym != self.neighbor_symbol: 

59 raise ValueError( 

60 ( 

61 "Invalid neighboring atom symbol. " 

62 + "Expected '{}' but found '{}' at index {}." 

63 ).format(self.neighbor_symbol, sym, self.neighbor_index) 

64 ) 

65 

66 def get_atom(self) -> rdchem.Atom: 

67 mol = self.compound.mol 

68 return mol.GetAtomWithIdx(self.index) 

69 

70 def get_neighbor_atom(self) -> rdchem.Atom | None: 

71 mol = self.compound.src_mol 

72 if mol is None: 

73 return None 

74 return mol.GetAtomWithIdx(self.neighbor_index) 

75 

76 

77def _to_mol(value: str | rdchem.Mol | None) -> rdchem.Mol | None: 

78 if isinstance(value, str): 

79 return rdmolfiles.MolFromSmiles(value) 

80 elif isinstance(value, rdchem.Mol): 

81 return value 

82 elif value is None: 

83 return None 

84 else: 

85 raise TypeError("Value must be str or rdchem.Mol.") 

86 

87 

88class Compound: 

89 def __init__( 

90 self, 

91 mol: str | rdchem.Mol, 

92 src_mol: str | rdchem.Mol | None = None, 

93 compound_set: CompoundSet | None = None, 

94 ): 

95 self.mol: rdchem.Mol = _to_mol(mol) 

96 if self.mol is None: 

97 raise ValueError( 

98 ( 

99 "Argument 'mol' must be either a valid smiles or an " 

100 + "rdkit molecule. (value='{}')" 

101 ).format(mol) 

102 ) 

103 self.src_mol = _to_mol(src_mol) 

104 self.boundaries: list[Boundary] = [] 

105 self.rules = [] 

106 self.active = True 

107 self.__compound_set = compound_set 

108 

109 def __str__(self): 

110 return "Compound '{}' | Boundaries: {} ".format( 

111 self.smiles, [(b.symbol, b.index) for b in self.boundaries] 

112 ) 

113 

114 @property 

115 def smiles(self) -> str: 

116 return rdmolfiles.MolToSmiles(self.mol) 

117 

118 @property 

119 def num_compounds(self) -> int: 

120 return len(self.smiles.split(".")) 

121 

122 @property 

123 def src_smiles(self) -> str | None: 

124 if self.src_mol is None: 

125 return None 

126 return rdmolfiles.MolToSmiles(self.src_mol) 

127 

128 @property 

129 def is_catalyst(self) -> bool: 

130 return self.smiles == self.src_smiles and len(self.boundaries) == 0 

131 

132 @property 

133 def compound_set(self) -> CompoundSet: 

134 if self.__compound_set is None: 

135 raise RuntimeError("This compound does not belong to a set.") 

136 return self.__compound_set 

137 

138 def add_boundary( 

139 self, 

140 index, 

141 symbol: str | None = None, 

142 neighbor_index: int | None = None, 

143 neighbor_symbol: str | None = None, 

144 ) -> Boundary: 

145 b = Boundary( 

146 self, 

147 index, 

148 symbol=symbol, 

149 neighbor_index=neighbor_index, 

150 neighbor_symbol=neighbor_symbol, 

151 ) 

152 b.verify() 

153 self.boundaries.append(b) 

154 return b 

155 

156 def update(self, new_mol: rdchem.Mol, merged_boundary: Boundary): 

157 self.mol = new_mol 

158 self.boundaries.remove(merged_boundary) 

159 

160 def concat(self, compound: Compound): 

161 if self.compound_set != compound.compound_set: 

162 raise ValueError("Compounds are not from the same set.") 

163 if len(self.boundaries) > 0 or len(self.boundaries) > 0: 

164 raise ValueError( 

165 "Can not concat compounds with open boundaries. Try merging them." 

166 ) 

167 src_mol = None 

168 try: 

169 src_mol = _to_mol("{}.{}".format(self.src_smiles, compound.src_smiles)) 

170 except Exception: 

171 pass 

172 self.mol = _to_mol("{}.{}".format(self.smiles, compound.smiles)) 

173 self.src_mol = src_mol 

174 self.rules.extend(compound.rules) 

175 self.compound_set.remove_compound(compound) 

176 

177 def reset_compound_set(self): 

178 self.__compound_set = None 

179 

180 

181class CompoundSet: 

182 def __init__(self): 

183 self.__compounds: list[Compound] = [] 

184 

185 def __len__(self): 

186 return len(self.__compounds) 

187 

188 @property 

189 def compounds(self) -> list[Compound]: 

190 return self.__compounds 

191 

192 @property 

193 def boundaries(self) -> list[Boundary]: 

194 boundaries = [] 

195 for c in self.compounds: 

196 boundaries.extend(c.boundaries) 

197 return boundaries 

198 

199 def add_compound( 

200 self, mol: str | rdchem.Mol, src_mol: str | rdchem.Mol | None = None 

201 ): 

202 compound = Compound(mol, src_mol, compound_set=self) 

203 self.__compounds.append(compound) 

204 return compound 

205 

206 def remove_compound(self, compound: Compound): 

207 if compound.compound_set != self: 

208 raise ValueError("Compound is not from this set.") 

209 self.__compounds.remove(compound) 

210 compound.reset_compound_set()