Coverage for SynRBL/SynAnalysis/analysis_utils.py: 12%

57 statements  

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

1import re 

2 

3from rdkit import Chem 

4from rdkit.Chem.rdMolDescriptors import CalcNumRings 

5from copy import deepcopy 

6 

7 

8def remove_atom_mapping_from_reaction_smiles(reaction_smiles): 

9 """ 

10 Remove atom mapping from a reaction SMILES string. 

11 

12 Parameters: 

13 - reaction_smiles (str): A reaction SMILES string with atom mapping. 

14 

15 Returns: 

16 - str: A reaction SMILES string without atom mapping. 

17 """ 

18 parts = reaction_smiles.split(">>") 

19 cleaned_parts = [Chem.CanonSmiles(re.sub(r":\d+", "", part)) for part in parts] 

20 cleaned_reaction_smiles = ">>".join(cleaned_parts) 

21 return cleaned_reaction_smiles 

22 

23 

24def calculate_chemical_properties(dictionary_list): 

25 """ 

26 Calculate chemical properties from a list of dictionaries containing reaction information. 

27 

28 Parameters: 

29 - dictionary_list (list of dict): A list of dictionaries with reaction information. 

30 

31 Returns: 

32 - list of dict: Updated list with added chemical properties. 

33 """ 

34 updated_list = deepcopy(dictionary_list) 

35 for entry in updated_list: 

36 reactant_smiles = entry["reactants"] 

37 product_smiles = entry["products"] 

38 reactant_mol = Chem.MolFromSmiles(reactant_smiles) 

39 product_mol = Chem.MolFromSmiles(product_smiles) 

40 

41 if reactant_mol is not None and product_mol is not None: 

42 num_carbon_reactants = sum( 

43 [atom.GetAtomicNum() == 6 for atom in reactant_mol.GetAtoms()] 

44 ) 

45 num_carbon_products = sum( 

46 [atom.GetAtomicNum() == 6 for atom in product_mol.GetAtoms()] 

47 ) 

48 entry["carbon_difference"] = abs(num_carbon_reactants - num_carbon_products) 

49 entry["total_carbons"] = num_carbon_reactants + num_carbon_products 

50 entry["total_bonds"] = abs( 

51 reactant_mol.GetNumBonds() - product_mol.GetNumBonds() 

52 ) 

53 entry["total_rings"] = abs( 

54 CalcNumRings(reactant_mol) - CalcNumRings(product_mol) 

55 ) 

56 else: 

57 entry["carbon_difference"] = "Invalid SMILES" 

58 entry["total_carbons"] = "Invalid SMILES" 

59 entry["total_bonds"] = "Invalid SMILES" 

60 entry["total_rings"] = "Invalid SMILES" 

61 reactant_fragment_count = len(reactant_smiles.split(".")) 

62 product_fragment_count = len(product_smiles.split(".")) 

63 total_fragment_count = reactant_fragment_count + product_fragment_count 

64 entry["fragment_count"] = total_fragment_count 

65 

66 return updated_list 

67 

68 

69def count_boundary_atoms_products_and_calculate_changes( 

70 list_of_dicts, reaction_col="new_reaction", mcs_col="mcs" 

71): 

72 """ 

73 Count boundary atoms in products and calculate changes in bonds and rings. 

74 

75 Parameters: 

76 - list_of_dicts (list of dict): A list of dictionaries with reaction information. 

77 

78 Returns: 

79 - list of dict: Updated list with calculated changes. 

80 """ 

81 for item in list_of_dicts: 

82 count = 0 

83 bond_change = 0 

84 ring_change = 0 

85 

86 if mcs_col in item.keys(): 

87 for i in item[mcs_col]["boundary_atoms_products"]: 

88 if isinstance(i, dict): 

89 count += 1 

90 elif isinstance(i, list): 

91 for j in i: 

92 if isinstance(j, dict): 

93 count += 1 

94 

95 reactant_product = item[reaction_col].split(">>") 

96 if len(reactant_product) == 2: 

97 reactant_smiles, product_smiles = reactant_product 

98 reactant_mol = Chem.MolFromSmiles(reactant_smiles) 

99 product_mol = Chem.MolFromSmiles(product_smiles) 

100 

101 if reactant_mol and product_mol: 

102 bond_change = abs( 

103 reactant_mol.GetNumBonds() - product_mol.GetNumBonds() 

104 ) 

105 ring_change = abs( 

106 CalcNumRings(reactant_mol) - CalcNumRings(product_mol) 

107 ) 

108 

109 item["num_boundary"] = count 

110 item["bond_change_merge"] = bond_change 

111 item["ring_change_merge"] = ring_change 

112 

113 return list_of_dicts