Example #1
0
    def from_mdtraj(mdtrajectory):
        """
        Construct a Trajectory object from an mdtraj.Trajectory object

        Parameters
        ----------
        mdtrajectory : mdtraj.Trajectory
            Input mdtraj.Trajectory

        Returns
        -------
        Trajectory
        """
        trajectory = Trajectory()

        velocities = \
            u.Quantity(np.zeros(mdtrajectory.xyz[0].shape, dtype=np.float32), u.nanometers / u.picoseconds)

        zero_momentum = paths.Momentum(velocities=velocities)

        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
            config = paths.Configuration(coordinates=coord, box_vectors=box_v)

            snap = paths.Snapshot(configuration=config, momentum=zero_momentum)
            trajectory.append(snap)

        return trajectory
Example #2
0
def snapshot_from_testsystem(testsystem, units=None):
    """
    Construct a Snapshot from openmm topology and state objects

    Parameters
    ----------
    omm_topology : openmm.Topology
        The filename of the .pdb file to be used

    Returns
    -------
    Snapshot
        the constructed Snapshot

    """

    velocities = np.zeros(testsystem.positions.shape)
    topology = testsystem.topology

    box_vectors = np.array([
        v / units['length']
        for v in testsystem.system.getDefaultPeriodicBoxVectors()
    ]) * units['length']

    snapshot = paths.Snapshot(
        coordinates=testsystem.positions,
        velocities=u.Quantity(velocities, units['velocity']),
        box_vectors=box_vectors,
        potential_energy=u.Quantity(0.0, units['energy']),
        kinetic_energy=u.Quantity(0.0, units['energy']),
        topology=paths.MDTrajTopology(md.Topology.from_openmm(topology)))

    return snapshot
Example #3
0
def snapshot_from_pdb(pdb_file, units=None):
    """
    Construct a Snapshot from the first frame in a pdb file without velocities

    Parameters
    ----------
    pdb_file : str
        The filename of the .pdb file to be used

    Returns
    -------
    Snapshot
        the constructed Snapshot

    """
    pdb = md.load(pdb_file)

    velocities = np.zeros(pdb.xyz[0].shape)

    snapshot = paths.Snapshot(
        coordinates=u.Quantity(pdb.xyz[0], units['length']),
        velocities=u.Quantity(velocities, units['velocity']),
        box_vectors=u.Quantity(pdb.unitcell_vectors[0], units['length']),
        potential_energy=u.Quantity(0.0, units['energy']),
        kinetic_energy=u.Quantity(0.0, units['energy']),
        topology=paths.MDTrajTopology(pdb.topology))

    return snapshot
Example #4
0
def empty_snapshot_from_openmm_topology(topology, units):
    """
    Return an empty snapshot from an openmm.Topology object using the specified units.

    Parameters
    ----------
    topology : openmm.Topology
        the topology representing the structure and number of atoms
    units : dict of {str : simtk.unit.Unit }
        representing a dict of string representing a dimension ('length', 'velocity', 'energy') pointing the
        the simtk.unit.Unit to be used

    Returns
    -------
    Snapshot
        the complete snapshot with zero coordinates and velocities

    """
    n_atoms = topology.n_atoms

    snapshot = paths.Snapshot(
        coordinates=u.Quantity(np.zeros((n_atoms, 3)), units['length']),
        velocities=u.Quantity(np.zeros((n_atoms, 3)), units['velocity']),
        box_vectors=u.Quantity(topology.setUnitCellDimensions(),
                               units['length']),
        potential_energy=u.Quantity(0.0, units['energy']),
        kinetic_energy=u.Quantity(0.0, units['energy']),
        topology=paths.MDTrajTopology(md.Topology.from_openmm(topology)))

    return snapshot
Example #5
0
    def _build_current_snapshot(self):
        # TODO: Add caching for this and mark if changed

        state = self.simulation.context.getState(getPositions=True,
                                                 getVelocities=True,
                                                 getEnergy=True)

        return paths.Snapshot(
            coordinates=state.getPositions(asNumpy=True),
            box_vectors=state.getPeriodicBoxVectors(asNumpy=True),
            potential_energy=state.getPotentialEnergy(),
            velocities=state.getVelocities(asNumpy=True),
            kinetic_energy=state.getKineticEnergy(),
            topology=self.topology)
Example #6
0
def trajectory_from_mdtraj(mdtrajectory):
    """
    Construct a Trajectory object from an mdtraj.Trajectory object

    Parameters
    ----------
    mdtrajectory : mdtraj.Trajectory
        Input mdtraj.Trajectory

    Returns
    -------
    Trajectory
        the constructed Trajectory instance
    """

    #TODO: Fix energies and move these to specialized CVs
    #TODO: We could also allow to have empty energies

    trajectory = paths.Trajectory()
    empty_momentum = paths.Momentum(
        velocities=u.Quantity(np.zeros(mdtrajectory.xyz[0].shape),
                              u.nanometer / u.picosecond),
        kinetic_energy=u.Quantity(0.0, u.kilojoule_per_mole))
    topology = paths.MDTrajTopology(mdtrajectory.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

        config = paths.Configuration(coordinates=coord,
                                     box_vectors=box_v,
                                     potential_energy=u.Quantity(
                                         0.0, u.kilojoule_per_mole),
                                     topology=topology)

        snap = paths.Snapshot(configuration=config,
                              momentum=empty_momentum,
                              topology=paths.MDTrajTopology(
                                  mdtrajectory.topology))
        trajectory.append(snap)

    return trajectory
Example #7
0
    def _build_current_snapshot(self):
        # TODO: Add caching for this and mark if changed

        tmp = self.template

        coordinates = u.Quantity(
            tmp.coordinates._value +
            np.random.normal(0.0, 0.02, tmp.coordinates.shape),
            tmp.coordinates.unit)
        velocities = u.Quantity(
            np.random.normal(0.0, 0.02, tmp.velocities.shape),
            tmp.velocities.unit)

        return paths.Snapshot(coordinates=coordinates,
                              box_vectors=tmp.box_vectors,
                              potential_energy=tmp.potential_energy,
                              velocities=velocities,
                              kinetic_energy=tmp.kinetic_energy,
                              topology=self.topology)
Example #8
0
    def test_storage(self):
        import os
        fname = "tps_network_storage_test.nc"
        if os.path.isfile(fname):
            os.remove(fname)
        topol = paths.ToyTopology(n_spatial=1, masses=[1.0], pes=None)
        self.template = paths.Snapshot(coordinates=[[0.0]],
                                       velocities=[[0.0]],
                                       topology=topol)
        states = [self.stateA, self.stateB, self.stateC]
        network_a = TPSNetwork(initial_states=states, final_states=states)
        assert_equal(len(network_a.sampling_transitions), 1)
        assert_equal(len(network_a.transitions), 6)
        storage_w = paths.storage.Storage(fname, "w", self.template)
        storage_w.save(network_a)
        storage_w.sync_all()

        storage_r = paths.storage.AnalysisStorage(fname)
        network_b = storage_r.networks[0]
        assert_equal(len(network_b.sampling_transitions), 1)
        assert_equal(len(network_b.transitions), 6)

        if os.path.isfile(fname):
            os.remove(fname)