Re: [Rdkit-discuss] Hiding/removing specific atoms in a RDKit molecule
Open-Source Cheminformatics and Machine Learning
Brought to you by:
glandrum
From: Paolo T. <pao...@gm...> - 2021-12-02 20:01:05
|
Hi Gianmarco, I am not aware of a method to simply hide atoms: here's a method t remove atoms given a list of indices, which should be what you need. Note that remove_selected_hs() replaces the "real" hydrogen with an implicit H on the parent atom, which I believe is what you want. from rdkit import Chem from rdkit.Chem import rdDistGeom mol = Chem.MolFromSmiles("OC[C@H]1OC(O)[C@H](O)[C@@H](O)[C@@H]1O") mol_h = Chem.AddHs(mol) rdDistGeom.EmbedMolecule(mol_h) 0 mol_h [image: image.png] hs = [a.GetIdx() for a in mol_h.GetAtoms() if a.GetAtomicNum() == 1] hs [12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23] # Let's remove any second H as an example hs_to_remove = [hs[i] for i in range(0, len(hs), 2)] hs_to_remove [12, 14, 16, 18, 20, 22] def remove_selected_hs(mol, hs_to_remove): def fix_explicit_hs(mol, ai): oa = mol.GetAtomWithIdx(ai) if oa.GetAtomicNum() > 1: oa.SetNumExplicitHs(oa.GetNumExplicitHs() + 1) if not hs_to_remove: return mol rwmol = Chem.RWMol(mol) ai_to_remove = [] for bond_idx in reversed(range(rwmol.GetNumBonds())): b = rwmol.GetBondWithIdx(bond_idx) bidx = b.GetBeginAtomIdx() remove_bidx = bidx in hs_to_remove eidx = b.GetEndAtomIdx() remove_eidx = eidx in hs_to_remove if (remove_bidx or remove_eidx): if remove_bidx: ai_to_remove.append(bidx) fix_explicit_hs(rwmol, eidx) if remove_eidx: ai_to_remove.append(eidx) fix_explicit_hs(rwmol, bidx) rwmol.RemoveBond(bidx, eidx) for atom_idx in sorted(ai_to_remove, reverse=True): rwmol.RemoveAtom(atom_idx) Chem.SanitizeMol(rwmol) return rwmol.GetMol() remove_selected_hs(mol_h, hs_to_remove) [image: image.png] Cheers, p. On Thu, Dec 2, 2021 at 1:19 PM Gianmarco Ghiandoni <ghi...@gm...> wrote: > Hi all, > > I have been working on a library that computes properties for molecules > where hydrogens are explicit. These properties are then saved into a > dictionary where keys are indices, then I am using the dictionary to depict > these properties on their corresponding hydrogens - also according to some > thresholds, which means that not all hydrogens are labelled. The result is > something like this: > > [image: image.png] > > Now my question is: How do I hide/remove the hydrogens that are not > labelled? I am storing their indices while iterating to append their labels > but there seem not to be a straightforward way to get rid of them. > > Thanks, > > Giammy > > -- > *Gianmarco* > _______________________________________________ > Rdkit-discuss mailing list > Rdk...@li... > https://lists.sourceforge.net/lists/listinfo/rdkit-discuss > |