def computeRMSD(): if len(ca_atoms) != len(ca_atoms_pdb): print "Error. Length mismatch!" exit() l = len(ca_atoms) fixed_coord = [] moving_coord = [] for i in range(l): if include_res_map[i] == 1: fixed_coord.append( [ca_atoms_pdb[i][0], ca_atoms_pdb[i][1], ca_atoms_pdb[i][2]]) moving_coord.append( [ca_atoms[i][0], ca_atoms[i][1], ca_atoms[i][2]]) if len(fixed_coord) == 0: return 0 fixed_coord = numpy.array(fixed_coord) moving_coord = numpy.array(moving_coord) sup = SVDSuperimposer() sup.set(fixed_coord, moving_coord) sup.run() rms = sup.get_rms() return rms
def compute_deviations(reader, mean_structure, indexed_mean_structure, indexes, num_confs, start=None, stop=None): """ Computes RMSF of each particle from the mean structure Parameters: reader (readers.ErikReader): An active reader on the trajectory file to analyze. mean_structure (numpy.array): The position of each particle in the mean configuration. A 3xN array. num_confs (int): The number of configurations in the reader. <optional> start (int): The starting configuration ID to begin averaging at. Used if parallel. <optional> stop (int): The configuration ID on which to end the averaging. Used if parallel. Returns: deviations (list): Each entry in the list is a numpy.array of the deviations for each particle at a given time. """ if stop is None: stop = num_confs else: stop = int(stop) if start is None: start = 0 else: start = int(start) confid = 0 # Use the single-value decomposition method for superimposing configurations sup = SVDSuperimposer() deviations = [] RMSDs = [] mysystem = reader.read(n_skip=start) while mysystem != False and confid < stop: mysystem.inbox() # calculate alignment transform cur_conf = mysystem.positions indexed_cur_conf = cur_conf[indexes] sup.set(indexed_mean_structure, indexed_cur_conf) sup.run() print("Frame number:", confid, "Time:", mysystem.time, "RMSD:", sup.get_rms()) # realign frame rot, tran = sup.get_rotran() # align structures and collect coordinates for each frame # compatible with json deviations.append( list( np.linalg.norm(np.einsum('ij, ki -> kj', rot, cur_conf) + tran - mean_structure, axis=1))) RMSDs.append(sup.get_rms() * 0.8518) confid += 1 mysystem = reader.read() return (deviations, RMSDs)
def computeRMSD(): if len(ca_atoms) != len(ca_atoms_pdb): print "Error. Length mismatch!" exit() l = len(ca_atoms) res = {} for ch in chain_id_list: fixed_coord = [] moving_coord = [] for i in range(l): if chain_id[i] == ch: fixed_coord.append([ ca_atoms_pdb[i][0], ca_atoms_pdb[i][1], ca_atoms_pdb[i][2] ]) moving_coord.append( [ca_atoms[i][0], ca_atoms[i][1], ca_atoms[i][2]]) if len(fixed_coord) > 0: fixed_coord = numpy.array(fixed_coord) moving_coord = numpy.array(moving_coord) sup = SVDSuperimposer() sup.set(fixed_coord, moving_coord) sup.run() rms = sup.get_rms() res[ch] = rms return res
def compute_centroid(reader, mean_structure, num_confs, start=None, stop=None): """ Compares each structure to the mean and returns the one with the lowest RMSF Parameters: reader (readers.LorenzoReader2): An active reader on the trajectory file to analyze. mean_structure (numpy.array): The position of each particle in the mean configuration. A 3xN array. num_confs (int): The number of configurations in the reader. <optional> start (int): The starting configuration ID to begin averaging at. Used if parallel. <optional> stop (int): The configuration ID on which to end the averaging. Used if parallel. Returns: centroid (numpy.array): The positions corresponding to the structure with the lowest RMSF to the mean. """ if stop is None: stop = num_confs else: stop = int(stop) if start is None: start = 0 else: start = int(start) confid = 0 # Use the single-value decomposition method for superimposing configurations sup = SVDSuperimposer() lowest_rmsf = 100000 #if you have a larger number than this, we need to talk... centroid_candidate = np.zeros_like(mean_structure) centroid_a1 = np.zeros_like(mean_structure) centroid_a3 = np.zeros_like(mean_structure) mysystem = reader.read(n_skip=start) while mysystem != False and confid < stop: mysystem.inbox() # calculate alignment transform cur_conf = mysystem.positions indexed_cur_conf = mysystem.positions[indexes] cur_conf_a1 = mysystem.a1s cur_conf_a3 = mysystem.a3s sup.set(mean_structure, indexed_cur_conf) sup.run() rot, tran = sup.get_rotran() cur_conf = np.einsum('ij, ki -> kj', rot, cur_conf) + tran cur_conf_a1 = np.einsum('ij, ki -> kj', rot, cur_conf_a1) cur_conf_a3 = np.einsum('ij, ki -> kj', rot, cur_conf_a3) RMSF = sup.get_rms() print("Frame number:", confid, "RMSF:", RMSF) if RMSF < lowest_rmsf: centroid_candidate = cur_conf centroid_a1 = cur_conf_a1 centroid_a3 = cur_conf_a3 lowest_rmsf = RMSF centroid_t = mysystem.time confid += 1 mysystem = reader.read() return centroid_candidate, centroid_a1, centroid_a3, lowest_rmsf, centroid_t
def RMSD_biopython(self, x, y): """Kalbsch algorithm""" sup = SVDSuperimposer() sup.set(x, y) sup.run() rot, tran = sup.get_rotran() return rot
def cal_rmsd(recon_c, raw_c, mask, gapmask): ### mask: max_len때문에 맞춰주는 것 ### gapmask: gap때문에 맞춰주는 것 try: mask = mask.bool().cpu().numpy() gapmask = gapmask.bool().cpu().numpy() raw_c = raw_c.view(-1, 3).cpu().numpy() target_atoms = ['N', 'CA', 'C', 'O'] recon_coords = {c: list() for c in target_atoms} for atom in recon_c.get_atoms(): atom_n = atom.get_name() if atom_n in target_atoms: recon_coords[atom_n].append(atom.get_coord()) for c in target_atoms: recon_coords[c] = np.stack(recon_coords[c]) recon_backbone = np.stack((recon_coords[c] for c in target_atoms), axis=1).reshape(-1, 3) a = np.repeat(mask, 4) sup = SVDSuperimposer() sup.set(raw_c[np.repeat(gapmask, 4)].reshape(-1, 3), recon_backbone[np.repeat(gapmask[mask], 4)].reshape(-1, 3)) sup.run() rot, trans = sup.get_rotran() transform_c = np.dot(recon_backbone, rot) + trans diff = raw_c[np.repeat(gapmask, 4)] - transform_c[np.repeat( gapmask[mask], 4)] rmsd = np.sqrt(np.sum(diff * diff) / np.sum(gapmask * 4)) except: rmsd = -1 return rmsd
def rmsd_distance(points, ref_points, sup_atoms, rmsd_atoms=None, multiple_rmsd_variants=False): (c1, c2) = points (p1, p2) = ref_points for atoms_list, c_res, p_res in (sup_atoms[0], c1, p1), (sup_atoms[1], c2, p2): for a in atoms_list: if a not in c_res or a not in p_res: return 1000.0 ref_p = [p1[a] for a in sup_atoms[0]] + [p2[a] for a in sup_atoms[1]] cur_p = [c1[a] for a in sup_atoms[0]] + [c2[a] for a in sup_atoms[1]] sup = SVDSuperimposer() sup.set(np.array(ref_p, 'f'), np.array(cur_p, 'f')) sup.run() if rmsd_atoms is not None: (rot, tran) = sup.get_rotran() if multiple_rmsd_variants: return min([ _rmsd_formula(points, ref_points, rot, tran, r) for r in rmsd_atoms ]) else: return _rmsd_formula(points, ref_points, rot, tran, rmsd_atoms) else: return sup.get_rms()
def create_DM(pdb_atoms_list, alignement_type): """ This function is used to clusterize all structures. """ svd = SVDSuperimposer() size = len(pdb_atoms_list) FullDM = DistanceMatrix(size) pbar = pg.ProgressBar(widgets=WIDGETS, maxval=(size * (size - 1) / 2)).start() counter = 0 for i, frame1 in enumerate(pdb_atoms_list): for j, frame2 in enumerate(pdb_atoms_list[i + 1:]): svd.set(frame1, frame2) svd.run() rms = svd.get_rms() #RMS with local alignement rms_raw = svd.get_init_rms( ) #RMS with the GLOBAL alignement made before if alignement_type == "l": FullDM.set(i, i + j + 1, rms) else: FullDM.set(i, i + j + 1, rms_raw) pbar.update(counter) counter += 1 pbar.finish() return FullDM
def normalize_points(points, n_type): assert n_type in NORMALIZED_BASE assert n_type in BASE_ATOMS p1, p2 = points norm_vec = [] p_vec = [] for a in BASE_ATOMS[n_type]: assert a in NORMALIZED_BASE[n_type] if a in p1: norm_vec.append(NORMALIZED_BASE[n_type][a]) p_vec.append(p1[a]) if len(p_vec) < 3: return (None, None) sup = SVDSuperimposer() sup.set(np.array(norm_vec, 'f'), np.array(p_vec, 'f')) sup.run() (rot, tran) = sup.get_rotran() new_points = [] for p in (p1, p2): atoms = list(p.keys()) vec = [] for a in atoms: vec.append(p[a]) new_vec = np.dot(np.array(vec, 'f'), rot) + tran new_points.append(dict(list(zip(atoms, new_vec)))) return new_points
def compute_transformation(c_ref,c): sup = SVDSuperimposer() sup.set(c_ref, c) sup.run() rms = sup.get_rms() (rot,tran) = sup.get_rotran() return (rms,rot,tran)
def align(indexes, ref_conf, mysystem): """ Aligns a single frame to the reference configuration. Parameters: indexes (list): The indexes of the particles to align. ref_conf (base_array): The reference configuration to align to. mysystem (base_array): The configuration to align. Returns: str: The aligned configuration in the format of the original trajectory file. """ sup = SVDSuperimposer() #Need to get rid of fix_diffusion artifacts or SVD doesn't work mysystem.inbox() indexed_cur_conf = mysystem.positions[indexes] #Superimpose the configuration to the reference sup.set(ref_conf.positions[indexes], indexed_cur_conf) sup.run() rot, tran = sup.get_rotran() #Apply rotation and translation in one step mysystem.positions = np.einsum('ij, ki -> kj', rot, mysystem.positions) + tran mysystem.a1s = np.einsum('ij, ki -> kj', rot, mysystem.a1s) mysystem.a3s = np.einsum('ij, ki -> kj', rot, mysystem.a3s) return mysystem.conf_to_str() # we finally need a string
def get_rot_tran(y, x): """Returns rotation, translation and RMDS values of the superimposed atoms.""" sup = SVDSuperimposer() sup.set(x, y) # AC over AD sup.run() rms = sup.get_rms() rot, tran = sup.get_rotran() return (rot, tran, rms)
def compute_deviations(reader, mean_structure, num_confs, start=None, stop=None): """ Computes RMSF of each particle from the mean structure Parameters: reader (readers.LorenzoReader2): An active reader on the trajectory file to analyze. mean_structure (numpy.array): The position of each particle in the mean configuration. A 3xN array. num_confs (int): The number of configurations in the reader. <optional> start (int): The starting configuration ID to begin averaging at. Used if parallel. <optional> stop (int): The configuration ID on which to end the averaging. Used if parallel. Returns: deviations (list): Each entry in the list is a numpy.array of the deviations for each particle at a given time. """ if stop is None: stop = num_confs else: stop = int(stop) if start is None: start = 0 else: start = int(start) confid = 0 # helper to fetch nucleotide positions fetch_np = lambda conf: np.array([n.cm_pos for n in conf._nucleotides]) # Use the single-value decomposition method for superimposing configurations sup = SVDSuperimposer() deviations = [] mysystem = reader._get_system(N_skip=start) while mysystem != False and confid < stop: mysystem.inbox_system() # calculate alignment transform cur_conf = fetch_np(mysystem) sup.set(mean_structure, cur_conf) sup.run() print("Frame number:", confid, "RMSF:", sup.get_rms()) # realign frame rot, tran = sup.get_rotran() # align structures and collect coordinates for each frame # compatible with json deviations.append( list( map( np.linalg.norm, np.array([np.dot(n_pos, rot) + tran for n_pos in cur_conf]) - mean_structure))) confid += 1 mysystem = reader._get_system() return deviations
def superimpose(X, Xtag): try: sup = SVDSuperimposer() sup.set(X, Xtag) sup.run() Xtag_super = sup.get_transformed() return Xtag_super except: return np.zeros((X.shape[0],X.shape[1]))
def doublets_dist(d1, d2): sup = SVDSuperimposer() sup.set(d1['vec'], d2['vec']) sup.run() rms1 = sup.get_rms() sup.set(d1['vec'], d2['vec2']) sup.run() rms2 = sup.get_rms() return min(rms1, rms2)
def super_pdb(coords1, coords2): if len(coords1) != len(coords2): print >> sys.stderr, 'ERROR: Structures with different length' sys.exit(1) svd = SVDSuperimposer() svd.set(np.array(coords1), np.array(coords2)) svd.run() rot, tran = svd.get_rotran() rmsd = svd.get_rms() return rmsd
def calculate_rmsd(atoms_x_coord, atoms_y_coord) -> float: super_imposer = SVDSuperimposer( ) # Calling the class that superposes atoms arrays super_imposer.set( atoms_x_coord, atoms_y_coord) # Vector y will be rotated and translated on vector x super_imposer.run() value = super_imposer.get_rms() # Get the value of RMSD return value
def change_basis(reader, align_conf, components, num_confs, start=None, stop=None): """ Transforms each configuration in a trajectory into a point in principal component space Parameters: reader (readers.ErikReader): An active reader on the trajectory file to analyze. align_conf (numpy.array): The position of each particle in the mean configuration. A 3xN array. components (numpy.array): The principal components of the trajectory. A 3*Nx3*N array. num_confs (int): The number of configurations in the reader. <optional> start (int): The starting configuration ID to begin averaging at. Used if parallel. <optional> stop (int): The configuration ID on which to end the averaging. Used if parallel. Returns: coordinates (numpy.array): The positions of each frame of the trajectory in principal component space. """ if stop is None: stop = num_confs else: stop = int(stop) if start is None: start = 0 else: start = int(start) mysystem = reader.read(n_skip=start) coordinates = np.empty((stop, len(mysystem.positions) * 3)) coordinates2 = np.empty((stop, len(mysystem.positions) * 3)) sup = SVDSuperimposer() confid = 0 while mysystem != False and confid < stop: print("-->", "frame", confid, "time={}".format(mysystem.time)) mysystem.inbox() cur_conf = mysystem.positions sup.set(align_conf, cur_conf) sup.run() rot, tran = sup.get_rotran() #equivalent to taking the dot product of the rotation array and every vector in the deviations array cur_conf = np.einsum('ij, ki -> kj', rot, cur_conf) + tran coordinates[confid] = np.dot(components, cur_conf.flatten()) confid += 1 mysystem = reader.read() return (coordinates)
def get_rmsd(coord1,coord2): if len(coord1)!=len(coord2): print >> sys.stderr.write("ERROR: The set of Coordinate have different size.") sys.exit(1) svd=SVDSuperimposer() svd.set(np.array(coord1), np.array(coord2)) svd.run() rmsd=svd.get_rms() #rot,tran=svd.get_rotran() T=svd.get_rotran() print("R", T[0]) print("T", T[1]) return(rmsd)
def get_rmsd(coord1, coord2): if len(coord1) != len(coord2): print >> sys.stderr, "ERROR: The sets of coordinates have different sizes" sys.exit(1) #system error >/dev/null or 2>/dev/null svd = SVDSuperimposer() svd.set(np.array(coord1), np.array(coord2)) #transform a list into numeric python svd.run() rmsd = svd.get_rms() rot, tran = svd.get_rotran() print 'R', rot print 'T', tran print 'RMSD', rmsd
def __sub__(self, other): """ Return rmsd between two fragments. Example: >>> rmsd=fragment1-fragment2 @return: rmsd between fragments @rtype: float """ sup=SVDSuperimposer() sup.set(self.coords_ca, other.coords_ca) sup.run() return sup.get_rms()
def __sub__(self, other): """ Return rmsd between two fragments. Example: >>> rmsd=fragment1-fragment2 @return: rmsd between fragments @rtype: float """ sup = SVDSuperimposer() sup.set(self.coords_ca, other.coords_ca) sup.run() return sup.get_rms()
def Superimpose(atoms1, atoms2): assert len(atoms1) == len(atoms2) #aligner = QCPSuperimposer() aligner = SVDSuperimposer() aligner.set(atoms1, atoms2) aligner.run() RMSD = aligner.get_rms() ## calculate the distance deviation at each position atoms2_transformed = aligner.get_transformed() diff = atoms1 - atoms2_transformed diff2 = np.power(diff, 2) deviations = np.sqrt(np.sum(diff2, axis=1)) return RMSD, deviations
def get_cov(reader, align_conf, num_confs, start=None, stop=None): """ Performs principal component analysis on deviations from the mean structure Parameters: reader (readers.ErikReader): An active reader on the trajectory file to analyze. align_conf (numpy.array): The position of each particle in the mean configuration. A 3xN array. num_confs (int): The number of configurations in the reader. <optional> start (int): The starting configuration ID to begin averaging at. Used if parallel. <optional> stop (int): The configuration ID on which to end the averaging. Used if parallel. Returns: deviations_marix (numpy.array): The difference in position from the mean for each configuration. """ if stop is None: stop = num_confs else: stop = int(stop) if start is None: start = 0 else: start = int(start) mysystem = reader.read(n_skip=start) covariation_matrix = np.zeros( (len(mysystem.positions) * 3, len(mysystem.positions) * 3)) sup = SVDSuperimposer() confid = 0 #for every configuration in the trajectory chunk, align it to the mean and compute positional difference for every particle while mysystem != False and confid < stop: print("-->", "frame", confid, "time={}".format(mysystem.time)) mysystem.inbox() cur_conf = mysystem.positions sup.set(align_conf, cur_conf) sup.run() rot, tran = sup.get_rotran() #equivalent to taking the dot product of the rotation array and every vector in the deviations array cur_conf = np.einsum('ij, ki -> kj', rot, cur_conf) + tran difference_matrix = (cur_conf - align_conf).flatten() covariation_matrix += np.einsum('i,j -> ij', difference_matrix, difference_matrix) confid += 1 mysystem = reader.read() return covariation_matrix
def run_sup3d(coord1, coord2): sup = SVDSuperimposer() sup.set( np.array(coord1), np.array(coord2) ) #set is setting the group of coordinates because i have initialized SVD, it is empty sup.run( ) #superimpose the coordinates, run does all the work. Then we compute the RMSD between vc1 and vc2 after transformation rmsd = sup.get_rms() rot, tran = sup.get_rotran( ) #shows the matrix of rotation and vector for translation tcoord = sup.get_transformed() print rmsd print rot print tran print tcoord #you obtain the set of coordinates to be superimposable to the se 1, so the set of coordinates after transformation. return
def _superimpose_atoms(ref_points, points, atoms): if ref_points is None or points is None or atoms is None: return (None, None, None, None) ref_vec = [] vec = [] for a in atoms: if a in ref_points and a in points: ref_vec.append(ref_points[a]) vec.append(points[a]) if len(vec) < 3: return (None, None, None, None) sup = SVDSuperimposer() sup.set(np.array(ref_vec, 'f'), np.array(vec, 'f')) sup.run() (rot, tran) = sup.get_rotran() rms = sup.get_rms() return (_apply_rot_tran(points, rot, tran), rot, tran, rms)
def computeRMSD(): if len(ca_atoms)!=len(ca_atoms_pdb): print "Error. Length mismatch!", len(ca_atoms), len(ca_atoms_pdb) exit() l = len(ca_atoms) fixed_coord = numpy.zeros((l, 3)) moving_coord = numpy.zeros((l, 3)) for i in range(0, l): fixed_coord[i] = numpy.array ([ca_atoms_pdb[i][0], ca_atoms_pdb[i][1], ca_atoms_pdb[i][2]]) moving_coord[i] = numpy.array ([ca_atoms[i][0], ca_atoms[i][1], ca_atoms[i][2]]) sup = SVDSuperimposer() sup.set(fixed_coord, moving_coord) sup.run() rms = sup.get_rms() return rms
def __sub__(self, other): """Return rmsd between two fragments. :return: rmsd between fragments :rtype: float Examples -------- This is an incomplete but illustrative example:: rmsd = fragment1 - fragment2 """ sup = SVDSuperimposer() sup.set(self.coords_ca, other.coords_ca) sup.run() return sup.get_rms()
def distance_matrix(CA): n_models = CA.shape[0] distances = np.zeros((n_models, n_models)) sup=SVDSuperimposer() for i in range(n_models): model1 = CA[i,:,:] for j in range(i+1,n_models): model2 = CA[j,:,:] sup.set(model1, model2) sup.run() rms=sup.get_rms() distances[i,j] = rms distances[j,i] = rms return distances
def get_pca(reader, align_conf, num_confs, start=None, stop=None): """ Performs principal component analysis on deviations from the mean structure Parameters: reader (readers.LorenzoReader2): An active reader on the trajectory file to analyze. mean_structure (numpy.array): The position of each particle in the mean configuration. A 3xN array. num_confs (int): The number of configurations in the reader. <optional> start (int): The starting configuration ID to begin averaging at. Used if parallel. <optional> stop (int): The configuration ID on which to end the averaging. Used if parallel. Returns: deviations_marix (numpy.array): The difference in position from the mean for each configuration. """ if stop is None: stop = num_confs else: stop = int(stop) if start is None: start = 0 else: start = int(start) mysystem = reader._get_system(N_skip=start) deviations_matrix = np.empty((stop, (len(align_conf)) * 3)) sup = SVDSuperimposer() confid = 0 #for every configuration in the trajectory chunk, align it to the mean and compute positional difference for every particle while mysystem != False and confid < stop: print("-->", mysystem._time) mysystem.inbox() cur_conf = fetch_np(mysystem) sup.set(align_conf, cur_conf) sup.run() rot, tran = sup.get_rotran() #equivalent to taking the dot product of the rotation array and every vector in the deviations array cur_conf = np.einsum('ij, ki -> kj', rot, cur_conf) + tran deviations_matrix[confid] = (cur_conf - align_conf).flatten() confid += 1 mysystem = reader._get_system() return deviations_matrix
def sel_straight(coords_arr, n_cc_helices): n_atoms_mono = int(coords_arr[0].shape[0] / n_cc_helices) chain_rmss = [] for coords in coords_arr: hi_all = [] for i in range(n_cc_helices): hi_all.append(coords[i * n_atoms_mono:(i + 1) * n_atoms_mono]) rmss = [] for i in range(n_cc_helices - 1): sup = SVDSuperimposer() sup.set(hi_all[i], hi_all[i + 1]) sup.run() rms = sup.get_rms() rmss.append(rms) chain_rmss.append(np.mean(rmss)) return np.argmin(chain_rmss), np.min(chain_rmss)
def computeRMSD(): if len(ca_atoms) != len(ca_atoms_pdb): print "Error. Length mismatch!" exit() l = len(ca_atoms) fixed_coord = numpy.zeros((l, 3)) moving_coord = numpy.zeros((l, 3)) for i in range(0, l): fixed_coord[i] = numpy.array( [ca_atoms_pdb[i][0], ca_atoms_pdb[i][1], ca_atoms_pdb[i][2]]) moving_coord[i] = numpy.array( [ca_atoms[i][0], ca_atoms[i][1], ca_atoms[i][2]]) sup = SVDSuperimposer() sup.set(fixed_coord, moving_coord) sup.run() rms = sup.get_rms() return rms
def set_atoms(self, fixed, moving): """Put (translate/rotate) the atoms in fixed on the atoms in moving, in such a way that the RMSD is minimized. @param fixed: list of (fixed) atoms @param moving: list of (moving) atoms @type fixed,moving: [L{Atom}, L{Atom},...] """ if not len(fixed) == len(moving): raise PDBException("Fixed and moving atom lists differ in size") l = len(fixed) fixed_coord = numpy.zeros((l, 3)) moving_coord = numpy.zeros((l, 3)) for i in range(0, len(fixed)): fixed_coord[i] = fixed[i].get_coord() moving_coord[i] = moving[i].get_coord() sup = SVDSuperimposer() sup.set(fixed_coord, moving_coord) sup.run() self.rms = sup.get_rms() self.rotran = sup.get_rotran()
def __init__(self, static, moving): """ Align two structures :param static: the reference structure :param moving: the structure to the aligned to the reference """ sup = SVDSuperimposer() sup.set(np.asarray(static), np.asarray(moving)) sup.run() rot, trans = sup.get_rotran() self.rms = sup.get_rms() self.static = static self.moving = [ np.dot(np.asarray(moving[atom]), rot) + trans for atom in range(len(moving)) ]
def compute_frag_RMSD(res_len): if len(ca_atoms)!=len(ca_atoms_pdb): print "Error. Length mismatch! target:frag", len(ca_atoms_pdb), len(ca_atoms) return 0 l = len(ca_atoms) N = res_len if l != N : print "atom list length mismatches the fragment length!", str(l), str(N) return 0 fixed_coord = numpy.zeros((l, 3)) moving_coord = numpy.zeros((l, 3)) for i in range(0, l): fixed_coord[i] = numpy.array ([ca_atoms_pdb[i][0], ca_atoms_pdb[i][1], ca_atoms_pdb[i][2]]) moving_coord[i] = numpy.array ([ca_atoms[i][0], ca_atoms[i][1], ca_atoms[i][2]]) sup = SVDSuperimposer() sup.set(fixed_coord, moving_coord) sup.run() rms = sup.get_rms() return rms
def align_models(CA): n_models = CA.shape[0] working_CA = np.copy(CA) sup=SVDSuperimposer() ref_model = working_CA[0, :, :] rms_total = 0 for i_model in range(1, n_models): sup.set(ref_model, working_CA[i_model]) sup.run() rms_total += sup.get_rms()**2 working_CA[i_model] = sup.get_transformed() rms_best = float("inf") epsilon = 0.001 while rms_best - rms_total > epsilon: rms_best = rms_total mean_model = np.mean(working_CA,0) rms_total = 0 for i_model in range(n_models): sup.set(mean_model, working_CA[i_model]) sup.run() rms_total += sup.get_rms()**2 working_CA[i_model] = sup.get_transformed() transformations = [] for start_model, result_model in zip(CA, working_CA): sup.set(result_model, start_model) sup.run() transformations.append(sup.get_rotran()) return transformations,np.sqrt(rms_total/n_models)
[50.68, -0.04, 51.54], [50.22, -0.02, 52.85]], 'f') y = array([[51.30, -2.99, 46.54], [51.09, -1.88, 47.58], [52.36, -1.20, 48.03], [52.71, -1.18, 49.38]], 'f') sup = SVDSuperimposer() # set the coords # y will be rotated and translated on x sup.set(x, y) # do the lsq fit sup.run() # get the rmsd rms = sup.get_rms() # get rotation (right multiplying!) and the translation rot, tran = sup.get_rotran() # rotate y on x manually y_on_x1 = dot(y, rot) + tran # same thing y_on_x2 = sup.get_transformed() def simple_matrix_print(matrix):
class SVDSuperimposerTest(unittest.TestCase): def setUp(self): self.x = array([[51.65, -1.90, 50.07], [50.40, -1.23, 50.65], [50.68, -0.04, 51.54], [50.22, -0.02, 52.85]]) self.y = array([[51.30, -2.99, 46.54], [51.09, -1.88, 47.58], [52.36, -1.20, 48.03], [52.71, -1.18, 49.38]]) self.sup = SVDSuperimposer() self.sup.set(self.x, self.y) def test_get_init_rms(self): x = array([[1.19, 1.28, 1.37], [1.46, 1.55, 1.64], [1.73, 1.82, 1.91]]) y = array([[1.91, 1.82, 1.73], [1.64, 1.55, 1.46], [1.37, 1.28, 1.19]]) self.sup.set(x, y) self.assertIsNone(self.sup.init_rms) init_rms = 0.8049844719 self.assertTrue( float('%.3f' % self.sup.get_init_rms()), float('%.3f' % init_rms)) def test_oldTest(self): self.assertTrue( array_equal(around(self.sup.reference_coords, decimals=3), around(self.x, decimals=3))) self.assertTrue( array_equal(around(self.sup.coords, decimals=3), around(self.y, decimals=3))) self.assertIsNone(self.sup.rot) self.assertIsNone(self.sup.tran) self.assertIsNone(self.sup.rms) self.assertIsNone(self.sup.init_rms) self.sup.run() self.assertTrue( array_equal(around(self.sup.reference_coords, decimals=3), around(self.x, decimals=3))) self.assertTrue( array_equal(around(self.sup.coords, decimals=3), around(self.y, decimals=3))) rot = array([[0.68304983, 0.53664371, 0.49543563], [-0.52277295, 0.83293229, -0.18147242], [-0.51005037, -0.13504564, 0.84947707]]) tran = array([38.78608157, -20.65451334, -15.42227366]) self.assertTrue( array_equal(around(self.sup.rot, decimals=3), around(rot, decimals=3))) self.assertTrue( array_equal(around(self.sup.tran, decimals=3), around(tran, decimals=3))) self.assertIsNone(self.sup.rms) self.assertIsNone(self.sup.init_rms) rms = 0.00304266526014 self.assertEqual( float('%.3f' % self.sup.get_rms()), float('%.3f' % rms)) rot_get, tran_get = self.sup.get_rotran() self.assertTrue( array_equal(around(rot_get, decimals=3), around(rot, decimals=3))) self.assertTrue( array_equal(around(tran_get, decimals=3), around(tran, decimals=3))) y_on_x1 = dot(self.y, rot) + tran y_x_solution = array( [[5.16518846e+01, -1.90018270e+00, 5.00708397e+01], [5.03977138e+01, -1.22877050e+00, 5.06488200e+01], [5.06801788e+01, -4.16095666e-02, 5.15368866e+01], [5.02202228e+01, -1.94372374e-02, 5.28534537e+01]]) self.assertTrue( array_equal(around(y_on_x1, decimals=3), around(y_x_solution, decimals=3))) y_on_x2 = self.sup.get_transformed() self.assertTrue( array_equal(around(y_on_x2, decimals=3), around(y_x_solution, decimals=3)))
def run_system(dir): pdb = os.path.basename(dir).split('_')[0] org_dir = os.getcwd() os.chdir(dir) f_coord = "coord.h5" f_RMSD = "RMSD.txt" f_OC = os.path.join("..","..","cmap_coordinates",pdb+'.txt') if not os.path.exists(f_OC): print "Missing coordinates for cmap", dir os.chdir(org_dir) return dir if not os.path.exists(f_coord): print "Missing coordinates, extract_coordinates.py first", dir os.chdir(org_dir) return dir if os.path.exists(f_RMSD) and not _FORCE: print "RMSD file exists, skipping", dir os.chdir(org_dir) return dir h5 = h5py.File(f_coord,'r') C = h5["coord"][:] h5.close() OC = np.loadtxt(f_OC) # Move the coordinates to something sensible #C -= C.mean(axis=0) #OC -= OC.mean(axis=0) median_OC = np.median([np.linalg.norm(a-b) for a,b in zip(OC,OC[1:])]) median_C = np.median([np.linalg.norm(a-b) for a,b in zip(C[-1],C[-1][1:])]) assert(C[0].shape == OC.shape) RMSD = [] org_RMSD = [] sup = SVDSuperimposer() RG = [] OC -= OC.mean(axis=0) OC_RG = ((np.linalg.norm(OC,axis=1)**2).sum()/len(OC)) ** 0.5 for cx in C: cx -= cx.mean(axis=0) rg_cx = ((np.linalg.norm(cx,axis=1)**2).sum()/len(cx)) ** 0.5 RG.append(rg_cx) sup.set(OC,cx) sup.run() RMSD.append(sup.get_rms()) org_RMSD.append(sup.get_init_rms()) rot, tran = sup.get_rotran() cx = np.dot(cx, rot) + tran RMSD = np.array(RMSD) org_RMSD = np.array(org_RMSD) RG = np.array(RG) #print dir, RMSD[-20:].mean(), org_RMSD[-20:].mean(),RG[-20:].mean() print "{} {: 0.4f} {: 0.4f}".format(dir, RMSD[-200:].mean(), RG[-200:].mean() / OC_RG) ''' from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.scatter(OC[:,0],OC[:,1],OC[:,2],'b') #ax.plot(OC[:,0],OC[:,1],OC[:,2],'k',alpha=0.5) ax.scatter(cx[:,0],cx[:,1],cx[:,2],color='r') #ax.plot(cx[:,0],cx[:,1],cx[:,2],'k',alpha=0.5) plt.show() exit() print OC #exit() ''' np.savetxt(f_RMSD,RMSD) os.chdir(org_dir) return dir