def run(self, q, p=None): p = np.random.standard_normal(q.shape) p0, q0 = p.copy(), q.copy() p -= 0.5 * self.dt * self.U.gradient(q) for t in range(self.T - 1): q += self.dt * p p -= self.dt * self.U.gradient(q) q += self.dt * p p -= 0.5 * self.dt * self.U.gradient(q) dH = (np.sum(p**2) - np.sum(p0**2)) / 2 + self.U(q) - self.U(q0) accept = False if dH <= 0 or np.log(np.random.uniform()) < -dH: accept = True if self.rmsd_max is not None: accept &= rmsd(q, q0) < self.rmsd_max if self.adapt_dt: self.dt *= 1.02 if accept else 0.98 return q
def test_lsq(self): rotation, score = self.lsq['svd'].optimum() rmsd_ = [ np.sqrt(score / len(self.coords[0])), self.lsq['svd'].rmsd(rotation.matrix), rmsd(*self.coords) ] lsq_ = [0.5 * score, self.lsq['svd'](rotation.matrix)] for name in ('euler', 'axisangle', 'expmap', 'axisangle'): dofs = self.lsq[name].trafo.from_rotation(rotation).dofs lsq_.append(self.lsq[name](dofs)) rmsd_ = np.round(rmsd_, 5) lsq_ = np.round(lsq_, 2) print(make_title('checking LSQ optimization using SVD')) print('RMSD: {0}'.format(rmsd_)) print(' LSQ: {0}'.format(lsq_)) tol = 1e-10 self.assertTrue(np.all(np.fabs(rmsd_ - rmsd_[0]) < tol)) self.assertTrue(np.all(np.fabs(lsq_ - lsq_[0]) < tol)) self.assertAlmostEqual(spin.distance( fit(*self.coords)[0], rotation.matrix), 0., delta=tol)
def find2SubGraph(self, thres, Clsts): ''' search 2 sub graph which are most likely to be in 1 rigid domain ''' #--------------------------- #Clsts = self.clusters() # ----------------------- Entry = self['DynDomEntry'] X = Entry.X # ----------------------- if len(Clsts) <= 2: print( 'Num of Par is too small to be merged!\n Return original graph' ) return (None, None, -1) Clsts_ProtG = [] val_max = -1 c1_max, c2_max = [], [] for idx1, C1 in enumerate(Clsts): for idx2, C2 in enumerate(Clsts): if idx1 < idx2: ProtG_C1 = [ idx for i in C1 for idx in self.vs[i]['Cluster'] ] ProtG_C2 = [ idx for i in C2 for idx in self.vs[i]['Cluster'] ] ProtG_C12 = [ idx for i in C1 + C2 for idx in self.vs[i]['Cluster'] ] X_1 = X[:, ProtG_C1, :] X_2 = X[:, ProtG_C2, :] X_12 = X[:, ProtG_C12, :] #if rmsd(X_1[0], X_1[1]) > rmsd(X_12[0], X_12[1]) or rmsd(X_2[0], X_2[1]) > rmsd(X_12[0], X_12[1]): # continue val = (rmsd(X_1[0], X_1[1]) + rmsd(X_2[0], X_2[1])) / rmsd( X_12[0], X_12[1]) if val > val_max: val_max = val c1_max = C1 c2_max = C2 if val_max > thres: return (c1_max, c2_max, val_max) else: return (None, None, val_max)
def report_progress(self, samples): info = (len(samples['s']), len(samples['X'][-1]), rmsd(*samples['X'][-2:]), float(samples['s'][-1]), samples['r_min'][-1], samples['eps'][-1], self.posteriors['X'].sampler.dt, np.sum(self.posteriors['X'].likelihood.N < 1.), radius_of_gyration(samples['X'][-1])) print GibbsSampler.output.format(*info)
def rmsd(self, Vertex_IDs=None): if Vertex_IDs is None: Vertex_IDs = range(len(self.vs)) Entry = self['DynDomEntry'] X = Entry.X Prot_IDs = [idx for i in Vertex_IDs for idx in self.vs[i]['Cluster']] X_Part = X[:, Prot_IDs, :] N = X_Part.shape[0] Arr = [] for idx1, x1 in enumerate(X_Part): for idx2, x2 in enumerate(X_Part): if idx1 < idx2: Arr.append(rmsd(x1, x2)) return np.max(Arr)
def test_rmsd(self): R, t = fit(*self.coords) out = '{0:.2f} ({1})' print(make_title('RMSD')) print(out.format(rmsd(*self.coords), 'SVD')) rmsds = [] for trafo in self.trafos[1:]: trafo.matrix_vector = R, t r = np.mean(np.sum((self.coords[0] - trafo(self.coords[1]))**2, 1))**0.5 print(out.format(r, trafo.rotation.name)) rmsds.append(r) self.assertAlmostEqual(np.std(rmsds), 0., delta=1e-10)
from csb.bio.utils import rmsd x = cg.load_example('4ake') N = len(x) K = N / 20 X = cg.kmeans(x, K) K = len(X) k = 10 params = cg.Parameters(x, X) params2 = cg.Parameters(x, X) params2._Z = np.zeros((len(x), k), 'i') print params.Z.shape print params._Z.shape L = cg.Likelihood(x, params) L2 = cg.KDLikelihood(x, params2, k=k) params.Z[...] = np.equal.outer(L.distances.argmin(1), np.arange(K)) params2.Z[...] = np.equal.outer(L2.distances.argmin(1), np.arange(k)) print L.chi2, L2.chi2 L.update_stats() L2.update_stats() print rmsd(L.mu, L2.mu), np.fabs(L.N - L2.N).max()
n_samples, int(config['replica']['samples_dump_interval']), burnin=burnin) ens = np.array([ sample.variables['structures'].reshape(-1, len(knowns[0]), 3) for sample in samples ]) ens_flat = ens.reshape(ens.shape[0] * ens.shape[1], -1, 3) figures_folder = output_folder + 'analysis/compare_to_known/' if not os.path.exists(figures_folder): os.makedirs(figures_folder) if True: ## plot histograms of RMSDs to known structures rmsds = [map(lambda x: rmsd(known, x), ens_flat) for known in knowns] max_rmsd = np.max(rmsds) min_rmsd = np.min(rmsds) fig = plt.figure() for i, known in enumerate(knowns): ax = fig.add_subplot(len(knowns), 1, i + 1) ax.hist(rmsds[i], bins=np.linspace(0.0, 6, np.sqrt(len(ens_flat)))) ax.set_xlabel('RMSD to ' + fnames[i]) #ax.set_xlim((0.3, 4.0)) fig.tight_layout() if save_figures: plt.savefig(figures_folder + 'RMSDs.pdf') else: plt.show()
ax.plot((true2_rg, true2_rg), (0, max(h[0])), c='b', label=label2) ax.set_xlabel('radius of gyration') ax.set_ylabel('# structures') ax.set_yticks([]) ax.legend() if show_plots: plt.show() if not True: from csb.bio.utils import rmsd rgs = map(radius_of_gyration, structures) rmsds1 = np.array(map(lambda x: rmsd(true1, x), structures)) rmsds2 = np.array(map(lambda x: rmsd(true2, x), structures)) n_bins = 80 fig = plt.figure() ax = fig.add_subplot(221) ax.hist(rmsds1, n_bins, label=label1, alpha=0.6) ax.set_xlabel('RMSD to {}'.format(label1)) ax.set_ylabel('# structures') ax = fig.add_subplot(222) ax.hist(rmsds2, n_bins, label=label2, alpha=0.6) ax.set_xlabel('RMSD to {}'.format(label2)) ax.set_ylabel('# structures') structures_flipped1 = structures.copy()
cpath = sims['common_path'] n_structures = sims['n_structures'] odirs = sims['output_dirs'] MAP_samples = [] for (n, odir) in zip(n_structures, odirs): cfg_file = cpath + odir + '/config.cfg' p = make_posterior(parse_config_file(cfg_file)) samples = load_samples_from_cfg(cfg_file, burnin=60000) Es = map(lambda x: -p.log_prob(**x.variables), samples) MAP_sample = samples[np.argmin(Es)] MAP_samples.append(MAP_sample.variables['structures'].reshape(n, 56, 3)) MAP_samples_flat = np.array([x for y in MAP_samples for x in y]) invar_rmsd = lambda x, y: min(rmsd(x, y), rmsd(x, -y)) tmp = StructureParser( os.path.expanduser('~/projects/ensemble_hic/data/proteins/1pga.pdb')) ref_1pga = tmp.parse().get_coordinates(['CA']) tmp = StructureParser( os.path.expanduser('~/projects/ensemble_hic/data/proteins/1shf.pdb')) ref_1shf = tmp.parse().get_coordinates(['CA']) rmsds_to_1pga = map(lambda x: invar_rmsd(ref_1pga, x), MAP_samples_flat) rmsds_to_1shf = map(lambda x: invar_rmsd(ref_1shf, x), MAP_samples_flat) labels = (np.array(rmsds_to_p1) < np.array(rmsds_to_p2)) for i, s in enumerate(MAP_samples_flat): ref = (ref_1pga, ref_1shf)[labels[i]] if rmsd(ref, -s) < rmsd(ref, s): MAP_samples_flat[i] *= -1 MAP_samples_flat[i] = fit_transform(ref, MAP_samples_flat[i])
[rex.history[pair].acceptance_rate() for pair in rex.history.pairs]) if False: from csb.bio.utils import rmsd from scipy.spatial.distance import squareform burnin = -400 thining = 1 #0 mask = np.ones(333, 'i') mask[:11] = 0 mask[47:66] = 0 x = X[burnin::thining].reshape(-1, 333, 3) x = np.compress(mask, x, 1) d = [ rmsd(xx, x[j]) for i, xx in enumerate(x) for j in range(i + 1, len(x)) ] from sklearn.cluster import spectral_clustering K = 4 membership = spectral_clustering(np.exp(-squareform(d)), n_clusters=K, eigen_solver='arpack') i = np.argsort(membership) matshow(squareform(d)[i][:, i], origin='lower')
def testRmsdMirrorImage(self): X, Y = X7, X7.copy() Y[:, 0] *= -1 rmsd = cbu.rmsd(X, Y) self.assertAlmostEqual(rmsd, 50.0)
def testRmsd(self): rmsd = cbu.rmsd(X1, X2) self.assertAlmostEqual(rmsd, (4./3.)**0.5)