From PyMOLWiki
Jump to: navigation, search
Type Python Script
Author(s) Zhenting Gao


Color the surface of a binding site by the distance to a specific atom.


  • Open PyMOL
  • Load PDB files
  • run this Python script inside PyMOL
  • call the function
    • colorByDistance anchor, bindingSite

Required Arguments


Optional Arguments




The Code

def colorByDistance(anchor='anchor', site='site'):
	#Based on:
	#Author: Zhenting Gao (
	#Update: 11/12/2018
	#Aim: Color atoms of a binding site based on their distance from a point
	#May also refer to
	# returns the length of the distance between atom A and atom B

	diff_len = lambda x,y : math.sqrt((x[0]-y[0])*(x[0]-y[0]) + (x[1]-y[1])*(x[1]-y[1]) + (x[2]-y[2])*(x[2]-y[2]))

	# fetch site from the PDB

	#fetch site, async=0

	# show it as surface

	#as surface

	# create the pseudoatom at the origin

	cmd.pseudoatom("pOrig", anchor, label="origin")

	# these are special PyMOL variables that will hold # the coordinates of 
	# the atoms and the  pseudoatom

	stored.origCoord = []
	stored.distCoord = []

	# copy the coordinates into those special variables 

	cmd.iterate_state(1, "pOrig", 'stored.origCoord.append((x,y,z))')
	cmd.iterate_state(1, site, 'stored.distCoord.append((x,y,z))')

	# extend origCoord to be the same length as the other

	stored.origCoord *= len(stored.distCoord)

	# calculate the distances

	stored.newB = map(lambda x,y: diff_len(x,y), stored.distCoord, stored.origCoord)
	# put them into the b-factor of the protein

	cmd.alter( site, "b=stored.newB.pop(0)")

	# color by rainbow_rev or any other
	# palette listed in "help spectrum"

	cmd.spectrum(expression="b", palette="rainbow", selection=site)
	cmd.set("surface_color","-1",site) #color the surface of the binding site by corresponding atom colors
cmd.extend('colorByDistance', colorByDistance)