Compile LeapPython and Manipulate protein structure with Leap Motion.

I was interested in Kinect as input device, because Kinect can detect motion as input it feels feature. 😉
And some days ago, I got new input device “Leap Motion”.
The Leap Motion controller is a small USB peripheral device which is designed to be placed on a physical desktop, facing upward. ( from wiki )
I want to use this device with PyMol!
Let’s try it.
At first to use Leap Motion from python, user need to get Leapmotion SDK. My environment is OSX, so I got SDK v2.3 from developer site.
Installation is little bit complicated for me, because I want to use LeapPython library from virtual environment but the library is linked with native python.
So, I build or change dynamic link following procedure.

In Python2.7, I changed link with following command.

iwatobipen$ cd /somepath/LeapSDK/lib
iwatobipen$ otool -L
	/Library/Frameworks/Python.framework/Versions/2.7/Python (compatibility version 2.7.0, current version 2.7.0)
	@loader_path/libLeap.dylib (compatibility version 0.7.0, current version 2.3.1)
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1197.1.1)
	/usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 120.0.0)
# change Python link from native to anaconda
iwatobipen$ install_name_tool -change /Library/Frameworks/Python.framework/Versions/2.7/Python /Users/iwatobipen/.pyenv/versions/anaconda-2.4.0/lib/libpython2.7.dylib

In Python3.5, I compiled using swig.
Newest swig will cause error, so I used swig version 3.0.3
I referred following url to do that.
OK Let’s go

# Copy Leap.h, LeapMath.h, Leap.i, and libLeap.dylib into one folder. And type following command.
# Generate LeapPython.cpp withswig -c++ -python -o LeapPython.cpp -interface LeapPython Leap.i
iwatobipen$ swig -c++ -python -o LeapPython.cpp -interface LeapPython Leap.i
# Compile and link 
clang++ -arch x86_64 -I/Users/iwatobipen/.pyenv/versions/anaconda-2.4.0/include/python2.7 LeapPython.cpp libLeap.dylib /Users/iwatobipen/.pyenv/versions/anaconda-2.4.0/lib/libpython2.7.dylib -shared -o

Now I got linked each python version in anaconda.

Unfortunately, I could not install pymol in python3.5. Following code was run in python2.7. I installed pymol by using conda install command.
Pymol Wiki provided sample code to use Leap Motion from pymol. I used the example. I added “path” which placed

import sys
import math
from pymol import cmd
sys.path.insert( 0, '/Users/iwatobipen/develop/py2env/LeapSDK/build/')

import Leap
from Leap import Matrix, Vector

class PymolListener(Leap.Listener):
    def __init__(self, *args, **kwargs):
        super(PymolListener, self).__init__(*args, **kwargs)

        self.prev_frame = None

        self.controller = Leap.Controller()

    def __del__(self):

        super(PymolListener, self).__del__()

    def update_view(self, frame):
        if not self.prev_frame:

        view = list(cmd.get_view())

        if frame.rotation_probability(self.prev_frame) > 0.1:
            m = frame.rotation_matrix(self.prev_frame)
            m *= Matrix(Vector(*view[0:3]),
            view[:9] = m.to_array_3x3()

        if frame.scale_probability(self.prev_frame) > 0.1:
            s = frame.scale_factor(self.prev_frame)
            delta_z = math.log(s) * 100.0
            view[11] += delta_z
            view[15] -= delta_z
            view[16] -= delta_z


    def on_frame(self, controller):
        frame = controller.frame()


        self.prev_frame = frame

listener = PymolListener()

Then run pymol and fetched sample pdb.

iwatobipen$ pymol
# from pymolconsole.
pymol> fetch 1atp
pymol> run

Result is …. It seems work well. Now, I am reading API reference to write code.

Call rdkit from pymol

PyMol is one of major tool for visualisation of protein, ligand, etc.
You know ‘Py’ means that pymol is written using python.
I want to embed pymol to web app but it’s difficult for me (technical reason..)
If anyone have advice to do it, please leave comment. 😉

Next, I have one question that, pymol can call rdkit ?
If it possible, it will be good information for me.
Think drug design based 3D structure is key for design, and lots of tools for visualise 3D structures.
….But complicated tools is not familiar for medchem.(really ?)
I wrote simple test script to call rdkit from pymol.
Script is following.

 from rdkit import Chem
 from rdkit.Chem import AllChem
 from pymol import cmd
 #from __future__ import print_function
 mol = Chem.MolFromSmiles( 'CCCS(=O)(=O)Nc1ccc(F)c(c1F)C(=O)c2c[nH]c3c2cc(cn3    )c4ccc(Cl)cc4' )
 hmol = Chem.AddHs( mol )
 AllChem.EmbedMolecule( hmol )
 AllChem.MMFFOptimizeMolecule( hmol )    
 #print( hmol.GetNumConformers( ))
 pdbstring = Chem.MolToPDBBlock( hmol )
 cmd.read_pdbstr( pdbstring, 'vemrafenib' )

Next, run script via pymol

iwatobipen$ pymol 
 PyMOL(TM) Molecular Graphics System, Version
 Copyright (c) Schrodinger, LLC.
 All Rights Reserved.

Work fine.!
Screen Shot 2015-11-23 at 10.08.14 PM
I’ll research more details about pymol api.

I went to tobuzoo yesterday.
I and my family enjoyed.