Hi there at RDKit,

I have a set of atom indices from a molecule I want to keep, and any atom which is not in this list I want to discard.

I thought of implementing this as follows:

#!/usr/bin/env python

from rdkit import Chem

mol = Chem.MolFromSmiles("CCC1CNCC1CC")

keep_atoms = [2,3,4] # assume these exist in the above as an example, you can print the atom ids to check

edit_mol = Chem.EditableMol(mol)
for atom in mol.GetAtoms():
    if atom.GetIdx() not in keep_atoms:
        edit_mol.RemoveAtom(atom.GetIdx())

I am not sure this is the best implementation (also because it does not work), but it's a try.

The end result should be an sdf file with only atoms 2,3,4 from the original molecule.

When I run the above I get:

[15:45:50] 

****
Range Error
idx
Violation occurred on line 143 in file /opt/RDKit_2011_12_1/Code/GraphMol/ROMol.cpp
Failed Expression: 0 <= 6 <= 5
****

Traceback (most recent call last):
  File "./test.py", line 12, in <module>
    edit_mol.RemoveAtom(atom.GetIdx())
RuntimeError: Range Error

I cannot quite understand this error.  Can anyone shed some light?
I mean this is related to me deleting 3 atoms from the molecule, so it somehow expects the range to be from 0 <= x <= 5  instead of 0 <= x <= 8... but why is there this check in place?  

Many Thanks

-
Jean-Paul Ebejer
Early Stage Researcher