Skip to content

IterateOverAllAtomsOfAResidue

dstoeckel edited this page Mar 16, 2015 · 2 revisions

How can I iterate over all atoms of a residue?

Iterate over all atoms in a residue using BALL's AtomIterator:

C++

#include <BALL/FORMAT/PDBFile.h>
#include <BALL/KERNEL/system.h>
#include <BALL/KERNEL/chain.h>
#include <BALL/KERNEL/residue.h>
#include <BALL/KERNEL/atom.h>

...
using namespace BALL;
using namespace std;
...

// read the PDB-file into a BALL::System
PDBFile f("myProtein.pdb");
System S;
f >> S;	

// check the first molecule
if (RTTI::isKindOf<Protein>(*(S.getMolecule(0))))
{
   // cast the system's first molecule to BALL::Protein
   Protein* protein = RTTI::castTo<Protein>(*(S.getMolecule(0)));

   // get the first chain
   Chain* chain =  protein->getChain(0);
   if (!chain || (chain->countResidues() == 0))
   {
      return;
   }

   // get the first residue
   Residue* residue = chain->getResidue(0);
   if (residue)
   {
      // iterate over all atoms
      AtomIterator a_it = residue->beginAtom();
      for (; +a_it; ++a_it)
      {
         cout << a_it->getElement().getSymbol() << endl;

         // get a pointer to the current atom by
         Atom* atom = &*a_it;
      }
   }
}

Note that +a_it equals a_it!=residue->endAtom() !

Note also: Never try to add or remove atoms from the residue while iterating over it! Your program 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)

# check the first protein
protein = S.getProtein(0)

# get the first chain
chain = protein.getChain(0)
if (chain != None):
	# get the first residue
	residue = chain.getResidue(0)
	if (residue != None):
		# iterate over all atoms
		for atom in atoms(residue):
                      print "atom: ", atom.getElement().getSymbol()
Clone this wiki locally