def xtc_coords(filename,idxs=None): """ Get the coordinates """ try: import xdrfile.libxdrfile2 as libxdr except ImportError: raise ImportError("libxdrfile2 library needed for xtc trajectories!") file = libxdr.xdrfile_open(filename, "r") natoms = libxdr.read_xtc_natoms(filename) nframes, offsets = libxdr.read_xtc_numframes(filename) box = np.zeros((3,3),dtype=np.float32) config = np.zeros((natoms,3),dtype=np.float32) if idxs is not None: # Seek to read frames specified by idxs. nframes = len(idxs) coords = np.zeros((nframes,3,natoms),dtype="float") for i in xrange(nframes): libxdr.xdr_seek(file,offsets[idxs[i]],0) status,step,timestmp,prec = libxdr.read_xtc(file, box, config) coords[i,:,:] = config.astype("float").T else: coords = np.zeros((nframes,3,natoms),dtype=np.float32) status = libxdr.exdrOK i = 0 while status == libxdr.exdrOK: status,step,timestmp,prec = libxdr.read_xtc(file, box, config) coords[i,:,:] = config.astype("float").T i += 1 libxdr.xdrfile_close(file) return coords
def read(self): assert self.model != None, "# Model uninitialized. call parse before read!" # read pdb if self.xtc == None: data = [] for line in self.fh: at = line[0:6].strip() if at == "ATOM" or at == "HETATM": vv = [float(line[30:38]), float(line[38:46]), float(line[46:54])] data.append(vv) if at == "ENDMDL": self.model.set_coords(np.array(data)) self.time += 1 # return step return self.time self.fh.close() return -1 # read xtc else: coords = np.zeros((self.natoms, 3), dtype=np.float32) box = np.zeros((3, 3), dtype=np.float32) status, step, time, prec = libxdrfile2.read_xtc(self.xtc, box, coords) self.model.set_coords(10.0 * coords) if status: libxdrfile2.xdrfile_close(self.xtc) return -1 else: return time
def next(self): status = libxdr.read_xtc(self.file, self.box, self.config)[0] return np.array(self.config, copy=True, dtype=np.float64), status
def _read(self, fp, box, x): status, step, ftime, prec = libxdrfile2.read_xtc(fp, box, x) return status, step, ftime
def _read( self, fp, box, x ): status, step, ftime, prec = libxdrfile2.read_xtc( fp, box, x ) return status, step, ftime
def next(self): status = libxdr.read_xtc(self.file, self.box, self.config)[0] return np.array(self.config,copy=True,dtype=np.float64), status