This project has moved. For the latest updates, please go here.

Basic Tutorial

Perhaps one of the most frequently used parts of the library is the module, which provides the Structure, Chain, Residue and Atom objects. You could easily build a Structure from scratch, but a far more common scenario is parsing a structure from a PDB file using one of the AbstractStructureParsers. All bio IO objects, including the StructureParser factory, are defined in and sub-packages:

>>> from import StructureParser
>>> p = StructureParser("/some/file/pdb1x80.ent")
>>> s = p.parse_structure()
>>> print(s)
<Structure: 1x80, 2 chains>

The last statement will return a instance, which is a composite hierarchical object:

>>> for chain_id in s.chains:
	chain = s.chains[chain_id]
	for residue in chain.residues:
	    for atom_id in residue.atoms:
	        atom = residue.atoms[atom_id]

Some of the inner objects in this hierarchy behave just like dictionaries (but are not):

>>> s.chains['A']        # access chain A by ID
<Chain A: Protein>
>>> s['A']               # the same
<Chain A: Protein>

Others behave like collections:

>>> chain.residues[10]               # 1-based access to the residues in the chain
<ProteinResidue [10]: PRO 10>
>>> chain[10]                        # 0-based, list-like access
<ProteinResidue [11]: GLY 11>

But all entities are iterable because they inherit the items iterator from AbstractEntity. The above loop can be shortened:

>>> for chain in s.items:
	for residue in chain.items:
	    for atom in residue.items:

or even more:

>>> from import Atom
>>> for atom in s.components(klass=Atom):

You may also be interested in extracting a sub-chain from this structure:

>>> s.chains['B'].subregion(3, 20)    # from positions 3 to 20, inclusive
<Chain B: Protein>

or modifying it in some way, for example, in order to append a new residue, try:

>>> from import ProteinResidue
>>> from import ProteinAlphabet
>>> residue = ProteinResidue(401, ProteinAlphabet.ALA)
>>> s.chains['A'].residues.append(residue)

Finally, you would probably want to save your structure back to a PDB file:

>>> s.to_pdb('/some/file/name.pdb')

Where to go from here

If you want to dive into statistics, you could peek inside csb.statistics and its sub-packages. For example, csb.statistics.pdf contains a collection of probability density objects, like Gaussian or Gamma.

But chances are you would first like to try reading some files, so you could start exploring right now. As we have already seen, provides PDB Structure parsers, for example and is all about reading FASTA Sequences and SequenceAlignments. Be sure to check out, and

If you are working with HHpred (ProfileHMMs, HHpredHits), then is for you. This package provides and, which are used to read .hhm and .hhr files.

Finally, if you want to make some nice plots with matplotlib, you may like the clean object-oriented interface of our Chart. See and maybe also to get started.

Last edited Oct 2, 2013 at 9:26 AM by kalev, version 2


No comments yet.