Coverage for SynRBL/SynMCSImputer/MissingGraph/molcurator.py: 94%
48 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
1from rdkit import Chem
2from rdkit.Chem import rdmolops
3from rdkit.Chem import AllChem
6class MoleculeCurator:
7 """
8 A class for curating molecules, which includes adding hydrogens to radicals,
9 standardizing diazo charges, and manually kekulizing molecules.
11 Methods:
12 - add_hydrogens_to_radicals: Adds hydrogens to radical sites in a molecule.
13 - standardize_diazo_charge: Converts a diazo compound with charged atoms to
14 its neutral form.
15 - manual_kekulize: Manually kekulizes a molecule to ensure valid structure.
16 """
18 @staticmethod
19 def add_hydrogens_to_radicals(mol: Chem.Mol) -> Chem.Mol:
20 """
21 Add hydrogen atoms to radical sites in a molecule.
23 Args:
24 - mol (Chem.Mol): RDKit molecule object.
26 Returns:
27 - Chem.Mol: The modified molecule with added hydrogens.
28 """
29 mol_with_h = Chem.RWMol(mol)
30 mol_with_h = rdmolops.AddHs(mol_with_h)
32 for atom in mol_with_h.GetAtoms():
33 num_radical_electrons = atom.GetNumRadicalElectrons()
34 if num_radical_electrons > 0:
35 atom.SetNumExplicitHs(atom.GetNumExplicitHs() + num_radical_electrons)
36 atom.SetNumRadicalElectrons(0)
38 curate_mol = Chem.RemoveHs(mol_with_h)
39 return curate_mol
41 @staticmethod
42 def standardize_diazo_charge(mol: Chem.Mol) -> Chem.Mol:
43 """
44 Convert a diazo compound with charged atoms to its neutral form.
46 Args:
47 - mol (Chem.Mol): A RDKit molecule object representing a diazo compound.
49 Returns:
50 - Chem.Mol: The neutralized molecule, or the original molecule if the
51 reaction doesn't occur.
52 """
53 neutral_mol = AllChem.ReactionFromSmarts(
54 "[N-]=[NH2+]>>[N:1]#[N:2]"
55 ).RunReactants((mol,))
56 return neutral_mol[0][0] if neutral_mol else mol
58 @staticmethod
59 def manual_kekulize(smiles: str) -> Chem.Mol:
60 """
61 Manually kekulizes a molecule represented by a SMILES string.
63 Args:
64 - smiles (str): A SMILES string representing the molecule.
66 Returns:
67 - Chem.Mol: The kekulized molecule, or None if it's not possible.
68 """
70 # Function to add a hydrogen to a specific atom in a molecule
71 def add_hydrogen(mol, atom_index):
72 edited_mol = Chem.RWMol(mol)
73 atom = edited_mol.GetAtomWithIdx(atom_index)
74 atom.SetNumExplicitHs(atom.GetNumExplicitHs() + 1)
75 return edited_mol.GetMol()
77 # Process a single component of the molecule
78 def process_component(component):
79 for atom_index in range(component.GetNumAtoms()):
80 try:
81 test_mol = add_hydrogen(component, atom_index)
82 rdmolops.SanitizeMol(test_mol)
83 return test_mol
84 except Exception:
85 continue
86 return None
88 components_smiles = smiles.split(".")
89 valid_components = []
91 for comp_smiles in components_smiles:
92 comp = Chem.MolFromSmiles(comp_smiles, sanitize=False)
93 valid_mol = process_component(comp)
94 if valid_mol:
95 valid_components.append(valid_mol)
97 if valid_components:
98 combined_mol = valid_components[0]
99 for i in range(1, len(valid_components)):
100 combined_mol = Chem.CombineMols(combined_mol, valid_components[i])
101 return combined_mol
102 else:
103 return None