Python-Ref > Cheminformatics > OpenBabel > Enumerating atoms and bonds
 
 

<-^^->
Klíčová slova
Moduly
Knihovní funkce

Enumerating atoms and bonds

How to process all atoms and bonds of a molecule and neighbors of an atom.
We start by showing how all atoms and bonds of a molecule can be accessed.
Expand/Shrink


  4  3  0  0  0  0  0  0  0  0999 V2000
  122.0000  129.0000    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
  148.2000  129.0000    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
  161.2994  106.3111    0.0000 N   0  0  0  0  0  0  0  0  0  0  0  0
  161.2994  151.6889    0.0000 O   0  0  0  0  0  0  0  0  0  0  0  0
  1  2  1  0  0  0  0
  2  3  1  0  0  0  0
  2  4  2  0  0  0  0
M  END
Zdroj: (babel3-1.py)
  1   import pybel
  2   import openbabel
  3   
  4   # readfile returns a generator, thus we need to loop
  5   for mol in pybel.readfile( "mol", "acetamide.mol"):
  6     # iteration over atoms - there is a shortcut in Pybel
  7     for a in mol.atoms:
  8       print a
  9       print a.OBAtom.GetAtomicNum()
 10     # for bonds there is no shortcut
 11     for pb in openbabel.OBMolBondIter( mol.OBMol):
 12       print pb
 13       print pb.GetBondOrder()
stdout:
Atom: 6 (122.00 129.00 0.00)
6
Atom: 6 (148.20 129.00 0.00)
6
Atom: 7 (161.30 106.31 0.00)
7
Atom: 8 (161.30 151.69 0.00)
8
<openbabel.OBBond; proxy of <Swig Object of type 'OpenBabel::OBEdgeBase *' at 0xa26cc0> >
1
<openbabel.OBBond; proxy of <Swig Object of type 'OpenBabel::OBEdgeBase *' at 0xc44f40> >
1
<openbabel.OBBond; proxy of <Swig Object of type 'OpenBabel::OBEdgeBase *' at 0xc44f90> >
2
Doba běhu: 144.5 ms
The following code shows how to query some more information about atom neighborhood.
Expand/Shrink


  4  3  0  0  0  0  0  0  0  0999 V2000
  122.0000  129.0000    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
  148.2000  129.0000    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
  161.2994  106.3111    0.0000 N   0  0  0  0  0  0  0  0  0  0  0  0
  161.2994  151.6889    0.0000 O   0  0  0  0  0  0  0  0  0  0  0  0
  1  2  1  0  0  0  0
  2  3  1  0  0  0  0
  2  4  2  0  0  0  0
M  END
Zdroj: (babel3-2.py)
  1   import pybel
  2   import openbabel
  3   
  4   # readfile returns a generator, thus we need to loop
  5   for mol in pybel.readfile( "mol", "acetamide.mol"):
  6     print "%-6s|%-8s|%-12s|%-12s|%-10s" % ("num", "index", "heavy-val", "hetero-val", "valence")
  7     # iteration over atoms - there is a shortcut in Pybel
  8     for a in mol.atoms:
  9       print "%-6s|%-8s|%-12s|%-12s|%-10s" % (a.atomicnum, a.idx, a.heavyvalence, a.heterovalence, a.valence)
stdout:
num   |index   |heavy-val   |hetero-val  |valence   
6     |1       |1           |0           |1         
6     |2       |3           |2           |3         
7     |3       |1           |0           |1         
8     |4       |1           |0           |1         
Doba běhu: 149.8 ms
The two pieces of code below show how to query neighboring atoms of an atom.
Expand/Shrink


  4  3  0  0  0  0  0  0  0  0999 V2000
  122.0000  129.0000    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
  148.2000  129.0000    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
  161.2994  106.3111    0.0000 N   0  0  0  0  0  0  0  0  0  0  0  0
  161.2994  151.6889    0.0000 O   0  0  0  0  0  0  0  0  0  0  0  0
  1  2  1  0  0  0  0
  2  3  1  0  0  0  0
  2  4  2  0  0  0  0
M  END
Zdroj: (babel3-3.py)
  1   import pybel
  2   import openbabel
  3   
  4   # readfile returns a generator, thus we need to loop
  5   for mol in pybel.readfile( "mol", "acetamide.mol"):
  6     print "--------Neighbor atoms------------"
  7     for a in mol.atoms:
  8       print "Atom %d" % a.idx
  9       # at first we iterate over atoms neighbors
 10       for neighbor in openbabel.OBAtomAtomIter( a.OBAtom):
 11         print "  Neighbor %d" % neighbor.GetIdx()
 12     print "--------Neighbor bonds------------"
 13     for a in mol.atoms:
 14       print "Atom %d" % a.idx
 15       # at first we iterate over atoms neighbors
 16       for bond in openbabel.OBAtomBondIter( a.OBAtom):
 17         print "  Bond %d, order %s" % (bond.GetIdx(), bond.GetBO())
 18       
stdout:
--------Neighbor atoms------------
Atom 1
  Neighbor 2
Atom 2
  Neighbor 1
  Neighbor 3
  Neighbor 4
Atom 3
  Neighbor 2
Atom 4
  Neighbor 2
--------Neighbor bonds------------
Atom 1
  Bond 0, order 1
Atom 2
  Bond 0, order 1
  Bond 1, order 1
  Bond 2, order 2
Atom 3
  Bond 1, order 1
Atom 4
  Bond 2, order 2
Doba běhu: 134.3 ms
Expand/Shrink


  4  3  0  0  0  0  0  0  0  0999 V2000
  122.0000  129.0000    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
  148.2000  129.0000    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0
  161.2994  106.3111    0.0000 N   0  0  0  0  0  0  0  0  0  0  0  0
  161.2994  151.6889    0.0000 O   0  0  0  0  0  0  0  0  0  0  0  0
  1  2  1  0  0  0  0
  2  3  1  0  0  0  0
  2  4  2  0  0  0  0
M  END
Zdroj: (babel3-4.py)
  1   import pybel
  2   import openbabel
  3   
  4   # readfile returns a generator, thus we need to loop
  5   for mol in pybel.readfile( "mol", "acetamide.mol"):
  6     start = mol.atoms[1]
  7     print "--------Neighbor atoms------------"
  8     for neighbor in openbabel.OBAtomAtomIter( start.OBAtom):
  9       print "Neighbor %d" % neighbor.GetIdx()
 10       # atom1.GetBond( atom2) returns bond between atom1 and atom2
 11       print "  Bond order %d" % neighbor.GetBond( start.OBAtom).GetBO()
 12     print "--------Neighbor bonds------------"
 13     for bond in openbabel.OBAtomBondIter( start.OBAtom):
 14       print "Bond order %d" % bond.GetBO()
 15       print "  (start atom %d)" % bond.GetBeginAtom().GetIdx()
 16       print "  (end   atom %d)" % bond.GetEndAtom().GetIdx()
 17       # bond.GetNbrAtom( atom) returns the other end of the bond
 18       print "  * Neighbor %d" % bond.GetNbrAtom( start.OBAtom).GetIdx()
stdout:
--------Neighbor atoms------------
Neighbor 1
  Bond order 1
Neighbor 3
  Bond order 1
Neighbor 4
  Bond order 2
--------Neighbor bonds------------
Bond order 1
  (start atom 1)
  (end   atom 2)
  * Neighbor 1
Bond order 1
  (start atom 2)
  (end   atom 3)
  * Neighbor 3
Bond order 2
  (start atom 2)
  (end   atom 4)
  * Neighbor 4
Doba běhu: 131.4 ms