MacrocycleWizard 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,426 @@
1
+ """MacrocycleWizard/MolD/specific/GroupD.py"""
2
+ """
3
+ Group D: Macrocycle Fragment Structure MolDs
4
+ """
5
+ from tqdm import tqdm
6
+ from rdkit import Chem
7
+ from rdkit.Chem.Lipinski import HeavyAtomCount
8
+ from rdkit.Chem import Descriptors
9
+ from ..base import _get_macrocycle_ring_mol_2
10
+ from ..base import MacrocycleCalculator
11
+
12
+ class GroupD(MacrocycleCalculator):
13
+ def get_D01_Substituent_Fsp3_Ratio(self):
14
+ """
15
+ Calculate the Substituent Fsp3: Fraction of substituent carbons that are sp3 hybridised
16
+ Note: Only considers substituents (fragments with ≥2 heavy atoms)
17
+ Returns:
18
+ list: List of Substituent Fsp3 values for each molecule
19
+ """
20
+ D01_Substituent_Fsp3_Ratio = []
21
+ for fragments in tqdm(self.fragment_smiles):
22
+ num_substituent_carbons = 0
23
+ num_substituent_sp3_carbons = 0
24
+ for frag_smi in fragments:
25
+ frag_mol = Chem.MolFromSmiles(frag_smi)
26
+ if frag_mol:
27
+ frag_heavy_atoms = HeavyAtomCount(frag_mol)
28
+ # Only consider substituents (≥2 heavy atoms)
29
+ if frag_heavy_atoms >= 2:
30
+ for atom in frag_mol.GetAtoms():
31
+ if atom.GetSymbol() == 'C':
32
+ num_substituent_carbons += 1
33
+ if atom.GetHybridization() == Chem.HybridizationType.SP3:
34
+ num_substituent_sp3_carbons += 1
35
+ # Calculate the fraction of sp3 carbons in substituents
36
+ if num_substituent_carbons == 0:
37
+ substituent_fsp3 = 0
38
+ else:
39
+ substituent_fsp3 = num_substituent_sp3_carbons / num_substituent_carbons
40
+ D01_Substituent_Fsp3_Ratio.append(substituent_fsp3)
41
+ return D01_Substituent_Fsp3_Ratio
42
+
43
+ def get_D02_Substituent_Flexibility_Ratio(self):
44
+ '''
45
+ Calculate substituent flexibility fraction
46
+ Formula: subFlexibility = ((numRotatableBonds)+1)/(HA-N-numPeripheralAtoms+1)
47
+ where N is the macrocycle ring size
48
+ '''
49
+ D02_Substituent_Flexibility_Ratio = []
50
+ for mol, ha, ring_size, fragments in tqdm(zip(self.mols,
51
+ self.HA,
52
+ self.ring_size,
53
+ self.fragment_smiles)):
54
+ peripheral_count = 0
55
+ for frag_smi in fragments:
56
+ frag_mol = Chem.MolFromSmiles(frag_smi)
57
+ if frag_mol:
58
+ frag_heavy_atoms = HeavyAtomCount(frag_mol)
59
+ # Count peripherals (=1 heavy atom)
60
+ if frag_heavy_atoms == 1:
61
+ peripheral_count += 1
62
+
63
+ denom = ha - ring_size - peripheral_count + 1
64
+ subFlexibility = (Descriptors.NumRotatableBonds(mol) + 1) / denom if (mol is not None and denom > 0) else 0.0
65
+ D02_Substituent_Flexibility_Ratio.append(subFlexibility)
66
+
67
+ return D02_Substituent_Flexibility_Ratio
68
+
69
+ def get_D03_Fragment_Rigid_Bond(self):
70
+ '''
71
+ Count the non-rotatable bonds linking the macrocycle ring and all fragments.
72
+ Calculated as total bonds between macrocycle and fragments minus rotatable bonds.
73
+ '''
74
+ D03_Fragment_Rigid_Bond = []
75
+ for mol in tqdm(self.mols):
76
+ # Get the macrocycle ring atoms
77
+ _, macrocycle_atoms = _get_macrocycle_ring_mol_2(mol)
78
+ macrocycle_atoms_set = set(macrocycle_atoms)
79
+ # Count the total number of bonds between ring and fragments
80
+ total_link_count = 0
81
+ # Count the number of rotatable bonds between ring and fragments
82
+ rotatable_count = 0
83
+ for bond in mol.GetBonds():
84
+ begin_idx = bond.GetBeginAtomIdx()
85
+ end_idx = bond.GetEndAtomIdx()
86
+ # Check if bond connects ring to non-ring (i.e., to fragments)
87
+ if (begin_idx in macrocycle_atoms_set and end_idx not in macrocycle_atoms_set) or \
88
+ (end_idx in macrocycle_atoms_set and begin_idx not in macrocycle_atoms_set):
89
+ # Count total links
90
+ total_link_count += 1
91
+ # Check if the bond is rotatable
92
+ if bond.GetBondType() == Chem.BondType.SINGLE and \
93
+ not bond.IsInRing() and \
94
+ not (mol.GetAtomWithIdx(begin_idx).GetAtomicNum() == 1 or
95
+ mol.GetAtomWithIdx(end_idx).GetAtomicNum() == 1): # Not to hydrogen
96
+ rotatable_count += 1
97
+ # Rigid bonds = total bonds - rotatable bonds
98
+ rigid_count = total_link_count - rotatable_count
99
+ D03_Fragment_Rigid_Bond.append(rigid_count)
100
+ return D03_Fragment_Rigid_Bond
101
+
102
+ def get_D04_Large_Substituent_Count(self):
103
+ """
104
+ Count the number of large substituents (≥5 heavy atoms)
105
+ """
106
+ D04_Large_Substituent_Count = []
107
+ for fragments in tqdm(self.fragment_smiles):
108
+ large_substituents = []
109
+ for frag_smi in fragments:
110
+ frag_mol = Chem.MolFromSmiles(frag_smi)
111
+ if frag_mol:
112
+ frag_heavy_atoms = HeavyAtomCount(frag_mol)
113
+ # Large substituents: ≥5 heavy atoms
114
+ if frag_heavy_atoms >= 5:
115
+ large_substituents.append(frag_heavy_atoms)
116
+ count_LargeSubs = len(large_substituents)
117
+ D04_Large_Substituent_Count.append(count_LargeSubs)
118
+ return D04_Large_Substituent_Count
119
+
120
+ def get_D05_Small_Substituent_Count(self):
121
+ """
122
+ Count the number of small substituents (2-4 heavy atoms)
123
+ """
124
+ D05_Small_Substituent_Count = []
125
+ for fragments in tqdm(self.fragment_smiles):
126
+ small_substituents = []
127
+ for frag_smi in fragments:
128
+ frag_mol = Chem.MolFromSmiles(frag_smi)
129
+ if frag_mol:
130
+ frag_heavy_atoms = HeavyAtomCount(frag_mol)
131
+ # Small substituents: 2-4 heavy atoms
132
+ if 2 <= frag_heavy_atoms <= 4:
133
+ small_substituents.append(frag_heavy_atoms)
134
+ count_SmallSubs = len(small_substituents)
135
+ D05_Small_Substituent_Count.append(count_SmallSubs)
136
+ return D05_Small_Substituent_Count
137
+
138
+ def get_D06_Normalised_Substituent_Heavy_Atom(self):
139
+ """
140
+ Calculate normalized substituent heavy atoms
141
+ Formula: (Sum of heavy atoms in all substituents) / (Total heavy atoms)
142
+ Note: Only counts substituents (≥2 heavy atoms)
143
+ """
144
+ D06_Normalised_Substituent_Heavy_Atom = []
145
+ for i, fragments in enumerate(tqdm(self.fragment_smiles)):
146
+ sum_substituent_heavy_atoms = 0
147
+ for frag_smi in fragments:
148
+ frag_mol = Chem.MolFromSmiles(frag_smi)
149
+ if frag_mol:
150
+ frag_heavy_atoms = HeavyAtomCount(frag_mol)
151
+ # Only count substituents (≥2 heavy atoms)
152
+ if frag_heavy_atoms >= 2:
153
+ sum_substituent_heavy_atoms += frag_heavy_atoms
154
+ if self.HA[i] > 0:
155
+ normalized_value = sum_substituent_heavy_atoms / self.HA[i]
156
+ else:
157
+ normalized_value = 0
158
+ D06_Normalised_Substituent_Heavy_Atom.append(normalized_value)
159
+ return D06_Normalised_Substituent_Heavy_Atom
160
+
161
+ def get_D07_Normalised_Substituent_to_Macrocycle_Ring_Size(self):
162
+ """
163
+ Calculate normalized number of substituents
164
+ Formula: (Number of substituents) / (Macrocycle ring size)
165
+ Note: Only counts substituents (≥2 heavy atoms)
166
+ """
167
+ D07_Normalised_Substituent_to_Macrocycle_Ring_Size = []
168
+ for i, (fragments, ring_size) in enumerate(tqdm(zip(self.fragment_smiles, self.ring_size))):
169
+ substituents_count = 0
170
+ for frag_smi in fragments:
171
+ frag_mol = Chem.MolFromSmiles(frag_smi)
172
+ if frag_mol:
173
+ frag_heavy_atoms = HeavyAtomCount(frag_mol)
174
+ # Only count substituents (≥2 heavy atoms)
175
+ if frag_heavy_atoms >= 2:
176
+ substituents_count += 1
177
+ if ring_size > 0:
178
+ normalised_num_sub_mrs = substituents_count / ring_size
179
+ else:
180
+ normalised_num_sub_mrs = 0
181
+ D07_Normalised_Substituent_to_Macrocycle_Ring_Size.append(normalised_num_sub_mrs)
182
+ return D07_Normalised_Substituent_to_Macrocycle_Ring_Size
183
+
184
+ def get_D08_Normalised_Peripheral_to_Macrocycle_Ring_Size(self):
185
+ """
186
+ Calculate normalized number of peripherals
187
+ Formula: (Number of peripherals) / (Macrocycle ring size)
188
+ Note: Only counts peripherals (=1 heavy atom)
189
+ """
190
+ D08_Normalised_Peripheral_to_Macrocycle_Ring_Size = []
191
+ for i, (fragments, ring_size) in enumerate(tqdm(zip(self.fragment_smiles, self.ring_size))):
192
+ peripheral_count = 0
193
+ for frag_smi in fragments:
194
+ frag_mol = Chem.MolFromSmiles(frag_smi)
195
+ if frag_mol:
196
+ frag_heavy_atoms = HeavyAtomCount(frag_mol)
197
+ # Only count peripherals (=1 heavy atom)
198
+ if frag_heavy_atoms == 1:
199
+ peripheral_count += 1
200
+ if ring_size > 0:
201
+ normalised_num_periph_mrs = peripheral_count / ring_size
202
+ else:
203
+ normalised_num_periph_mrs = 0
204
+ D08_Normalised_Peripheral_to_Macrocycle_Ring_Size.append(normalised_num_periph_mrs)
205
+ return D08_Normalised_Peripheral_to_Macrocycle_Ring_Size
206
+
207
+ def get_D09_Max_Substituent_Size(self):
208
+ """
209
+ Calculate the number of heavy atoms in the largest substituent.
210
+ Note: Only considers substituents (≥2 heavy atoms)
211
+ """
212
+ D09_Max_Substituent_Size = []
213
+ for fragments in tqdm(self.fragment_smiles):
214
+ max_size = 0
215
+ if fragments:
216
+ substituent_sizes = []
217
+ for frag_smi in fragments:
218
+ frag_mol = Chem.MolFromSmiles(frag_smi)
219
+ if frag_mol:
220
+ frag_heavy_atoms = HeavyAtomCount(frag_mol)
221
+ # Only consider substituents (≥2 heavy atoms)
222
+ if frag_heavy_atoms >= 2:
223
+ substituent_sizes.append(frag_heavy_atoms)
224
+ if substituent_sizes:
225
+ max_size = max(substituent_sizes)
226
+ D09_Max_Substituent_Size.append(max_size)
227
+ return D09_Max_Substituent_Size
228
+
229
+ def get_D10_Min_Substituent_Size(self):
230
+ """
231
+ Calculate the number of heavy atoms in the smallest substituent.
232
+ Note: Only considers substituents (≥2 heavy atoms), excludes peripherals
233
+ """
234
+ D10_Min_Substituent_Size = []
235
+
236
+ for fragments in tqdm(self.fragment_smiles):
237
+ min_size = 0
238
+ if fragments:
239
+ substituent_sizes = []
240
+ for frag_smi in fragments:
241
+ frag_mol = Chem.MolFromSmiles(frag_smi)
242
+ if frag_mol:
243
+ frag_heavy_atoms = HeavyAtomCount(frag_mol)
244
+ # Only consider substituents (≥2 heavy atoms)
245
+ if frag_heavy_atoms >= 2:
246
+ substituent_sizes.append(frag_heavy_atoms)
247
+
248
+ if substituent_sizes:
249
+ min_size = min(substituent_sizes)
250
+
251
+ D10_Min_Substituent_Size.append(min_size)
252
+
253
+ return D10_Min_Substituent_Size
254
+
255
+ def get_D11_Mean_Substituent_Size(self):
256
+ """
257
+ Calculate the average number of heavy atoms across all substituents.
258
+ Note: Only considers substituents (≥2 heavy atoms)
259
+ """
260
+ D11_Mean_Substituent_Size = []
261
+
262
+ for fragments in tqdm(self.fragment_smiles):
263
+ mean_size = 0
264
+ if fragments:
265
+ substituent_sizes = []
266
+ for frag_smi in fragments:
267
+ frag_mol = Chem.MolFromSmiles(frag_smi)
268
+ if frag_mol:
269
+ frag_heavy_atoms = HeavyAtomCount(frag_mol)
270
+ # Only consider substituents (≥2 heavy atoms)
271
+ if frag_heavy_atoms >= 2:
272
+ substituent_sizes.append(frag_heavy_atoms)
273
+
274
+ if substituent_sizes:
275
+ mean_size = sum(substituent_sizes) / len(substituent_sizes)
276
+
277
+ D11_Mean_Substituent_Size.append(mean_size)
278
+
279
+ return D11_Mean_Substituent_Size
280
+
281
+ def get_D12_Substituent_Size_Standard_Deviation(self):
282
+ """
283
+ Calculate the standard deviation of substituent sizes.
284
+ Note: Only considers substituents (≥2 heavy atoms)
285
+ """
286
+ D12_Substituent_Size_Standard_Deviation = []
287
+
288
+ for fragments in tqdm(self.fragment_smiles):
289
+ std_dev = 0
290
+ if fragments and len(fragments) >= 2:
291
+ substituent_sizes = []
292
+ for frag_smi in fragments:
293
+ frag_mol = Chem.MolFromSmiles(frag_smi)
294
+ if frag_mol:
295
+ frag_heavy_atoms = HeavyAtomCount(frag_mol)
296
+ # Only consider substituents (≥2 heavy atoms)
297
+ if frag_heavy_atoms >= 2:
298
+ substituent_sizes.append(frag_heavy_atoms)
299
+
300
+ if len(substituent_sizes) >= 2:
301
+ # Calculate population standard deviation
302
+ mean = sum(substituent_sizes) / len(substituent_sizes)
303
+ variance = sum((x - mean) ** 2 for x in substituent_sizes) / len(substituent_sizes)
304
+ std_dev = variance ** 0.5
305
+
306
+ D12_Substituent_Size_Standard_Deviation.append(std_dev)
307
+
308
+ return D12_Substituent_Size_Standard_Deviation
309
+
310
+ def get_D13_Substituent_Size_Coefficient_of_Variation(self):
311
+ """
312
+ Calculate the coefficient of variation (CV) of substituent sizes.
313
+ Note: Only considers substituents (≥2 heavy atoms)
314
+ """
315
+ D13_Substituent_Size_Coefficient_of_Variation = []
316
+
317
+ for fragments in tqdm(self.fragment_smiles):
318
+ cv = 0
319
+ if fragments and len(fragments) >= 2:
320
+ substituent_sizes = []
321
+ for frag_smi in fragments:
322
+ frag_mol = Chem.MolFromSmiles(frag_smi)
323
+ if frag_mol:
324
+ frag_heavy_atoms = HeavyAtomCount(frag_mol)
325
+ # Only consider substituents (≥2 heavy atoms)
326
+ if frag_heavy_atoms >= 2:
327
+ substituent_sizes.append(frag_heavy_atoms)
328
+
329
+ if substituent_sizes:
330
+ mean = sum(substituent_sizes) / len(substituent_sizes)
331
+ if mean > 0 and len(substituent_sizes) >= 2:
332
+ variance = sum((x - mean) ** 2 for x in substituent_sizes) / len(substituent_sizes)
333
+ std_dev = variance ** 0.5
334
+ cv = std_dev / mean
335
+
336
+ D13_Substituent_Size_Coefficient_of_Variation.append(cv)
337
+
338
+ return D13_Substituent_Size_Coefficient_of_Variation
339
+
340
+ def get_D14_Max_Pair_Spacing(self):
341
+ """Calculate maximum Pair_Spacing between substituted atoms on the macrocycle"""
342
+ D14_Max_Pair_Spacing = []
343
+ for spacing_by_atom_pair in tqdm(self.pair_spacings):
344
+ if spacing_by_atom_pair and any(spacing != float('inf') for spacing in spacing_by_atom_pair.values()):
345
+ # Filter out infinity values, consider only valid Pair_Spacing values
346
+ valid_pair_spacings = [spacing for spacing in spacing_by_atom_pair.values() if spacing != float('inf')]
347
+ if valid_pair_spacings:
348
+ max_pair_spacing = max(valid_pair_spacings)
349
+ D14_Max_Pair_Spacing.append(max_pair_spacing)
350
+ else:
351
+ D14_Max_Pair_Spacing.append(0)
352
+ else:
353
+ D14_Max_Pair_Spacing.append(0)
354
+ return D14_Max_Pair_Spacing
355
+
356
+ def get_D15_Min_Pair_Spacing(self):
357
+ """Calculate minimum Pair_Spacing between substituted atoms on the macrocycle"""
358
+ D15_Min_Pair_Spacing = []
359
+ for spacing_by_atom_pair in tqdm(self.pair_spacings):
360
+ if spacing_by_atom_pair and any(spacing != float('inf') for spacing in spacing_by_atom_pair.values()):
361
+ # Filter out infinity values, consider only valid Pair_Spacing values
362
+ valid_pair_spacings = [spacing for spacing in spacing_by_atom_pair.values() if spacing != float('inf')]
363
+ if valid_pair_spacings:
364
+ min_pair_spacing = min(valid_pair_spacings)
365
+ D15_Min_Pair_Spacing.append(min_pair_spacing)
366
+ else:
367
+ D15_Min_Pair_Spacing.append(0)
368
+ else:
369
+ D15_Min_Pair_Spacing.append(0)
370
+ return D15_Min_Pair_Spacing
371
+
372
+ def get_D16_Mean_Pair_Spacing(self):
373
+ """Calculate average Pair_Spacing between substituted atoms on the macrocycle"""
374
+ D16_Mean_Pair_Spacing = []
375
+ for spacing_by_atom_pair in tqdm(self.pair_spacings):
376
+ if spacing_by_atom_pair and any(spacing != float('inf') for spacing in spacing_by_atom_pair.values()):
377
+ # Filter out infinity values, consider only valid Pair_Spacing values
378
+ valid_pair_spacings = [spacing for spacing in spacing_by_atom_pair.values() if spacing != float('inf')]
379
+ if valid_pair_spacings:
380
+ mean_pair_spacing = sum(valid_pair_spacings) / len(valid_pair_spacings)
381
+ D16_Mean_Pair_Spacing.append(mean_pair_spacing)
382
+ else:
383
+ D16_Mean_Pair_Spacing.append(0)
384
+ else:
385
+ D16_Mean_Pair_Spacing.append(0)
386
+ return D16_Mean_Pair_Spacing
387
+
388
+ def get_D17_Pair_Spacing_Standard_Deviation(self):
389
+ """Calculate standard deviation of Pair_Spacing values on the macrocycle"""
390
+ D17_Pair_Spacing_Standard_Deviation = []
391
+ for spacing_by_atom_pair in tqdm(self.pair_spacings):
392
+ if spacing_by_atom_pair and len(spacing_by_atom_pair) >= 2 and any(spacing != float('inf') for spacing in spacing_by_atom_pair.values()):
393
+ # Filter out infinity values, consider only valid Pair_Spacing values
394
+ valid_pair_spacings = [spacing for spacing in spacing_by_atom_pair.values() if spacing != float('inf')]
395
+ if len(valid_pair_spacings) >= 2:
396
+ mean = sum(valid_pair_spacings) / len(valid_pair_spacings)
397
+ variance = sum((x - mean) ** 2 for x in valid_pair_spacings) / len(valid_pair_spacings)
398
+ std_dev = variance ** 0.5
399
+ D17_Pair_Spacing_Standard_Deviation.append(std_dev)
400
+ else:
401
+ D17_Pair_Spacing_Standard_Deviation.append(0)
402
+ else:
403
+ D17_Pair_Spacing_Standard_Deviation.append(0)
404
+ return D17_Pair_Spacing_Standard_Deviation
405
+
406
+ def get_D18_Pair_Spacing_Coefficient_of_Variation(self):
407
+ """Calculate coefficient of variation for Pair_Spacing values on the macrocycle"""
408
+ D18_Pair_Spacing_Coefficient_of_Variation = []
409
+ for spacing_by_atom_pair in tqdm(self.pair_spacings):
410
+ if spacing_by_atom_pair and len(spacing_by_atom_pair) >= 2 and any(spacing != float('inf') for spacing in spacing_by_atom_pair.values()):
411
+ # Filter out infinity values, consider only valid Pair_Spacing values
412
+ valid_pair_spacings = [spacing for spacing in spacing_by_atom_pair.values() if spacing != float('inf')]
413
+ if len(valid_pair_spacings) >= 2:
414
+ mean = sum(valid_pair_spacings) / len(valid_pair_spacings)
415
+ if mean > 0:
416
+ variance = sum((x - mean) ** 2 for x in valid_pair_spacings) / len(valid_pair_spacings)
417
+ std_dev = variance ** 0.5
418
+ cv = std_dev / mean
419
+ D18_Pair_Spacing_Coefficient_of_Variation.append(cv)
420
+ else:
421
+ D18_Pair_Spacing_Coefficient_of_Variation.append(0)
422
+ else:
423
+ D18_Pair_Spacing_Coefficient_of_Variation.append(0)
424
+ else:
425
+ D18_Pair_Spacing_Coefficient_of_Variation.append(0)
426
+ return D18_Pair_Spacing_Coefficient_of_Variation