Iterate sses

From PyMOLWiki
Revision as of 12:57, 9 October 2007 by Gilleain (talk | contribs) (same change as for the ss command.)
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

A slightly more complex version of ss that allows the user to pass in a function to act on the sse list. Of course, this requires the user to know about the internals of the sse class, but since this code is all open I doubt that matters..

def iterate_sses(selection, action):

    class SSE(object):

        def __init__(self, start, typ, sseNumber):
            self.start, self.typ = start, typ
            self.end = -1
            self.sseNumber = sseNumber

        def __repr__(self):
            return "%s-%s %s" % (self.start, self.end, self.typ)

    stored.pairs = []
    cmd.iterate(selection, "stored.pairs.append((resi, ss))")
    num, currentType = stored.pairs[0]

    sseNumber = 1
    sses = [SSE(num, currentType, sseNumber)]
    currentSSE = sses[0]
    for resi, ssType in stored.pairs:
        if ssType == currentType:
            currentSSE.end = resi
        else:
            sseNumber += 1
            sses.append(SSE(resi, ssType, sseNumber))
            currentSSE = sses[-1]
            currentType = ssType

    for sse in sses:
        action(sse)

cmd.extend("iterate_sses", iterate_sses)

As an example, here is a function that makes a series of selections, one for each sse, called "H1", "E2", and so on. Use it like: "iterate_sses('my_protein', doSelect)".

def doSelect(sse):
    cmd.select("%s%s" % (sse.typ, sse.sseNumber), "i. %s-%s" % (sse.start, sse.end))