def convert_mdtraj_to_oemol(traj: md.Trajectory) -> oechem.OEMol: """ This method converts an mdtraj Trajectory to an OEMol via saving as a PDBfile and reading in with OpenEye. Although this seems hacky, it seems less error-prone than trying to manually construct the OEMol. Parameters ---------- mdtraj: md.Trajectory The trajectory to turn into an OEMol Returns ------- mol : oechem.OEMol The trajectory represented as an OEMol """ # create a temporary file with a PDB suffix and save with MDTraj pdb_file = tempfile.NamedTemporaryFile(delete=False, suffix=".pdb") traj.save(pdb_file.name) pdb_file.close() # Now use the openeye oemolistream to read in this file as an OEMol: ifs = oechem.oemolistream() ifs.open(pdb_file.name) ifs.SetFormat(oechem.OEFormat_PDB) mol = oechem.OEMol() oechem.OEReadMolecule(ifs, mol) # close the stream and delete the temporary pdb file ifs.close() os.unlink(pdb_file.name) return mol
def mdtraj_to_oemol(snapshot: md.Trajectory): """ Create an OEMol from an MDTraj file by writing and reading NOTE: This uses terrible heuristics Parameters ---------- snapshot : mdtraj.Trajectory MDTraj Trajectory with a single snapshot Returns ------- oemol : openeye.oechem.OEMol The OEMol """ from openeye import oechem with tempfile.TemporaryDirectory() as tmpdir: filename = os.path.join(tmpdir, "tmp.pdb") # Write the PDB file snapshot.save(filename) # Read it with OpenEye with oechem.oemolistream(filename) as ifs: for mol in ifs.GetOEGraphMols(): return mol
def convert_mdtraj_to_oemol(traj: md.Trajectory) -> oechem.OEMol: """ This method converts an mdtraj Trajectory to an OEMol via saving as a PDBfile and reading in with OpenEye. Although this seems hacky, it seems less error-prone than trying to manually construct the OEMol. Parameters ---------- mdtraj: md.Trajectory The trajectory to turn into an OEMol Returns ------- mol : oechem.OEMol The trajectory represented as an OEMol """ #create a temporary file with a PDB suffix and save with MDTraj pdb_file = tempfile.NamedTemporaryFile(delete=False, suffix=".pdb") traj.save(pdb_file.name) pdb_file.close() #Now use the openeye oemolistream to read in this file as an OEMol: ifs = oechem.oemolistream() ifs.open(pdb_file.name) ifs.SetFormat(oechem.OEFormat_PDB) mol = oechem.OEMol() oechem.OEReadMolecule(ifs, mol) #close the stream and delete the temporary pdb file ifs.close() os.unlink(pdb_file.name) return mol
def test_unitcell(): # make sure that bogus unitcell vecotrs are not saved top = md.load(get_fn('native.pdb')).restrict_atoms(range(5)).topology t = Trajectory(xyz=np.random.randn(100, 5, 3), topology=top) # xtc dcd binpos trr h5 pdb nc lh5 for fn in [temp1, temp2, temp3, temp4, temp5, temp6, temp6, temp8]: t.save(fn) f = lambda: eq(md.load(fn, top=top).unitcell_vectors, None) f.description = 'unitcell preservation in %s' % os.path.splitext(fn)[1] yield f
def test_unitcell(): # make sure that bogus unitcell vecotrs are not saved top = md.load(get_fn('native.pdb')).restrict_atoms(range(5)).topology t = Trajectory(xyz=np.random.randn(100, 5, 3), topology=top) # xtc dcd binpos trr h5 pdb nc lh5 for fn in [temp1, temp2, temp3, temp4, temp5, temp6, temp6, temp8]: t.save(fn) f = lambda: eq(md.load(fn, top=top).unitcell_vectors, None) f.description = 'unitcell preservation in %s' % os.path.splitext(fn)[1] yield f