Iterate sses

From PyMOLWiki
Revision as of 06:25, 9 October 2007 by Gilleain (talk | contribs) (New page: 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,...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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, ss in stored.pairs:
        if ss == currentType:
            currentSSE.end = resi
        else:
            sseNumber += 1
            sses.append(SSE(resi, ss, sseNumber))
            currentSSE = sses[-1]
            currentType = ss

    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))