Saturday, August 6, 2011

PyMOL (1)


If you're interested in Python and bioinformatics you've undoubtedly heard of Pymol.

If you're at a place that cares about molecular biology (my institution does not), you may even have a site license. I have a copy that I got for educational use about 3 years ago, and still have some examples that I made for a class I taught. That version has a Cocoa GUI.

There is also an open source version that is up on SourceForge (here). I thought it would be a good challenge to try to build the open source Pymol.

[ Update: I think the Cocoa GUI one is MacPymol here). I just noticed this on the legacy page. ]

The dependencies are listed in the README as:


PyMOL has the following external dependencies:
1. OpenGL
2. GLUT library for OpenGL (or freeglut)
3. Python (v 2.1 or better) compiled with threads support
4. libpng (can compiled without it, but image saves won't work)
5. Tcl/Tk (for the external GUI)
6. Numerical Python (technically optional - now using numpy instead)
7. Python megawidgets (Pmw -- only required for the "external" GUI)
8. FreeType2 (if you want nice fonts)


Most of these are already present in Lion, so I didn't think it would be too difficult. What I failed to realize is that the Tcl/Tk in Lion is apparently messed up (brief SO discussion here).

I downloaded pymol-v1.4.1.tar.bz2 from the main page at SourceForge, and did the standard distutils thing:

python setup.py build

The first error was:

layer0/ShaderMgr.c:173: error: ‘GLEW_OK’ undeclared (first use in this function)

The top Google hit shows it's related to OpenGL and glut.
If present, there should be a file glew.h (and likely glut.h).

> find /usr -iname *glut*
/usr/X11/include/GL/glut.h
/usr/X11/include/GL/glutf90.h
/usr/X11/lib/libglut.3.7.dylib
/usr/X11/lib/libglut.3.dylib
/usr/X11/lib/libglut.dylib
/usr/X11/lib/pkgconfig/glut.pc
> find /usr -iname *glew*
> find /usr -iname *GLEW*
>

No glew.

brew install glew

works and we have


$ find /usr/local/lib -iname *glew*
/usr/local/lib/libGLEW.1.5.8.dylib
/usr/local/lib/libGLEW.1.5.dylib
/usr/local/lib/libGLEW.a
/usr/local/lib/libGLEW.dylib
/usr/local/lib/pkgconfig/glew.pc

and we find "GLEW_OK defined on line 15273 of glew.h. Unfortunately, the build fails with a link error at the end:


ld: warning: ignoring file /usr/local/lib/libGLEW.dylib, file was built for unsupported file format which is not the architecture being linked (i386)
ld: duplicate symbol __CShaderMgr in build/temp.macosx-10.7-intel-2.7/layer2/RepCylBond.o and build/temp.macosx-10.7-intel-2.7/layer2/RepCartoon.o for architecture i386

> file /usr/local/lib/libGLEW.dylib
/usr/local/lib/libGLEW.dylib: Mach-O 64-bit dynamically linked shared library x86_64


So it seems that Homebrew has built x86_64, which can't work with what pymol is building (i386). What I should do is rebuild libGLEW.dylib with the correct architecture. I haven't accomplished that yet. Instead, I noticed a page that discusses this very same problem. One answer is in a different thread (here):

Kasper, not sure if this will help you, but I think the GLEW stuff was
added recently in V1.4.x and it seems to be still somewhat experimental.
May be to get you started you could switch to v1.3r1 from the repository
and try to embedd that version. V1.3.x should not contain the new GL
stuff.


So let's try 1.3. I grabbed pymol-1.3r2-src.tar.bz2 from here and it got past the previous problem but then dies with:

/usr/X11R6/include/ft2build.h:56:38: error: freetype/config/ftheader.h: No such file or directory

It's looking in /usr/X11R6 and can't find freetype which is in /usr/X11 (my post here).

So.. I modify the setup.py in pymol:

                     
inc_dirs=["ov/src",
"layer0","layer1","layer2",
"layer3","layer4","layer5",
"/usr/X11R6/include",
# my additions
"/usr/X11/include",
"/usr/X11/include/freetype2",
"/usr/local/include/GL",

EXT+"/include",
EXT+"/include/GL",
EXT+"/include/freetype2",
"modules/cealign/src",
"modules/cealign/src/tnt",
]
libs=[]
pyogl_libs = []
lib_dirs=[]
def_macros=[("_PYMOL_MODULE",None),
("_PYMOL_LIBPNG",None),
("_PYMOL_FREETYPE",None),
]
ext_comp_args=[]
ext_link_args=[
"-L/usr/X11R6/lib", "-lGL", "-lXxf86vm",
"-L"+EXT+"/lib", "-lpng", "-lglut", "-lfreetype",
# my additions
"-lGLEW"
]


And now it builds.

But.. after doing ./pymol the GUI hangs. I think it's Python (IDLE?), which I never use.

However, I can get pymol to run from the command line with no GUI:

> pymol -c
PyMOL(TM) Molecular Graphics System, Version 1.3.
..
Command mode. No graphics front end.
Detected 2 CPU cores. Enabled multithreaded rendering.
PyMOL: normal program termination.

I ran a short script and made a figure, it looks fine.

So then, we get wise words from Ned Deily (here), and see (here)

- You should not rely on the Apple-suppled Pythons if you want to use IDLE.


I thought about putting Tcl/Tk 8.4 in a different place, etc.

But finally I said (f*** it) and I reformatted my drive to add a new partition, and then used my USB flash drive with the Lion Installer (post here), and then I installed MacPorts and did

$ sudo port install pymol
---> Computing dependencies for pymol
---> Dependencies to be installed: freetype bzip2 zlib glew libpng mesa makedepend pkgconfig glib2 autoconf help2man gettext expat libiconv gperf ncurses ncursesw p5-locale-gettext perl5 perl5.12 perl5 perl5 m4 automake libtool xorg-xproto py27-libxml2 libxml2 libxml2 python27 db46 gdbm openssl python_select readline sqlite3 python27 xorg-dri2proto xorg-glproto xorg-libXfixes xorg-fixesproto xorg-libX11 xorg-bigreqsproto xorg-inputproto xorg-kbproto xorg-libXau xorg-libXdmcp xorg-libxcb xorg-libpthread-stubs xorg-xcb-proto xorg-util-macros xorg-xcmiscproto xorg-xextproto xorg-xf86bigfontproto xorg-xtrans xorg-libXi xorg-libXext xorg-libXmu xorg-libXt xorg-libsm xorg-libice py26-numeric python26 py26-pmw py26-tkinter tk Xft2 fontconfig xrender xorg-renderproto tcl xorg-libXScrnSaver xorg-scrnsaverproto xdpyinfo xorg-libXinerama xorg-xineramaproto xorg-libXtst xorg-recordproto xorg-libXxf86vm xorg-xf86vidmodeproto xorg-libdmx xorg-dmxproto

Incredible! Everything but the kitchen sink. Perhaps even more incredible, it worked. An hour or two later, we finish with:


---> Attempting to fetch pymol-1.4_2.darwin_11.x86_64.tbz2 from http://packages.macports.org/pymol
---> Fetching pymol
---> Verifying checksum(s) for pymol
---> Extracting pymol
---> Applying patches to pymol
---> Configuring pymol
---> Building pymol
---> Staging pymol into destroot
---> Installing pymol @1.4_2
---> Activating pymol @1.4_2
---> Cleaning pymol
$

I got 1LMB.pdb from the PDB, and I ran this script with pymol script.pml:

load ~/Desktop/1LMB.PDB
hide everything
select D1, chain 1
select D2, chain 2
select R1, chain 3
select R2, chain 4
deselect all
show sticks, D1
show sticks, D2
show cartoon, R1
show cartoon, R2
color red, R1
zoom active, -10
move x, 4
bg_color white
ray 1200,1200
png ~/Desktop/x.png

and I got what's at the top of the post. Pretty cool! More later on this topic.