Skip to content

IterateOverAllBondsInAnAtomContainer

dstoeckel edited this page Mar 16, 2015 · 2 revisions

How can I iterate over all bonds in an AtomContainer ?

Iterate over all bonds of an AtomContainer using the BALL_FOREACH_BOND-macro:

#include <BALL/KERNEL/forEach.h>
#include <BALL/KERNEL/bond.h> 
#include <BALL/COMMON/logStream.h> 
#include <BALL/KERNEL/atomContainer.h> 

using namespace BALL;


AtomContainer ac;
...
AtomIterator ait;
Atom::BondIterator bit;
BALL_FOREACH_BOND(ac, ait, bit)
{
  if (bit->getOrder() == Bond::ORDER__UNKNOWN)
  {
    // Get the bond length
    float length = bit->getLength();
   
    // Print the bond
    Log.info() << bit->getFirstAtom()->getFullName() << " " <<  bit->getSecondAtom()->getFullName() << " "  << length << std::endl;
  }
}

Note: Using a simple nested AtomIterator-BondIterator-construction will consider each bond twice!

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

Note: the BALL_FOREACH_BOND-macro gets an AtomContainer. BALL-objects, that can be runtime-cast to an AtomContainer are for example a Chain, a Residue, a Molecule, a Protein, or a System.

Clone this wiki locally