User:Speleo3

From PyMOLWiki
Revision as of 11:34, 14 June 2011 by Speleo3 (talk | contribs) (build pymol)
Jump to: navigation, search

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

Contact

  • speleo3/users.sourceforge.net
  • thomas.holder/tuebingen.mpg.de

Scripts written by me

Scripts Pastebin

Some random scripts with no dedicated PyMOLWiki page.

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

    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:
        models.remove(reference)
    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):
    '''
DESCRIPTION

    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.sort()
cmd.extend('mse2met', mse2met)

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

    Remove alternative location atoms.

ARGUMENTS

    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.sort()
cmd.extend('remove_alt', remove_alt)
def cbm(selection='(all)'):
    '''
DESCRIPTION

    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'):
    '''
DESCRIPTION

    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):
    '''
DESCRIPTION

    Extend selection by connected secondary structure elements.

ARGUMENTS

    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),
            'stored.x.add((model,segi,chain,resv,ss))')
    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]):
            continue
        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)
            sele_list.append(sele)
            if not quiet:
                print ss_names.get(ss, ss), sele
    sele = ' or '.join(sele_list)
    if name is not None:
        cmd.select(name, sele)
    if not quiet:
        print 'Selection:', sele
    return sele
cmd.extend('select_extendbyss', select_extendbyss)
def diff(sele1, sele2, byres=1, name=None, operator='in', quiet=0):
    '''
DESCRIPTION

    Difference between two molecules

ARGUMENTS

    sele1 = string: atom selection

    sele2 = string: atom selection

    byres = 0/1: report residues, not atoms (does not affect selection)
    {default: 1}

    operator = in/like/align: operator to match atoms {default: in}

SEE ALSO

    symdiff
    '''
    byres, quiet = int(byres), int(quiet)
    if name is None:
        name = cmd.get_unused_name('diff')
    if operator == 'align':
        alnobj = cmd.get_unused_name('__aln')
        cmd.align(sele1, sele2, cycles=0, transform=0, object=alnobj)
        sele = '(%s) and not %s' % (sele1, alnobj)
        cmd.select(name, sele)
        cmd.delete(alnobj)
    else:
        sele = '(%s) and not ((%s) %s (%s))' % (sele1, sele1, operator, sele2)
        cmd.select(name, sele)
    if not quiet:
        if byres:
            seleiter = 'byca ' + name
            expr = 'print "/%s/%s/%s/%s`%s" % (model,segi,chain,resn,resi)'
        else:
            seleiter = name
            expr = 'print "/%s/%s/%s/%s`%s/%s" % (model,segi,chain,resn,resi,name)'
        cmd.iterate(seleiter, expr)
    return name

def symdiff(sele1, sele2, byres=1, name=None, operator='in', quiet=0):
    '''
DESCRIPTION

    Symmetric difference between two molecules

SEE ALSO

    diff
    '''
    byres, quiet = int(byres), int(quiet)
    if name is None:
        name = cmd.get_unused_name('symdiff')
    tmpname = cmd.get_unused_name('__tmp')
    diff(sele1, sele2, byres, name, operator, quiet)
    diff(sele2, sele1, byres, tmpname, operator, quiet)
    cmd.select(name, tmpname, merge=1)
    cmd.delete(tmpname)
    return name

cmd.extend('symdiff', symdiff)
cmd.extend('diff', diff)
def polarpairs(sel1, sel2, cutoff=4.0, angle=63.0, name='', quiet=1):
    '''
ARGUMENTS

    sel1, sel2 = string: atom selections

    cutoff = float: distance cutoff

    angle = float: h-bond angle cutoff in degrees. If angle="default", take
    "h_bond_max_angle" setting. If angle=0, do not detect h-bonding.

SEE ALSO

    cmd.find_pairs, cmd.distance
    '''
    cutoff = float(cutoff)
    quiet = int(quiet)
    if angle == 'default':
        angle = cmd.get('h_bond_max_angle', cmd.get_object_list(sel1)[0])
    angle = float(angle)
    mode = 1 if angle > 0 else 0
    x = cmd.find_pairs('(%s) and donors' % sel1, '(%s) and acceptors' % sel2,
            cutoff=cutoff, mode=mode, angle=angle) + \
        cmd.find_pairs('(%s) and acceptors' % sel1, '(%s) and donors' % sel2,
            cutoff=cutoff, mode=mode, angle=angle)
    x = sorted(set(x))
    if not quiet:
        print 'Settings: cutoff=%.1fangstrom angle=%.1fdegree' % (cutoff, angle)
        print 'Found %d polar contacts' % (len(x))
    if len(name) > 0:
        for p in x:
            cmd.distance(name, '(%s`%s)' % p[0], '(%s`%s)' % p[1])
    return x

cmd.extend('polarpairs', polarpairs)

Launch interactive python terminal with PyMOL process: (see also Launching From a Script)

#!/usr/bin/python2.6 -i

import sys, os

# autocompletion
import readline
import rlcompleter
readline.parse_and_bind('tab: complete')

# pymol environment
moddir='/opt/pymol-svn/modules'
sys.path.insert(0, moddir)
os.putenv('PYMOL_PATH', os.path.join(moddir, 'pymol/pymol_path'))

# pymol launching
import pymol
pymol.pymol_argv = ['pymol','-qc'] + sys.argv[1:]
pymol.finish_launching()
cmd = pymol.cmd

Build PyMOL: (see also Linux Install)

#!/bin/bash -e

prefix=/opt/pymol-svn
modules=$prefix/modules

python setup.py build install \
    --home=$prefix \
    --install-purelib=$modules \
    --install-platlib=$modules

export PYTHONPATH=$modules:$PYTHONPATH
python setup2.py install
install pymol $prefix/