Beispiel #1
0
def trajectory_to_mdtraj(trajectory, md_topology=None):
    """
    Construct a `mdtraj.Trajectory` object from an :obj:`Trajectory` object

    Parameters
    ----------
    trajectory : :obj:`openpathsampling.engines.Trajectory`
        Input Trajectory

    Returns
    -------
    :obj:`mdtraj.Trajectory`
        the constructed Trajectory instance
    """
    if not hasattr(trajectory, 'to_mdtraj'):
        try:
            _ = len(trajectory)
        except TypeError:
            trajectory = Trajectory([trajectory])
        else:
            trajectory = Trajectory(trajectory)

    # For now, let's keep all the code in one place, and better for
    # engines.openmm.tools to require engines.trajectory than vice versa
    return trajectory.to_mdtraj(md_topology)
def trajectory_to_mdtraj(trajectory, md_topology=None):
    """
    Construct a `mdtraj.Trajectory` object from an :obj:`Trajectory` object

    Parameters
    ----------
    trajectory : :obj:`openpathsampling.engines.Trajectory`
        Input Trajectory

    Returns
    -------
    :obj:`mdtraj.Trajectory`
        the constructed Trajectory instance
    """
    if not hasattr(trajectory, 'to_mdtraj'):
        try:
            _ = len(trajectory)
        except TypeError:
            trajectory = Trajectory([trajectory])
        else:
            trajectory = Trajectory(trajectory)

    # TODO: The following would work if we remove trajectory.to_mdtraj()
    # For now, let's keep all the code in one place, and better for
    # engines.openmm.tools to require engines.trajectory than vice versa
    # output = trajectory.xyz
    # traj = md.Trajectory(output, md_topology)
    # traj.unitcell_vectors = trajectory.box_vectors
    return trajectory.to_mdtraj(md_topology)
def trajectory_to_mdtraj(trajectory, md_topology=None):
    """
    Construct a `mdtraj.Trajectory` object from an :obj:`Trajectory` object

    Parameters
    ----------
    trajectory : :obj:`openpathsampling.engines.Trajectory`
        Input Trajectory

    Returns
    -------
    :obj:`mdtraj.Trajectory`
        the constructed Trajectory instance
    """
    if not hasattr(trajectory, 'to_mdtraj'):
        try:
            _ = len(trajectory)
        except TypeError:
            trajectory = Trajectory([trajectory])
        else:
            trajectory = Trajectory(trajectory)

    # TODO: The following would work if we remove trajectory.to_mdtraj()
    # For now, let's keep all the code in one place, and better for
    # engines.openmm.tools to require engines.trajectory than vice versa
    # output = trajectory.xyz
    # traj = md.Trajectory(output, md_topology)
    # traj.unitcell_vectors = trajectory.box_vectors
    return trajectory.to_mdtraj(md_topology)
Beispiel #4
0
def trajectory_from_mdtraj(mdtrajectory,
                           simple_topology=False,
                           velocities=None):
    """
    Construct a Trajectory object from an mdtraj.Trajectory object

    Parameters
    ----------
    mdtrajectory : mdtraj.Trajectory
        Input mdtraj.Trajectory
    simple_topology : bool
        if `True` only a simple topology with n_atoms will be created.
        This cannot be used with complex CVs but loads and stores very fast
    velocities : np.array
        velocities in units of nm/ps

    Returns
    -------
    openpathsampling.engines.Trajectory
        the constructed Trajectory instance
    """
    trajectory = Trajectory()
    vel_unit = u.nanometer / u.picosecond

    if simple_topology:
        topology = Topology(*mdtrajectory.xyz[0].shape)
    else:
        topology = MDTrajTopology(mdtrajectory.topology)

    if velocities is None:
        empty_vel = u.Quantity(np.zeros(mdtrajectory.xyz[0].shape), vel_unit)

    if mdtrajectory.unitcell_vectors is not None:
        box_vects = u.Quantity(mdtrajectory.unitcell_vectors, u.nanometers)
    else:
        box_vects = [None] * len(mdtrajectory)

    engine = TopologyEngine(topology)

    for frame_num in range(len(mdtrajectory)):
        # mdtraj trajectories only have coordinates and box_vectors
        coord = u.Quantity(mdtrajectory.xyz[frame_num], u.nanometers)
        if velocities is not None:
            vel = u.Quantity(velocities[frame_num], vel_unit)
        else:
            vel = empty_vel

        box_v = box_vects[frame_num]

        statics = Snapshot.StaticContainer(coordinates=coord,
                                           box_vectors=box_v)
        kinetics = Snapshot.KineticContainer(velocities=vel)

        snap = Snapshot(statics=statics, kinetics=kinetics, engine=engine)
        trajectory.append(snap)

    return trajectory
Beispiel #5
0
def trajectory_from_mdtraj(mdtrajectory, simple_topology=False):
    """
    Construct a Trajectory object from an mdtraj.Trajectory object

    Parameters
    ----------
    mdtrajectory : mdtraj.Trajectory
        Input mdtraj.Trajectory
    simple_topology : bool
        if `True` only a simple topology with n_atoms will be created.
        This cannot be used with complex CVs but loads and stores very fast

    Returns
    -------
    openpathsampling.engines.Trajectory
        the constructed Trajectory instance
    """

    trajectory = Trajectory()
    empty_kinetics = Snapshot.KineticContainer(
        velocities=u.Quantity(
            np.zeros(mdtrajectory.xyz[0].shape), u.nanometer / u.picosecond)
    )
    if simple_topology:
        topology = Topology(*mdtrajectory.xyz[0].shape)
    else:
        topology = MDTrajTopology(mdtrajectory.topology)

    engine = TopologyEngine(topology)

    for frame_num in range(len(mdtrajectory)):
        # mdtraj trajectories only have coordinates and box_vectors
        coord = u.Quantity(mdtrajectory.xyz[frame_num], u.nanometers)
        if mdtrajectory.unitcell_vectors is not None:
            box_v = u.Quantity(mdtrajectory.unitcell_vectors[frame_num],
                               u.nanometers)
        else:
            box_v = None

        statics = Snapshot.StaticContainer(
            coordinates=coord,
            box_vectors=box_v
        )

        snap = Snapshot(
            statics=statics,
            kinetics=empty_kinetics,
            engine=engine
        )
        trajectory.append(snap)

    return trajectory
Beispiel #6
0
def reduce_trajectory_box_vectors(trajectory):
    """Trajectory with reduced box vectors.

    OpenMM has strict requirements on the box vectors describing the unit
    cell. In some cases, such as trajectories loaded from files that have
    rounded the box vectors, these conditions might not be satisfied. This
    method forces the box vectors to meet OpenMM's criteria.

    Parameters
    ----------
    trajectory : :class:`.Trajectory`
        input trajectory

    Returns
    -------
    :class:`.Trajectory`
        trajectory with correctly reduced box vectors
    """
    return Trajectory([
        snap.copy_with_replacement(box_vectors=reduced_box_vectors(snap))
        for snap in trajectory
    ])