From PyMOLWiki
Revision as of 05:15, 25 January 2011 by Speleo3 (talk | contribs) (Scripts Pastebin: cbm, dev2b, select_extendbyss)
Jump to: navigation, search

My name is Thomas Holder and I am a bioinformatician at the MPI for Developmental Biology in Tübingen, Germany.


  • speleo3/
  • thomas.holder/

Scripts written by me

Scripts Pastebin

Some random scripts with no dedicated PyMOLWiki page.

def extra_fit(selection='(all)', reference=None, method=cmd.align):

    Like "intra_fit", but for multiple objects instead of
    multiple states.
    models = cmd.get_object_list(selection)
    if reference is None:
        reference = models[0]
        models = models[1:]
    elif reference in models:
    if cmd.is_string(method):
        method = eval(method)
    for model in models:
        print model, method(model, reference)
cmd.extend('extra_fit', extra_fit)
def mse2met(selection='all', quiet=1):

    Mutate selenomethionine to methionine
    quiet = int(quiet)
    x = cmd.alter('(%s) and MSE/SE' % selection, 'name="SD";elem="S"')
    cmd.alter('(%s) and MSE/' % selection, 'resn="MET";type="ATOM"')
    if not quiet:
        print 'Altered %d MSE residues to MET' % (x)
cmd.extend('mse2met', mse2met)

def remove_alt(selection='all', keep='A', quiet=1):

    Remove alternative location atoms.


    selection = string: atom selection

    keep = string: AltLoc to keep {default: A}
    cmd.remove('(%s) and not alt +%s' % (selection, keep), quiet=int(quiet))
    cmd.alter(selection, 'alt=""')
cmd.extend('remove_alt', remove_alt)
def cbm(selection='(all)'):

    Color by molecule
    col = 2
    for model in cmd.get_object_list(selection):
        cmd.color(col, '%s and (%s)' % (model, selection))
        col += 1
cmd.extend('cbm', cbm)
def dev2b(selection='name CA'):

    Determine the RMSD per residue for a multi-state object and assign b-factor
    import numpy
    stored.x = dict()
    stored.b = dict()
    for state in range(1, cmd.count_states()+1):
        cmd.iterate_state(state, selection, 'stored.x.setdefault((model,segi,chain,resi,name), []).append([x,y,z])')
    for key, coord_list in stored.x.iteritems():
        b_sq = numpy.array(coord_list).var(0).mean() # var over states, mean over x,y,z
        stored.b[key] = numpy.sqrt(b_sq) * 10.0
    cmd.alter(selection, 'b = stored.b[model,segi,chain,resi,name]')
cmd.extend('dev2b', dev2b)
def select_extendbyss(selection, name=None, quiet=0):

    Extend selection by connected secondary structure elements.


    selection = string: selection-expression

    name = string: create a named atom selection if not None {default: None}
    def in_intervals(i, intervals):
        for interval in intervals:
            if interval[0] <= i and i <= interval[1]:
                return True
        return False
    quiet = int(quiet)
    stored.x = set()
    # only iterate over CAs since for example PyMOL's dss command just
    # assignes ss to CAs.
    cmd.iterate('bycalpha (%s)' % (selection),
    elements = dict()
    for model,segi,chain,resv,ss in stored.x:
        key = (model,segi,chain,ss)
        elements.setdefault(key, [])
        if in_intervals(resv, elements[key]):
        stored.b = set()
        cmd.iterate('/%s/%s/%s//CA and ss "%s"' % key, 'stored.b.add(resv)')
        resv_min = resv
        resv_max = resv
        while (resv_min - 1) in stored.b:
            resv_min -= 1
        while (resv_max + 1) in stored.b:
            resv_max += 1
        elements[key].append((resv_min, resv_max))
    sele_list = []
    ss_names = {'S': 'Strand', 'H': 'Helix', '': 'Loop'}
    for key in elements:
        model,segi,chain,ss = key
        for resv_min,resv_max in elements[key]:
            sele = '/%s/%s/%s/%d-%d' % (model, segi, chain, resv_min, resv_max)
            if not quiet:
                print ss_names.get(ss, ss), sele
    sele = ' or '.join(sele_list)
    if name is not None:, sele)
    if not quiet:
        print 'Selection:', sele
    return sele
cmd.extend('select_extendbyss', select_extendbyss)