Renumber

From PyMOLWiki
Revision as of 06:31, 31 January 2012 by Speleo3 (talk | contribs) (created)
Jump to navigation Jump to search

renumber sets new residue numbers (resi) for a polymer based on connectivity.

Example

This examples takes a pdb structure with insertion codes and sets a new, linear numbering based on Q8N2U3_HUMAN.

fetch 1h4w, async=0

# move everything which is not polymer to another chain
alter not polymer, chain="B"

# renumber polymer, first 27 residues of Q8N2U3_HUMAN missing.
renumber chain A, 28

The Script

from pymol import cmd, CmdException

def renumber(selection='all', start=1, startsele=None, quiet=1):
    '''
DESCRIPTION

    Set residue numbering (resi) based on connectivity.

ARGUMENTS

    selection = string: atom selection to renumber {default: all}

    start = integer: counting start {default: 1}

    startsele = string: residue to start counting from {default: first in
    selection}
    '''
    start, quiet = int(start), int(quiet)
    model = cmd.get_model(selection)
    cmd.iterate(selection, 'atom_it.next().model = model',
            space={'atom_it': iter(model.atom)})
    if startsele is not None:
        startidx = cmd.index('first (' + startsele + ')')[0]
        for atom in model.atom:
            if (atom.model, atom.index) == startidx:
                startatom = atom
                break
        else:
            print ' Error: startsele not in selection'
            raise CmdException
    else:
        startatom = model.atom[0]
    for atom in model.atom:
        atom.adjacent = []
        atom.visited = False
    for bond in model.bond:
        atoms = [model.atom[i] for i in bond.index]
        atoms[0].adjacent.append(atoms[1])
        atoms[1].adjacent.append(atoms[0])
    def traverse(atom, resi):
        atom.resi = resi
        atom.visited = True
        for other in atom.adjacent:
            if other.visited:
                continue
            if (atom.name, other.name) in [('C','N'), ("O3'", 'P')]:
                traverse(other, resi+1)
            elif (atom.name, other.name) in [('N','C'), ('P', "O3'")]:
                traverse(other, resi-1)
            elif (atom.name, other.name) not in [('SG', 'SG')]:
                traverse(other, resi)
    traverse(startatom, start)
    cmd.alter(selection, 'resi = atom_it.next().resi',
            space={'atom_it': iter(model.atom)})

cmd.extend('renumber', renumber)

# vi:expandtab:smarttab

See Also