How to use the mdtraj.Topology.from_dataframe function in mdtraj

To help you get started, we’ve selected a few mdtraj examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github msmbuilder / msmbuilder / test / test_msm.py View on Github external
# Now let's make make the output assignments start with zero at the first position.
    i0 = trimmed_assignments[0][0]
    if i0 == 1:
        for m in trimmed_assignments:
            m *= -1
            m += 1
    
    pairs = msm.draw_samples(trimmed_assignments, 2000)

    samples = map_drawn_samples(pairs, data)
    mu = np.mean(samples, axis=1)
    eq(mu, np.array([[0., 0., 0.0], [25., 25., 25.]]), decimal=1)

    # We should make sure we can sample from Trajectory objects too...
    # Create a fake topology with 1 atom to match our input dataset
    top = md.Topology.from_dataframe(pd.DataFrame({"serial":[0], "name":["HN"], "element":["H"], "resSeq":[1], "resName":"RES", "chainID":[0]}), bonds=np.zeros(shape=(0, 2), dtype='int'))
    trajectories = [md.Trajectory(x[:, np.newaxis], top) for x in data]  # np.newaxis reshapes the data to have a 40000 frames, 1 atom, 3 xyz

    trj_samples = map_drawn_samples(pairs, trajectories)
    mu = np.array([t.xyz.mean(0)[0] for t in trj_samples])
    eq(mu, np.array([[0., 0., 0.0], [25., 25., 25.]]), decimal=1)
github openpathsampling / openpathsampling / opentis / todict.py View on Github external
for key, el in elements.iteritems():
            try:
                md.element.Element(
                            number=int(el[0]), name=el[1], symbol=el[2], mass=float(el[3])
                         )
                simtk.openmm.app.Element(
                            number=int(el[0]), name=el[1], symbol=el[2], mass=float(el[3])*units.amu
                         )
            except(AssertionError):
                pass

        atoms = pd.DataFrame(top_dict['atoms'], columns=top_dict['atom_columns'])
        bonds = np.array(top_dict['bonds'])

        return md.Topology.from_dataframe(atoms, bonds)
github ADicksonLab / wepy / src / wepy_tools / sim_makers / toys / randomwalk.py View on Github external
JSON string representing the topology of system being simulated.

        """
        n_atoms = 1
        data = []
        for i in range(n_atoms):
            data.append(dict(serial=i, name="H", element="H",
                             resSeq=i + 1, resName="UNK", chainID=0))

        data = pd.DataFrame(data)

        xyz = np.zeros((1, 1, 3))
        unitcell_lengths = 0.943 * np.ones((1, 3))
        unitcell_angles = 90 * np.ones((1, 3))

        top = mdj.Topology.from_dataframe(data, bonds=np.zeros((0, 2), dtype='int'))

        json_top_str = mdtraj_to_json_topology(top)

        return json_top_str
github choderalab / openmoltools / gaff2xml / gafftools.py View on Github external
def to_mdtraj(self):
        atoms, bonds = self.atoms, self.bonds
        atoms_mdtraj = atoms[["name", "resName"]]
        atoms_mdtraj["serial"] = atoms.index
        atoms_mdtraj["element"] = atoms.atype.map(gaff_elements)
        atoms_mdtraj["resSeq"] = np.ones(len(atoms))
        atoms_mdtraj["chainID"] = np.ones(len(atoms))

        bonds_mdtraj = bonds[["id0", "id1"]].values
        offset = bonds_mdtraj.min()
        bonds_mdtraj -= offset

        top = mdtraj.Topology.from_dataframe(atoms_mdtraj, bonds_mdtraj)
        xyzlist = np.array([atoms[["x","y","z"]].values])
        xyzlist /= 10.0  # Convert from angstrom to nanometer
        traj = mdtraj.Trajectory(xyzlist, top)
        return traj
github ADicksonLab / wepy / examples / randomwalk / gen_rw_top.py View on Github external
data = []
    for i in range(n_atoms):
        data.append(dict(serial=i, name="H", element="H", resSeq=i + 1, resName="UNK", chainID=0))

    # convert to pandas data frame format
    data = pd.DataFrame(data)

    # creates the array of position for the system
    xyz = np.zeros((1, 1, 3))

    # defines box vector lengths
    unitcell_lengths = 0.943 * np.ones((1, 3))
    unitcell_angles = 90 * np.ones((1, 3))

    # create the topology and the trajectory of teh randomwalk systes
    top = mdj.Topology.from_dataframe(data, bonds=np.zeros((0, 2), dtype='int'))
    traj = mdj.Trajectory(xyz, top, unitcell_lengths=unitcell_lengths, unitcell_angles=unitcell_angles)

    #save the trajectory in h5 and pdb format
    traj.save_hdf5("tmp_mdtraj_system.h5")

    # we need a JSON string for now in the topology section of the
    # HDF5 so we just load the topology from the hdf5 file
    top_h5 = h5py.File("tmp_mdtraj_system.h5")

    # it is in bytes so we need to decode to a string, which is in JSON format
    json_top_str = top_h5['topology'][0].decode()
    top_h5.close()

    # write the JSON topology out
    with open("randomwalk_system.top.json", mode='w') as json_wf:
        json_wf.write(json_top_str)
github choderalab / yank / Yank / yank.py View on Github external
def __setstate__(self, serialization):
        topology_dict = serialization['topology']
        atoms = pandas.read_json(topology_dict['atoms'], orient='records')
        bonds = np.array(topology_dict['bonds'])
        self._topology = mdtraj.Topology.from_dataframe(atoms, bonds)
        self._ligand_atoms = serialization['ligand_atoms']
        self._solvent_atoms = serialization['solvent_atoms']
        self._regions = serialization['regions']
github openpathsampling / openpathsampling / openpathsampling / engines / openmm / topology.py View on Github external
# for key, el in elements.iteritems():
        #     try:
        #         md.element.Element(
        #                     number=int(el[0]), name=el[1], symbol=el[2], mass=float(el[3])
        #                  )
        #         simtk.openmm.app.Element(
        #                     number=int(el[0]), name=el[1], symbol=el[2], mass=float(el[3])*units.amu
        #                  )
        #     except(AssertionError):
        #         pass

        atoms = pd.DataFrame(top_dict['atoms'], columns=top_dict['atom_columns'])
        bonds = np.array(top_dict['bonds'])

        md_topology = md.Topology.from_dataframe(atoms, bonds)

        return cls(md_topology, dct['subsets'])
github dwhswenson / contact_map / contact_map / contact_map.py View on Github external
def _deserialize_topology(topology_json):
        """Create MDTraj topology from JSON-serialized version"""
        table, bonds = json.loads(topology_json)
        topology_df = pd.read_json(table)
        topology = md.Topology.from_dataframe(topology_df,
                                              np.array(bonds))
        return topology