Skip to content

Instantly share code, notes, and snippets.

@jbarnoud
Created May 27, 2018 06:14
Show Gist options
  • Select an option

  • Save jbarnoud/cacd0957d3df01d1577f640b20e86039 to your computer and use it in GitHub Desktop.

Select an option

Save jbarnoud/cacd0957d3df01d1577f640b20e86039 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# How does `trjcat` reacts in different scenarios?"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"ExecuteTime": {
"end_time": "2018-05-27T06:13:07.419139Z",
"start_time": "2018-05-27T06:13:07.041727Z"
}
},
"outputs": [],
"source": [
"import subprocess\n",
"import numpy as np\n",
"import MDAnalysis as mda\n",
"import os"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"ExecuteTime": {
"end_time": "2018-05-27T06:13:07.428620Z",
"start_time": "2018-05-27T06:13:07.420471Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"'0.18.1-dev'"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mda.__version__"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"ExecuteTime": {
"end_time": "2018-05-27T06:13:07.485535Z",
"start_time": "2018-05-27T06:13:07.430012Z"
}
},
"outputs": [],
"source": [
"def build_trajectories(*sequences):\n",
" \"\"\"\n",
" Expose trjcat behavior for a given scenario.\n",
" \n",
" A scenario is given as a series of time sequences. The result is\n",
" returned as a list of time and origin of the frame. Each element of that\n",
" result list correspond to a frame of the conatenated trajectory, The\n",
" first element of each tuple is the time it corresponds to, the second\n",
" element is the index of the input the frame comes from.\n",
" \"\"\"\n",
" template = 'trjcat_test{}.xtc'\n",
" final_name = 'trjcat_test_cat.xtc'\n",
" \n",
" # Use an empty universe to have a topology\n",
" utop = mda.Universe.empty(1, trajectory=True)\n",
" \n",
" # Create synthetic trajectories. The times come from the user input,\n",
" # the coordinates indicate the index of the input.\n",
" for index, subseq in enumerate(sequences):\n",
" coords = np.zeros((len(subseq), 1, 3), dtype=np.float32) + index\n",
" u = mda.Universe(utop._topology, coords)\n",
" out_traj = mda.coordinates.XTC.XTCWriter(template.format(index),\n",
" n_atoms=len(u.atoms))\n",
" with out_traj:\n",
" for ts, time in zip(u.trajectory, subseq):\n",
" # The time step needs a box to avoid a warning\n",
" ts.dimensions = [10, 10, 10, 90, 90, 90]\n",
" # The time is set from the user input\n",
" ts.time = time\n",
" out_traj.write(ts)\n",
" \n",
" subprocess.call(['gmx', 'trjcat', '-o', final_name, '-f']\n",
" + [template.format(index)\n",
" for index, _ in enumerate(sequences)])\n",
" u = mda.Universe(utop._topology, final_name)\n",
" final_seq = [(ts.time, int(ts.positions[0, 0])) for ts in u.trajectory]\n",
" \n",
" # Clean the files\n",
" del u # Maybe we can try to close the file before deleting it?\n",
" for index, _ in enumerate(sequences):\n",
" os.remove(template.format(index))\n",
" os.remove(final_name)\n",
" # Clean the offset to avoid a warning\n",
" os.remove('.{}_offsets.npz'.format(final_name))\n",
" \n",
" return final_seq"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"ExecuteTime": {
"end_time": "2018-05-27T06:13:07.522604Z",
"start_time": "2018-05-27T06:13:07.487276Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"[(0.0, 0), (1.0, 0), (2.0, 0), (3.0, 0), (5.0, 1), (6.0, 1), (7.0, 1)]"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"build_trajectories([0, 1, 2, 3], [5, 6, 7])"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"ExecuteTime": {
"end_time": "2018-05-27T06:13:07.577814Z",
"start_time": "2018-05-27T06:13:07.527599Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"[(0.0, 0),\n",
" (1.0, 0),\n",
" (2.0, 1),\n",
" (3.0, 1),\n",
" (4.0, 2),\n",
" (5.0, 2),\n",
" (6.0, 2),\n",
" (7.0, 2)]"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"build_trajectories([0, 1, 2, 3], [2, 3, 4, 5], [4, 5, 6, 7])"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"ExecuteTime": {
"end_time": "2018-05-27T06:13:07.631861Z",
"start_time": "2018-05-27T06:13:07.580580Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"[(0.0, 1), (1.0, 1), (2.0, 1), (4.0, 1)]"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"build_trajectories([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 4])"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"ExecuteTime": {
"end_time": "2018-05-27T06:13:07.739296Z",
"start_time": "2018-05-27T06:13:07.637023Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"[(0.0, 1),\n",
" (1.0, 1),\n",
" (2.0, 1),\n",
" (3.0, 1),\n",
" (4.0, 1),\n",
" (5.0, 1),\n",
" (6.0, 1),\n",
" (7.0, 1),\n",
" (8.0, 1),\n",
" (9.0, 1)]"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"build_trajectories([0, 1, 2, 4], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"ExecuteTime": {
"end_time": "2018-05-27T06:13:07.799234Z",
"start_time": "2018-05-27T06:13:07.745348Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"[(0.0, 0),\n",
" (1.0, 0),\n",
" (2.0, 3),\n",
" (3.0, 2),\n",
" (5.0, 4),\n",
" (6.0, 4),\n",
" (7.0, 4),\n",
" (8.0, 4),\n",
" (9.0, 4)]"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"build_trajectories([0, 1, 2, 3], [2], [3], [2, 3, 4, 5, 6], [5, 6, 7, 8, 9])"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"ExecuteTime": {
"end_time": "2018-05-27T06:13:07.863896Z",
"start_time": "2018-05-27T06:13:07.803406Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"[(0.0, 0),\n",
" (1.0, 0),\n",
" (2.0, 3),\n",
" (3.0, 1),\n",
" (5.0, 4),\n",
" (6.0, 4),\n",
" (7.0, 4),\n",
" (8.0, 4),\n",
" (9.0, 4)]"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"build_trajectories([0, 1, 2, 3], [3], [2], [2, 3, 4, 5, 6], [5, 6, 7, 8, 9])"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"ExecuteTime": {
"end_time": "2018-05-27T06:13:07.912673Z",
"start_time": "2018-05-27T06:13:07.867085Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"[(0.0, 0),\n",
" (1.0, 0),\n",
" (2.0, 1),\n",
" (3.0, 1),\n",
" (4.0, 1),\n",
" (5.0, 2),\n",
" (6.0, 2),\n",
" (7.0, 2),\n",
" (8.0, 2),\n",
" (9.0, 2)]"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"build_trajectories([0, 1, 2, 3], [2, 3, 4, 5, 6], [5, 6, 7, 8, 9])"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"ExecuteTime": {
"end_time": "2018-05-27T06:13:07.969307Z",
"start_time": "2018-05-27T06:13:07.915906Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"[(0.0, 2),\n",
" (1.0, 2),\n",
" (2.0, 1),\n",
" (3.0, 1),\n",
" (4.0, 1),\n",
" (5.0, 0),\n",
" (6.0, 0),\n",
" (7.0, 0),\n",
" (8.0, 0),\n",
" (9.0, 0)]"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"build_trajectories([5, 6, 7, 8, 9], [2, 3, 4, 5, 6], [0, 1, 2, 3])"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"ExecuteTime": {
"end_time": "2018-05-27T06:13:08.030434Z",
"start_time": "2018-05-27T06:13:07.975922Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"[(0.0, 0),\n",
" (1.0, 0),\n",
" (2.0, 0),\n",
" (3.0, 0),\n",
" (5.0, 1),\n",
" (6.0, 1),\n",
" (7.0, 1),\n",
" (8.0, 1),\n",
" (9.0, 1)]"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"build_trajectories([0, 1, 2, 3], [5, 6, 7, 8, 9])"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"ExecuteTime": {
"end_time": "2018-05-27T06:13:08.064889Z",
"start_time": "2018-05-27T06:13:08.033557Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"[(0.0, 0),\n",
" (1.0, 0),\n",
" (2.0, 0),\n",
" (3.0, 0),\n",
" (4.0, 1),\n",
" (5.0, 2),\n",
" (6.0, 2),\n",
" (7.0, 2),\n",
" (8.0, 2),\n",
" (9.0, 2)]"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"build_trajectories([0, 1, 2, 3], [4], [5, 6, 7, 8, 9])"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"ExecuteTime": {
"end_time": "2018-05-27T06:13:08.132904Z",
"start_time": "2018-05-27T06:13:08.067214Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"[(0.0, 8), (1.0, 8), (2.0, 8), (3.0, 8)]"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"build_trajectories([0], [0, 1, 2, 3], [0], [0], [0, 1, 2, 3], [0], [0], [0], [0, 1, 2, 3])"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"ExecuteTime": {
"end_time": "2018-05-27T06:13:08.183070Z",
"start_time": "2018-05-27T06:13:08.135039Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"[(0.0, 0), (1.0, 1)]"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"build_trajectories([0, 1, 2, 3], [1])"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"ExecuteTime": {
"end_time": "2018-05-27T06:13:08.219324Z",
"start_time": "2018-05-27T06:13:08.185573Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"[(0.0, 0), (1.0, 0), (2.0, 0), (3.0, 1)]"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"build_trajectories([0, 1, 2, 3], [3])"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"ExecuteTime": {
"end_time": "2018-05-27T06:13:08.281314Z",
"start_time": "2018-05-27T06:13:08.232134Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"[(0.0, 0),\n",
" (1.0, 0),\n",
" (2.0, 1),\n",
" (3.0, 1),\n",
" (4.0, 3),\n",
" (5.0, 2),\n",
" (6.0, 2),\n",
" (7.0, 2),\n",
" (8.0, 4)]"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"build_trajectories([0, 1, 2, 3], [2, 3, 4, 5, 6], [5, 6, 7, 8, 9], [4], [8])"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"ExecuteTime": {
"end_time": "2018-05-27T06:13:08.336350Z",
"start_time": "2018-05-27T06:13:08.284910Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"[(0.0, 4), (1.0, 4), (2.0, 4), (3.0, 4)]"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"build_trajectories(*([[0, 1, 2, 3]] * 5))"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"ExecuteTime": {
"end_time": "2018-05-27T06:13:08.390264Z",
"start_time": "2018-05-27T06:13:08.338545Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"[(0.0, 1), (1.0, 1), (2.0, 1), (3.0, 1), (5.0, 2), (6.0, 2), (7.0, 2)]"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"build_trajectories([0, 1, 2, 3, 4], [0, 1, 2, 3], [5, 6, 7])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"hide_input": false,
"kernelspec": {
"display_name": "apricot (py3.6.2)",
"language": "python",
"name": "apricot"
},
"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.6.3"
},
"toc": {
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"toc_cell": false,
"toc_position": {},
"toc_section_display": "block",
"toc_window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment