Skip to content

Instantly share code, notes, and snippets.

@jaimergp
Created March 14, 2017 11:51
Show Gist options
  • Save jaimergp/926d395ceabeeb88c565469ff0f442b3 to your computer and use it in GitHub Desktop.
Save jaimergp/926d395ceabeeb88c565469ff0f442b3 to your computer and use it in GitHub Desktop.
Create movies from GAUDI solutions with pychimera & gaudiview
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"You have used an unregistered copy of Chimera for 152 days.\n",
"You can either register now by visiting:\n",
" http://www.cgl.ucsf.edu/cgi-bin/chimera_registration.py\n",
"or by choosing 'Registration...' from the 'Help' menu next\n",
"time you start Chimera with the gui enabled.\n",
"\n",
"Registration is free. By providing the information requested\n",
"you will be helping us document the impact this software is\n",
"having in the scientific community. The information you supply\n",
"will only be used for reporting summary statistics to NIH.\n"
]
}
],
"source": [
"# Run this cell to complete Chimera initialization\n",
"from pychimera import enable_chimera, enable_chimera_inline, chimera_view\n",
"enable_chimera()\n",
"enable_chimera_inline()\n",
"import chimera"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from gaudiview.extensions.gaudi import GaudiModel\n",
"from glob import glob\n",
"import Midas\n",
"import os\n",
"\n",
"# Monkey patch PIL to prevent DeprecationErrors\n",
"import PIL\n",
"def fromstring(*args, **kw):\n",
" return PIL.Image.frombytes(*args, **kw)\n",
"PIL.Image.fromstring = fromstring\n",
"\n",
"def log_progress(sequence, every=None, size=None, name='Items'):\n",
" \"\"\"\n",
" Taken from https://github.com/alexanderkuk/log-progress\n",
" \"\"\"\n",
" from ipywidgets import IntProgress, HTML, VBox\n",
" from IPython.display import display\n",
"\n",
" is_iterator = False\n",
" if size is None:\n",
" try:\n",
" size = len(sequence)\n",
" except TypeError:\n",
" is_iterator = True\n",
" if size is not None:\n",
" if every is None:\n",
" if size <= 200:\n",
" every = 1\n",
" else:\n",
" every = int(size / 200) # every 0.5%\n",
" else:\n",
" assert every is not None, 'sequence is iterator, set every'\n",
"\n",
" if is_iterator:\n",
" progress = IntProgress(min=0, max=1, value=1)\n",
" progress.bar_style = 'info'\n",
" else:\n",
" progress = IntProgress(min=0, max=size, value=0)\n",
" label = HTML()\n",
" box = VBox(children=[label, progress])\n",
" display(box)\n",
"\n",
" index = 0\n",
" try:\n",
" for index, record in enumerate(sequence, 1):\n",
" if index == 1 or index % every == 0:\n",
" if is_iterator:\n",
" label.value = '{name}: {index} / ?'.format(name=name, index=index)\n",
" else:\n",
" progress.value = index\n",
" label.value = u'{name}: {index} / {size}'.format(name=name, index=index,size=size)\n",
" yield record\n",
" except:\n",
" progress.bar_style = 'danger'\n",
" raise\n",
" else:\n",
" progress.bar_style = 'success'\n",
" progress.value = index\n",
" label.value = \"{name}: {index}\".format(name=name, index=str(index or '?'))"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"outputfile = \"test1vrh_OK/results/test.gaudi-output\"\n",
"ligand_reference_file = \"test1vrh_OK/ligand_reference.mol2\"\n",
"protein_reference_file = \"test1vrh_OK/protein.mol2\""
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"moviepath = os.path.join(os.path.dirname(outputfile), 'movie')\n",
"os.makedirs(moviepath)\n",
"ligand_ref = chimera.openModels.open(ligand_reference_file)[0]\n",
"protein_ref = chimera.openModels.open(protein_reference_file)[0]\n",
"model = GaudiModel(outputfile)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"sorted_by_rmsd = []\n",
"for key, score in model.data['GAUDI.results'].items():\n",
" mols, meta = model.parse_zip(os.path.join(model.basedir, key))\n",
" ligand, protein = sorted(mols, key=lambda m: m.numAtoms)\n",
" chimera.openModels.close([protein])\n",
" rmsd = Midas.rmsd(ligand_ref.atoms, ligand.atoms)\n",
" sorted_by_rmsd.append((rmsd, ligand))\n",
"\n",
"sorted_by_rmsd.sort(reverse=True) # Sort by RMSD from larger to smaller"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"ligand_ref.display = True\n",
"chimera.runCommand('focus #0 zr < 25')\n",
"chimera.runCommand('~show #{}'.format(protein_ref.id))"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"ligand_ref.display = False\n",
"for rmsd, ligand in sorted_by_rmsd:\n",
" ligand.display = False"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [],
"source": [
"# Screenshot every solution from larger RMSD to smaller RMSD, with depicted residues within 5A and ball-stick repr\n",
"total_solutions = len(sorted_by_rmsd)\n",
"for i, (rmsd, ligand) in enumerate(log_progress(sorted_by_rmsd, name=\"Solutions\")):\n",
" ligand.display = True\n",
" chimera.runCommand('repr bs #{}'.format(ligand.id))\n",
" chimera.runCommand('show #{} zr < 5'.format(ligand.id))\n",
" chimera.runCommand('copy file {}/frame_{:03d}.png width 800 height 600'.format(moviepath, i))\n",
" ligand.display = False\n",
"\n",
"# Zoom-in last frame\n",
"ligand.display = True\n",
"extent = chimera.viewer.camera.extent\n",
"for j in log_progress(range(20), name=\"Zooming\"):\n",
" chimera.viewer.camera.extent = extent - j*(extent/30)\n",
" chimera.runCommand('copy file {}/frame_{:03d}.png width 800 height 600'.format(moviepath, i+j))"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"ffmpeg version 3.2.2 Copyright (c) 2000-2016 the FFmpeg developers\n",
" built with gcc 6.2.1 (GCC) 20160830\n",
" configuration: --prefix=/usr --disable-debug --disable-static --disable-stripping --enable-avisynth --enable-avresample --enable-fontconfig --enable-gmp --enable-gnutls --enable-gpl --enable-ladspa --enable-libass --enable-libbluray --enable-libfreetype --enable-libfribidi --enable-libgsm --enable-libiec61883 --enable-libmodplug --enable-libmp3lame --enable-libopencore_amrnb --enable-libopencore_amrwb --enable-libopenjpeg --enable-libopus --enable-libpulse --enable-libschroedinger --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libv4l2 --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxvid --enable-netcdf --enable-shared --enable-version3 --enable-x11grab\n",
" libavutil 55. 34.100 / 55. 34.100\n",
" libavcodec 57. 64.101 / 57. 64.101\n",
" libavformat 57. 56.100 / 57. 56.100\n",
" libavdevice 57. 1.100 / 57. 1.100\n",
" libavfilter 6. 65.100 / 6. 65.100\n",
" libavresample 3. 1. 0 / 3. 1. 0\n",
" libswscale 4. 2.100 / 4. 2.100\n",
" libswresample 2. 3.100 / 2. 3.100\n",
" libpostproc 54. 1.100 / 54. 1.100\n",
"Input #0, image2, from '1vrh_movie/1vrh_frame_%3d.png':\n",
" Duration: 00:00:11.08, start: 0.000000, bitrate: N/A\n",
" Stream #0:0: Video: png, rgb24(pc), 800x600, 25 fps, 25 tbr, 25 tbn, 25 tbc\n",
"\u001b[1;36m[libx264 @ 0x55a4c81218e0] \u001b[0musing cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 AVX2 LZCNT BMI2\n",
"\u001b[1;36m[libx264 @ 0x55a4c81218e0] \u001b[0mprofile High, level 3.1\n",
"\u001b[1;36m[libx264 @ 0x55a4c81218e0] \u001b[0m264 - core 148 r2708 86b7198 - H.264/MPEG-4 AVC codec - Copyleft 2003-2016 - http://www.videolan.org/x264.html - options: cabac=1 ref=3 deblock=1:0:0 analyse=0x3:0x113 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=12 lookahead_threads=2 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1 b_bias=0 direct=1 weightb=1 open_gop=0 weightp=2 keyint=250 keyint_min=25 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=crf mbtree=1 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00\n",
"Output #0, mp4, to 'out.mp4':\n",
" Metadata:\n",
" encoder : Lavf57.56.100\n",
" Stream #0:0: Video: h264 (libx264) ([33][0][0][0] / 0x0021), yuv420p, 800x600, q=-1--1, 25 fps, 12800 tbn, 25 tbc\n",
" Metadata:\n",
" encoder : Lavc57.64.101 libx264\n",
" Side data:\n",
" cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: -1\n",
"Stream mapping:\n",
" Stream #0:0 -> #0:0 (png (native) -> h264 (libx264))\n",
"Press [q] to stop, [?] for help\n",
"frame= 553 fps=183 q=-1.0 Lsize= 1636kB time=00:00:22.00 bitrate= 609.3kbits/s dup=276 drop=0 speed=7.29x \n",
"video:1629kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.442380%\n",
"\u001b[1;36m[libx264 @ 0x55a4c81218e0] \u001b[0mframe I:4 Avg QP:21.67 size: 67808\n",
"\u001b[1;36m[libx264 @ 0x55a4c81218e0] \u001b[0mframe P:155 Avg QP:23.92 size: 6737\n",
"\u001b[1;36m[libx264 @ 0x55a4c81218e0] \u001b[0mframe B:394 Avg QP:21.29 size: 894\n",
"\u001b[1;36m[libx264 @ 0x55a4c81218e0] \u001b[0mconsecutive B-frames: 0.9% 9.8% 7.6% 81.7%\n",
"\u001b[1;36m[libx264 @ 0x55a4c81218e0] \u001b[0mmb I I16..4: 10.9% 28.0% 61.1%\n",
"\u001b[1;36m[libx264 @ 0x55a4c81218e0] \u001b[0mmb P I16..4: 0.4% 2.1% 2.6% P16..4: 8.7% 4.6% 3.9% 0.0% 0.0% skip:77.7%\n",
"\u001b[1;36m[libx264 @ 0x55a4c81218e0] \u001b[0mmb B I16..4: 0.1% 0.3% 0.3% B16..8: 6.4% 0.4% 0.2% direct: 0.4% skip:91.9% L0:54.9% L1:43.8% BI: 1.3%\n",
"\u001b[1;36m[libx264 @ 0x55a4c81218e0] \u001b[0m8x8 transform intra:37.4% inter:38.8%\n",
"\u001b[1;36m[libx264 @ 0x55a4c81218e0] \u001b[0mcoded y,uvDC,uvAC intra: 50.3% 57.0% 52.1% inter: 2.1% 2.9% 2.1%\n",
"\u001b[1;36m[libx264 @ 0x55a4c81218e0] \u001b[0mi16 v,h,dc,p: 55% 27% 12% 6%\n",
"\u001b[1;36m[libx264 @ 0x55a4c81218e0] \u001b[0mi8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 16% 16% 56% 2% 2% 1% 3% 2% 3%\n",
"\u001b[1;36m[libx264 @ 0x55a4c81218e0] \u001b[0mi4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 19% 20% 13% 6% 9% 9% 11% 7% 7%\n",
"\u001b[1;36m[libx264 @ 0x55a4c81218e0] \u001b[0mi8c dc,h,v,p: 56% 21% 16% 7%\n",
"\u001b[1;36m[libx264 @ 0x55a4c81218e0] \u001b[0mWeighted P-Frames: Y:0.0% UV:0.0%\n",
"\u001b[1;36m[libx264 @ 0x55a4c81218e0] \u001b[0mref P L0: 56.7% 11.5% 22.2% 9.6%\n",
"\u001b[1;36m[libx264 @ 0x55a4c81218e0] \u001b[0mref B L0: 77.1% 17.7% 5.2%\n",
"\u001b[1;36m[libx264 @ 0x55a4c81218e0] \u001b[0mref B L1: 98.2% 1.8%\n",
"\u001b[1;36m[libx264 @ 0x55a4c81218e0] \u001b[0mkb/s:603.10\n"
]
}
],
"source": [
"# Convert PNG frames to mp4 movies\n",
"os.chdir(moviepath)\n",
"!ffmpeg -i frame_%3d.png -filter:v \"setpts=2.0*PTS\" -c:v libx264 -pix_fmt yuv420p out.mp4"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"!xdg-open out.mp4"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.12"
},
"widgets": {
"state": {
"7cdebbc195ca4a4894e891a8a0067cf4": {
"views": [
{
"cell_index": 7
}
]
},
"7ffcdff9c3ef428bbf635b912375f738": {
"views": [
{
"cell_index": 7
}
]
}
},
"version": "1.2.0"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment