Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Existing traj integration #45

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions adaptivemd/analysis/pyemma/_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

def remote_analysis(
trajectories,
traj_name='output.dcd',
selection=None,
features=None,
topfile='input.pdb',
Expand All @@ -40,10 +39,7 @@ def remote_analysis(

Parameters
----------
trajectories : Sized of `Trajectory`
a list of `Trajectory` objects
traj_name : str
name of the trajectory file with the trajectory directory given
trajectories : Trajectory file paths
selection : str
an atom subset selection string as used in mdtraj .select
features : dict or list or None
Expand Down Expand Up @@ -122,8 +118,7 @@ def apply_feat_part(featurizer, parts):

print '#trajectories :', len(trajectories)

files = [os.path.join(t, traj_name) for t in trajectories]
inp = pyemma.coordinates.source(files, feat)
inp = pyemma.coordinates.source(trajectories, feat)

tica_obj = pyemma.coordinates.tica(
inp, lag=tica_lag, dim=tica_dim, kinetic_map=False)
Expand Down
15 changes: 13 additions & 2 deletions adaptivemd/analysis/pyemma/emma.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,23 @@ def execute(
# ups, one of the trajectories does not have the required type!
return

if len(set(traj.types[outtype].stride for traj in trajs)) > 1:
# using different strides in trajectories
return

if len(set(traj.types[outtype].selection for traj in trajs)) > 1:
# different selection strings among trajectories
return

ty = trajs[0].types[outtype]

traj_paths = []
for traj in trajs:
traj_paths.append(traj.file(traj.types[outtype].filename).path)

t.call(
remote_analysis,
trajectories=trajs,
traj_name=ty.filename, # we need the filename in the traj folder
trajectories=traj_paths,
selection=ty.selection, # tell pyemma the subsets of atoms
features=features,
topfile=input_pdb,
Expand Down
1 change: 1 addition & 0 deletions docs/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ Examples Notebooks
examples/example4
examples/example5
examples/example6
examples/example7
6 changes: 6 additions & 0 deletions docs/examples/example7.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.. _example7:

Example 7 - Miscellaneous
=========================

.. notebook:: examples/tutorial/7_example_misc.ipynb
305 changes: 305 additions & 0 deletions examples/tutorial/7_example_misc.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,305 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"source": [
"## Importing existing trajectory data"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"source": [
"In many cases, some trajectory data already exists before running an adaptive simulation. It is thus most efficiently to import this data into the framework. This works in principle by creating `Trajectory` objects and adding them to the `Project`. Since all of the trajectory-related data however is stored in the `Engine` object that generated it, this needs to be created as well."
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"source": [
"### Imports"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true,
"deletable": true,
"editable": true
},
"outputs": [],
"source": [
"import sys, os"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"outputs": [],
"source": [
"from adaptivemd import Project"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"source": [
"Let's open our `test` project by its name. If you completed the previous example this should all work out of the box."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"deletable": true,
"editable": true
},
"outputs": [],
"source": [
"project = Project('tutorial')"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"source": [
"Open all connections to the `MongoDB` and `Session` so we can get started."
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"source": [
"### Create an import `Engine`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true,
"deletable": true,
"editable": true
},
"outputs": [],
"source": [
"from adaptivemd import Trajectory\n",
"import time"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true,
"deletable": true,
"editable": true
},
"outputs": [],
"source": [
"pdb_file = File('file://init.pdb').named('initial_pdb').load()"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"source": [
"Since it is not desired to expand the trajectories at this point, system and integrator files are not given. In principle, if compatible restart files are available, one could create a complete engine and expand existing trajectories."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true,
"deletable": true,
"editable": true
},
"outputs": [],
"source": [
"import_engine = OpenMMEngine(pdb_file=pdb_file,\n",
" system_file=None,\n",
" integrator_file=None,\n",
" args=None\n",
" ).named('openmm-import')"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"source": [
"Now, to use the same `Modeller` as for the trajectories generated with `AdaptiveMD`, we build compatible output types. This means, they should contain the original file names with the respective strides and be named accordly. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true,
"deletable": true,
"editable": true
},
"outputs": [],
"source": [
"import_engine.add_output_type('master', 'old-file-name-full.dcd', \n",
" stride=stride_full)\n",
"import_engine.add_output_type('protein', 'old-file-name-protein.dcd', \n",
" stride=stride_prot, \n",
" selection='protein')"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"source": [
"## Initialize `Trajectory` objects\n",
"To add the actual file paths, `Trajectory` objects have to be initialized. \n",
"- `Trajectory` locations are folders, not files, and end with '/'.\n",
"- `frame` can be None if the initial frame is not known.\n",
"- `length` as defined by the engine time step, not by the output/save rate of an output type.\n",
"- `engine`: import engine defined above.\n",
"\n",
"The example below uses a list of trajectory folders to import, `existing_trajectory_paths`. Note that when adding this path with the `shared://` prefix, it must be a relative path in the root shared cluster file system. For absolute paths, use `worker://` instead. The trajectory lengths are known and stored in `existing_trajectory_lengths`.\n",
"\n",
"The `created` variable has to be set a creation time in order to let the database know the trajectory already exists. In the example below, the (arbitrary) import time is used."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true,
"deletable": true,
"editable": true
},
"outputs": [],
"source": [
"trajs = []\n",
"for traj_path, traj_length in zip(existing_trajectory_paths, \n",
" existing_trajectory_lengths):\n",
" traj = Trajectory('shared://' + traj_path,\n",
" frame=None,\n",
" length=traj_length,\n",
" engine=import_engine)\n",
" traj.created = time.time()\n",
" trajs.append(traj)"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"source": [
"#### Add the trajectories to the project"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true,
"deletable": true,
"editable": true
},
"outputs": [],
"source": [
"map(project.files.add, trajs)"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": true,
"editable": true
},
"source": [
"Let's check if the trajectories have been added:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true,
"deletable": true,
"editable": true
},
"outputs": [],
"source": [
"len(project.trajectories)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true,
"deletable": true,
"editable": true
},
"outputs": [],
"source": [
"project.close()"
]
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "py27_mar17",
"language": "python",
"name": "py27_mar17"
},
"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.13"
}
},
"nbformat": 4,
"nbformat_minor": 1
}