Skip to content

IterateOverAllResiduesOfAChain

dstoeckel edited this page Mar 16, 2015 · 2 revisions

How can I iterate over all Residues of a Chain?

C++

Iterate over all Chains in a Protein using BALL's ResidueIterator:

#include <BALL/FORMAT/PDBFile.h>
#include <BALL/KERNEL/system.h>
#include <BALL/KERNEL/chain.h>
#include <BALL/KERNEL/residue.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
   if (protein->countChains() > 0)
   { 
      Chain* chain =  protein->getChain(0);
   
      // iterate over all residues and count alaines
      int num_ala = 0;
      for (ResidueIterator r_it = chain->beginResidue(); +r_it; ++r_it)
      {
         if (r_it->getName() == String("ALA"))
         {
            num_ala++;
         }
         
         // get a pointer to the current residue by
         Residue* residue = &*r_it;
      }
      cout << "Number of alanines: " <<  num_ala << endl;
   }
}

Note, that +res_it equals res_it != chain->endResidue()!

Note also: Never try to add or remove residues from a chain while iterating over it! Your program will crash!

python

import sys
from BALL import *

# read the PDB-file into a BALL::System
f = PDBFile("myProtein.pdb")
S = System()
f.read(S)

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

print "Number of chains: ", protein.countChains()  
   
# get the first chain
if (protein.countChains() > 0):
	chain = protein.getChain(0)

 	# iterate over all residues and count alanines
	num_ala = 0;
	for residue in residues(chain):
		if (residue.getName() == "ALA"):
			num_ala = num_ala + 1		        
	print "Number of alanines: " , num_ala

Clone this wiki locally