From PyMOLWiki
Jump to navigation Jump to search


This script returns the names of all objects (in state 1) that are within a certain cutoff distance from your provided target. For example, if you load an MD trajectory and are watching the binding pocket, you can select the ligand and then find out which loaded files are within 5 Ang, say, of that ligand atom.

The Code

# -*- coding: utf-8 -*-
# findObjectsNearby.pml -- Get object names within a given radius of the target
def findObjectsNearby( target="", radius=2.0, doPrint=False, fileName="" ):
		finds all PyMOL object names within [radius] Angstroms of [target].
		target,		the base selection/object around which to search for things
		radius,		the radius of the sphere centered around target within which to search
		doPrint,	boolean, if True print the results to console, False no printing
		fileName,	string, if not blank the list called [fileName] is written to disk
		[list] of results or None if bad input.
		* This function ALWAYS returns a list of results or None if the user submits malformed input.
		* The user may opt to print the list to console and also save it to a file.

		* Find all objects nearby 1j01 and heteroatoms that aren't water.  (Contrived example)
		findObjectsNearby 1j01 and (het not resn HOH), 5.5, doPrint=True

		Jason Vertrees, 2009.  Simplified with the help of Warren DeLano.
	if ( len(target)==0 ):
		print "Error: please provide a target selection."
		return None
	stored.objs = {}
	cmd.iterate_state(1, "(" + target + ") around " + str(radius), "stored.objs[model]=1" )

	# save to file?
	if len(fileName) != 0:
			outFile = open(fileName, 'wb')
			for x in stored.objs.keys(): f.write( x + "\n" )
		except IOError:
			print "Error: couldn't open/write to output file, ", fileName
	# print?
	if doPrint:
		print stored.objs.keys()

	return stored.objs.keys()

cmd.extend("findObjectsNearby", findObjectsNearby)


If the script isn't working like you think it should, then make sure that you're in state #1.