Skip to content

IterateOverAllChainsOfAProtein

dstoeckel edited this page Mar 16, 2015 · 2 revisions

How can I iterate over all Chains of a Protein?

Iterate over all chains in a protein using BALL's ChainIterator:

C++

#include <BALL/FORMAT/PDBFile.h>
#include <BALL/KERNEL/system.h>
#include <BALL/KERNEL/chain.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 number of chains in the protein
   cout << "Number of chains in the protein: " << protein->countChains() << endl;

   // iterate over all chains
   for (ChainIterator ch_it = protein->beginChain(); +ch_it; ++ch_it)
   {
      // get the number of residues per chain
      cout << "Number of residues: " << ch_it->countResidues()  << endl;
     
      // get a pointer to the current Chain by
      Chain* chain = &*ch_it;
   }
}

Note that +ch_it equals ch_it != protein->endChain()!

Note also: Never try to add or remove chains from the protein while iterating its elements! 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 molecule
protein = S.getMolecule(0)
	    
# get the number of chains in the protein
print "Number of chains in the protein: " , len(chains(protein))

#iterate over all chains
for chain in chains(protein):
	# get the number of residues per chain
	print "Number of residues: ", chain.countResidues()

Clone this wiki locally