Coverage for SynRBL/SynMCSImputer/SubStructure/substructure_analyzer.py: 100%

24 statements  

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

1from typing import List, Tuple 

2from rdkit import Chem 

3from rdkit.Chem.rdchem import Mol 

4 

5 

6class SubstructureAnalyzer: 

7 """ 

8 A class for analyzing substructures within molecules using RDKit. 

9 

10 Methods: 

11 remove_substructure_atoms: Removes a specified substructure from a 

12 molecule and returns the number of resulting fragments. 

13 sort_substructures_by_fragment_count: Sorts a list of substructures based 

14 on the number of fragments resulting from their removal. 

15 identify_optimal_substructure: Identifies the most relevant substructure 

16 from a list of potential substructures based on fragment count. 

17 """ 

18 

19 def __init__(self): 

20 pass 

21 

22 def remove_substructure_atoms( 

23 self, parent_mol: Mol, substructure: Tuple[int, ...] 

24 ) -> int: 

25 """ 

26 Removes specified atoms (substructure) from a molecule and returns the 

27 number of resulting fragments. 

28 

29 Parameters: 

30 parent_mol (Mol): The parent molecule. 

31 substructure (Tuple[int, ...]): Indices of atoms in the substructure. 

32 

33 Returns: 

34 int: The number of fragments resulting from the removal of the 

35 substructure. 

36 """ 

37 rw_mol = Chem.RWMol(parent_mol) 

38 for atom_idx in sorted(substructure, reverse=True): 

39 if atom_idx < rw_mol.GetNumAtoms(): 

40 rw_mol.RemoveAtom(atom_idx) 

41 new_mol = rw_mol.GetMol() 

42 return len(Chem.GetMolFrags(new_mol)) 

43 

44 def sort_substructures_by_fragment_count( 

45 self, substructures: List[Tuple[int, ...]], fragment_counts: List[int] 

46 ) -> List[Tuple[int, ...]]: 

47 """ 

48 Sorts a list of substructures based on the number of fragments 

49 resulting from their removal. 

50 

51 Parameters: 

52 substructures (List[Tuple[int, ...]]): List of substructures 

53 represented by atom indices. 

54 fragment_counts (List[int]): List of fragment counts corresponding to 

55 each substructure. 

56 

57 Returns: 

58 List[Tuple[int, ...]]: Sorted list of substructures based on fragment 

59 counts. 

60 """ 

61 paired_list = list(zip(substructures, fragment_counts)) 

62 paired_list.sort(key=lambda x: x[1]) 

63 return [pair[0] for pair in paired_list] 

64 

65 def identify_optimal_substructure( 

66 self, parent_mol: Mol, child_mol: Mol 

67 ) -> Tuple[int, ...]: 

68 """ 

69 Identifies the most relevant substructure within a parent molecule 

70 given a child molecule. 

71 

72 Parameters: 

73 parent_mol (Mol): The parent molecule. 

74 child_mol (Mol): The child molecule whose substructures are to be 

75 analyzed. 

76 

77 Returns: 

78 Tuple[int, ...]: The atom indices of the identified substructure in 

79 the parent molecule. 

80 """ 

81 substructures = parent_mol.GetSubstructMatches(child_mol) 

82 if len(substructures) > 1: 

83 fragment_counts = [ 

84 self.remove_substructure_atoms(parent_mol, substructure) 

85 for substructure in substructures 

86 ] 

87 sorted_substructures = self.sort_substructures_by_fragment_count( 

88 substructures, fragment_counts 

89 ) 

90 return sorted_substructures[0] 

91 else: 

92 return substructures[0] if substructures else ()