def mutualInformation(self, x, y): """ calculate mutual information :param x: a feature vector :param y: a feature vector :return: """ from dockml import algorithms algo = algorithms.BasicAlgorithm() HX = algo.entropy1D(x) HY = algo.entropy1D(y) HXY = algo.entropy2D(x, y) return HX + HY - HXY
def generate_cmap(self, shape='array', switch=False): """Calculate atom cmap data. Parameters ---------- shape: str, the type of cmap output, array like or matrix like. Default is array. Options are: array, matrix switch: bool, default is False apply a switch function to the contact map calculation Returns ------- self: the instance itself """ if not self.distmtx_computed_: self.distance_matrix() if not self.cmap_computed_: if switch: sf = algorithms.BasicAlgorithm() vf = np.vectorize(sf.switchFuction) cmap = vf(self.dist_matrix_, d0=self.cutoff * 2.0) else: cmap = (self.dist_matrix_ <= self.cutoff) * 1.0 if shape == "array": pass elif shape == "matrix": cmap = cmap.reshape((cmap.shape[0], self.atom_group_a.shape[0], self.atom_group_b.shape[0])) else: pass self.cmap_ = cmap self.cmap_computed_ = True return self
def plot2dFes(filename, dtype=[], zlim=[], xlab="X", ylab="Y", xyzcols=[0, 1, 2], title=None, label="FES (kJ/mol)", dpi=2000, savefile=None, fz=12, level=30, pmf=False, MIN=1.0, cmaptype='bwr', xshift=0.0, yshift=0.0, xlim=[], ylim=[], mesh=False, sep=",", ) : # load file if len(dtype) : #fes = np.loadtxt(filename, comments=["#","@"], # dtype={'names':('Rec', 'Lig', 'Cmap'), 'formats':(dtype[0], dtype[1], dtype[2])}, # usecols=xyzcols, delimiter=sep, skiprows=1) with open(filename) as lines: dat = [x.split() for x in lines if (len(x) and x[0] not in ['#', '@'])] fes = [] for _d in dat: fes.append([_d[xyzcols[0]], _d[xyzcols[1]], float(_d[xyzcols[2]])]) #fes = np.array(fes).astype(np.float) fes = pd.DataFrame(fes, columns=['Rec', 'Lig', 'Cmap']) x_size = len(set(fes['Rec'])) y_size = len(set(fes['Lig'])) #z = np.reshape(fes['Cmap'], (y_size, x_size)) z = fes.values[:, 2].reshape((y_size, x_size)) if mesh: x = sorted(list(set(fes['Rec'].astype(np.float)))) y = sorted(list(set(fes['Lig'].astype(np.float)))) x.append(x[1] - x[0] + x[-1]) y.append(y[1] - y[0] + y[-1]) x = np.asarray(x) + xshift y = np.asarray(y) + yshift else: x = [float(str(item).split("_")[0]) for item in list(fes['Rec'])] y = [float(str(item).split("_")[0]) for item in list(fes['Lig'])] # 1d arrary to 2d x = np.reshape(np.asarray(x) + xshift, (y_size, x_size)) y = np.reshape(np.asarray(y) + yshift, (y_size, x_size)) else: fes = np.loadtxt(filename, comments="#", usecols=xyzcols, delimiter=sep, skiprows=1) # get x and y size x_size = len(set(fes[:, 0])) y_size = len(set(fes[:, 1])) print("X Y size", x_size, y_size) if mesh: x = sorted(list(set(fes[:, 0].astype(np.float)))) y = sorted(list(set(fes[:, 1].astype(np.float)))) x.append(x[1] - x[0] + x[-1]) y.append(y[1] - y[0] + y[-1]) x = np.asarray(x) + xshift y = np.asarray(y) + yshift else: # 1d arrary to 2d x = np.reshape(fes[:, 0] + xshift, (y_size, x_size)) y = np.reshape(fes[:, 1] + yshift, (y_size, x_size)) z = np.reshape(fes[:, 2], (y_size, x_size)) if pmf: # PMF algo = algorithms.BasicAlgorithm() # vectorize the PMF function, and try to apply it to all element of a list PMF = np.vectorize(algo.pmf) MAX = np.max(z.ravel()) z = PMF(z, MIN, kt=2.5, max=MAX) # plot data cmaptypes = ['bwr', 'seismic', 'Spectral', 'hot', 'cool', 'binary', 'gray'] cm_cmap = [cm.bwr, cm.seismic, cm.Spectral, cm.hot, cm.cool, cm.binary, cm.gray] if mesh: plt.pcolormesh(x.astype(np.float), y.astype(np.float), z, cmap=cm_cmap[cmaptypes.index(cmaptype)]) else: plt.contourf(x, y, z, level, cmap=cm_cmap[cmaptypes.index(cmaptype)]) plt.colorbar(label=label, ) if len(zlim): plt.clim(vmin=zlim[0], vmax=zlim[1]) # labels plt.xlabel(xlab, fontsize=fz) plt.ylabel(ylab, fontsize=fz) if len(xlim): plt.xlim(xlim) if len(ylim): plt.ylim(ylim) if title: plt.title(title) if savefile: plt.savefig(savefile, dpi=dpi) plt.show() return 1
def plot2dScatter(filenames, xlim=[], ylim=[], xcol=0, ycol=0, colors=[], cmaptype='bwr', title=None, label="FES (kJ/mol)", dpi=2000, savefile=None, fz=12, xshift=0.0, yshift=0.0, xscale=1.0, yscale=1.0, timescale=0.001, xlab="X", ylab="Y", marker='x', alpha=0.8, pmf=False, bins=20, minX=1.0, sep=",", gradient=True ) : """ Get X data from the first file and Y data from next file in filenames draw a time series scatter Parameters ---------- filenames xlim ylim xcol ycol colors cmaptype title label dpi savefile fz xshift yshift xscale yscale timescale xlab ylab marker alpha pmf Returns ------- """ X = np.loadtxt(filenames[0], comments=["#", "@"], usecols=[xcol, ], dtype=float, delimiter=sep, skiprows=1) X = X * xscale + np.repeat(xshift[0], X.shape[0]) Y = np.loadtxt(filenames[0], comments=["#", "@"], usecols=[ycol, ], dtype=float, delimiter=sep, skiprows=1) Y = Y * yscale + np.repeat(yshift[0], Y.shape[0]) if pmf: algo = algorithms.BasicAlgorithm() x, y, z = algo.pmf2d(X, Y, minX=minX, bins=bins) plt.pcolormesh(x, y, z, cmap=cmaptype) cb = plt.colorbar() cb.set_label(label, fontsize=12) else: if gradient: colors = np.arange(X.shape[0]) * timescale plt.scatter(X, Y, c=colors, marker=marker, alpha=alpha, cmap=cmaptype) # draw color bar if necessary cb = plt.colorbar() cb.set_label(label, fontsize=12) else: plt.scatter(X, Y, c=colors, marker=marker, alpha=alpha) plt.xlabel(xlab, fontsize=fz) plt.ylabel(ylab, fontsize=fz) if len(xlim): plt.xlim(xlim) if len(ylim): plt.ylim(ylim) if title: plt.title(title) if savefile: plt.savefig(savefile, dpi=dpi) plt.show()