Esempio n. 1
0
    def file(self, f):
        """
        Return a file location to a file inside the trajectory folder

        Parameters
        ----------
        f : str or `OutputTypeDescription`
            the filename to be appended to the trajectories directory

        Returns
        -------
        `File`
            the object containing the location

        """
        if isinstance(f, six.string_types):
            return File(os.path.join(self.location, f))
        elif isinstance(f, OutputTypeDescription):
            return self.file(f.filename)
Esempio n. 2
0
# You should have received a copy of the GNU Lesser General Public
# License along with MDTraj. If not, see <http://www.gnu.org/licenses/>.
##############################################################################
from __future__ import absolute_import

import os
import ujson

# from adaptivemd.task import PythonTask
from adaptivemd.file import Location, File
from adaptivemd.engine import (Engine, Frame, Trajectory,
                               TrajectoryGenerationTask,
                               TrajectoryExtensionTask)

exec_file = File(
    'file://' +
    os.path.join(os.path.dirname(__file__), 'openmmrun.py')).load()


class OpenMMEngine(Engine):
    """
    OpenMM Engine to be used by Adaptive MD

    Attributes
    ----------
    system_file : `File`
        the system.xml file for OpenMM
    integrator_file : `File`
        the integrator.xml file for OpenMM
    pdb_file : `File`
        the .pdb file for the topology
Esempio n. 3
0
    def run(self,
            target,
            resource_name=None,
            export_path=None,
            cpu_threads=1,
            gpu_contexts=0,
            mpi_rank=0):

        t = TrajectoryGenerationTask(self,
                                     target,
                                     cpu_threads=cpu_threads,
                                     gpu_contexts=gpu_contexts,
                                     mpi_rank=mpi_rank)

        if resource_name is None:
            resource_name = list()

        elif isinstance(resource_name, str):
            resource_name = [resource_name]

        assert isinstance(resource_name, list)

        t.resource_name = resource_name

        if export_path:
            t.append(export_path)

        initial_pdb = t.pre_link(self['pdb_file_stage'],
                                 Location('initial.pdb'))
        t.pre_link(self['system_file_stage'])
        t.pre_link(self['integrator_file_stage'])
        t.pre_link(self['_executable_file_stage'], hard=True)

        if target.frame in [self['pdb_file'], self['pdb_file_stage']]:
            input_pdb = initial_pdb

        elif isinstance(target.frame, File):
            loc = Location('coordinates.%s' % target.frame.extension)
            input_pdb = t.get(target.frame, loc)

        elif isinstance(target.frame, Frame):
            input_traj = t.pre_link(target.frame.trajectory, 'source/')
            input_pdb = File('input.pdb')

            # frame index is in canonical stride = 1
            # we need to figure out which frame in the traj this actually is
            # also, we need a traj with full coordinates / selection = None

            ty, idx = target.frame.index_in_outputs

            if ty is None:
                # cannot use a trajectory where we do not have full coordinates
                return

            cmd = (
                'mdconvert -o {target} -i {index} -t {pdb} {source}'.format(
                    target=input_pdb,  # input.pdb is used as starting structure
                    index=idx,  # the index from the source trajectory
                    pdb=initial_pdb,  # use the main pdb
                    source=input_traj.outputs(ty)))  # we pick output ty

            t.pre.append(cmd)

        else:
            # for now we assume that if the initial frame is None or
            # not specific use the engines internal. That should be changed
            # todo: Raise exception here

            return

        # this represents our output trajectory
        output = Trajectory('traj/',
                            target.frame,
                            length=target.length,
                            engine=self)

        # create the directory
        t.touch(output)

        # TODO option for retry
        #       -  use filenames from engine
        cmd = 'python openmmrun.py {args} {types} -s {system} -i {integrator} -t {pdb} --length {length} {output}'.format(
            pdb=input_pdb,
            types=self._create_output_str(),
            length=target.length,
            system=self['system_file'].basename,
            integrator=self['integrator_file'].basename,
            output=output,
            args=self.args,
        )

        # TODO option for retry
        # TODO use filenames from engine
        retry = '\nj=0\ntries=3\n'
        retry += '\ntrajfile=traj/protein.dcd\n\n'
        "\n\n"
        retry += (
            'function timestamp {{\n'
            ' date +\'%Y-%m-%d %H:%M:%S.%3N\'\n'
            '}}\n'
            'while [ ! -f "$trajfile" ] && [ $j -le "$tries" ]\n'
            'do\n'
            ' {0} & last="$!"\n'
            ' sleep 20\n'
            '\n'
            ' echo "$(timestamp) running.sh - checking for trajectory output"\n'
            ' echo "$(timestamp) openmmrun  - PID: $last"\n'
            ' echo "$(ps faux | grep $last | grep -v grep)"\n'
            ' echo "$(timestamp) trajectory output"\n'
            ' echo "$(ls -gt traj)"\n'
            '\n'
            ' if [ ! -f $trajfile ]\n'
            ' then\n'
            '  j=$((j+1))\n'
            '  echo "$(timestamp) output not found"\n'
            '\n'
            '  if [ "$(ps faux | grep $last | grep -v grep)" ]\n'
            '  then\n'
            '   kill "$last" || echo "kill missed"\n'
            '\n'
            '   echo "$(timestamp) found and kill last=$last"\n'
            '   echo "$(timestamp) waiting to check on kill"\n'
            '   sleep 5\n'
            '\n'
            '   if [ "$(ps faux | grep $last | grep -v grep)" ]\n'
            '   then\n'
            '    echo "$(timestamp) superkilling last=$last"\n'
            '    kill -9 "$last" || echo "superkill missed"\n'
            '\n'
            '   fi\n'
            '  fi\n'
            '\n'
            '  echo "$(timestamp) kill is done"\n'
            '\n'
            ' else\n'
            '  echo "$(timestamp) seems like the trajectory is rolling out"\n'
            '  j=$((tries+1))\n'
            '\n'
            ' fi\n'
            ' echo "$(timestamp) Done $j retry iter of main task loop"\n'
            '\n'
            'done\n'
            '\n'
            'wait "$last"\n'
            'echo "$(timestamp) Done with main task loop"\n')

        cmd = 'python openmmrun.py {args} {types} -s {system} -i {integrator} -t {pdb} --length {length} {output}'.format(
            pdb=input_pdb,
            types=self._create_output_str(),
            length=target.length,
            system=self['system_file'].basename,
            integrator=self['integrator_file'].basename,
            output=output,
            args=self.args,
        )

        cmd = retry.format(cmd)

        t.append(cmd)
        t.put(output, target)

        return t
Esempio n. 4
0
    def run(self,
            target,
            resource_name=None,
            export_path=None,
            cpu_threads=1,
            gpu_contexts=0,
            mpi_rank=0):

        t = TrajectoryGenerationTask(self,
                                     target,
                                     cpu_threads=cpu_threads,
                                     gpu_contexts=gpu_contexts,
                                     mpi_rank=mpi_rank)

        if resource_name is None:
            resource_name = list()
        elif isinstance(resource_name, str):
            resource_name = [resource_name]

        assert isinstance(resource_name, list)
        t.resource_name = resource_name

        if export_path:
            t.append(export_path)

        initial_pdb = t.pre_link(self['pdb_file_stage'],
                                 Location('initial.pdb'))
        t.pre_link(self['system_file_stage'])
        t.pre_link(self['integrator_file_stage'])
        t.pre_link(self['_executable_file_stage'])

        if target.frame in [self['pdb_file'], self['pdb_file_stage']]:
            input_pdb = initial_pdb

        elif isinstance(target.frame, File):
            loc = Location('coordinates.%s' % target.frame.extension)
            input_pdb = t.get(target.frame, loc)

        elif isinstance(target.frame, Frame):
            input_traj = t.pre_link(target.frame.trajectory, 'source/')
            input_pdb = File('input.pdb')

            # frame index is in canonical stride = 1
            # we need to figure out which frame in the traj this actually is
            # also, we need a traj with full coordinates / selection = None

            ty, idx = target.frame.index_in_outputs

            if ty is None:
                # cannot use a trajectory where we do not have full coordinates
                return

            cmd = (
                'mdconvert -o {target} -i {index} -t {pdb} {source}'.format(
                    target=input_pdb,  # input.pdb is used as starting structure
                    index=idx,  # the index from the source trajectory
                    pdb=initial_pdb,  # use the main pdb
                    source=input_traj.outputs(ty)))  # we pick output ty

            t.pre.append(cmd)

        else:
            # for now we assume that if the initial frame is None or
            # not specific use the engines internal. That should be changed
            # todo: Raise exception here

            return

        # this represents our output trajectory
        output = Trajectory('traj/',
                            target.frame,
                            length=target.length,
                            engine=self)

        # create the directory
        t.touch(output)

        # TODO option for retry
        #       -  use filenames from engine
        cmd = 'python openmmrun.py {args} {types} -s {system} -i {integrator} -t {pdb} --length {length} {output}'.format(
            pdb=input_pdb,
            types=self._create_output_str(),
            length=target.length,
            system=self['system_file'].basename,
            integrator=self['integrator_file'].basename,
            output=output,
            args=self.args,
        )

        t.append(cmd)

        t.put(output, target)

        return t