Last active
March 7, 2020 11:55
-
-
Save orbeckst/ddbe912c665340791752c6622575cc6a to your computer and use it in GitHub Desktop.
On-the-fly transformations blog post
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# OTF blog post " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"scrolled": true | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"application/vnd.jupyter.widget-view+json": { | |
"model_id": "a5c66d7395164a90a1e8b8b673f48756", | |
"version_major": 2, | |
"version_minor": 0 | |
}, | |
"text/plain": [ | |
"_ColormakerRegistry()" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"\n", | |
"import warnings\n", | |
"warnings.filterwarnings('ignore') # some attributes are missing \n", | |
"import MDAnalysis as mda\n", | |
"from MDAnalysis import transformations\n", | |
"import nglview as nv\n", | |
"\n", | |
"import MDAnalysisData\n", | |
"\n", | |
"import numpy as np\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Data" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"peptide = MDAnalysisData.datasets.fetch_membrane_peptide()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"scrolled": true | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"/Users/oliver/MDAnalysis_data/membrane_peptide/memb_pept.tpr\n", | |
"/Users/oliver/MDAnalysis_data/membrane_peptide/memb_pept.xtc\n" | |
] | |
} | |
], | |
"source": [ | |
"print(peptide.topology)\n", | |
"print(peptide.trajectory)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 73, | |
"metadata": { | |
"scrolled": true | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"application/vnd.jupyter.widget-view+json": { | |
"model_id": "62379b9de53747bebcc7f5b68254f4a6", | |
"version_major": 2, | |
"version_minor": 0 | |
}, | |
"text/plain": [ | |
"NGLWidget(max_frame=100)" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"u = mda.Universe(peptide.topology, peptide.trajectory)\n", | |
"u.transfer_to_memory(step=10)\n", | |
"view = nv.show_mdanalysis(u)\n", | |
"view.add_unitcell()\n", | |
"view.control.rotate(mda.lib.transformations.quaternion_from_euler(-np.pi/2, np.pi/3, np.pi/6, 'rzyz').tolist())\n", | |
"view.control.zoom(-0.3)\n", | |
"view" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 74, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"application/vnd.jupyter.widget-view+json": { | |
"model_id": "5f214def7db04db3bf3c6138108424d9", | |
"version_major": 2, | |
"version_minor": 0 | |
}, | |
"text/plain": [ | |
"IntProgress(value=0, description='Rendering ...')" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"from nglview.contrib.movie import MovieMaker\n", | |
"movie = MovieMaker(view, fps=24, in_memory=True, output='peptide_raw.gif')\n", | |
"movie.make()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Unwrap " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 101, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from MDAnalysis import transformations\n", | |
"\n", | |
"# a custom atom group can be passed as an argument. In this case we will use all the atoms\n", | |
"# in the Universe u\n", | |
"u = mda.Universe(peptide.topology, peptide.trajectory)\n", | |
"u.transfer_to_memory(step=10)\n", | |
"\n", | |
"ag = u.atoms\n", | |
"# we define the transformation\n", | |
"workflow = transformations.unwrap(ag)\n", | |
"\n", | |
"u.trajectory.add_transformations(workflow)\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 102, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"application/vnd.jupyter.widget-view+json": { | |
"model_id": "08ccd0c8a54444039c4fcde541a96fc7", | |
"version_major": 2, | |
"version_minor": 0 | |
}, | |
"text/plain": [ | |
"NGLWidget(max_frame=100)" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"view = nv.show_mdanalysis(u)\n", | |
"view.add_unitcell()\n", | |
"view.control.rotate(mda.lib.transformations.quaternion_from_euler(-np.pi/2, np.pi/3, np.pi/6, 'rzyz').tolist())\n", | |
"#view.control.zoom(-0.3)\n", | |
"view" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 103, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"application/vnd.jupyter.widget-view+json": { | |
"model_id": "93486664ac9444cb8347bbb2e5cdef9b", | |
"version_major": 2, | |
"version_minor": 0 | |
}, | |
"text/plain": [ | |
"IntProgress(value=0, description='Rendering ...')" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"movie = MovieMaker(view, fps=24, in_memory=True, output='peptide_wrapped.gif')\n", | |
"movie.make()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Center " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 85, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"u = mda.Universe(peptide.topology, peptide.trajectory)\n", | |
"u.transfer_to_memory(step=10)\n", | |
"\n", | |
"prot = u.select_atoms(\"protein\")\n", | |
"ag = u.atoms\n", | |
"# we will use mass as weights for the center calculation\n", | |
"workflow = (transformations.unwrap(ag),\n", | |
" transformations.center_in_box(prot, center='mass'),\n", | |
" transformations.wrap(ag, compound='fragments'))\n", | |
"u.trajectory.add_transformations(*workflow)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 86, | |
"metadata": { | |
"scrolled": true | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"application/vnd.jupyter.widget-view+json": { | |
"model_id": "25297a79008b4c01b2f77b029db4e1d6", | |
"version_major": 2, | |
"version_minor": 0 | |
}, | |
"text/plain": [ | |
"NGLWidget(max_frame=100)" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"view = nv.show_mdanalysis(u)\n", | |
"view.add_unitcell()\n", | |
"view.control.rotate(mda.lib.transformations.quaternion_from_euler(-np.pi/2, np.pi/3, np.pi/3, 'rzyz').tolist())\n", | |
"view" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 87, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"application/vnd.jupyter.widget-view+json": { | |
"model_id": "fbcf1a66f8e54b18a177aae631230e9c", | |
"version_major": 2, | |
"version_minor": 0 | |
}, | |
"text/plain": [ | |
"IntProgress(value=0, description='Rendering ...')" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"movie = MovieMaker(view, fps=24, in_memory=True, output='peptide_centered.gif')\n", | |
"movie.make()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## fit " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 89, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"u = mda.Universe(peptide.topology, peptide.trajectory)\n", | |
"u.transfer_to_memory(step=10)\n", | |
"\n", | |
"prot = u.select_atoms(\"protein\")\n", | |
"# we load another universe to define the reference\n", | |
"# it uses the same input files, but this doesn't have to be always the case\n", | |
"ref_u = u.copy()\n", | |
"reference = ref_u.select_atoms(\"protein\")\n", | |
"ag = u.atoms\n", | |
"workflow = (transformations.unwrap(ag),\n", | |
" transformations.center_in_box(prot, center='mass'),\n", | |
" transformations.wrap(ag, compound='fragments'),\n", | |
" transformations.fit_rot_trans(prot, reference))\n", | |
"u.trajectory.add_transformations(*workflow)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 90, | |
"metadata": { | |
"scrolled": true | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"application/vnd.jupyter.widget-view+json": { | |
"model_id": "486c7353c589485f95a881caddcd7f65", | |
"version_major": 2, | |
"version_minor": 0 | |
}, | |
"text/plain": [ | |
"NGLWidget(max_frame=100)" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"view = nv.show_mdanalysis(u)\n", | |
"view.add_unitcell()\n", | |
"view.control.rotate(mda.lib.transformations.quaternion_from_euler(-np.pi/2, np.pi/3, np.pi/3, 'rzyz').tolist())\n", | |
"view" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 91, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"application/vnd.jupyter.widget-view+json": { | |
"model_id": "a6586427d3944173bc8664fdc70f7781", | |
"version_major": 2, | |
"version_minor": 0 | |
}, | |
"text/plain": [ | |
"IntProgress(value=0, description='Rendering ...')" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"movie = MovieMaker(view, fps=24, in_memory=True, output='peptide_fitted.gif')\n", | |
"movie.make()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 92, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"application/vnd.jupyter.widget-view+json": { | |
"model_id": "a1562e6dc1d7439abf5ce687052c5454", | |
"version_major": 2, | |
"version_minor": 0 | |
}, | |
"text/plain": [ | |
"NGLWidget(max_frame=100)" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"w = nv.show_mdanalysis(prot)\n", | |
"w.add_line()\n", | |
"w.control.zoom(0.5)\n", | |
"w\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 93, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"application/vnd.jupyter.widget-view+json": { | |
"model_id": "670ba0b0afe241bbbac9ac1c73044be3", | |
"version_major": 2, | |
"version_minor": 0 | |
}, | |
"text/plain": [ | |
"IntProgress(value=0, description='Rendering ...')" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"movie = MovieMaker(w, fps=24, in_memory=True, output='peptideonly_fitted.gif')\n", | |
"movie.make()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### xy fit " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 94, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"u = mda.Universe(peptide.topology, peptide.trajectory)\n", | |
"u.transfer_to_memory(step=10)\n", | |
"\n", | |
"prot = u.select_atoms(\"protein\")\n", | |
"ref_u = u.copy()\n", | |
"reference = ref_u.select_atoms(\"protein\")\n", | |
"ag = u.atoms\n", | |
"workflow = (transformations.unwrap(ag),\n", | |
" transformations.center_in_box(prot),\n", | |
" transformations.wrap(ag, compound='fragments'),\n", | |
" transformations.fit_rot_trans(prot, reference, plane='xy', weights=\"mass\"))\n", | |
"u.trajectory.add_transformations(*workflow)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 95, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"application/vnd.jupyter.widget-view+json": { | |
"model_id": "0c4dd8aa4e23403c829ee7d16af3fc49", | |
"version_major": 2, | |
"version_minor": 0 | |
}, | |
"text/plain": [ | |
"NGLWidget(max_frame=100)" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"protein_P = u.select_atoms(\"protein or name P\")\n", | |
"view = nv.show_mdanalysis(protein_P)\n", | |
"view.add_line()\n", | |
"view.control.rotate(mda.lib.transformations.quaternion_from_euler(-np.pi/2, np.pi/3, 0, 'rzyz').tolist())\n", | |
"view\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 96, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"application/vnd.jupyter.widget-view+json": { | |
"model_id": "ad471e554ad8429587f4dbacfca25ee8", | |
"version_major": 2, | |
"version_minor": 0 | |
}, | |
"text/plain": [ | |
"IntProgress(value=0, description='Rendering ...')" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"movie = MovieMaker(view, fps=24, output='peptide_P_fitted_xy.gif')\n", | |
"movie.make()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## custom 2 " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 97, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def protein_up_by_2(ag):\n", | |
" def wrapped(ts):\n", | |
" # here's where the magic happens \n", | |
" # we create a numpy float32 array to avoid reduce floating\n", | |
" # point errors\n", | |
" ag.positions += np.asarray([0,0,20])\n", | |
" return ts\n", | |
" return wrapped" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 105, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"u = mda.Universe(peptide.topology, peptide.trajectory)\n", | |
"u.transfer_to_memory(step=10)\n", | |
"\n", | |
"ag = u.atoms\n", | |
"prot = u.select_atoms(\"protein\")\n", | |
"workflow = (transformations.unwrap(ag),\n", | |
" protein_up_by_2(prot),\n", | |
" transformations.wrap(ag, compound='fragments'))\n", | |
"u.trajectory.add_transformations(*workflow)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 107, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"application/vnd.jupyter.widget-view+json": { | |
"model_id": "d30a42df976148fba00107514ac54b48", | |
"version_major": 2, | |
"version_minor": 0 | |
}, | |
"text/plain": [ | |
"NGLWidget(max_frame=100)" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"view = nv.show_mdanalysis(u)\n", | |
"view.add_unitcell()\n", | |
"view.control.rotate(mda.lib.transformations.quaternion_from_euler(-np.pi/2, np.pi/3, np.pi/6, 'rzyz').tolist())\n", | |
"view.control.zoom(-0.3)\n", | |
"view" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 108, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"application/vnd.jupyter.widget-view+json": { | |
"model_id": "7ea1babf7204431aa6b3c420e15f58ef", | |
"version_major": 2, | |
"version_minor": 0 | |
}, | |
"text/plain": [ | |
"IntProgress(value=0, description='Rendering ...')" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"movie = MovieMaker(view, fps=24, output='peptide_up2.gif')\n", | |
"movie.make()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.7.6" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment