Thanks Paolo. Makes sense.

On 1/8/2021 4:54 AM, Paolo Tosco wrote:
Hi Brian,

when you fetch a Chem.Atom object from a Chem.Mol a Python object is created on-the-fly that wraps the underlying C++ object. Every time you do this, a new Python object is created. This does not apply only to GetNeighbors(); please see an example below:

In [1]: from rdkit import Chem

In [2]: mol = Chem.MolFromSmiles("C")

In [3]: a0_1 = mol.GetAtomWithIdx(0)

In [4]: a0_2 = mol.GetAtomWithIdx(0)

In [5]: a0_1 == a0_2
Out[5]: False

In [6]: a0_1
Out[6]: <rdkit.Chem.rdchem.Atom at 0x7fd2de00e5d0>

In [7]: a0_2
Out[7]: <rdkit.Chem.rdchem.Atom at 0x7fd2de0195d0>

In [8]: a0_1.GetIdx() == a0_2.GetIdx()
Out[8]: True

In short, you cannot rely on comparing Chem.Atom object identity, but you can certainly rely on their index.

Cheers,
p.

On Fri, Jan 8, 2021 at 5:00 AM Brian Peterson <brian.peter...@molsight.com <mailto:brian.peter...@molsight.com>> wrote:

    Hello all. When traversing a molecule's atoms with
    atom.GetNeighbors(),
    the object returned for an atom is not necessarily the same object
    returned when the atom is indexed directly from the molecule. This
    means
    one can't directly compare two atoms as objects found while
    traversing a
    molecule via neighbors to see if they are the same atom. It seems
    a new
    object is created for the atom returned by .GetNeighbors(). Is this
    expected behavior and what is the reason for this? I've found that
    the
    comparison can be done by using atom.GetIdx(), but wanted to use the
    objects themselves for various reasons. Thanks.

    Example:
    -------------------------

    from rdkit import Chem

    m = Chem.MolFromSmiles('C(F)(Cl)(Br)(I)')
    atoms = [myatom for myatom in m.GetAtoms()]
    for myatom in atoms:
         print(myatom.GetIdx(),myatom.GetSymbol(),myatom)

    print("The neighbors of {} are:".format(atoms[1].GetSymbol()))
    for newatom in atoms[1].GetNeighbors():
         print(newatom.GetIdx(),newatom.GetSymbol(),newatom)

    print("C0 == C0 ?")
    print(atoms[0] == newatom)

    Output:
    ----------------------------
    0 C <rdkit.Chem.rdchem.Atom object at 0x7f0d429ca760>
    1 F <rdkit.Chem.rdchem.Atom object at 0x7f0d429ca850>
    2 Cl <rdkit.Chem.rdchem.Atom object at 0x7f0d429ca940>
    3 Br <rdkit.Chem.rdchem.Atom object at 0x7f0d429caa30>
    4 I <rdkit.Chem.rdchem.Atom object at 0x7f0d429cab20>
    The neighbors of F are:
    0 C <rdkit.Chem.rdchem.Atom object at 0x7f0d429cac10>
    C0 == C0 ?
    False






    _______________________________________________
    Rdkit-discuss mailing list
    Rdkit-discuss@lists.sourceforge.net
    <mailto:Rdkit-discuss@lists.sourceforge.net>
    https://lists.sourceforge.net/lists/listinfo/rdkit-discuss
    <https://lists.sourceforge.net/lists/listinfo/rdkit-discuss>


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

Reply via email to