Hello Paolo,

I knew about the existence of EM objects but not of RWs. That looks great,
thank you. I have just tested the function and it does exactly what I
needed.

Best,

Giammy

On Thu, 2 Dec 2021 at 20:00, Paolo Tosco <paolo.tosco.m...@gmail.com> wrote:

> 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 <ghiandon...@gmail.com>
> 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
>> Rdkit-discuss@lists.sourceforge.net
>> https://lists.sourceforge.net/lists/listinfo/rdkit-discuss
>>
>

-- 
*Gianmarco*
_______________________________________________
Rdkit-discuss mailing list
Rdkit-discuss@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/rdkit-discuss

Reply via email to