User:Speleo3

From PyMOLWiki
Revision as of 03:59, 25 July 2011 by Speleo3 (talk | contribs) (save_settings)
Jump to navigation Jump to 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 save_settings(filename='~/.pymolrc-settings.py', quiet=1):
    '''
DESCRIPTION

    Dumps all settings with non-default values to ~/.pymolrc-settings.py

    Feature Request: Save settings for later use - ID: 1009951
    https://sourceforge.net/tracker/?func=detail&aid=1009951&group_id=4546&atid=354546
    '''
    from pymol.setting import get_name_list
    quiet = int(quiet)
    # temporatily load default settings and remember them
    cmd.reinitialize('store_defaults')
    cmd.reinitialize('original_settings')
    original = [(name, cmd.get(name)) for name in get_name_list()]
    cmd.reinitialize('settings')
    # dump to file
    filename = cmd.exp_path(filename)
    f = open(filename, 'w')
    print >> f, '# AUTOGENERATED FILE'
    print >> f, 'from pymol import cmd'
    count = 0
    for name, o_value in original:
        value = cmd.get(name)
        if value != o_value:
            print >> f, 'cmd.set("%s", %s)' % (name, repr(value))
            if not quiet:
                print 'set %s, %s # default: %s' % (name, value, o_value)
            count += 1
    f.close()
    if not quiet:
        print 'Dumped %d settings to %s' % (count, filename)
cmd.extend('save_settings', save_settings)

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/