Iterate sses: Difference between revisions

From PyMOLWiki
Jump to navigation Jump to search
(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,...)
 
No edit summary
 
(2 intermediate revisions by 2 users not shown)
Line 21: Line 21:
     sses = [SSE(num, currentType, sseNumber)]
     sses = [SSE(num, currentType, sseNumber)]
     currentSSE = sses[0]
     currentSSE = sses[0]
     for resi, ss in stored.pairs:
     for resi, ssType in stored.pairs:
         if ss == currentType:
         if ssType == currentType:
             currentSSE.end = resi
             currentSSE.end = resi
         else:
         else:
             sseNumber += 1
             sseNumber += 1
             sses.append(SSE(resi, ss, sseNumber))
             sses.append(SSE(resi, ssType, sseNumber))
             currentSSE = sses[-1]
             currentSSE = sses[-1]
             currentType = ss
             currentType = ssType


     for sse in sses:
     for sse in sses:
Line 41: Line 41:
     cmd.select("%s%s" % (sse.typ, sse.sseNumber), "i. %s-%s" % (sse.start, sse.end))
     cmd.select("%s%s" % (sse.typ, sse.sseNumber), "i. %s-%s" % (sse.start, sse.end))
</source>
</source>
[[Category:Script_Library|Iterate SSes]]
[[Category:Structural_Biology_Scripts]]

Latest revision as of 07:43, 30 April 2009

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