def diff(self, other_angle, next_stem_length=1): ''' Calculate the distance between the start and end of the hypothetical next stem defined by these two angles. :param other_angle: The other angle stat :param next_stem_length: the length of the stem that is attached to this angle. :param return: The distance between the starts and ends of the two hypothetical next stems defined by this angle and the other angle ''' this_stem_start = np.array( ftuv.spherical_polar_to_cartesian([self.r1, self.u1, self.v1])) this_stem_end = np.array( ftuv.spherical_polar_to_cartesian( [next_stem_length, self.u, self.v])) other_stem_start = np.array( ftuv.spherical_polar_to_cartesian( [other_angle.r1, other_angle.u1, other_angle.v1])) other_stem_end = np.array( ftuv.spherical_polar_to_cartesian( [next_stem_length, other_angle.u, other_angle.v])) return ftuv.vec_distance(this_stem_start, other_stem_start) + ftuv.vec_distance( this_stem_start + this_stem_end, other_stem_start + other_stem_end)
def test_spherical_coordinate_transforms(self): for vec in [np.array([0,0,1]), np.array([0,2,0]), np.array([3,0,0]), np.array([4,5,0]), np.array([6,0,7]), np.array([8,9,0.4])]: sphe=ftuv.spherical_cartesian_to_polar(vec) nptest.assert_allclose(ftuv.spherical_polar_to_cartesian(sphe),vec, atol=0.0000001) nptest.assert_allclose(ftuv.spherical_polar_to_cartesian([1,0,math.pi/2]), np.array([0,0,1]), atol=0.0000001) nptest.assert_allclose(ftuv.spherical_polar_to_cartesian([2, math.pi/2, math.pi/4]), np.array([1,1,0])*2/math.sqrt(2), atol=0.0000001) nptest.assert_allclose(ftuv.spherical_cartesian_to_polar(np.array([0,2,2])/math.sqrt(2)), [2,math.pi/4,math.pi/2], atol=0.0000001)
def get_angle(self): ''' Return the angle between the two connected stems. ''' return ftuv.vec_angle( np.array([-1., 0., 0.]), ftuv.spherical_polar_to_cartesian([1, self.u, self.v]))
def updateProjection(self, _=None): direction = ftuv.spherical_polar_to_cartesian( np.array([ 1, math.radians(self.theta.get()), math.radians(self.phi.get()) ])) self.proj = fpp.Projection2D(self.cg, project_virtual_atoms=True, proj_direction=direction, project_virtual_residues=list( range(1, len(self.cg.seq) + 1))) self.update()
def deviation_from(self, stat2): """ How much does the other stat differ from this stat? :param stat2: Another AngleStat :returns: A 4-tuple: The positional deviation in Angstrom, and 3 absolute angular deviations in radians. The angular deviations are u, v and t """ ret = [] pos1 = ftuv.spherical_polar_to_cartesian(self.position_params()) pos2 = ftuv.spherical_polar_to_cartesian(stat2.position_params()) ret.append(ftuv.magnitude(pos1 - pos2)) log.debug("Position difference is %f", ret[-1]) for attr in ["u", "v", "t"]: raw_diff = getattr(self, attr) - getattr(stat2, attr) # Map the difference to a value between 0 and pi raw_diff_on_circle = abs( (raw_diff + math.pi / 2) % (math.pi) - math.pi / 2) log.debug("Angular difference for %s is %f, mapped to %f", attr, raw_diff, raw_diff_on_circle) ret.append(raw_diff_on_circle) return tuple(ret)
def deviation_from(self, stat2): """ How much does the other stat differ from this stat? :param stat2: Another AngleStat :returns: A 4-tuple: The positional deviation in Angstrom, and 3 absolute angular deviations in radians. The angular deviations are u, v and t """ ret = [] pos1 = ftuv.spherical_polar_to_cartesian(self.position_params()) pos2 = ftuv.spherical_polar_to_cartesian(stat2.position_params()) ret.append(ftuv.magnitude(pos1 - pos2)) log.debug("Position difference is %f", ret[-1]) for attr in ["u", "v", "t"]: raw_diff = getattr(self, attr) - getattr(stat2, attr) #Map the difference to a value between 0 and pi raw_diff_on_circle = abs((raw_diff + math.pi / 2) % (math.pi) - math.pi / 2) log.debug("Angular difference for %s is %f, mapped to %f", attr, raw_diff, raw_diff_on_circle) ret.append(raw_diff_on_circle) return tuple(ret)
def test_spherical_coordinate_transforms(self): for vec in [ np.array([0, 0, 1]), np.array([0, 2, 0]), np.array([3, 0, 0]), np.array([4, 5, 0]), np.array([6, 0, 7]), np.array([8, 9, 0.4]) ]: sphe = ftuv.spherical_cartesian_to_polar(vec) nptest.assert_allclose(ftuv.spherical_polar_to_cartesian(sphe), vec, atol=0.0000001) nptest.assert_allclose(ftuv.spherical_polar_to_cartesian( [1, 0, math.pi / 2]), np.array([0, 0, 1]), atol=0.0000001) nptest.assert_allclose(ftuv.spherical_polar_to_cartesian( [2, math.pi / 2, math.pi / 4]), np.array([1, 1, 0]) * 2 / math.sqrt(2), atol=0.0000001) nptest.assert_allclose(ftuv.spherical_cartesian_to_polar( np.array([0, 2, 2]) / math.sqrt(2)), [2, math.pi / 4, math.pi / 2], atol=0.0000001)
def output_long_range_distances(bg): for key1 in bg.longrange.keys(): for key2 in bg.longrange[key1]: if bg.has_connection(key1, key2): continue #point1 = bg.get_point(key1) #point2 = bg.get_point(key2) (i1,i2) = cuv.line_segment_distance(bg.coords[key1][0], bg.coords[key1][1], bg.coords[key2][0], bg.coords[key2][1]) vec1 = bg.coords[key1][1] - bg.coords[key2][0] basis = cuv.create_orthonormal_basis(vec1) coords2 = cuv.change_basis(i2 - i1, basis, cuv.standard_basis) (r, u, v) = cuv.spherical_polar_to_cartesian(coords2) seq1 = 'x' seq2 = 'x' ''' if key1[0] != 's' and key[0] != 'i': seq1 = bg.get_seq(key1) if key2[0] != 's' and key2[0] != 'i': seq2 = bg.get_seq(key2) ''' print "%s %s %d %s %s %d %f %s %s %s %f" % (key1, key1[0], bg.get_length(key1), key2, key2[0], bg.get_length(key2), cuv.magnitude(i2-i1), seq1, seq2, "Y", v)
def updateProjection(self, _=None): direction = ftuv.spherical_polar_to_cartesian( np.array([1, math.radians(self.theta.get()), math.radians(self.phi.get())])) self.proj = fpp.Projection2D(self.cg, project_virtual_atoms=True, proj_direction=direction, project_virtual_residues=list(range(1, len(self.cg.seq) + 1))) self.update()
parser=generateParser() if __name__=="__main__": args = parser.parse_args() #Get the stats object conf_stats = ftms.get_conformation_stats(data_file(args.stats_file)) angle_stats = conf_stats.angle_stats colors = plt.cm.Spectral(np.linspace(0, 1, 10)) colors=np.array(list(colors)*500) markers="o"*10+"v"*10+"<"*10+">"*10+"."*10+"s"*10+"^"*10+"8"*10+"*"*10+"+"*10 markers=markers*500 for key in angle_stats.keys(): point_to=[] to_clust=[] angles=[] for stat in angle_stats[key]: stem_start = np.array(ftuv.spherical_polar_to_cartesian([stat.r1, stat.u1, stat.v1])) #stem_vec = np.array(ftuv.spherical_polar_to_cartesian([1, stat.u, stat.v])) #point1 = stem_start+stem_vec stem_vec = np.array(ftuv.spherical_polar_to_cartesian([1, stat.u, stat.v])) #point2 = stem_start+stem_vec #stem_vec = np.array(ftuv.spherical_polar_to_cartesian([100, stat.u, stat.v]))/100 #point3 = stem_start+stem_vec #point_to.append(list(point1)+list(point2)+list(point3)) point_to.append(list(stem_start)+list(stem_vec)) to_clust.append(list(stem_vec)) angles.append([stat.u, stat.v]) point_to=np.array(point_to) if len(point_to)<10: continue #db = sklearn.cluster.DBSCAN(eps=args.threshold, min_samples=3).fit(to_clust) #labels_db = db.labels_
if __name__ == "__main__": args = parser.parse_args() #Get the stats object conf_stats = ftms.get_conformation_stats(data_file(args.stats_file)) angle_stats = conf_stats.angle_stats colors = plt.cm.Spectral(np.linspace(0, 1, 10)) colors = np.array(list(colors) * 500) markers = "o" * 10 + "v" * 10 + "<" * 10 + ">" * 10 + "." * 10 + "s" * 10 + "^" * 10 + "8" * 10 + "*" * 10 + "+" * 10 markers = markers * 500 for key in angle_stats.keys(): point_to = [] to_clust = [] angles = [] for stat in angle_stats[key]: stem_start = np.array( ftuv.spherical_polar_to_cartesian([stat.r1, stat.u1, stat.v1])) #stem_vec = np.array(ftuv.spherical_polar_to_cartesian([1, stat.u, stat.v])) #point1 = stem_start+stem_vec stem_vec = np.array( ftuv.spherical_polar_to_cartesian([1, stat.u, stat.v])) #point2 = stem_start+stem_vec #stem_vec = np.array(ftuv.spherical_polar_to_cartesian([100, stat.u, stat.v]))/100 #point3 = stem_start+stem_vec #point_to.append(list(point1)+list(point2)+list(point3)) point_to.append(list(stem_start) + list(stem_vec)) to_clust.append(list(stem_vec)) angles.append([stat.u, stat.v]) point_to = np.array(point_to) if len(point_to) < 10: continue #db = sklearn.cluster.DBSCAN(eps=args.threshold, min_samples=3).fit(to_clust)
def get_angle(self): ''' Return the angle between the two connected stems. ''' return ftuv.vec_angle(np.array([-1., 0., 0.]), ftuv.spherical_polar_to_cartesian([1, self.u, self.v]))
def main(): parser = OptionParser() #parser.add_option('-o', '--options', dest='some_option', default='yo', help="Place holder for a real option", type='str') parser.add_option('-l', '--loops', dest='loops', default=True, action='store_false', help='Toggle loop reconstruction') parser.add_option('','--drop-into-debugger', dest='drop_into_debugger', default=False, action='store_true') parser.add_option('-f', '--fragments', dest='fragments', default=False, action='store_true', help='Reconstruct using fragments.') parser.add_option('', '--output-file', dest='output_file', default='out.pdb', type='str') parser.add_option('-s', '--samples', dest='samples', default=10, type='int', help='The number of samples to get from Barnacle') parser.add_option('-a', '--average_atoms', dest='average_atoms', default=False, action='store_true', help='Reconstruct using the positions of the average atoms') parser.add_option('', '--fr', dest='fruchterman_reingold', default=False, action='store_true', help='Use the Fruchterman Reingold graph layout instead of CCD to close loops') (options, args) = parser.parse_args() if len(args) < 1: print >>sys.stderr, "Usage: ./reconstruct.py temp.comp" print >>sys.stderr, "Reconstruct a spatial model to full-atom accuracy." sys.exit(1) #print >>sys.stderr, "reconstructing model:", args[0] sm = models.SpatialModel(ftmc.CoarseGrainRNA(args[0])) sm.sample_native_stems() sm.create_native_stem_models() if not options.average_atoms: chain = rtor.reconstruct_stems(sm) if options.loops: if options.average_atoms: with warnings.catch_warnings(): warnings.simplefilter("ignore") chain = rtor.reconstruct_from_average(sm) rtor.output_chain(chain, 'temp.pdb') return elif options.fragments: sm.sampled_from_bg() sampled_bulges = sm.get_sampled_bulges() ''' rtor.reconstruct_bulge_with_fragment(chain, sm, 'x2') rtor.output_chain(chain, options.output_file) sys.exit(1) ''' ''' for b in it.chain(sm.bg.iloop_iterator(), sm.bg.mloop_iterator(), sm.bg.hloop_iterator()): ''' for b in it.chain(sm.bg.floop_iterator(), sm.bg.tloop_iterator(), sm.bg.iloop_iterator(), sm.bg.mloop_iterator(), sm.bg.hloop_iterator()): if b not in sm.bg.sampled.keys(): print >>sys.stderr, "interpolating... %s" % (b) (as1, as2) = sm.bg.get_bulge_angle_stats(b) bulge_vec = np.array(cuv.spherical_polar_to_cartesian((as1.r1, as1.u1, as1.v1))) #bulge_vec = np.array(cuv.spherical_polar_to_cartesian((as2.r1, as2.u1, as2.v1))) size = sm.bg.get_bulge_dimensions(b) best_bv_dist = 1000000. best_bv = None connections = list(sm.bg.edges[b]) ang_type = sm.bg.connection_type(b, connections) for ang_s in cbs.get_angle_stats()[(size[0], size[1], ang_type)]: pot_bulge_vec = np.array(cuv.spherical_polar_to_cartesian((ang_s.r1, ang_s.u1, ang_s.v1))) pot_bv_dist = cuv.magnitude(bulge_vec - pot_bulge_vec) if pot_bv_dist < best_bv_dist: best_bv_dist = pot_bv_dist best_bv_ang_s = ang_s best_bv = pot_bulge_vec sm.angle_defs[b][ang_type] = best_bv_ang_s rtor.reconstruct_with_fragment(chain, sm, b, move_all_angles=True, close_loop=not options.fruchterman_reingold) continue rtor.reconstruct_with_fragment(chain, sm, b, close_loop=not options.fruchterman_reingold) else: try: rtor.replace_bases(chain, sm.bg) rtor.output_chain(chain, options.output_file) #rtor.reconstruct_loop(chain, sm, 'b1', 0, samples=options.samples, consider_contacts=False) rtor.reconstruct_loops(chain, sm, samples=options.samples, consider_contacts=False) except Exception as e: if options.drop_into_debugger: pdb.post_mortem() else: raise #rtor.reconstruct_loops(chain, sm) chain = rtor.reorder_residues(chain, sm.bg) rtor.replace_bases(chain, sm.bg) rtor.output_chain(chain, 'out_pre.pdb') if options.fruchterman_reingold: import fruchterman_reingold.smooth_fragments as frs frs.smooth_fragments(chain, sm.bg, iterations=20) rtor.output_chain(chain, options.output_file)