Skip to content

Python_IdentifyNeighborResidues

dstoeckel edited this page Mar 16, 2015 · 2 revisions

How can I identify residues/atoms within a certain spatial proximity of special atoms within BALLView?

In BALLView identifying residues or atoms in a certain spatial proximity of special atoms for e.g. creating fancy representations of the binding pocket can be done as described in the following:

  • Select the ligand(s) in BALLView's Structures window.

  • Copy the following script into the BALLView's Python window and set the variable maxdistance to a radius of your choice (Angstroem)

  • Run the following script.

Please note: For simplicity the following script only considers the first system of BALLView. However, this can be easily changed.

#!/usr/bin/python

maxdistance = 10

# get the first system
system = getSystems()[0]


# collect the atoms within maxdistance to the selected atoms
cl = []
for atom1 in atoms(system):
  if not atom1.isSelected():
    continue
  for residue in residues(system):
    close=False
    for atom2 in atoms(residue):
      dist = (atom1.getPosition() - atom2.getPosition()).getLength()
      if not atom2.isSelected() and dist < maxdistance:
        close=True
        break
    if close:
      cl.append(residue)

# delete the old selection
system.deselect()

# and select the relevant atoms for a follwing task, e.g. creating a representation
for residue in cl:
  for atom in atoms(residue):
    if not atom.isSelected():
      atom.select()

# inform BALLView to update its scene
getMainControl().update(system)

To create a representation of the new selection

  • Click the right-mouse-button while pointing somewhere in BALLView's Structures window and select Highlight Selection to convert the scripts selection into a BALLView Highlighting.

  • Click the right-mouse-button while pointing on one of the highlighted residues in the Structures window, choose Create Representation, and select a representation mode of your choice.

NOTE:

  • Take care that you have to remain consistent in your use of either tabs or plain spaces for python indentation, since python relies on the relative indentation of nested blocks to determine the grouping of statements correctly.

  • Take care, that you copy the two empty lines at the end of the given script, since python needs them to interpret the end of the script.

  • For running this script outside of BALLView you need to import the BALL library:

#!python
import BALL

or

#!python
from BALL import *
Clone this wiki locally