Skip to content

IterateOverAllBondsOfAnAtom

dstoeckel edited this page Mar 16, 2015 · 2 revisions

How can I iterate over all bonds of an atom?

Iterate over all bonds of an atom using the BondIterator:

C++

#include <BALL/KERNEL/atom.h>


// create a system and read the contents of the PDB file
System S;
file >> S;
file.close();

// now we open a fragment database
FragmentDB fragment_db("");

// and normalize the atom names, i.e. we convert different
// naming standards to the PDB naming scheme - just in case!
S.apply(fragment_db.normalize_names);

// now we add any missing hydrogens to the residues
// the data on the hydrogen positions stems from the
// fragment database. However the hydrogen positions 
// created in this way are only good estimates
S.apply(fragment_db.add_hydrogens);

// now we create the bonds between the atoms (PDB files hardly
// ever contain a complete set of CONECT records)  
S.apply(fragment_db.build_bonds);



BALL::Atom::BondIterator bi;
BALL::Atom* atom = ...;
BALL::Atom* partner;
for (bi = atom->beginBond(); +bi; ++bi)
{
  std::cout << "Current bond order: " << bi->getOrder() << std::endl;

  // Get the other bond partner atom
  partner = bi->getPartner(*atom);
}

Note, that _+bi _ equals bi != atom->endAtom() !

Note also: Never try to add or remove atoms/bonds from the AtomContainer while iterating over it! Your programm will crash!

Python

import sys
from BALL import *

# read the PDB-file into a BALL::System
f = PDBFile(sys.argv[1])
S = System()
f.read(S)

# now we open a fragment database
fdb = FragmentDB("")

# and normalize the atom names, i.e. we convert different
# naming standards to the PDB naming scheme - just in case!
S.apply(fdb.normalize_names)

# now we add any missing hydrogens to the residues
# the data on the hydrogen positions stems from the
# fragment database. However the hydrogen positions 
# created in this way are only good estimates
S.apply(fdb.add_hydrogens)

# now we create the bonds between the atoms (PDB files hardly
# ever contain a complete set of CONECT records) 
S.apply(fdb.build_bonds)
 
# check the first molecule
protein = S.getProtein(0)
chain = protein.getChain(0)
  
if (chain != None):
  # get all residues
  for residue in residues(chain):
    if (residue != None):
      # get all atoms
      for atom in atoms(residue):
        if (atom != None):
          # get all bonds
          for bond in bonds(atom):
            print "Current bond order: " , bond.getOrder()
            # Get the other bond partner atom
            partner = bond.getPartner(atom)
            print "Atom ", atom.getName(), " bonded to ", partner.getName(), ": ", bond.getLength()

Clone this wiki locally