Coverage for SynRBL/SynMCSImputer/SubStructure/extract_common_mcs.py: 76%
108 statements
« prev ^ index » next coverage.py v7.4.3, created at 2024-04-11 11:17 +0200
« prev ^ index » next coverage.py v7.4.3, created at 2024-04-11 11:17 +0200
1import logging
3from collections import Counter
4from rdkit import Chem
5from joblib import Parallel, delayed
6from typing import List, Dict, Tuple
8logger = logging.getLogger("SynRBL")
11class ExtractMCS:
12 """
13 A class to extract and analyze the most common Maximum Common Substructure
14 (MCS) from a list of MCS results.
15 Provides functionality to determine the most common elements, the top n
16 common elements, and to calculate the corrected individual overlap
17 percentage for multiple conditions. Additionally, extracts the MCS results
18 that meet a specified threshold for commonality.
19 """
21 def __init__(self):
22 pass
24 @staticmethod
25 def get_num_atoms(smiles):
26 """
27 Calculate the number of atoms in a molecule represented by a SMILES
28 string.
30 Args:
31 smiles (str): A string representing a molecule in SMILES format.
33 Returns:
34 int: The number of atoms in the molecule. Returns 0 if the SMILES
35 string is invalid or an error occurs.
36 """
37 try:
38 molecule = Chem.MolFromSmarts(smiles)
39 if molecule is not None:
40 return molecule.GetNumAtoms()
41 else:
42 return 0
43 except Exception:
44 return 0
46 @staticmethod
47 def calculate_total_number_atoms_mcs_parallel(condition, n_jobs=4):
48 """
49 Calculate the total number of atoms in the MCS results for each
50 dictionary in a condition using parallel processing.
52 Args:
53 condition (list): A list of dictionaries, each containing
54 'mcs_results', which are lists of SMILES strings.
55 n_jobs (int, optional): The number of jobs to run in parallel.
56 Defaults to 4.
58 Returns:
59 list: A list containing the total number of atoms in the MCS results
60 for each dictionary in the condition.
61 """
63 def calculate_atoms_for_dict(d):
64 return sum(ExtractMCS.get_num_atoms(mcs) for mcs in d["mcs_results"])
66 total_number_atoms = Parallel(n_jobs=n_jobs, verbose=0)(
67 delayed(calculate_atoms_for_dict)(d) for d in condition
68 )
69 return total_number_atoms
71 def get_popular_elements_from_list(self, elements_list):
72 """
73 Get the most popular elements in a flat list.
75 :param elements_list: A list containing elements.
76 :return: A list of elements that appear with the highest frequency in
77 the provided list.
78 """
79 element_count = Counter(elements_list)
80 max_frequency = max(element_count.values(), default=0)
81 return [
82 element
83 for element, count in element_count.items()
84 if count == max_frequency
85 ]
87 def get_top_n_common_elements(self, elements_list, top_n=2):
88 """
89 Get the top n most common elements from a list of sets.
91 :param elements_list: A list of sets, each containing elements.
92 :param top_n: The number of top elements to retrieve.
93 :return: A list of the top n most common elements across all sets.
94 """
95 flattened_elements = [
96 element for element_set in elements_list for element in element_set
97 ]
98 return [
99 element for element, _ in Counter(flattened_elements).most_common(top_n)
100 ]
102 def calculate_corrected_individual_overlap_percentage(self, *conditions):
103 """
104 Calculate the corrected individual overlap percentage across multiple
105 conditions.
107 :param conditions: A variable number of conditions, each a list of
108 dictionaries containing 'mcs_results'.
109 :return: A list of overlap percentages for each index across the
110 conditions.
111 :raises ValueError: If all conditions do not have the same number of
112 cases.
113 """
114 if not all(len(condition) == len(conditions[0]) for condition in conditions):
115 raise ValueError("All conditions must have the same number of cases")
117 num_conditions = len(conditions)
118 list_overlap_percentages = []
119 reference_results_list = []
121 for idx in range(len(conditions[0])):
122 list_length = [
123 len(condition[idx]["mcs_results"]) for condition in conditions
124 ]
125 len_popular = self.get_popular_elements_from_list(list_length)[0]
126 current_results = [
127 set(condition[idx]["mcs_results"]) for condition in conditions
128 ]
129 reference_results = self.get_top_n_common_elements(
130 current_results, top_n=len_popular
131 )
132 reference_results_list.append(reference_results)
133 overlap_count = sum(
134 sorted(reference_results) == sorted(set(condition[idx]["mcs_results"]))
135 for condition in conditions
136 )
137 overlap_percentage = (overlap_count / num_conditions) * 100
138 list_overlap_percentages.append(overlap_percentage)
140 return list_overlap_percentages, reference_results_list
142 def extract_common_mcs_index(self, lower_threshold, upper_threshold, *conditions):
143 """
144 Extract MCS results that meet a specified threshold for commonality.
146 :param threshold: The percentage threshold for commonality.
147 :param conditions: A variable number of conditions, each a list of
148 dictionaries containing 'mcs_results'.
149 :return: A list of dictionaries representing the MCS results that meet
150 the specified threshold.
151 """
152 (
153 overlap_percentages,
154 reference_results_list,
155 ) = self.calculate_corrected_individual_overlap_percentage(*conditions)
156 threshold_index = [
157 lower_threshold <= i <= upper_threshold for i in overlap_percentages
158 ]
159 # mcs_common = [d for d, b in zip(conditions[0], threshold_index) if b]
160 return threshold_index, reference_results_list
162 @staticmethod
163 def compare_conditions_and_get_largest(
164 total_atoms_conditions: List[List[int]], *conditions: List[List[Dict]]
165 ) -> Tuple[List[Dict], List[List[str]]]:
166 """
167 Compare the total number of atoms across different conditions and find
168 the condition with the largest MCS for each index.
169 In case of a tie, compares the total number of atoms in the first
170 SMILES/SMARTS for those conditions.
172 Args:
173 - total_atoms_conditions (list): A list of lists, where each sublist
174 contains the total number of atoms for each MCS result in a condition.
176 Returns:
177 - Tuple:
178 - A list of dictionaries, each representing the condition with the
179 largest MCS for a given index.
180 - A reference list of the biggest MCS for each index across the
181 conditions.
182 """
183 results = []
184 reference_list = []
185 min_length = min(len(total) for total in total_atoms_conditions)
187 for idx in range(min_length):
188 tied_conditions = []
189 max_atoms = 0
190 max_condition = -1
192 # First pass: Find conditions with the largest total atom counts
193 for condition_idx, total in enumerate(total_atoms_conditions):
194 if idx < len(total):
195 if total[idx] > max_atoms:
196 max_atoms = total[idx]
197 tied_conditions = [(condition_idx, total[idx])]
198 elif total[idx] == max_atoms:
199 tied_conditions.append((condition_idx, total[idx]))
201 # Second pass: In case of a tie, compare the first SMILES/SMARTS
202 if len(tied_conditions) > 1:
203 max_first_smarts_atoms = 0
204 winning_condition = -1
205 for condition_idx, _ in tied_conditions:
206 first_smarts = (
207 conditions[condition_idx][idx]["mcs_results"][0]
208 if conditions[condition_idx][idx]["mcs_results"]
209 else ""
210 )
211 first_smarts_atoms = ExtractMCS.get_num_atoms(first_smarts)
212 if first_smarts_atoms > max_first_smarts_atoms:
213 max_first_smarts_atoms = first_smarts_atoms
214 winning_condition = condition_idx
216 # Finalize the winning condition
217 if winning_condition != -1:
218 max_condition = winning_condition
219 max_mcs = conditions[max_condition][idx]["mcs_results"]
220 else:
221 max_condition = tied_conditions[0][0] if tied_conditions else -1
222 max_mcs = (
223 conditions[max_condition][idx]["mcs_results"]
224 if max_condition != -1
225 else []
226 )
228 if max_condition != -1:
229 result_entry = {
230 "name": f"Condition {max_condition + 1}",
231 "biggest_mcs": max_mcs,
232 }
233 results.append(result_entry)
234 reference_list.append(max_mcs)
236 return results, reference_list
238 def extract_matching_conditions(
239 self,
240 lower_threshold,
241 upper_threshold,
242 *conditions,
243 extraction_method="ensemble",
244 using_threshold=False,
245 ):
246 """
247 Extract and return the first matching condition for each index that
248 meets the threshold.
250 :param threshold_index: A list of boolean values indicating whether
251 each condition meets the threshold.
252 :param conditions: A list of conditions, each a list of dictionaries
253 containing 'mcs_results'.
254 :param reference_results_list: A list of reference results to match
255 against the conditions.
256 :return: A list of dictionaries representing the matching condition
257 for each index that meets the threshold.
258 """
260 threshold_index = (
261 []
262 ) # Initialize threshold_index to an empty list or a suitable default value
264 if extraction_method == "ensemble":
265 threshold_index, reference_results_list = self.extract_common_mcs_index(
266 lower_threshold, upper_threshold, *conditions
267 )
268 elif extraction_method == "largest_mcs":
269 total_atoms_conditions = [
270 ExtractMCS.calculate_total_number_atoms_mcs_parallel(
271 condition, n_jobs=4
272 )
273 for condition in conditions
274 ]
275 _, reference_results_list = ExtractMCS.compare_conditions_and_get_largest(
276 total_atoms_conditions, *conditions
277 )
278 if using_threshold:
279 threshold_index, _ = self.extract_common_mcs_index(
280 lower_threshold, upper_threshold, *conditions
281 )
282 else:
283 threshold_index = [True] * len(
284 conditions[0]
285 ) # Make sure to set threshold_index in this branch
287 results = []
288 for key, value in enumerate(threshold_index):
289 if value:
290 try:
291 for condition in conditions:
292 if sorted(reference_results_list[key]) == sorted(
293 condition[key]["mcs_results"]
294 ):
295 results.append(condition[key])
296 break
298 except Exception as e:
299 logger.error(f"Error processing condition at index {key}: {e}")
300 continue
301 return results, threshold_index