Coverage for SynRBL/SynMCSImputer/MissingGraph/find_graph_dict.py: 61%

56 statements  

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

1import pandas as pd 

2 

3from rdkit import Chem 

4from rdkit.rdBase import BlockLogs 

5from joblib import Parallel, delayed 

6from typing import List 

7from SynRBL.SynMCSImputer.MissingGraph.find_missing_graphs import FindMissingGraphs 

8from SynRBL.SynMCSImputer.MissingGraph.uncertainty_graph import GraphMissingUncertainty 

9 

10 

11def find_single_graph(mcs_mol_list, sorted_reactants_mol_list): 

12 """ 

13 Find missing parts, boundary atoms, and nearest neighbors for a list of reactant molecules 

14 using a corresponding list of MCS (Maximum Common Substructure) molecules. 

15 

16 Parameters: 

17 - mcs_mol_list (list of rdkit.Chem.Mol): List of RDKit molecule objects representing the MCS, 

18 corresponding to each molecule in sorted_reactants_mol_list. 

19 - sorted_reactants_mol_list (list of rdkit.Chem.Mol): The list of RDKit molecule objects to analyze. 

20 

21 Returns: 

22 - Dictionary containing: 

23 - 'smiles' (list of list of str): SMILES representations of the missing parts for each molecule. 

24 - 'boundary_atoms_products' (list of list of dict): Lists of boundary atoms for each molecule. 

25 - 'nearest_neighbor_products' (list of list of dict): Lists of nearest neighbors for each molecule. 

26 - 'issue' (list): Any issues encountered during processing. 

27 """ 

28 missing_results = { 

29 "smiles": [], 

30 "boundary_atoms_products": [], 

31 "nearest_neighbor_products": [], 

32 "issue": [], 

33 } 

34 for i in zip(sorted_reactants_mol_list, mcs_mol_list): 

35 try: 

36 ( 

37 mols, 

38 boundary_atoms_products, 

39 nearest_neighbor_products, 

40 ) = FindMissingGraphs.find_missing_parts_pairs(i[0], i[1]) 

41 missing_results["smiles"].append([Chem.MolToSmiles(mol) for mol in mols]) 

42 missing_results["boundary_atoms_products"].append(boundary_atoms_products) 

43 missing_results["nearest_neighbor_products"].append( 

44 nearest_neighbor_products 

45 ) 

46 missing_results["issue"].append([]) 

47 except Exception as e: 

48 missing_results["smiles"].append([]) 

49 missing_results["boundary_atoms_products"].append([]) 

50 missing_results["nearest_neighbor_products"].append([]) 

51 missing_results["issue"].append( 

52 "FindMissingGraphs.find_missing_parts() failed:" + str(e) 

53 ) 

54 return missing_results 

55 

56 

57def find_single_graph_parallel(mcs_mol_list, sorted_reactants_mol_list, n_jobs=4): 

58 """ 

59 Find missing parts, boundary atoms, and nearest neighbors for a list of reactant molecules 

60 using a corresponding list of MCS (Maximum Common Substructure) molecules in parallel. 

61 

62 Parameters: 

63 - mcs_mol_list (list of rdkit.Chem.Mol): List of RDKit molecule objects representing the MCS, 

64 corresponding to each molecule in sorted_reactants_mol_list. 

65 - sorted_reactants_mol_list (list of rdkit.Chem.Mol): The list of RDKit molecule objects to analyze. 

66 - n_jobs (int): The number of parallel jobs to run. Default is -1, which uses all available CPU cores. 

67 

68 Returns: 

69 - List of dictionaries, where each dictionary contains: 

70 - 'smiles' (list of str): SMILES representations of the missing parts for each molecule. 

71 - 'boundary_atoms_products' (list of dict): Lists of boundary atoms for each molecule. 

72 - 'nearest_neighbor_products' (list of dict): Lists of nearest neighbors for each molecule. 

73 - 'issue' (str): Any issues encountered during processing. 

74 """ 

75 

76 def process_single_pair(reactant_mol, mcs_mol): 

77 try: 

78 block = BlockLogs() 

79 ( 

80 mols, 

81 boundary_atoms_products, 

82 nearest_neighbor_products, 

83 ) = FindMissingGraphs.find_missing_parts_pairs(reactant_mol, mcs_mol) 

84 del block 

85 return { 

86 "smiles": [ 

87 Chem.MolToSmiles(mol) if mol is not None else None for mol in mols 

88 ], 

89 "boundary_atoms_products": boundary_atoms_products, 

90 "nearest_neighbor_products": nearest_neighbor_products, 

91 "issue": "", 

92 } 

93 except Exception as e: 

94 return { 

95 "smiles": [], 

96 "boundary_atoms_products": [], 

97 "nearest_neighbor_products": [], 

98 "issue": str(e), 

99 } 

100 

101 results = Parallel(n_jobs=n_jobs, verbose=0)( 

102 delayed(process_single_pair)(reactant_mol, mcs_mol) 

103 for reactant_mol, mcs_mol in zip(sorted_reactants_mol_list, mcs_mol_list) 

104 ) 

105 return results 

106 

107 

108def find_graph_dict(mcs_dict, n_jobs: int = 4): 

109 """ 

110 Function to find missing graphs for a given MCS dictionary. 

111 """ 

112 msc_df = pd.DataFrame(mcs_dict) 

113 

114 mcs_results = msc_df["mcs_results"].to_list() 

115 sorted_reactants = msc_df["sorted_reactants"].to_list() 

116 

117 mcs_mol_list = smiles_to_mol_parallel(mcs_results, useSmiles=False) 

118 sorted_reactants_mol_list = smiles_to_mol_parallel(sorted_reactants, useSmiles=True) 

119 

120 missing_results = find_single_graph_parallel( 

121 mcs_mol_list, sorted_reactants_mol_list, n_jobs=n_jobs 

122 ) 

123 

124 missing_results = GraphMissingUncertainty(missing_results, threshold=2).fit() 

125 

126 return missing_results 

127 

128 

129def convert_smiles_to_mols( 

130 smiles_list: List[str], useSmiles: bool = True 

131) -> List[Chem.rdchem.Mol]: 

132 """ 

133 Convert a list of SMILES strings to a list of RDKit molecule objects. 

134 

135 Args: 

136 smiles_list: A list containing SMILES strings. 

137 

138 Returns: 

139 A list containing RDKit molecule objects. 

140 """ 

141 mol_list = [] 

142 for smiles in smiles_list: 

143 try: 

144 if useSmiles: 

145 mol = Chem.MolFromSmiles(smiles, sanitize=True) 

146 else: 

147 mol = Chem.MolFromSmarts(smiles) 

148 mol_list.append(mol) 

149 except Exception: 

150 mol_list.append(None) 

151 return mol_list 

152 

153 

154def smiles_to_mol_parallel( 

155 smiles_lists: List[List[str]], n_jobs: int = 4, useSmiles: bool = True 

156) -> List[List[Chem.Mol]]: 

157 """ 

158 Convert a list of lists of SMILES strings to a list of lists of RDKit molecule objects using parallel processing. 

159 

160 :param smiles_lists: List of lists containing SMILES strings. 

161 :param n_jobs: The number of jobs to run in parallel. -1 means using all processors. 

162 :return: A list of lists containing RDKit molecule objects. 

163 """ 

164 mol_lists = Parallel(n_jobs=n_jobs)( 

165 delayed(convert_smiles_to_mols)(smiles_list, useSmiles) 

166 for smiles_list in smiles_lists 

167 ) 

168 return mol_lists