def changeLoop(filename, loopname): posted=request.get_json(force=True) if posted["action"]=="change" and posted["method"]=="random": sm=get_sm(filename) sm.load_sampled_elems() if loopname not in sm.bg.coords: abort(404) original_cg=copy.deepcopy(sm.bg) new_filename=get_new_filename() smCache.renameSM(filename, new_filename) change_elem(sm, loopname) cg=sm.bg centroid0 = ftuv.get_vector_centroid(ftug.bg_virtual_residues(original_cg)) centroid1 = ftuv.get_vector_centroid(ftug.bg_virtual_residues(cg)) crds0 = ftuv.center_on_centroid(ftug.bg_virtual_residues(original_cg)) crds1 = ftuv.center_on_centroid(ftug.bg_virtual_residues(cg)) rot_mat = ftur.optimal_superposition(crds0, crds1) for k in cg.coords.keys(): cg.coords[k] = (np.dot(rot_mat, cg.coords[k][0] - centroid1), np.dot(rot_mat, cg.coords[k][1] - centroid1)) if k[0] == 's': cg.twists[k] = (np.dot(rot_mat, cg.twists[k][0]), np.dot(rot_mat, cg.twists[k][1])) filename=get_new_filename() cg.to_cg_file("user_files/"+filename) return jsonify({"url": url_for("structure_main", filename=filename)}) else: abort(403)
def test_cg_rmsd(self): cg1 = ftmc.CoarseGrainRNA('test/forgi/threedee/data/1GID_A.cg') cg2 = ftmc.CoarseGrainRNA('test/forgi/threedee/data/1GID_A_sampled.cg') residues1 = ftug.bg_virtual_residues(cg1) residues2 = ftug.bg_virtual_residues(cg2) self.assertAlmostEqual(ftme.rmsd(residues1, residues2), ftme.cg_rmsd(cg1, cg2))
def main(): usage = """ python cg_rmsd.py file1.cg file2.cg Calculate the RMSD between two coarse grain models. """ num_args= 2 parser = OptionParser(usage=usage) #parser.add_option('-o', '--options', dest='some_option', default='yo', help="Place holder for a real option", type='str') #parser.add_option('-u', '--useless', dest='uselesss', default=False, action='store_true', help='Another useless option') (options, args) = parser.parse_args() if len(args) < num_args: parser.print_help() sys.exit(1) cg1 = ftmc.CoarseGrainRNA(args[0]) cg2 = ftmc.CoarseGrainRNA(args[1]) vrs1 = ftug.bg_virtual_residues(cg1) vrs2 = ftug.bg_virtual_residues(cg2) print ftur.centered_rmsd(vrs1, vrs2)
def changeLoop(filename, loopname): posted = request.get_json(force=True) if posted["action"] == "change" and posted["method"] == "random": sm = get_sm(filename) sm.load_sampled_elems() if loopname not in sm.bg.coords: abort(404) original_cg = copy.deepcopy(sm.bg) new_filename = get_new_filename() smCache.renameSM(filename, new_filename) change_elem(sm, loopname) cg = sm.bg centroid0 = ftuv.get_vector_centroid( ftug.bg_virtual_residues(original_cg)) centroid1 = ftuv.get_vector_centroid(ftug.bg_virtual_residues(cg)) crds0 = ftuv.center_on_centroid(ftug.bg_virtual_residues(original_cg)) crds1 = ftuv.center_on_centroid(ftug.bg_virtual_residues(cg)) rot_mat = ftur.optimal_superposition(crds0, crds1) for k in cg.coords.keys(): cg.coords[k] = (np.dot(rot_mat, cg.coords[k][0] - centroid1), np.dot(rot_mat, cg.coords[k][1] - centroid1)) if k[0] == 's': cg.twists[k] = (np.dot(rot_mat, cg.twists[k][0]), np.dot(rot_mat, cg.twists[k][1])) filename = get_new_filename() cg.to_cg_file("user_files/" + filename) return jsonify({"url": url_for("structure_main", filename=filename)}) else: abort(403)
def align_cgs(cgs): ''' Align each coarse grain RNA to the first one. The points representing each coarse grain RNA molecule will be the virtual residues. @param cgs: A list of CoarseGrainRNA structures. @return: Nothing, the cgs are modified in place ''' centroid0 = ftuv.get_vector_centroid(ftug.bg_virtual_residues(cgs[0])) crds0 = ftuv.center_on_centroid(ftug.bg_virtual_residues(cgs[0])) for cg in cgs: centroid1 = ftuv.get_vector_centroid(ftug.bg_virtual_residues(cg)) crds1 = ftuv.center_on_centroid(ftug.bg_virtual_residues(cg)) rot_mat = ftur.optimal_superposition(crds0, crds1) for k in cg.coords.keys(): cg.coords[k] = (np.dot(rot_mat, cg.coords[k][0] - centroid1), np.dot(rot_mat, cg.coords[k][1] - centroid1)) if k[0] == 's': cg.twists[k] = (np.dot(rot_mat, cg.twists[k][0]), np.dot(rot_mat, cg.twists[k][1]))
def __init__(self, sm_orig, plotter=None, plot_color=None, silent=False, output_file=sys.stdout, save_n_best=3, dists=[], no_rmsd=False, save_iterative_cg_measures=False): ''' @param sm_orig: The original Spatial Model against which to collect statistics. ''' self.energy_rmsd_structs = [] self.counter = 0 self.plotter = plotter self.plot_color = plot_color self.silent = silent self.verbose = False self.output_file = output_file self.save_n_best = save_n_best self.sm_orig = sm_orig self.energy_orig = None self.step_save = 0 self.save_iterative_cg_measures = save_iterative_cg_measures self.dists = dists self.highest_rmsd = 0. self.lowest_rmsd = 10000000000. self.no_rmsd = no_rmsd self.creation_time = time.time() try: self.centers_orig = ftug.bg_virtual_residues(sm_orig.bg) self.confusion_matrix_calculator = ftme.ConfusionMatrix(sm_orig.bg) except KeyError: # if there are no coordinates provided in the original # bulge graph file, then don't calculate rmsds self.centers_orig = None self.confusion_matrix_calculator = None
def main(args): cgs = [] projs = [] for file_ in args.files: cgs.append(ftmc.CoarseGrainRNA(file_)) try: projs.append(ftmp.Projection2D(cgs[-1])) except ValueError: projs.append(None) p_rmsds = OrderedDict() cg_rmsds = OrderedDict() p_rmsds[0] = 0.0 cg_rmsds[0] = 0.0 if "," in args.max_diff: diffs = map(int, args.max_diff.split(",")) else: diffs = range(1, int(args.max_diff) + 1) for diff in diffs: print ("diff {}".format(diff)) prmsd = 0 cgrmsd = 0 count = 0 pcount = 0 for i in range(0, len(cgs) - diff): count += 1 try: vrs1 = np.array([x for p in sorted(projs[i]._coords.keys()) for x in projs[i]._coords[p]]) vrs2 = np.array([x for p in sorted(projs[i + diff]._coords.keys()) for x in projs[i + diff]._coords[p]]) prmsd += ftms.rmsd(vrs1, vrs2) pcount += 1 except: pass cgrmsd += ftms.cg_rmsd(cgs[i], cgs[i + diff]) if count: cg_rmsds[diff] = cgrmsd / count if pcount: p_rmsds[diff] = prmsd / pcount print "projection RMSDs:", p_rmsds print "3D-RMSDs:", cg_rmsds import matplotlib.pyplot as plt if args.target_structure and args.hausdorff_reference: fig, (ax, axT, axH) = plt.subplots(3) elif args.target_structure: fig, (ax, axT) = plt.subplots(2) elif args.hausdorff_reference: fig, (ax, axH) = plt.subplots(2) else: fig, ax = plt.subplots() if args.target_structure: target = ftmc.CoarseGrainRNA(args.target_structure) target_rmsds = [] target_proj_rmsds = [] try: target_proj = ftmp.Projection2D(target) except ValueError: pass else: target_vrs = np.array([x for p in sorted(target_proj._coords.keys()) for x in target_proj._coords[p]]) for proj in projs: try: vrs1 = np.array([x for p in sorted(proj._coords.keys()) for x in proj._coords[p]]) prmsd = ftms.rmsd(vrs1, target_vrs) target_proj_rmsds.append(prmsd) except: target_proj_rmsds.append(float("nan")) target_vrs = ftug.bg_virtual_residues(target) for cg in cgs: vrs1 = ftug.bg_virtual_residues(cg) cgrmsd = ftms.rmsd(vrs1, target_vrs) target_rmsds.append(cgrmsd) xval = np.arange(len(cgs)) * args.step_size if target_proj_rmsds: axT.plot(xval, target_proj_rmsds, label="Projection", color="green") axT.plot(xval, target_rmsds, label="3D", color="blue") axT.set_title("RMSD to target structure") axT.set_xlabel("step") axT.set_ylabel("RMSD") leg = axT.legend(framealpha=0.8, fancybox=True) leg.get_frame().set_linewidth(0.0) plt.subplots_adjust(hspace=0.4) if args.hausdorff_reference: ref_img = scipy.ndimage.imread(args.hausdorff_reference) ref_quarter = scipy.ndimage.zoom(ref_img, 0.3) > 150 degrees = get_refimg_longest_axis(ref_img) global_optima = [] local_optima = [] for cg in cgs: score, img, params = fph.try_parameters(ref_img, args.hausdorff_scale, cg, degrees) local_optima.append(score) s, i, params = fph.globally_minimal_distance( ref_quarter, args.hausdorff_scale, cg, start_points=40, starting_rotations=degrees, virtual_atoms=False ) score, img, params = fph.locally_minimal_distance( ref_img, args.hausdorff_scale, cg, params[1], params[2], params[0], maxiter=200 ) global_optima.append(score) axH.plot(xval, global_optima, label="Global Minimization", color="red") axH.plot(xval, local_optima, label="Local Minimization", color="lavender") axH.set_title("Hausdorff Distance to reference image") axH.set_xlabel("step") axH.set_ylabel("Hausdorff Distance") leg = axH.legend(framealpha=0.8, fancybox=True) leg.get_frame().set_linewidth(0.0) plt.subplots_adjust(hspace=0.4) p_rmsds = np.array(p_rmsds.items()) cg_rmsds = np.array(cg_rmsds.items()) ax.plot(p_rmsds[:, 0] * args.step_size, p_rmsds[:, 1], label="Projection", color="green", marker="o") ax.plot(cg_rmsds[:, 0] * args.step_size, cg_rmsds[:, 1], label="3D", color="blue", marker="o") ax.set_title("Average RMSD between structures X steps apart.") ax.set_xlabel("steps apart") ax.set_ylabel("RMSD") leg = ax.legend(framealpha=0.8, fancybox=True) leg.get_frame().set_linewidth(0.0) fig.patch.set_facecolor("white") if args.save_fig: with open(args.save_fig, "w") as f: pickle.dump(fig, f) plt.show()
def test_cg_rmsd(self): cg1 = ftmc.CoarseGrainRNA('test/forgi/threedee/data/1GID_A.cg') cg2 = ftmc.CoarseGrainRNA('test/forgi/threedee/data/1GID_A_sampled.cg') residues1 = ftug.bg_virtual_residues(cg1) residues2 = ftug.bg_virtual_residues(cg2) self.assertAlmostEqual(ftur.centered_rmsd(residues1, residues2), ftme.cg_rmsd(cg1,cg2))
#! /usr/bin/python import forgi.threedee.model.coarse_grain as ftmc import fess.builder.models as fbm import forgi.threedee.utilities.vector as ftuv import forgi.threedee.utilities.graph_pdb as ftug import sys if __name__=="__main__": filename=sys.argv[1] print ("BUILDSTRUCTURE: Filename is ", filename) cg=ftmc.CoarseGrainRNA(filename) print ("BUILDSTRUCTURE: cg is ", cg) sm=fbm.SpatialModel(cg) print ("BUILDSTRUCTURE: sampling ") sm.sample_stats() print ("BUILDSTRUCTURE: traverse and build ") sm.traverse_and_build() print("BUILDSTRUCTURE: Writing File") centroid0 = ftuv.get_vector_centroid(ftug.bg_virtual_residues(sm.bg)) for k in cg.coords.keys(): cg.coords[k] = ((cg.coords[k][0] - centroid0), (cg.coords[k][1] - centroid0)) sm.bg.to_cg_file(filename) print ("BUILDSTRUCTURE: DONE")
def main(args): cgs = [] projs = [] for file_ in args.files: cgs.append(ftmc.CoarseGrainRNA(file_)) try: projs.append(ftmp.Projection2D(cgs[-1])) except ValueError: projs.append(None) p_rmsds = OrderedDict() cg_rmsds = OrderedDict() p_rmsds[0] = 0. cg_rmsds[0] = 0. if "," in args.max_diff: diffs = map(int, args.max_diff.split(",")) else: diffs = range(1, int(args.max_diff) + 1) for diff in diffs: print("diff {}".format(diff)) prmsd = 0 cgrmsd = 0 count = 0 pcount = 0 for i in range(0, len(cgs) - diff): count += 1 try: vrs1 = np.array([ x for p in sorted(projs[i]._coords.keys()) for x in projs[i]._coords[p] ]) vrs2 = np.array([ x for p in sorted(projs[i + diff]._coords.keys()) for x in projs[i + diff]._coords[p] ]) prmsd += ftms.rmsd(vrs1, vrs2) pcount += 1 except: pass cgrmsd += ftms.cg_rmsd(cgs[i], cgs[i + diff]) if count: cg_rmsds[diff] = cgrmsd / count if pcount: p_rmsds[diff] = prmsd / pcount print("projection RMSDs:", p_rmsds) print("3D-RMSDs:", cg_rmsds) import matplotlib.pyplot as plt if args.target_structure and args.hausdorff_reference: fig, (ax, axT, axH) = plt.subplots(3) elif args.target_structure: fig, (ax, axT) = plt.subplots(2) elif args.hausdorff_reference: fig, (ax, axH) = plt.subplots(2) else: fig, ax = plt.subplots() if args.target_structure: target = ftmc.CoarseGrainRNA(args.target_structure) target_rmsds = [] target_proj_rmsds = [] try: target_proj = ftmp.Projection2D(target) except ValueError: pass else: target_vrs = np.array([ x for p in sorted(target_proj._coords.keys()) for x in target_proj._coords[p] ]) for proj in projs: try: vrs1 = np.array([ x for p in sorted(proj._coords.keys()) for x in proj._coords[p] ]) prmsd = ftms.rmsd(vrs1, target_vrs) target_proj_rmsds.append(prmsd) except: target_proj_rmsds.append(float("nan")) target_vrs = ftug.bg_virtual_residues(target) for cg in cgs: vrs1 = ftug.bg_virtual_residues(cg) cgrmsd = ftms.rmsd(vrs1, target_vrs) target_rmsds.append(cgrmsd) xval = np.arange(len(cgs)) * args.step_size if target_proj_rmsds: axT.plot(xval, target_proj_rmsds, label="Projection", color="green") axT.plot(xval, target_rmsds, label="3D", color="blue") axT.set_title("RMSD to target structure") axT.set_xlabel("step") axT.set_ylabel("RMSD") leg = axT.legend(framealpha=0.8, fancybox=True) leg.get_frame().set_linewidth(0.0) plt.subplots_adjust(hspace=0.4) if args.hausdorff_reference: ref_img = scipy.ndimage.imread(args.hausdorff_reference) ref_quarter = (scipy.ndimage.zoom(ref_img, 0.3) > 150) degrees = get_refimg_longest_axis(ref_img) global_optima = [] local_optima = [] for cg in cgs: score, img, params = fph.try_parameters(ref_img, args.hausdorff_scale, cg, degrees) local_optima.append(score) s, i, params = fph.globally_minimal_distance( ref_quarter, args.hausdorff_scale, cg, start_points=40, starting_rotations=degrees, virtual_atoms=False) score, img, params = fph.locally_minimal_distance( ref_img, args.hausdorff_scale, cg, params[1], params[2], params[0], maxiter=200) global_optima.append(score) axH.plot(xval, global_optima, label="Global Minimization", color="red") axH.plot(xval, local_optima, label="Local Minimization", color="lavender") axH.set_title("Hausdorff Distance to reference image") axH.set_xlabel("step") axH.set_ylabel("Hausdorff Distance") leg = axH.legend(framealpha=0.8, fancybox=True) leg.get_frame().set_linewidth(0.0) plt.subplots_adjust(hspace=0.4) p_rmsds = np.array(list(p_rmsds.items())) cg_rmsds = np.array(list(cg_rmsds.items())) ax.plot(p_rmsds[:, 0] * args.step_size, p_rmsds[:, 1], label="Projection", color="green", marker="o") ax.plot(cg_rmsds[:, 0] * args.step_size, cg_rmsds[:, 1], label="3D", color="blue", marker="o") ax.set_title("Average RMSD between structures X steps apart.") ax.set_xlabel("steps apart") ax.set_ylabel("RMSD") leg = ax.legend(framealpha=0.8, fancybox=True) leg.get_frame().set_linewidth(0.0) fig.patch.set_facecolor('white') if args.save_fig: with open(args.save_fig, "w") as f: pickle.dump(fig, f) plt.show()
def update_statistics(self, energy_function, sm, prev_energy, tracking_energies = None, tracked_energies=None): ''' Add a newly sampled structure to the set of statistics. :param energy_function: The energy_function used to evaluate the structure. :param sm: The spatial model that was sampled. :param prev_energy: The evaluated (accepted) energy of the current step :tracking_energyis: The energy_functions which are calculated for statistics, but not used for sampling. :tracked_energies: The energy values of the tracking_energies. ''' self.counter += 1 if self.energy_orig is None: self.energy_orig = 0. try: self.sm_orig.bg.add_all_virtual_residues() self.energy_orig = energy_function.eval_energy(self.sm_orig) except KeyError: # most likely no native structure was provided pass energy = prev_energy #energy = energy_function.eval_energy(sm, background=True) if energy_function.uses_background(): energy_nobg = energy_function.eval_energy(sm, background=False) else: energy_nobg=energy mcc = None if self.centers_orig is not None: r = 0. if not self.no_rmsd: centers_new = ftug.bg_virtual_residues(sm.bg) r = cbr.centered_rmsd(self.centers_orig, centers_new) #r = cbr.drmsd(self.centers_orig, centers_new) cm = self.confusion_matrix_calculator.evaluate(sm.bg) mcc = ftme.mcc(cm) else: # no original coordinates provided so we can't calculate rmsds r = 0. dist = None dist2 = None cg = sm.bg dists = [] for (self.dist1, self.dist2) in self.dists: node1 = cg.get_node_from_residue_num(self.dist1) node2 = cg.get_node_from_residue_num(self.dist2) pos1, len1 = cg.get_position_in_element(self.dist1) pos2, len2 = cg.get_position_in_element(self.dist2) #fud.pv('node1, node2, pos1, pos2') vec1 = cg.coords[node1][1] - cg.coords[node1][0] vec2 = cg.coords[node2][1] - cg.coords[node2][0] #mid1 = (cg.coords[node1][0] + cg.coords[node1][1]) / 2 #mid2 = (cg.coords[node2][0] + cg.coords[node2][1]) / 2 mid1 = cg.coords[node1][0] + pos1 * (vec1 / len1) mid2 = cg.coords[node2][0] + pos2 * (vec2 / len2) #fud.pv('mid1, mid2') dists += [ftuv.vec_distance(mid1, mid2)] #self.energy_rmsd_structs += [(energy, r, sm.bg)] self.energy_rmsd_structs += [(energy_nobg, r, copy.deepcopy(sm.bg))] #self.energy_rmsd_structs += [(energy, r, sm.bg.copy())] sorted_energies = sorted(self.energy_rmsd_structs, key=lambda x: x[0]) self.energy_rmsd_structs = sorted_energies[:self.save_n_best] if r > self.highest_rmsd: self.highest_rmsd = r if r < self.lowest_rmsd: self.lowest_rmsd = r lowest_energy = sorted_energies[0][0] lowest_rmsd = sorted_energies[0][1] ''' if energy == lowest_energy: for key in sm.angle_defs: print >>sys.stderr, key, str(sm.angle_defs[key]) ''' if not self.silent: if self.verbose: ''' for energy_func in energy_function.energies: print energy_func.__class__.__name__, energy_func.eval_energy(sm) ''' _, rog=fbe.length_and_rog(sm.bg) #output_str = u"native_energy [{:s} {:d}]: {:3d} {:5.03g} {:5.3f} ROG: {:5.3f} | min: output_str = u"native_energy [%s %d]: %3d %5.03g %5.3f ROG: %5.3f | min: %5.2f (%5.2f) %5.2f | extreme_rmsds: %5.2f %5.2f (%.2f)" % ( sm.bg.name, sm.bg.seq_length, self.counter, energy, r , rog, lowest_energy, self.energy_orig, lowest_rmsd, self.lowest_rmsd, self.highest_rmsd, energy_nobg) output_str += " |" # assume that the energy function is a combined energy if isinstance(self.energy_function, fbe.CombinedEnergy): for e in self.energy_function.iterate_energies(): if isinstance(e,fbe.DistanceExponentialEnergy): output_str += " [clamp {},{}: {:.1f}]".format(e.from_elem, e.to_elem, e.get_distance(sm)) if tracked_energies and tracking_energies: output_str += " | [tracked Energies]" for i,e in enumerate(tracking_energies): sn=e.shortname() if len(sn)>12: sn=sn[:9]+"..." output_str += " [{}]: ".format(sn) output_str += "%5.03g" % (tracked_energies[i]) elif tracking_energies: output_str += " | [tracked Energies]" for e in tracking_energies: sn=e.shortname() if len(sn)>12: sn=sn[:9]+"..." output_str += " [{}]: ".format(sn) output_str += "%5.03g" % (e.eval_energy(sm)) if dist: output_str += " | dist %.2f" % (dist) for dist2 in dists: if dist2 is not None: output_str += " | [dist2: %.2f]" % (dist2) if mcc is not None: output_str += " | [mcc: %.3f]" % (mcc) output_str += " [time: %.1f]" % (time.time() - self.creation_time) #Print to both STDOUT and the log file. if self.output_file != sys.stdout: print (output_str.strip()) if self.output_file != None: print(output_str, file=self.output_file) self.output_file.flush() self.update_plots(energy, r) ''' if self.counter % 1000 == 0: import pdb; pdb.set_trace() ''' if self.counter % 10 == 0: if not self.silent: self.save_top(self.save_n_best, counter=self.counter) if self.step_save > 0 and self.counter % self.step_save == 0: #If a projection match energy was used, save the optimal projection direction to the file. if isinstance(self.energy_function, fbe.CombinedEnergy): for e in self.energy_function.iterate_energies(): if hasattr(e, "accepted_projDir"): sm.bg.project_from=e.accepted_projDir sm.bg.to_cg_file(os.path.join(cbc.Configuration.sampling_output_dir, 'step%06d.coord' % (self.counter)))