PyMOL: colour by residue

PyMOL is a handy free way of viewing three dimensional protein structures. It allows you to toggle between different representations of the protein – such as cartoon, surface, sticks, etc. – which all have their own pros and cons.

However one thing I felt that PyMOL lacked was an easy way to visually distinguish residues based on type. Whist you can easily differentiate atom types based on colour in the colour menu, and even choose which colour you wish carbons to show up as whilst keeping heteroatoms different colours, this assigned carbon colour would be constant throughout the entire protein.


Looking online I found a few scripts which loosely coloured residues according to properties (e.g. one colour for hydrophobic, one for hydrophilic, one for positively charged, one for negatively charged), but the best I had encountered so far was the ‘colour by residue’ option in NGL Viewer – previously the default viewer option on the RCSB Protein Data Bank’s website.

This setting assigns a unique colour to every residue such that similar amino acids have similar but distinguishable hues (e.g. leucine and isoleucine are slightly different shades of green), allowing for both easy identification of residue type and easy inspection of the overall geography of a protein structure: for instance, similarly blue-coloured lysines and arginines are often seen on the outskirts of the protein, whilst the core is often a mix of greens (leucine and isoleucine) and grey (phenylalanine).

With this in mind I decided to write a script to colour residues in PyMOL according to a similar colour scheme – with two important differences. Firstly, whereas in the NGL Viewer option every atom in the residue was coloured the same, I decided to keep the heteroatoms (oxygen, nitrogen and cysteine) as their standard colours (red, blue and yellow respectively). Secondly, whilst the NGL Viewer option colours the backbone portion of every residue the same as the side chain, and colours glycine (which is just the backbone) as white, I decided to colour the backbone region of every residue as white. This, I felt, had two advantages: firstly, it allows the viewer to easily pick out the trajectory of the backbone when the side chains are all visible; and secondly it highlights glycine’s role as a ‘blank’ amino acid that has no side chain to speak of, as it blends into the overall backbone.

Here is the script:

# COLOUR BY RESIDUE - PATRICK BRENNAN, UNIVERSITY OF OXFORD - AUGUST 2020

from pymol import cmd

def ngl(selection='all'):
        cmd.select ('backbone_carbons','name C+CA')
        cmd.select ('ALA','name CB and resn ALA')
        cmd.select ('ARG','name CB+CG+CD+CZ and resn ARG')
        cmd.select ('ASN','name CB+CG and resn ASN')
        cmd.select ('ASP','name CB+CG and resn ASP')
        cmd.select ('CYS','name CB and resn CYS')
        cmd.select ('GLN','name CB+CG+CD and resn GLN')
        cmd.select ('GLU','name CB+CG+CD and resn GLU')
        cmd.select ('HIS','name CB+CG+CD2+CE1 and resn HIS')
        cmd.select ('ILE','name CB+CG1+CG2+CD1 and resn ILE')
        cmd.select ('LEU','name CB+CG+CD1+CD2 and resn LEU')
        cmd.select ('LYS','name CB+CG+CD+CE and resn LYS')
        cmd.select ('MET','name CB+CG+CE and resn MET')
        cmd.select ('PHE','name CB+CG+CD1+CD2+CE1+CE2+CZ and resn PHE')
        cmd.select ('PRO','name CB+CG+CD and resn PRO')
        cmd.select ('SER','name CB and resn SER')
        cmd.select ('THR','name CB+CG2 and resn THR')
        cmd.select ('TRP','name CB+CG+CD1+CD2+CE2+CE3+CZ2+CZ3+CH2 and resn TRP')
        cmd.select ('TYR','name CB+CG+CD1+CD2+CE1+CE2+CZ and resn TYR')
        cmd.select ('VAL','name CB+CG1+CG2 and resn VAL')

        print selection
        code = {'backbone_carbons':'white','ALA':'lime','ARG':'density','ASN':'deepsalmon','ASP':'warmpink','CYS':'paleyellow','GLN':'tv_red','GLU':'ruby','HIS':'slate','ILE':'forest','LEU':'smudge','LYS':'deepblue','MET':'sand','PHE':'gray40','PRO':'gray20','SER':'tv_orange','THR':'brown','TRP':'palegreen','TYR':'wheat','VAL':'pink'}
        cmd.select ('none')
        for elem in code:
                line='color '+code[elem]+','+elem+'&'+selection
                print line
                cmd.do (line)

cmd.extend (‘ngl',ngl)


You can see in the script that every residue has its own unique colour, with names as imaginative as ‘deepsalmon’, ‘smudge’ and ‘wheat’ (whether or not Theresa May keeps a high tyrosine diet is beyond the scope of this blog post).

Copy the text and paste into a new file called – for example – ‘colour.py’. You can call it whatever you like, as long as it has ‘.py’ at the end.

Then, from the same directory as this new file, open PyMOL with any suitable structure. Once open, go to the command line in PyMOL and enter ‘run colour.py’ (or subbing in whatever you’ve called your file). Hit enter. Then enter ‘ngl’, and hit enter again.

The cartoon representation of the protein should now have turned white. Now go the ‘show’ menu on the right and choose a different representation that will show the residues’ side chains (e.g. ‘sticks’). Select this, and you will see that the side chains are all different colours. You may find it helpful to keep the cartoon representation in place over the sticks, or remove it completely and rely on the white backbone to follow the protein’s overarching geography.

If you are using PyMOL in a diretory that is different from the one where ‘colour.py’ is saved, enter ‘run’, followed by the full path to where ‘colour.py’ is saved. Alternatively, save a copy of ‘colour.py’ to the working trajectory and enter ‘run colour.py’ as before.

Of course you can alter the script to customise the residues colours to be whatever you desire by changing the values in the dictionary – just make sure to pick colour names used by PyMOL. A nice update on this script may be to change the colour list such that it is optimal for colour blind individuals. I’m not aware of any residue colour selections that take this into account (also I only made this script for myself and I am not colour blind), but seeing as I am now sharing it I would obviously like it to be as inclusive as possible, so any colour suggestions are welcome.

Author