Module xi_covutils.pdb

Compute some stuff on PDB file

Expand source code
"""
    Compute some stuff on PDB file
"""
from functools import reduce
from operator import add
from warnings import filterwarnings
from Bio.PDB.Atom import Atom
from Bio.PDB.PDBIO import Select
from Bio.PDB.PDBExceptions import PDBConstructionWarning

class DefaultSelect(Select):
    """
    Default Select class. Select everthing from first model.
        :param Select: Parent select
    """
    def accept_atom(self, atom):
        return True
    def accept_chain(self, chain):
        return True
    def accept_model(self, model):
        return model.get_id() == 0
    def accept_residue(self, residue):
        return True

class CarbonAlphaSelect(DefaultSelect):
    """
    Select Alpha carbon atoms of first model.
        :param DefaultSelect: parent select class
    """
    def accept_atom(self, atom):
        return atom.get_name() == 'CA'

def _centroid_atom(atoms):
    filterwarnings('ignore', category=PDBConstructionWarning)
    centroid = reduce(add, (atom.get_coord() for atom in atoms), 0)/len(atoms)
    return Atom('Centroid', centroid, 0, 0, 0, 'Centroid', 1)

def _calc_rg(atoms):
    centroid = _centroid_atom(atoms)
    return reduce(add, ((atom-centroid)**2 for atom in atoms), 0)

def collect_atoms(structure, select):
    """
    Retrieve all selected atoms from a structure
        :param structure: a Bio.PDB.Structure.Structure object
        :param select: a Bio.PDB.PDBIO.Select subclass object
    """
    atoms = [atom
             for model in structure
             for chain in model
             for residue in chain
             for atom in residue
             if select.accept_model(model)
             and select.accept_chain(chain)
             and select.accept_residue(residue)
             and select.accept_atom(atom)]
    return atoms


def gyration_radius(structure, select=CarbonAlphaSelect(), by_chain=False):
    """
    Compute radius of gyration of a protein.
    Only takes into account alpha carbon atoms.
        :param structure: a Bio.PDB.Structure.Structure object
    """
    if by_chain:
        results = {
            'rg': {},
            'compactness' : {}
        }
        for current_chain in structure.get_chains():
            class ChainedSelect(select.__class__):
                """Inner select class for chains"""
                def __init__(self, chain):
                    self.c_chain = chain.id
                def accept_chain(self, chain):
                    """Overriden accept_chain method."""
                    return self.c_chain == chain.id
            chained_select = ChainedSelect(current_chain)
            atoms = collect_atoms(structure, chained_select)
            rad_of_gyr = _calc_rg(atoms)
            results['rg'][current_chain.id] = rad_of_gyr
            results['compactness'][current_chain.id] = rad_of_gyr/len(atoms)

        return results

    atoms = collect_atoms(structure, select)
    rad_of_gyr = _calc_rg(atoms)
    return {
        'rg': {'all': rad_of_gyr},
        'compactness': {'all':rad_of_gyr/len(atoms)}}

Functions

def collect_atoms(structure, select)

Retrieve all selected atoms from a structure :param structure: a Bio.PDB.Structure.Structure object :param select: a Bio.PDB.PDBIO.Select subclass object

Expand source code
def collect_atoms(structure, select):
    """
    Retrieve all selected atoms from a structure
        :param structure: a Bio.PDB.Structure.Structure object
        :param select: a Bio.PDB.PDBIO.Select subclass object
    """
    atoms = [atom
             for model in structure
             for chain in model
             for residue in chain
             for atom in residue
             if select.accept_model(model)
             and select.accept_chain(chain)
             and select.accept_residue(residue)
             and select.accept_atom(atom)]
    return atoms
def gyration_radius(structure, select=<Select all>, by_chain=False)

Compute radius of gyration of a protein. Only takes into account alpha carbon atoms. :param structure: a Bio.PDB.Structure.Structure object

Expand source code
def gyration_radius(structure, select=CarbonAlphaSelect(), by_chain=False):
    """
    Compute radius of gyration of a protein.
    Only takes into account alpha carbon atoms.
        :param structure: a Bio.PDB.Structure.Structure object
    """
    if by_chain:
        results = {
            'rg': {},
            'compactness' : {}
        }
        for current_chain in structure.get_chains():
            class ChainedSelect(select.__class__):
                """Inner select class for chains"""
                def __init__(self, chain):
                    self.c_chain = chain.id
                def accept_chain(self, chain):
                    """Overriden accept_chain method."""
                    return self.c_chain == chain.id
            chained_select = ChainedSelect(current_chain)
            atoms = collect_atoms(structure, chained_select)
            rad_of_gyr = _calc_rg(atoms)
            results['rg'][current_chain.id] = rad_of_gyr
            results['compactness'][current_chain.id] = rad_of_gyr/len(atoms)

        return results

    atoms = collect_atoms(structure, select)
    rad_of_gyr = _calc_rg(atoms)
    return {
        'rg': {'all': rad_of_gyr},
        'compactness': {'all':rad_of_gyr/len(atoms)}}

Classes

class CarbonAlphaSelect

Select Alpha carbon atoms of first model. :param DefaultSelect: parent select class

Expand source code
class CarbonAlphaSelect(DefaultSelect):
    """
    Select Alpha carbon atoms of first model.
        :param DefaultSelect: parent select class
    """
    def accept_atom(self, atom):
        return atom.get_name() == 'CA'

Ancestors

Methods

def accept_atom(self, atom)

Overload this to reject atoms for output.

Expand source code
def accept_atom(self, atom):
    return atom.get_name() == 'CA'
class DefaultSelect

Default Select class. Select everthing from first model. :param Select: Parent select

Expand source code
class DefaultSelect(Select):
    """
    Default Select class. Select everthing from first model.
        :param Select: Parent select
    """
    def accept_atom(self, atom):
        return True
    def accept_chain(self, chain):
        return True
    def accept_model(self, model):
        return model.get_id() == 0
    def accept_residue(self, residue):
        return True

Ancestors

  • Bio.PDB.PDBIO.Select

Subclasses

Methods

def accept_atom(self, atom)

Overload this to reject atoms for output.

Expand source code
def accept_atom(self, atom):
    return True
def accept_chain(self, chain)

Overload this to reject chains for output.

Expand source code
def accept_chain(self, chain):
    return True
def accept_model(self, model)

Overload this to reject models for output.

Expand source code
def accept_model(self, model):
    return model.get_id() == 0
def accept_residue(self, residue)

Overload this to reject residues for output.

Expand source code
def accept_residue(self, residue):
    return True