FindObjectsNearby

From PyMOLWiki
Revision as of 11:24, 21 July 2009 by Inchoate (talk | contribs)
Jump to navigation Jump to search

Overview

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="" ):
	"""
	DESCRIPTION:
		finds all PyMOL object names within [radius] Angstroms of [target].
	
	PARAMETERS:
		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
	
	RETURNS:
		[list] of results or None if bad input.
		
	NOTES:
		* 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.

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

	AUTHOR:
		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:
		try:
			outFile = open(fileName, 'wb')
			for x in stored.objs.keys(): f.write( x + "\n" )
			outFile.close()
		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)

Troubleshooting

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