# ColorByDistance

Type Python Script Zhenting Gao

## Introduction

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

## Usage

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

Text

Text

Text

## The Code

```def colorByDistance(anchor='anchor', site='site'):
#Based on: https://pymolwiki.org/index.php/Spectrum#Intermediate
#Author: Zhenting Gao (zhentgpicasa@gmail.com)
#Update: 11/12/2018
#Aim: Color atoms of a binding site based on their distance from a point
# 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

#pseudoatom pOrig, pos=(0,0,0), 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, anchor, '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)
print(stored.newB)
# 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)```