def start_trace(): """ Function to connect VMD function `connect_plot` with VMD variable change of `vmd_frame` """ global fig molid = evaltcl("molinfo top") evaltcl("trace variable vmd_frame({}) w connect_plot".format(molid)) fig.canvas.mpl_connect('close_event', stop_trace)
def run(fes_file='fes.dat', colvar='COLVAR', connect=True): """ Run routine to plot the free energy surface and connect the current VMD to a point of it. Parameters ---------- fes_file : str path to fes file. (Default is 'fes.dat'.) colvar : str path to COLVAR file (Default is 'COLVAR'.) connect : bool Switch to connect the plot to the current VMD frame. """ assert os.path.exists(fes_file), "FES File: {} not found".format(fes_file) assert os.path.exists(colvar), "COLVAR file: {} not found".format(colvar) molid = evaltcl("molinfo top") numframes = int(evaltcl('molinfo {} get numframes'.format(molid))) current_frame = int(evaltcl('molinfo {} get frame'.format(molid))) # load files global colvar_data, fes_edges, fes_data header, fes_edges, fes_data = load(fes_file) dimensions = len(fes_edges) colvar_data = load_colvar(colvar, [cv.name for cv in header]) global fig, ax if dimensions == 1: # create plot fig, ax = plt.subplots() line = plot_1D(header, fes_data, fes_edges, ax=ax) elif dimensions == 2: # create plot fig, ax = plt.subplots() img, cbar = plot_2D(header, fes_data, ax=ax) else: raise NotImplementedError("Only 1D and 2D free energy landscapes are implemented." + \ "\nFound a {}D landscape!".format(dimensions)) # draw global point point, = ax.plot([0], [0], 'r*', markersize=12) try: fig.show() except AttributeError as e: print(e) update(current_frame) if connect: print("register connect_plot") register() print("Connect plot to frames") start_trace() print("Stop connect_plot with:\n" + "trace vdelete vmd_frame({}) w connect_plot".format(molid))
def register(): """ Function to register VMD function `connect_plot` which calls the python function `update($frame)` from within VMD """ evaltcl(""" proc connect_plot {name index op} { # name == vmd_frame # index == molecule id of the newly changed frame # op == w set frame [molinfo $index get frame] gopython -command "update($frame)" return } """)
def set_pbc(xscFile): """ Sets the systems periodic boundaries." """ xscFile = open(xscFile,"r") for line in xscFile: continue items = line.split() xDim = items[1] yDim = items[5] zDim = items[9] #set pbd pbcCommand = ("package require pbctools; pbc set { %s %s %s }" % (xDim, yDim, zDim)) evaltcl(pbcCommand) xscFile.close()
def load_assign_data(cond, npyfile): """ Function to load and assign data. Parameters ---------- cond : str VMD selection string npyfile : str path to numpy `.npy` file. """ # get number of frames n_frames = int(evaltcl('molinfo top get numframes')) # read in data data = np.load(npyfile) if n_frames != data.shape[0]: print("something is off") # create atomselection and assign data sel = AtomSel(cond) for n in range(n_frames): sel.frame(n) sel.set('user', data[n, :].tolist())
def expand_data_to_residue(condition, column='user', n_frames=None): """ Function to expand the data stored in column from a per atom data to a per residue data. Parameter --------- condition : str condition string to select the atomselect containing the data column : str, optional column where the data is stored. Default is `'user'`. n_frames : int or None, optional Number of frames to consinder. If `None` all frames are taken. Default is `None` Example ------- >>> expand_data_to_residue(condition='type OW', column='user', n_frames=100) """ # get number of frames if n_frames is None: n_frames = int(evaltcl('molinfo top get numframes')) sel_data = AtomSel(condition) residue = sel_data.get('residue') sel_residues = AtomSel('same residue as ({})'.format(condition)) # get the list of resdiue IDs list_residues = sel_residues.get('residue') # create a dictionay with the counts per residue dict_counts = dict( np.array(np.unique(list_residues, return_counts=True)).T) # get the counts (keep order of the residues as in sel !) counts_residues = np.vectorize(dict_counts.__getitem__)(residue) for n in range(n_frames): sel_data.frame(n) sel_residues.frame(n) data = sel_data.get(column) data_residue = np.repeat(data, counts_residues) sel_residues.set(column, data_residue.tolist())
def stop_trace(): """ Function to disconnect VMD function `connect_plot` with VMD variable change of `vmd_frame` """ molid = evaltcl("molinfo top") evaltcl("trace vdelete vmd_frame({}) w connect_plot".format(molid))