def dfunc(p, centers): #cpos = 372; natoms = 124 indr = np.arange(9, 122 * 3) natoms_r = 119 xi = p[indr].reshape((natoms_r, 3)) xi -= np.mean(xi, axis=0) xi = xi.T.astype(np.float64) d = np.empty((centers.shape[0], )) for ci, ck in enumerate(centers): cen = ck[indr].reshape((natoms_r, 3)) d[ci] = qcp.CalcRMSDRotationalMatrix(xi, cen.T.astype(np.float64), natoms_r, None, None, center_ref=0, center_conf=1, copy=0) return d.astype(pcoord_dtype)
def interpolate_conformations(confA, confB, N): assert confA.shape == confB.shape natoms = confA.shape[0] x0 = confA.copy() xN = confB.copy() # Align confA and confB x0 -= np.mean(x0, axis=0) xN -= np.mean(xN, axis=0) R = np.zeros((9, ), dtype=np.float64) rmsd = qcp.CalcRMSDRotationalMatrix(x0.T.astype(np.float64), xN.T.astype(np.float64), natoms, R, None) print('RMSD: {}'.format(rmsd)) R = np.matrix(R.reshape(3, 3)) xN = xN * R # Interpolate coordinates form x0 to xN confs = np.zeros((N, natoms * 3)) x0 = np.ravel(x0) xN = np.ravel(xN) for k in xrange(natoms * 3): confs[:, k] = np.linspace(x0[k], xN[k], N) return confs
def average_position(self, n_iter): """Get average position of replicas in each bin as of n_iter for the the user selected update interval """ nbins = self.system.bin_mapper.nbins ndim = self.system.pcoord_ndim natoms = ndim // 6 cpos = natoms * 3 avg_pos = np.zeros((nbins, ndim), dtype=self.system.pcoord_dtype) sum_bin_weight = np.zeros((nbins, ), dtype=self.system.pcoord_dtype) start_iter = max(n_iter - min(self.windowsize, n_iter), 1) stop_iter = n_iter + 1 for n in xrange(start_iter, stop_iter): with self.data_manager.lock: iter_group = self.data_manager.get_iter_group(n) seg_index = iter_group['seg_index'][...] pcoords = iter_group['pcoord'][:, -1, :] # Only read final point bin_indices = self.system.bin_mapper.assign(pcoords) weights = seg_index['weight'] uniq_indices = np.unique(bin_indices) for indx in uniq_indices: ii = np.where(bin_indices == indx)[0] bpc = pcoords[ii, :cpos] nmemb = bpc.shape[0] # (1) Align all structures to confA xref = self.strings.centers[0, :cpos].reshape((natoms, 3)) xref -= np.mean(xref, axis=0) xref = xref.T.astype(np.float64) for k in xrange(nmemb): xi = bpc[k, :].reshape((natoms, 3)) R = np.zeros((9, ), dtype=np.float64) qcp.CalcRMSDRotationalMatrix(xref, xi.T.astype(np.float64), natoms, R, None, center_ref=0, center_conf=1, copy=0) R = np.matrix(R.reshape(3, 3)) xi[:] = xi * R avg_pos[indx, :cpos] += xi.ravel() * weights[ii[k]] sum_bin_weight += np.bincount(bin_indices.astype(np.int), weights=weights, minlength=nbins) # Some bins might have zero samples so exclude to avoid divide by zero occ_ind = np.nonzero(sum_bin_weight) avg_pos[occ_ind] /= sum_bin_weight[occ_ind][:, np.newaxis] return avg_pos, sum_bin_weight
def _h_rmsd(self, coord1, coord2): return pyqcprot.CalcRMSDRotationalMatrix(coord1, coord2, None, None)
frag_b[1][6] = -10.001 frag_b[2][6] = 17.996 # Allocate rotation array rot = numpy.zeros((9,),dtype=numpy.float64) # Calculate center of geometry comA = numpy.sum(frag_a,axis=1)/N comB = numpy.sum(frag_b,axis=1)/N # Center each fragment frag_a = frag_a - comA.reshape(3,1) frag_b = frag_b - comB.reshape(3,1) # Calculate rmsd and rotation matrix rmsd = qcp.CalcRMSDRotationalMatrix(frag_a,frag_b,N,rot,None) print 'qcp rmsd = ',rmsd print 'rotation matrix:' print rot.reshape((3,3)) # Calculate rmsd after applying rotation def rmsd(a,b): """Returns RMSD between two coordinate sets a and b.""" return numpy.sqrt(numpy.sum(numpy.power(a-b,2))/a.shape[1]) # rotate frag_b to obtain optimal alignment frag_br = frag_b.T*numpy.matrix(rot.reshape((3,3))) rmsd = rmsd(frag_br.T,frag_a) print 'rmsd after applying rotation: ',rmsd
# reference centre of mass system ref_com = ref_atoms.centerOfMass() ref_coordinates = ref_atoms.coordinates() - ref_com # allocate the array for selection atom coords traj_coordinates = traj_atoms.coordinates().copy() # R: rotation matrix that aligns r-r_com, x~-x~com # (x~: selected coordinates, x: all coordinates) # Final transformed traj coordinates: x' = (x-x~_com)*R + ref_com for k, ts in enumerate(frames): # shift coordinates for rotation fitting # selection is updated with the time frame x_com = traj_atoms.centerOfMass() traj_coordinates[:] = traj_atoms.coordinates() - x_com R = numpy.zeros((9, ), dtype=numpy.float64) # Need to transpose coordinates such that the coordinate array is # 3xN instead of Nx3. Also qcp requires that the dtype be float64 a = ref_coordinates.T.astype('float64') b = traj_coordinates.T.astype('float64') rmsd[k] = qcp.CalcRMSDRotationalMatrix(a, b, natoms, R, None) R = numpy.matrix(R.reshape(3, 3)) # Transform each atom in the trajectory (use inplace ops to avoid copying arrays) ts._pos -= x_com ts._pos[:] = ts._pos * R # R acts to the left & is broadcasted N times. ts._pos += ref_com writer.write(traj.atoms) # write whole input trajectory system numpy.savetxt('rmsd.out', rmsd)