Zero residues: Difference between revisions

From PyMOLWiki
Jump to navigation Jump to search
(New page: == OVERVIEW == This script will renumber all the residues such that the first one is numbered 0. This is often helpful when dealing with alignments. == EXAMPLE == <source lang="python"> zero_residues ...)
 
(added chains argument)
 
(6 intermediate revisions by 2 users not shown)
Line 1: Line 1:
== OVERVIEW ==
== OVERVIEW ==
This script will renumber all the residues such that the first one is numbered 0.  This is often helpful when dealing with alignments.
This script will renumber all the residues such that the first one is numbered 0.  This is often helpful when dealing with alignments.
=== Ordering from Sequence End ===
If you want to change numbering based off the last residue's number in the sequence, just replace '''first''' in the code with '''last'''.


== EXAMPLE ==
== EXAMPLE ==
Line 6: Line 9:
zero_residues 1AFK
zero_residues 1AFK
zero_residues *
zero_residues *
# make the first residue's number, 30.
zero_residues 1AFK, 30
</source>
</source>


Line 13: Line 19:
== CODE ==
== CODE ==
<source lang="python">
<source lang="python">
import pymol
from pymol import cmd, stored
 
def zero_residues(sel1,offset=0,chains=0):
        """
DESCRIPTION
 
    Renumbers the residues so that the first one is zero, or offset
 
USAGE
 
    zero_residues selection [, offset [, chains ]]
 
EXAMPLES
 
    zero_residues protName            # first residue is 0
    zero_residues protName, 5        # first residue is 5
    zero_residues protName, chains=1  # each chain starts at 0
    zero_residues *
        """
        offset = int(offset)
 
        # variable to store the offset
        stored.first = None
        # get the names of the proteins in the selection
 
        names = ['(model %s and (%s))' % (p, sel1)
                        for p in cmd.get_object_list('(' + sel1 + ')')]
 
        if int (chains):
                names = ['(%s and chain %s)' % (p, chain)
                                for p in names
                                for chain in cmd.get_chains(p)]


def zero_residues(sel1):
        # for each name shown
"""
        for p in names:
PURPOSE: renumbers the residues so that the first one is zero.
                # get this offset
USAGE: zero_residues protName
                ok = cmd.iterate("first %s and polymer and n. CA" % p,"stored.first=resv")
EXAMPLE: zero_residues *
                # don't waste time if we don't have to
EXAMPLE: zero_residues 1ESR
                if not ok or stored.first == offset:
"""
                        continue;
                # reassign the residue numbers
# variable to store the offset
                cmd.alter("%s" % p, "resi=str(int(resi)-%s)" % str(int(stored.first)-offset))
stored.first = None
                # update pymol
# get the names of the proteins in the selection
names = cmd.get_names(sel1)


# for each name shown
        cmd.rebuild()
for p in names:
# get this offset
cmd.iterate("first %s and polymer and n. CA" % p,"stored.first=resi")
# don't waste time if we don't have to
if ( stored.first == 0 ):
continue;
# reassign the residue numbers
cmd.alter("%s" % p, "resi=str(int(resi)-%s)" % stored.first)
# update pymol
cmd.sort()


# let pymol know about the function
# let pymol know about the function
Line 44: Line 69:
</source>
</source>


[[Category:Script_Library]]
[[Category:Script_Library|Zero Residues]]
[[Category:ObjSel_Scripts]]

Latest revision as of 06:06, 26 January 2013

OVERVIEW

This script will renumber all the residues such that the first one is numbered 0. This is often helpful when dealing with alignments.

Ordering from Sequence End

If you want to change numbering based off the last residue's number in the sequence, just replace first in the code with last.

EXAMPLE

zero_residues 1AFK
zero_residues *

# make the first residue's number, 30.
zero_residues 1AFK, 30

INSTALL

Copy the source code below, to "zero_residues.py" and then simply run the file. The command, "zero_residues" will now be defined and can be used as in the examples above.

CODE

from pymol import cmd, stored

def zero_residues(sel1,offset=0,chains=0):
        """
DESCRIPTION

    Renumbers the residues so that the first one is zero, or offset

USAGE

    zero_residues selection [, offset [, chains ]]

EXAMPLES

    zero_residues protName            # first residue is 0
    zero_residues protName, 5         # first residue is 5
    zero_residues protName, chains=1  # each chain starts at 0
    zero_residues *
        """
        offset = int(offset)

        # variable to store the offset
        stored.first = None
        # get the names of the proteins in the selection

        names = ['(model %s and (%s))' % (p, sel1)
                        for p in cmd.get_object_list('(' + sel1 + ')')]

        if int (chains):
                names = ['(%s and chain %s)' % (p, chain)
                                for p in names
                                for chain in cmd.get_chains(p)]

        # for each name shown
        for p in names:
                # get this offset
                ok = cmd.iterate("first %s and polymer and n. CA" % p,"stored.first=resv")
                # don't waste time if we don't have to
                if not ok or stored.first == offset:
                        continue;
                # reassign the residue numbers
                cmd.alter("%s" % p, "resi=str(int(resi)-%s)" % str(int(stored.first)-offset))
                # update pymol

        cmd.rebuild()

# let pymol know about the function
cmd.extend("zero_residues", zero_residues)