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]:
In [7]: a0_2
Out[7]:
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
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
1 F
2 Cl
3 Br
4 I
The neighbors of F are:
0 C
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