Skip to content

Commit

Permalink
error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ljwoods2 committed Dec 4, 2024
1 parent 399228f commit 8721df8
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 77 deletions.
136 changes: 89 additions & 47 deletions activity/activity.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,21 @@
"\n",
"u = mda.Universe(\"sample_simulation/imdgroup.gro\", \"imd://localhost:9999\", buffer_size=100*1024**2)\n",
"\n",
"# Get an atom from the topology\n",
"# Universe -> AtomGroup -> Atom\n",
"atom = u.atoms[0]\n",
"print(\" time [ position ] [ velocity ] [ force ] [ box ]\")\n",
"\n",
"# Analysis loop\n",
"for ts in u.trajectory:\n",
" print(f'{ts.time:8.3f} {atom.position:8.3f} {atom.velocity:8.3f} {atom.force:8.3f} {u.dimensions[0:3]}')"
"try:\n",
" # Get an atom from the topology\n",
" # Universe -> AtomGroup -> Atom\n",
" atom = u.atoms[0]\n",
" print(\" time [ position ] [ velocity ] [ force ] [ box ]\")\n",
"\n",
" # Analysis loop\n",
"\n",
" for ts in u.trajectory:\n",
" print(f'{ts.time:8.3f} {atom.position} {atom.velocity} {atom.force} {u.dimensions[0:3]}')\n",
"\n",
"except Exception as e:\n",
" print(e)\n",
"finally:\n",
" u.trajectory.close()"
]
},
{
Expand All @@ -101,13 +108,21 @@
"\n",
"u = mda.Universe(\"sample_simulation/imdgroup.gro\", \"imd://localhost:9999\", buffer_size=100*1024**2)\n",
"\n",
"# Universe -> AtomGroup -> Atom\n",
"nter = u.select_atoms(\"resid 1 and name CA\").atoms[0]\n",
"cter = u.select_atoms(\"resid 129 and name CA\").atoms[0]\n",
"try:\n",
"\n",
" # Universe -> AtomGroup -> Atom\n",
" nter = u.select_atoms(\"resid 1 and name CA\").atoms[0]\n",
" cter = u.select_atoms(\"resid 129 and name CA\").atoms[0]\n",
"\n",
"\n",
"for ts in u.trajectory:\n",
" distance = np.sqrt(np.sum((nter.position - cter.position) ** 2))\n",
" print(f'At time {ts.time:4.3f} ps, the n-terminus and c-terminus are {distance:4.3f} Angstroms apart', end='\\r')"
" for ts in u.trajectory:\n",
" distance = np.sqrt(np.sum((nter.position - cter.position) ** 2))\n",
" print(f'At time {ts.time:4.3f} ps, the n-terminus and c-terminus are {distance:4.3f} Angstroms apart', end='\\r')\n",
"\n",
"except Exception as e:\n",
" print(e)\n",
"finally:\n",
" u.trajectory.close()"
]
},
{
Expand All @@ -132,22 +147,28 @@
"\n",
"u = mda.Universe(\"sample_simulation/imdgroup.gro\", \"imd://localhost:9999\", buffer_size=100*1024**2)\n",
"\n",
"nter = u.select_atoms(\"resid 1 and name CA\").atoms[0]\n",
"cter = u.select_atoms(\"resid 129 and name CA\").atoms[0]\n",
"\n",
"graph = LiveTimeseriesGraph(\n",
" time_window=1.0, # ps\n",
" dt=0.010, # ps\n",
" title='Distance vs. Time',\n",
" y_label='Distance (Å)',\n",
" legend_label='Nter-Cter',\n",
")\n",
" \n",
"for ts in u.trajectory:\n",
" distance = np.sqrt(np.sum((nter.position - cter.position) ** 2))\n",
" graph.update(ts.time, distance)\n",
"try:\n",
"\n",
"\n"
" nter = u.select_atoms(\"resid 1 and name CA\").atoms[0]\n",
" cter = u.select_atoms(\"resid 129 and name CA\").atoms[0]\n",
"\n",
" graph = LiveTimeseriesGraph(\n",
" time_window=1.0, # ps\n",
" dt=0.010, # ps\n",
" title='Distance vs. Time',\n",
" y_label='Distance (Å)',\n",
" legend_label='Nter-Cter',\n",
" )\n",
"\n",
"\n",
" for ts in u.trajectory:\n",
" distance = np.sqrt(np.sum((nter.position - cter.position) ** 2))\n",
" graph.update(ts.time, distance)\n",
"\n",
"except Exception as e:\n",
" print(e)\n",
"finally:\n",
" u.trajectory.close()"
]
},
{
Expand All @@ -169,17 +190,25 @@
"import MDAnalysis as mda\n",
"\n",
"u = mda.Universe(\"sample_simulation/imdgroup.gro\", \"imd://localhost:9999\", buffer_size=100*1024**2)\n",
"sel = u.select_atoms(\"not resname SOL\")\n",
"\n",
"desired_length = 10\n",
"i = 0\n",
"with mda.Writer(\"sample_simulation/protein.trr\", sel.atoms.n_atoms) as w:\n",
" for ts in u.trajectory:\n",
" w.write(sel.atoms)\n",
" i += 1\n",
" \n",
" if i == desired_length:\n",
" break\n"
"try:\n",
"\n",
" sel = u.select_atoms(\"not resname SOL\")\n",
"\n",
" desired_length = 10\n",
" i = 0\n",
" with mda.Writer(\"sample_simulation/protein.trr\", sel.atoms.n_atoms) as w:\n",
" for ts in u.trajectory:\n",
" w.write(sel.atoms)\n",
" i += 1\n",
" \n",
" if i == desired_length:\n",
" break\n",
"\n",
"except Exception as e:\n",
" print(e)\n",
"finally:\n",
" u.trajectory.close()"
]
},
{
Expand Down Expand Up @@ -243,10 +272,16 @@
"\n",
"u = mda.Universe(\"sample_simulation/imdgroup.gro\", \"imd://localhost:9999\", buffer_size=100*1024**2)\n",
"\n",
"## Graph setup (optional)\n",
"try:\n",
" ## Graph setup (optional)\n",
"\n",
"for ts in u.trajectory:\n",
" ## Your analysis code here"
" for ts in u.trajectory:\n",
" ## Your analysis code here\n",
"\n",
"except Exception as e:\n",
" print(e)\n",
"finally:\n",
" u.trajectory.close()"
]
},
{
Expand Down Expand Up @@ -317,16 +352,23 @@
"\n",
"u = mda.Universe(\"sample_simulation/imdgroup.gro\", \"imd://localhost:9999\", buffer_size=100*1024**2)\n",
"\n",
"## Graph setup (optional)\n",
"try:\n",
"\n",
" ## Graph setup (optional)\n",
"\n",
" for ts in u.trajectory:\n",
" ## Your analysis code here\n",
"\n",
"for ts in u.trajectory:\n",
" ## Your analysis code here"
"except Exception as e:\n",
" print(e)\n",
"finally:\n",
" u.trajectory.close()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "workshop",
"display_name": "imdclient-test",
"language": "python",
"name": "python3"
},
Expand All @@ -340,7 +382,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.7"
"version": "3.12.0"
}
},
"nbformat": 4,
Expand Down
74 changes: 44 additions & 30 deletions activity/solutions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,24 @@
"\n",
"u = mda.Universe(\"sample_simulation/imdgroup.gro\", \"imd://localhost:9999\", buffer_size=100*1024**2)\n",
"\n",
"graph = LiveTimeseriesGraph(\n",
" time_window=1.0, # ps\n",
" dt=0.010, # ps\n",
" title='Number of water molecules within 4 Angstroms of protein vs. Time',\n",
" y_label='Number of water molecules',\n",
")\n",
"\n",
"for ts in u.trajectory:\n",
" # Select all water molecules that are within 4 angstroms of a non-water (protein) molecule\n",
" sel = u.select_atoms(\"(resname SOL) and (around 4 not resname SOL)\")\n",
" graph.update(ts.time, len(sel))"
"try:\n",
"\n",
" graph = LiveTimeseriesGraph(\n",
" time_window=1.0, # ps\n",
" dt=0.010, # ps\n",
" title='Number of water molecules within 4 Angstroms of protein vs. Time',\n",
" y_label='Number of water molecules',\n",
" )\n",
"\n",
" for ts in u.trajectory:\n",
" # Select all water molecules that are within 4 angstroms of a non-water (protein) molecule\n",
" sel = u.select_atoms(\"(resname SOL) and (around 4 not resname SOL)\")\n",
" graph.update(ts.time, len(sel))\n",
"\n",
"except Exception as e:\n",
" print(e)\n",
"finally:\n",
" u.trajectory.close()"
]
},
{
Expand All @@ -56,30 +63,37 @@
"\n",
"u = mda.Universe(\"sample_simulation/imdgroup.gro\", \"imd://localhost:9999\", buffer_size=100*1024**2)\n",
"\n",
"graph = LiveTimeseriesGraph(\n",
" time_window=1.0, # ps\n",
" dt=0.010, # ps\n",
" title='Radius of gyration vs. Time',\n",
" y_label='Radius of gyration (Angstroms)',\n",
")\n",
"try:\n",
"\n",
" graph = LiveTimeseriesGraph(\n",
" time_window=1.0, # ps\n",
" dt=0.010, # ps\n",
" title='Radius of gyration vs. Time',\n",
" y_label='Radius of gyration (Angstroms)',\n",
" )\n",
"\n",
" sel = u.select_atoms(\"protein\")\n",
" total_mass = np.sum(u.atoms.masses)\n",
"\n",
"sel = u.select_atoms(\"protein\")\n",
"total_mass = np.sum(u.atoms.masses)\n",
" for ts in u.trajectory:\n",
" \n",
" center_of_mass = sel.center_of_mass()\n",
" pos = sel.positions\n",
" masses = sel.masses\n",
"\n",
"for ts in u.trajectory:\n",
" \n",
" center_of_mass = sel.center_of_mass()\n",
" pos = sel.positions\n",
" masses = sel.masses\n",
" ri_sq = np.square((pos - center_of_mass))\n",
" ri_sq = np.sum(ri_sq, axis=1)\n",
" sq = np.sum(ri_sq * masses)\n",
" sq = sq / total_mass\n",
"\n",
" ri_sq = np.square((pos - center_of_mass))\n",
" ri_sq = np.sum(ri_sq, axis=1)\n",
" sq = np.sum(ri_sq * masses)\n",
" sq = sq / total_mass\n",
" radius_of_gyration = np.sqrt(sq)\n",
"\n",
" radius_of_gyration = np.sqrt(sq)\n",
" graph.update(ts.time, radius_of_gyration)\n",
"\n",
" graph.update(ts.time, radius_of_gyration)"
"except Exception as e:\n",
" print(e)\n",
"finally:\n",
" u.trajectory.close()"
]
}
],
Expand Down

0 comments on commit 8721df8

Please sign in to comment.