2. 'Through hardship to the stars' or how to read documentation.

2.1 Mutating a protein and making a movie.

2.1.1 Importing pymol as a module.

In [2]:
# Importing pymol module

import __main__
__main__.pymol_argv = [ 'pymol', '-x' ]

import pymol
import IPython
pymol.finish_launching()
from pymol import cmd,stored

2.1.2 Fetching the structure and coloring ligand and protein.

In [7]:
cmd.reinitialize()
cmd.bg_color('white')

cmd.fetch('1lmp', 'putative_protein')
cmd.remove('solvent')
cmd.color('silver')
cmd.select('ligand', selection='hetatm')
cmd.color('wheat', 'hetatm')

2.1.3 Protein before mutation procedure.

In [11]:
# What do we see?

cmd.set('ray_trace_mode', 1)
cmd.png('first_look.png', '1920', '1080', ray=1)
In [12]:
IPython.display.Image('first_look.png', retina=True)
Out[12]:

2.1.4 Defining a mutation residue.

In [150]:
# What do we want to mutate?
# Resi 97 is an arginine

cmd.color('red', 'resi 97')
cmd.hide('cartoon', 'resi 97')
cmd.show('sticks', 'resi 97')
In [ ]:
cmd.set('ray_trace_mode', 1)
cmd.png('protein_without_mutation.png', '1920', '1080', ray=1)
In [27]:
IPython.display.Image('protein_without_mutation.png', retina=True)
Out[27]:

2.1.5 Mutation PROCESS and the result.

In [8]:
# Mutate ARG97 to GLY97

residue_to_mutate = 'resi 97'

cmd.wizard('mutagenesis')
cmd.refresh_wizard()
cmd.get_wizard().do_select('{0}'.format(residue_to_mutate))
cmd.get_wizard().set_mode('GLY')
cmd.refresh_wizard()
cmd.get_wizard().apply()
cmd.set_wizard()
In [9]:
# How does a protein mutant look like?

cmd.center('putative_protein')
cmd.zoom('putative_protein')
In [ ]:
cmd.set('ray_trace_mode', 1)
cmd.png('protein_with_mutation.png', '1920', '1080', ray=1)
In [32]:
IPython.display.Image('protein_with_mutation.png', retina=True)
Out[32]:

2.1.6 Displaying native protein and putative protein.

In [10]:
cmd.fetch('1lmp', 'native_protein')
cmd.remove('solvent')
cmd.select('putative_ligand', 'putative_protein and hetatm')
cmd.select('native_ligand', 'native_protein and hetatm')

cmd.show('cartoon', '(putative_protein or native_protein) and (not *ligand)')
cmd.colour('silver', 'putative_protein')
cmd.colour('skyblue', 'native_protein')

cmd.show('sticks', '*ligand')
cmd.colour('wheat', '*ligand')

cmd.show('sticks', residue_to_mutate)
cmd.colour('lightpink', 'native_protein and '+ residue_to_mutate)
cmd.colour('red', 'putative_protein and '+ residue_to_mutate)

cmd.translate('[-35, 0, 0]', 'native_protein')
cmd.center('putative_protein + native_protein')
cmd.zoom('putative_protein + native_protein')
In [11]:
cmd.set('ray_trace_mode', 1)
cmd.png('proteins_face_to_face.png', '1920', '1800', ray=1)
In [12]:
IPython.display.Image('proteins_face_to_face.png', retina=True)
Out[12]:

2.1.7 Creating a movie.

In [13]:
num_frames = 480

cmd.hide('cartoon', residue_to_mutate)
cmd.show('sticks', residue_to_mutate)

# Creating 480 blank frames
cmd.mset('1 x{0}'.format(int(num_frames)))

# Store initial position of camera and native_protein
cmd.frame('1')
cmd.mview('store')
cmd.mview('store', object='native_protein')

# Align two proteins
# Position of camera is not altered
# Native_protein slides along the X axis by 35 pt
cmd.frame('90')
cmd.mview('store')
cmd.translate('[35, 0, 0]', object='native_protein')
cmd.mview('store', object='native_protein')

# Close-up of the proteins aligned
# Position of camera changes
# Native_protein is still aligned
cmd.frame('210')
cmd.center('native_protein')
cmd.zoom('native_protein')
cmd.mview('store', object='native_protein')
cmd.mview('store')

# Close-up of the mutated residues
# Position of camera changes
# Native_protein is still aligned
cmd.frame('300')
cmd.mview('store', object='native_protein')
cmd.center(residue_to_mutate)
cmd.zoom(residue_to_mutate)
cmd.mview('store')

# Camera turns around Y axis by 135 degrees
# Native_protein is still aligned
cmd.frame('480')
cmd.mview('store', object='native_protein')
cmd.center(residue_to_mutate)
cmd.zoom(residue_to_mutate)
cmd.turn('y', 135)
cmd.mview('store')

# Fill in the gaps between the frames
cmd.mview('reinterpolate')
cmd.mview('reinterpolate', object='native_protein')

2.1.8 Saving 480 frames and creating a mocie with suitable codec.

In [14]:
import os

cmd.set('ray_trace_mode', 1)

for i in range(num_frames):
    cmd.frame(i)
    cmd.png(os.path.join(os.getcwd(), 'pics', '{0}.png'.format(str(i))), '1920', '1080', ray=1)
In [16]:
import subprocess
import os

where_am_i = os.getcwd()
# bash_command = 'ffmpeg -i "$where_am_i"/pics/%d.png -vcodec libx264 -crf 15 -hide_banner -loglevel panic "$where_am_i"/test.mp4'
subprocess.run(['ffmpeg', '-i', '"$where_am_i"/pics/%d.png', '-vcodec',
                 'libx264', '-crf', '15', '-hide_banner', '-loglevel',
                 'panic', '"$where_am_i"/test.mp4'], shell=True)

# %%bash
# where_am_i=`echo $PWD`
# ffmpeg -i "$where_am_i"/pics/%d.png -vcodec libx264 -crf 15 -hide_banner -loglevel panic "$where_am_i"/align.mp4
Out[16]:
CompletedProcess(args=['ffmpeg', '-i', '"$where_am_i"/pics/%d.png', '-vcodec', 'libx264', '-crf', '15', '-hide_banner', '-loglevel', 'panic', '"$where_am_i"/test.mp4'], returncode=1)

2.1.9 Enjoying the video!

In [1]:
from base64 import b64encode
from IPython.display import HTML

video = open('align.mp4', 'rb').read()
encoded = b64encode(video)
HTML(data='''<video width=700px controls>
<source src="data:video/mp4; base64,{0}" type="video/mp4" />
</video>'''.format(encoded.decode('ascii')))
Out[1]:

2.2 Attaching TAMRA to a protein.

2.2.1 Fetching TAMRA molecule.

In [18]:
cmd.reinitialize()
cmd.bg_color('white')
In [16]:
# Downloading tamra molecule structure

import urllib.request

file_name = 'tamra.sdf'
url = 'https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/cid/2762604/record/SDF/?response_type=save&record_type=3d'
urllib.request.urlretrieve(url, file_name)
Out[16]:
('tamra.sdf', <http.client.HTTPMessage at 0x7fd220834978>)
In [19]:
# TAMRA is not pdb-file, so atoms do not have ids we get used to.
# However there are ids!

# Removing the 4th id is the essential step, because the 4th id is an 
# oxygen atom, that prevents the formation of the future ester bond with a protein.

cmd.load(file_name, 'tamra')
cmd.remove('tamra and id 4')
cmd.select('acid', 'id 32')
cmd.util.cbas('tamra')
In [20]:
cmd.set('ray_trace_mode', 1)
cmd.png('tamra.png', '1920', '1800', ray=1)
In [21]:
IPython.display.Image('tamra.png', retina=True)
Out[21]:

2.2.2 Fetching a protein.

In [96]:
cmd.fetch('1lmp', 'native_protein')
cmd.remove('solvent')
cmd.show('cartoon', 'native_protein and (not hetatm)')
cmd.color('silver', 'native_protein and (not hetatm)')
cmd.color('wheat', 'hetatm')
cmd.util.cbas('tamra')
cmd.hide('cartoon', 'resi 81')
cmd.show('sticks', 'resi 81')
cmd.color('salmon', 'resi 81')
cmd.select('base', 'native_protein and resi 81 and n. OG')
cmd.util.cbaw('resi 81')
In [37]:
cmd.set('ray_trace_mode', 1)
cmd.png('base.png', '1920', '1800', ray=1)
In [38]:
IPython.display.Image('base.png', retina=True)
Out[38]:

