{"action":"created","issue":{"url":"https://api.github.com/repos/pytraj/pytraj/issues/260","labels_url":"https://api.github.com/repos/pytraj/pytraj/issues/260/labels{/name}","comments_url":"https://api.github.com/repos/pytraj/pytraj/issues/260/comments","events_url":"https://api.github.com/repos/pytraj/pytraj/issues/260/events","html_url":"https://github.com/pytraj/pytraj/issues/260","id":73550036,"number":260,"title":"reimaging and interfacing with mdraj","user":{"login":"evanfeinberg","id":1760982,"avatar_url":"https://avatars.githubusercontent.com/u/1760982?v=3","gravatar_id":"","url":"https://api.github.com/users/evanfeinberg","html_url":"https://github.com/evanfeinberg","followers_url":"https://api.github.com/users/evanfeinberg/followers","following_url":"https://api.github.com/users/evanfeinberg/following{/other_user}","gists_url":"https://api.github.com/users/evanfeinberg/gists{/gist_id}","starred_url":"https://api.github.com/users/evanfeinberg/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/evanfeinberg/subscriptions","organizations_url":"https://api.github.com/users/evanfeinberg/orgs","repos_url":"https://api.github.com/users/evanfeinberg/repos","events_url":"https://api.github.com/users/evanfeinberg/events{/privacy}","received_events_url":"https://api.github.com/users/evanfeinberg/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-05-06T08:08:33Z","updated_at":"2015-05-06T08:12:17Z","closed_at":null,"body":"Hi, \r\n\r\nOn suggestion of @swails , I'm moving https://github.com/mdtraj/mdtraj/issues/814 to here. Thanks to @hainm , I have much cleaner code now for reimaging a single pdb. load_mdtraj was very helpful.\r\n\r\nSince I want to add pairwise distances as features to my trajectory, I need to reimage all trajectories. However, I realize that while it's easy to go from mdtraj --> pytraj, it's nontrivial to go from pytraj --> mdtraj , either directly or via HDF5 format. So I had to develop a little workaround. It seems to work, but it would be nice if it were cleaner. The gist is that I load a .h5 trajectory in mdtraj, use pytraj's load_mdtraj feature to transfer it over, autoimage() it, save it as a dcd trajectory, separately save a frame as a pdb to act as the topology, re-load that dcd/pdb combo into mdtraj, and then save it once again as a .h5 to complete the cycle. It would be great if I could circumvent the middle steps and just dump a pytraj object straight to h5, or convert it back somehow to an mdtraj object. Any thoughts? \r\n\r\nCheers,\r\nEvan\r\n\r\n'''\r\nimport mdtraj as md\r\nimport os\r\nimport h5py\r\nimport multiprocessing as mp\r\nfrom functools import partial \r\nfrom pytraj import adict \r\n\r\ndef get_trajectory_files(traj_dir, ext = \".h5\"):\r\n\ttraj_files = []\r\n\tfor traj in os.listdir(traj_dir):\r\n\t\t\tif traj.endswith(ext):\r\n\t\t\t\ttraj_files.append(\"%s/%s\" %(traj_dir,traj))\r\n\treturn sorted(traj_files)\r\n\r\ndef reimage_traj(traj_file, save_dir):\r\n\ttraj = md.load(traj_file)\r\n\ttopology = md.load_frame(traj_file,index=0)\r\n\r\n\ttraj_pytraj = mdio.load_mdtraj(traj)\r\n\ttraj_pytraj.autoimage()\r\n\r\n\tfilename = traj_file.split(\"/\")[len(traj_file.split(\"/\"))-1]\r\n\tfilename = filename.split(\".\")[0]\r\n\tdcd_filename = \"%s.dcd\" %filename\r\n\ttop_filename = \"%s.pdb\" %filename\r\n\th5_filename = \"%s.h5\" %filename\r\n\tnew_dcd_file = \"%s/%s\" %(save_dir, dcd_filename)\r\n\tnew_top_file = \"%s/%s\" %(save_dir, top_filename)\r\n\tnew_h5_file = \"%s/%s\" %(save_dir, h5_filename)\r\n\tprint new_dcd_file\r\n\tprint new_top_file\r\n\ttraj_pytraj.save(new_dcd_file)\r\n\r\n\ttopology.save(new_top_file)\r\n\r\n\tnew_traj = md.load(new_dcd_file, top = new_top_file)\r\n\tnew_traj.save(new_h5_file)\r\n\treturn\r\n\r\n\r\ndef reimage_trajs(traj_dir):\r\n\tnew_dir = \"%s_reimaged\" %traj_dir\r\n\r\n\tif not os.path.exists(new_dir): os.makedirs(new_dir)\r\n\r\n\ttrajs = get_trajectory_files(traj_dir)\r\n\t\r\n\treimage = partial(reimage_traj, save_dir = new_dir)\r\n\r\n\tnum_workers = mp.cpu_count()\r\n\tpool = mp.Pool(num_workers)\r\n\tpool.map(reimage, trajs)\r\n\tpool.terminate()\r\n\treturn\r\n\r\ntraj_dir = \"/scratch/users/enf/b2ar_analysis/subsampled_allprot_combined\"\r\nreimage_trajs(traj_dir)\r\n'''\r\n"},"comment":{"url":"https://api.github.com/repos/pytraj/pytraj/issues/comments/99370033","html_url":"https://github.com/pytraj/pytraj/issues/260#issuecomment-99370033","issue_url":"https://api.github.com/repos/pytraj/pytraj/issues/260","id":99370033,"user":{"login":"evanfeinberg","id":1760982,"avatar_url":"https://avatars.githubusercontent.com/u/1760982?v=3","gravatar_id":"","url":"https://api.github.com/users/evanfeinberg","html_url":"https://github.com/evanfeinberg","followers_url":"https://api.github.com/users/evanfeinberg/followers","following_url":"https://api.github.com/users/evanfeinberg/following{/other_user}","gists_url":"https://api.github.com/users/evanfeinberg/gists{/gist_id}","starred_url":"https://api.github.com/users/evanfeinberg/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/evanfeinberg/subscriptions","organizations_url":"https://api.github.com/users/evanfeinberg/orgs","repos_url":"https://api.github.com/users/evanfeinberg/repos","events_url":"https://api.github.com/users/evanfeinberg/events{/privacy}","received_events_url":"https://api.github.com/users/evanfeinberg/received_events","type":"User","site_admin":false},"created_at":"2015-05-06T08:12:17Z","updated_at":"2015-05-06T08:12:17Z","body":"Sorry the formatting got kind of messed up. I made a gist version here. In the mean time, while pytraj --> mdtraj is nontrivial, I hope someone finds this useful: \r\n\r\nhttps://gist.github.com/evanfeinberg/6d1379b1556cef92d0ec#file-reimage-py"}}