2.2.3 Attaching TAMRA molecule.

In [97]:
cmd.fuse('acid', 'base')
In [92]:
# Command cmd.torsion() is an optional command, that
# can improve the view.

# cmd.torsion(80)
In [98]:
cmd.unpick()
In [99]:
cmd.set('ray_trace_mode', 1)
cmd.png('tamra_sealed.png', '1920', '1800', ray=1)
In [100]:
IPython.display.Image('tamra_sealed.png', retina=True)
Out[100]:

2.3 Creating alpha-helix of 100 alanines.

2.3.1 Creating one alanine residue.

In [3]:
cmd.reinitialize()
cmd.bg_color('white')
In [4]:
amino_acid = 'ala'
chain_length = 101

cmd.fragment('{0}'.format(str(amino_acid)))
cmd.show('sticks', '{0}'.format(str(amino_acid)))
cmd.util.cbas('{0}'.format(str(amino_acid)))

2.3.2 Attaching alanines one by one to the previous residue.

In [5]:
# It is the simpliest way to do this (define -ss option).
# Another way is to cmd.set_dihedral() PHI (approx. -60 degrees)
# and PSI (approx. -45 degrees)

for i in range(2, chain_length):
    cmd.edit('i. {0} and n. C'.format(int(i)))
    cmd.editor.attach_amino_acid('pk1', '{0}'.format(str(amino_acid)), ss=1)
In [8]:
cmd.center('pkmol')
cmd.zoom('pkmol')

cmd.hide('cartoon')
In [12]:
cmd.set('ray_trace_mode', 1)
cmd.png('poly_chain.png', '2200', '1800', ray=1)
In [13]:
IPython.display.Image('poly_chain.png', retina=True)
Out[13]:
In [14]:
# PHI angle
cmd.get_dihedral('i. 3 and n. C', 'i. 4 and n. N', 'i. 4 and n. CA', 'i. 4 and n. C')
Out[14]:
-56.9999885559082
In [15]:
# PSI angle
cmd.get_dihedral('i. 4 and n. N', 'i. 4 and n. CA', 'i. 4 and n. C', 'i. 5 and n. N')
Out[15]:
-47.000267028808594

An average sum of PHI and PSI angles values equals to -105 degrees (wiki).

In our example the sum equals to approximately -114 degrees.

2.4 Creating B-form of DNA.

2.4.1 Fetching an example of 'perfect DNA'.

In [19]:
cmd.reinitialize()
cmd.bg_color('white')
In [ ]:
dna_length = 101

cmd.fetch('1bna', 'perfect_dna')

2.4.2 Applying transformation matrix of one pair to newly created.

In [20]:
cmd.create('a_t', 'c. B and i. 20 + c. A and i. 5')
cmd.create('g_c', 'c. B and i. 21 + c. A and i. 4')

# '1' is used as a primer for next pairs 
cmd.create('1', 'a_t')
cmd.pair_fit('a_t', 'g_c')
transformation_matrix = cmd.get_object_matrix('a_t')

for i in range(2, dna_length):
    cmd.create('{0}'.format(i), '{0}'.format(i-1))
    cmd.transform_selection('{0}'.format(i), transformation_matrix)

2.4.3 Enjoying the beauty of the molecule!

In [21]:
cmd.remove('perfect_dna')

cmd.show('surface')
cmd.set('transparency', 0.2)
cmd.center('all')
cmd.zoom('all')
cmd.util.cbas('all')

####

# These commands do not work :(

# cmd.set('cartoon_tube_radius', 0.8) 

# cmd.set('cartoon_nucleic_acid_mode', 0)
# cmd.set('cartoon_ladder_mode', 1)
# cmd.set('cartoon_ring_mode', 2)
# cmd.set('cartoon_ring_finder', 1)

####
In [22]:
cmd.set('ray_trace_mode', 1)
cmd.png('dna_chain.png', '1920', '1800', ray=1)
In [24]:
IPython.display.Image('dna_chain.png', retina=True)
Out[24]: